View Javadoc

1   /* 
2    * Copyright (c) 2005-2011, Fraunhofer-Gesellschaft
3    * All rights reserved.
4    * 
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are
7    * met:
8    * 
9    * (1) Redistributions of source code must retain the above copyright
10   *     notice, this list of conditions and the disclaimer at the end.
11   *     Redistributions in binary form must reproduce the above copyright
12   *     notice, this list of conditions and the following disclaimer in
13   *     the documentation and/or other materials provided with the
14   *     distribution.
15   * 
16   * (2) Neither the name of Fraunhofer nor the names of its
17   *     contributors may be used to endorse or promote products derived
18   *     from this software without specific prior written permission.
19   * 
20   * DISCLAIMER
21   * 
22   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25   * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26   * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33   *  
34   */
35  package org.ogf.graap.wsag.client.remote;
36  
37  import org.apache.muse.util.xml.XmlUtils;
38  import org.apache.muse.ws.addressing.soap.SoapFault;
39  import org.apache.xmlbeans.XmlException;
40  import org.apache.xmlbeans.XmlNCName;
41  import org.apache.xmlbeans.XmlObject;
42  import org.apache.xmlbeans.XmlOptions;
43  import org.ogf.graap.wsag.api.WsagConstants;
44  import org.ogf.graap.wsag.api.client.AgreementService;
45  import org.ogf.graap.wsag.api.client.WsClient;
46  import org.ogf.graap.wsag.api.exceptions.ResourceUnavailableException;
47  import org.ogf.graap.wsag.api.exceptions.ResourceUnknownException;
48  import org.ogf.schemas.graap.wsAgreement.AgreementContextType;
49  import org.ogf.schemas.graap.wsAgreement.AgreementIdDocument;
50  import org.ogf.schemas.graap.wsAgreement.AgreementStateDocument;
51  import org.ogf.schemas.graap.wsAgreement.AgreementStatePropertiesType;
52  import org.ogf.schemas.graap.wsAgreement.AgreementStateType;
53  import org.ogf.schemas.graap.wsAgreement.GuaranteeTermStateType;
54  import org.ogf.schemas.graap.wsAgreement.ServiceTermStateType;
55  import org.ogf.schemas.graap.wsAgreement.TermTreeType;
56  import org.ogf.schemas.graap.wsAgreement.TerminateInputType;
57  import org.ogf.schemas.graap.wsAgreement.TerminateResponseDocument;
58  import org.w3c.dom.Element;
59  
60  /**
61   * Default implementation of an agreement service client.
62   * 
63   * @author Oliver Waeldrich
64   * 
65   */
66  public class RemoteAgreementServiceImpl implements AgreementService
67  {
68  
69      private WsrfResourceClient client;
70  
71      /**
72       * 
73       * @param client
74       *            the WSRF client to use
75       */
76      public RemoteAgreementServiceImpl( WsrfResourceClient client )
77      {
78          this.client = client;
79      }
80  
81      /**
82       * {@inheritDoc}
83       */
84      public WsClient getWebServiceClient()
85      {
86          return client;
87      }
88  
89      /**
90       * {@inheritDoc}
91       */
92      public AgreementContextType getContext() throws ResourceUnavailableException
93      {
94          try
95          {
96              Element body =
97                  XmlUtils.createElement( WsagConstants.WSRF_GET_RESOURCE_PROPERTY_QNAME, "wsag:Context" );
98              body.setAttribute( WsagConstants.WSAG_PREFIX_DECLARATION, WsagConstants.NAMESPACE_URI );
99  
100             Element response = client.invoke( WsagConstants.WSRF_GET_RESOURCE_PROPERTY_ACTION, body );
101 
102             AgreementContextType respDoc =
103                 AgreementContextType.Factory.parse( XmlUtils.getFirstElement( response ),
104                                                     new XmlOptions().setLoadReplaceDocumentElement( null ) );
105 
106             return respDoc;
107         }
108         catch ( SoapFault e )
109         {
110             throw new ResourceUnavailableException( e );
111         }
112         catch ( XmlException e )
113         {
114             throw new ResourceUnavailableException( e );
115         }
116     }
117 
118     /**
119      * {@inheritDoc}
120      */
121     public GuaranteeTermStateType[] getGuaranteeTermStates() throws ResourceUnavailableException
122     {
123         try
124         {
125             Element body =
126                 XmlUtils.createElement( WsagConstants.WSRF_GET_RESOURCE_PROPERTY_QNAME,
127                                         "wsag:GuaranteeTermState" );
128             body.setAttribute( WsagConstants.WSAG_PREFIX_DECLARATION, WsagConstants.NAMESPACE_URI );
129 
130             Element response = client.invoke( WsagConstants.WSRF_GET_RESOURCE_PROPERTY_ACTION, body );
131 
132             AgreementStatePropertiesType props =
133                 AgreementStatePropertiesType.Factory.parse( response,
134                                                             new XmlOptions().setLoadReplaceDocumentElement( null ) );
135 
136             GuaranteeTermStateType[] states = props.getGuaranteeTermStateArray();
137 
138             return states;
139 
140         }
141         catch ( SoapFault e )
142         {
143             throw new ResourceUnavailableException( e );
144         }
145         catch ( XmlException e )
146         {
147             throw new ResourceUnavailableException( e );
148         }
149     }
150 
151     /**
152      * {@inheritDoc}
153      */
154     public String getAgreementId() throws ResourceUnavailableException
155     {
156         try
157         {
158             Element body =
159                 XmlUtils.createElement( WsagConstants.WSRF_GET_RESOURCE_PROPERTY_QNAME, "wsag:AgreementId" );
160             body.setAttribute( WsagConstants.WSAG_PREFIX_DECLARATION, WsagConstants.NAMESPACE_URI );
161 
162             Element response = client.invoke( WsagConstants.WSRF_GET_RESOURCE_PROPERTY_ACTION, body );
163 
164             AgreementIdDocument id =
165                 AgreementIdDocument.Factory.parse( response,
166                                                    new XmlOptions().setLoadReplaceDocumentElement( null ) );
167 
168             return id.getAgreementId();
169         }
170         catch ( SoapFault e )
171         {
172             throw new ResourceUnavailableException( e );
173         }
174         catch ( XmlException e )
175         {
176             throw new ResourceUnavailableException( e );
177         }
178     }
179 
180     /**
181      * {@inheritDoc}
182      */
183     public String getName() throws ResourceUnavailableException
184     {
185         try
186         {
187             Element body =
188                 XmlUtils.createElement( WsagConstants.WSRF_GET_RESOURCE_PROPERTY_QNAME, "wsag:Name" );
189             body.setAttribute( WsagConstants.WSAG_PREFIX_DECLARATION, WsagConstants.NAMESPACE_URI );
190 
191             Element response = client.invoke( WsagConstants.WSRF_GET_RESOURCE_PROPERTY_ACTION, body );
192 
193             XmlNCName respDoc =
194                 XmlNCName.Factory.parse( response, new XmlOptions().setLoadReplaceDocumentElement( null ) );
195 
196             return respDoc.getStringValue();
197         }
198         catch ( SoapFault e )
199         {
200             throw new ResourceUnavailableException( e );
201         }
202         catch ( XmlException e )
203         {
204             throw new ResourceUnavailableException( e );
205         }
206     }
207 
208     /**
209      * {@inheritDoc}
210      */
211     public ServiceTermStateType[] getServiceTermStates() throws ResourceUnavailableException
212     {
213         try
214         {
215             Element body =
216                 XmlUtils.createElement( WsagConstants.WSRF_GET_RESOURCE_PROPERTY_QNAME,
217                                         "wsag:ServiceTermState" );
218             body.setAttribute( WsagConstants.WSAG_PREFIX_DECLARATION, WsagConstants.NAMESPACE_URI );
219 
220             Element response = client.invoke( WsagConstants.WSRF_GET_RESOURCE_PROPERTY_ACTION, body );
221 
222             AgreementStatePropertiesType props =
223                 AgreementStatePropertiesType.Factory.parse( response,
224                                                             new XmlOptions().setLoadReplaceDocumentElement( null ) );
225 
226             ServiceTermStateType[] states = props.getServiceTermStateArray();
227 
228             return states;
229 
230         }
231         catch ( SoapFault e )
232         {
233             throw new ResourceUnavailableException( e );
234         }
235         catch ( XmlException e )
236         {
237             throw new ResourceUnavailableException( e );
238         }
239     }
240 
241     /**
242      * {@inheritDoc}
243      */
244     public AgreementStateType getState() throws ResourceUnavailableException
245     {
246         try
247         {
248             Element body =
249                 XmlUtils.createElement( WsagConstants.WSRF_GET_RESOURCE_PROPERTY_QNAME, "wsag:AgreementState" );
250             body.setAttribute( WsagConstants.WSAG_PREFIX_DECLARATION, WsagConstants.NAMESPACE_URI );
251 
252             Element response = client.invoke( WsagConstants.WSRF_GET_RESOURCE_PROPERTY_ACTION, body );
253 
254             AgreementStateDocument respDoc =
255                 (AgreementStateDocument) XmlObject.Factory.parse( XmlUtils.getFirstElement( response ) );
256 
257             return respDoc.getAgreementState();
258 
259         }
260         catch ( SoapFault e )
261         {
262             throw new ResourceUnavailableException( e );
263         }
264         catch ( XmlException e )
265         {
266             throw new ResourceUnavailableException( e );
267         }
268     }
269 
270     /**
271      * {@inheritDoc}
272      */
273     public TermTreeType getTerms() throws ResourceUnavailableException
274     {
275         try
276         {
277             Element body =
278                 XmlUtils.createElement( WsagConstants.WSRF_GET_RESOURCE_PROPERTY_QNAME, "wsag:Terms" );
279             body.setAttribute( WsagConstants.WSAG_PREFIX_DECLARATION, WsagConstants.NAMESPACE_URI );
280 
281             Element response = client.invoke( WsagConstants.WSRF_GET_RESOURCE_PROPERTY_ACTION, body );
282 
283             TermTreeType respDoc =
284                 TermTreeType.Factory.parse( XmlUtils.getFirstElement( response ),
285                                             new XmlOptions().setLoadReplaceDocumentElement( null ) );
286 
287             return respDoc;
288         }
289         catch ( SoapFault e )
290         {
291             throw new ResourceUnavailableException( e );
292         }
293         catch ( XmlException e )
294         {
295             throw new ResourceUnavailableException( e );
296         }
297     }
298 
299     /**
300      * {@inheritDoc}
301      */
302     public void terminate( TerminateInputType reason ) throws ResourceUnavailableException
303     {
304         try
305         {
306             Element inputType =
307                 XmlUtils.createElement( WsagConstants.WSAG_TERMINATE_AGREEMENT_INPUT_QNAME,
308                                         reason.getDomNode() );
309             Element body = XmlUtils.createElement( WsagConstants.WSAG_TERMINATE_AGREEMENT_QNAME );
310             body.appendChild( body.getOwnerDocument().importNode( inputType, true ) );
311             Element response = client.invoke( WsagConstants.WSAG_TERMINATE_AGREEMENT_ACTION, body );
312 
313             TerminateResponseDocument respDoc = TerminateResponseDocument.Factory.parse( response );
314             respDoc.getTerminateResponse();
315         }
316         catch ( SoapFault e )
317         {
318             throw new ResourceUnavailableException( e );
319         }
320         catch ( XmlException e )
321         {
322             throw new ResourceUnavailableException( e );
323         }
324         catch ( Exception e )
325         {
326             throw new ResourceUnavailableException( e );
327         }
328     }
329 
330     /**
331      * {@inheritDoc}
332      */
333     public void destroy() throws ResourceUnknownException, ResourceUnavailableException
334     {
335         try
336         {
337             client.destroy();
338         }
339         catch ( SoapFault e )
340         {
341             throw new ResourceUnavailableException( e );
342         }
343     }
344 }