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.wsrf.impl;
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.exceptions.ResourceUnavailableException;
45  import org.ogf.graap.wsag.api.exceptions.ResourceUnknownException;
46  import org.ogf.schemas.graap.wsAgreement.AgreementContextType;
47  import org.ogf.schemas.graap.wsAgreement.AgreementIdDocument;
48  import org.ogf.schemas.graap.wsAgreement.AgreementStateDocument;
49  import org.ogf.schemas.graap.wsAgreement.AgreementStatePropertiesType;
50  import org.ogf.schemas.graap.wsAgreement.AgreementStateType;
51  import org.ogf.schemas.graap.wsAgreement.GuaranteeTermStateType;
52  import org.ogf.schemas.graap.wsAgreement.ServiceTermStateType;
53  import org.ogf.schemas.graap.wsAgreement.TermTreeType;
54  import org.ogf.schemas.graap.wsAgreement.TerminateInputType;
55  import org.ogf.schemas.graap.wsAgreement.TerminateResponseDocument;
56  import org.w3c.dom.Element;
57  
58  /**
59   * Default implementation of an agreement service client.
60   * 
61   * @author Oliver Waeldrich
62   * 
63   */
64  public class WsrfAgreementService
65  {
66  
67      private final WsrfResourceClient client;
68  
69      /**
70       * 
71       * @param client
72       *            the WSRF client to use
73       */
74      public WsrfAgreementService( WsrfResourceClient client )
75      {
76          this.client = client;
77      }
78  
79      /**
80       * {@inheritDoc}
81       */
82      public WsrfResourceClient getWebServiceClient()
83      {
84          return client;
85      }
86  
87      /**
88       * {@inheritDoc}
89       */
90      public AgreementContextType getContext() throws ResourceUnavailableException
91      {
92          try
93          {
94              Element body =
95                  XmlUtils.createElement( WsagConstants.WSRF_GET_RESOURCE_PROPERTY_QNAME, "wsag:Context" );
96              body.setAttribute( WsagConstants.WSAG_PREFIX_DECLARATION, WsagConstants.NAMESPACE_URI );
97  
98              Element response = client.invoke( WsagConstants.WSRF_GET_RESOURCE_PROPERTY_ACTION, body );
99  
100             AgreementContextType respDoc =
101                 AgreementContextType.Factory.parse( XmlUtils.getFirstElement( response ),
102                     new XmlOptions().setLoadReplaceDocumentElement( null ) );
103 
104             return respDoc;
105         }
106         catch ( SoapFault e )
107         {
108             throw new ResourceUnavailableException( e );
109         }
110         catch ( XmlException e )
111         {
112             throw new ResourceUnavailableException( e );
113         }
114     }
115 
116     /**
117      * {@inheritDoc}
118      */
119     public GuaranteeTermStateType[] getGuaranteeTermStates() throws ResourceUnavailableException
120     {
121         try
122         {
123             Element body =
124                 XmlUtils.createElement( WsagConstants.WSRF_GET_RESOURCE_PROPERTY_QNAME,
125                     "wsag:GuaranteeTermState" );
126             body.setAttribute( WsagConstants.WSAG_PREFIX_DECLARATION, WsagConstants.NAMESPACE_URI );
127 
128             Element response = client.invoke( WsagConstants.WSRF_GET_RESOURCE_PROPERTY_ACTION, body );
129 
130             AgreementStatePropertiesType props =
131                 AgreementStatePropertiesType.Factory.parse( response,
132                     new XmlOptions().setLoadReplaceDocumentElement( null ) );
133 
134             GuaranteeTermStateType[] states = props.getGuaranteeTermStateArray();
135 
136             return states;
137 
138         }
139         catch ( SoapFault e )
140         {
141             throw new ResourceUnavailableException( e );
142         }
143         catch ( XmlException e )
144         {
145             throw new ResourceUnavailableException( e );
146         }
147     }
148 
149     /**
150      * {@inheritDoc}
151      */
152     public String getAgreementId() throws ResourceUnavailableException
153     {
154         try
155         {
156             Element body =
157                 XmlUtils.createElement( WsagConstants.WSRF_GET_RESOURCE_PROPERTY_QNAME, "wsag:AgreementId" );
158             body.setAttribute( WsagConstants.WSAG_PREFIX_DECLARATION, WsagConstants.NAMESPACE_URI );
159 
160             Element response = client.invoke( WsagConstants.WSRF_GET_RESOURCE_PROPERTY_ACTION, body );
161 
162             AgreementIdDocument id =
163                 AgreementIdDocument.Factory.parse( response,
164                     new XmlOptions().setLoadReplaceDocumentElement( null ) );
165 
166             return id.getAgreementId();
167         }
168         catch ( SoapFault e )
169         {
170             throw new ResourceUnavailableException( e );
171         }
172         catch ( XmlException e )
173         {
174             throw new ResourceUnavailableException( e );
175         }
176     }
177 
178     /**
179      * {@inheritDoc}
180      */
181     public String getName() throws ResourceUnavailableException
182     {
183         try
184         {
185             Element body =
186                 XmlUtils.createElement( WsagConstants.WSRF_GET_RESOURCE_PROPERTY_QNAME, "wsag:Name" );
187             body.setAttribute( WsagConstants.WSAG_PREFIX_DECLARATION, WsagConstants.NAMESPACE_URI );
188 
189             Element response = client.invoke( WsagConstants.WSRF_GET_RESOURCE_PROPERTY_ACTION, body );
190 
191             XmlNCName respDoc =
192                 XmlNCName.Factory.parse( response, new XmlOptions().setLoadReplaceDocumentElement( null ) );
193 
194             return respDoc.getStringValue();
195         }
196         catch ( SoapFault e )
197         {
198             throw new ResourceUnavailableException( e );
199         }
200         catch ( XmlException e )
201         {
202             throw new ResourceUnavailableException( e );
203         }
204     }
205 
206     /**
207      * {@inheritDoc}
208      */
209     public ServiceTermStateType[] getServiceTermStates() throws ResourceUnavailableException
210     {
211         try
212         {
213             Element body =
214                 XmlUtils.createElement( WsagConstants.WSRF_GET_RESOURCE_PROPERTY_QNAME,
215                     "wsag:ServiceTermState" );
216             body.setAttribute( WsagConstants.WSAG_PREFIX_DECLARATION, WsagConstants.NAMESPACE_URI );
217 
218             Element response = client.invoke( WsagConstants.WSRF_GET_RESOURCE_PROPERTY_ACTION, body );
219 
220             AgreementStatePropertiesType props =
221                 AgreementStatePropertiesType.Factory.parse( response,
222                     new XmlOptions().setLoadReplaceDocumentElement( null ) );
223 
224             ServiceTermStateType[] states = props.getServiceTermStateArray();
225 
226             return states;
227 
228         }
229         catch ( SoapFault e )
230         {
231             throw new ResourceUnavailableException( e );
232         }
233         catch ( XmlException e )
234         {
235             throw new ResourceUnavailableException( e );
236         }
237     }
238 
239     /**
240      * {@inheritDoc}
241      */
242     public AgreementStateType getState() throws ResourceUnavailableException
243     {
244         try
245         {
246             Element body =
247                 XmlUtils.createElement( WsagConstants.WSRF_GET_RESOURCE_PROPERTY_QNAME, "wsag:AgreementState" );
248             body.setAttribute( WsagConstants.WSAG_PREFIX_DECLARATION, WsagConstants.NAMESPACE_URI );
249 
250             Element response = client.invoke( WsagConstants.WSRF_GET_RESOURCE_PROPERTY_ACTION, body );
251 
252             AgreementStateDocument respDoc =
253                 (AgreementStateDocument) XmlObject.Factory.parse( XmlUtils.getFirstElement( response ) );
254 
255             return respDoc.getAgreementState();
256 
257         }
258         catch ( SoapFault e )
259         {
260             throw new ResourceUnavailableException( e );
261         }
262         catch ( XmlException e )
263         {
264             throw new ResourceUnavailableException( e );
265         }
266     }
267 
268     /**
269      * {@inheritDoc}
270      */
271     public TermTreeType getTerms() throws ResourceUnavailableException
272     {
273         try
274         {
275             Element body =
276                 XmlUtils.createElement( WsagConstants.WSRF_GET_RESOURCE_PROPERTY_QNAME, "wsag:Terms" );
277             body.setAttribute( WsagConstants.WSAG_PREFIX_DECLARATION, WsagConstants.NAMESPACE_URI );
278 
279             Element response = client.invoke( WsagConstants.WSRF_GET_RESOURCE_PROPERTY_ACTION, body );
280 
281             TermTreeType respDoc =
282                 TermTreeType.Factory.parse( XmlUtils.getFirstElement( response ),
283                     new XmlOptions().setLoadReplaceDocumentElement( null ) );
284 
285             return respDoc;
286         }
287         catch ( SoapFault e )
288         {
289             throw new ResourceUnavailableException( e );
290         }
291         catch ( XmlException e )
292         {
293             throw new ResourceUnavailableException( e );
294         }
295     }
296 
297     /**
298      * {@inheritDoc}
299      */
300     public void terminate( TerminateInputType reason ) throws ResourceUnavailableException
301     {
302         try
303         {
304             Element inputType =
305                 XmlUtils.createElement( WsagConstants.WSAG_TERMINATE_AGREEMENT_INPUT_QNAME,
306                     reason.getDomNode() );
307             Element body = XmlUtils.createElement( WsagConstants.WSAG_TERMINATE_AGREEMENT_QNAME );
308             body.appendChild( body.getOwnerDocument().importNode( inputType, true ) );
309             Element response = client.invoke( WsagConstants.WSAG_TERMINATE_AGREEMENT_ACTION, body );
310 
311             TerminateResponseDocument respDoc = TerminateResponseDocument.Factory.parse( response );
312             respDoc.getTerminateResponse();
313         }
314         catch ( SoapFault e )
315         {
316             throw new ResourceUnavailableException( e );
317         }
318         catch ( XmlException e )
319         {
320             throw new ResourceUnavailableException( e );
321         }
322         catch ( Exception e )
323         {
324             throw new ResourceUnavailableException( e );
325         }
326     }
327 
328     /**
329      * {@inheritDoc}
330      */
331     public void destroy() throws ResourceUnknownException, ResourceUnavailableException
332     {
333         try
334         {
335             client.destroy();
336         }
337         catch ( SoapFault e )
338         {
339             throw new ResourceUnavailableException( e );
340         }
341     }
342 }