View Javadoc

1   /* 
2    * Copyright (c) 2007, 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 java.util.Date;
38  import java.util.Properties;
39  
40  import javax.security.auth.x500.X500Principal;
41  import javax.xml.namespace.QName;
42  
43  import org.apache.muse.ws.addressing.EndpointReference;
44  import org.apache.muse.ws.addressing.WsaConstants;
45  import org.apache.muse.ws.addressing.soap.SoapFault;
46  import org.apache.muse.ws.resource.properties.set.SetRequest;
47  import org.apache.muse.ws.resource.remote.WsResourceClient;
48  import org.apache.xmlbeans.XmlException;
49  import org.apache.xmlbeans.XmlObject;
50  import org.apache.xmlbeans.XmlString;
51  import org.ogf.graap.wsag.api.client.WsClient;
52  import org.ogf.graap.wsag.api.security.ISecurityProperties;
53  import org.ogf.graap.wsag.security.core.SecurityConstants;
54  import org.ogf.graap.wsag4j.types.engine.ServerIdentityDocument;
55  import org.w3.x2005.x08.addressing.EndpointReferenceDocument;
56  import org.w3.x2005.x08.addressing.EndpointReferenceType;
57  import org.w3c.dom.Element;
58  import org.w3c.dom.Node;
59  
60  /**
61   * AbstractClient
62   * 
63   * @author Oliver Waeldrich
64   * 
65   */
66  public class WsrfResourceClient implements WsClient
67  {
68  
69      /**
70       * Definition of an anonymous SOAP sender EPR.
71       */
72      public static final EndpointReferenceType ANONYMOUS_EPR =
73          convertMuseEndpoint( WsaConstants.ANONYMOUS_EPR );
74  
75      /**
76       * Key to lookup extra header fields.
77       */
78      @Deprecated
79      public static final String EXTRA_HEADERS = "http://de.fraunhofer.scai.wsag4j/extra-headers";
80  
81      //
82      // the MUSE WS client, all requests are delegated to that client
83      //
84      private WsResourceClient client;
85  
86      //
87      // the SOAP client, it sends the SOAP message
88      //
89      private Axis2SoapClient soapClient;
90  
91      //
92      // the login context used to create this client
93      //
94      private ISecurityProperties securityProperties;
95  
96      /**
97       * The default SOAP client used for network communication.
98       */
99      private static Axis2SoapClient defaultSOAPClient = null;
100 
101     /**
102      * @return the defaultSOAPClient
103      */
104     public static Axis2SoapClient getDefaultSOAPClient()
105     {
106         return defaultSOAPClient;
107     }
108 
109     /**
110      * @param defaultSOAPClient
111      *            the default SOAP client to set
112      */
113     public static void setDefaultSOAPClient( Axis2SoapClient defaultSOAPClient )
114     {
115         WsrfResourceClient.defaultSOAPClient = defaultSOAPClient;
116     }
117 
118     /**
119      * Specifies whether the default client should be used or not (default: false).
120      */
121     private static boolean useDefaultSOAPClient = false;
122 
123     /**
124      * @return the useDefaultSOAPClient
125      */
126     public static boolean isUseDefaultSOAPClient()
127     {
128         return useDefaultSOAPClient;
129     }
130 
131     /**
132      * @param useDefaultSOAPClient
133      *            the useDefaultSOAPClient to set
134      */
135     public static void setUseDefaultSOAPClient( boolean useDefaultSOAPClient )
136     {
137         WsrfResourceClient.useDefaultSOAPClient = useDefaultSOAPClient;
138     }
139 
140     //
141     // private constructors
142     //
143     private WsrfResourceClient( EndpointReferenceType destination, EndpointReferenceType source,
144                                 ISecurityProperties securityProperties )
145     {
146         if ( ( useDefaultSOAPClient ) && ( defaultSOAPClient != null ) )
147         {
148             soapClient = defaultSOAPClient;
149         }
150         else
151         {
152             soapClient = new Axis2SoapClient( securityProperties );
153         }
154 
155         client = new WsResourceClient( convertEndpoint( destination ), convertEndpoint( source ), soapClient );
156     }
157 
158     /**
159      * Creates a WSRFResourceClient with the given endpoint and the provided security properties.
160      * 
161      * @param destination
162      *            the WSRF resource endpoint
163      * 
164      * @param securityProperties
165      *            the security properties to use
166      * 
167      */
168     public WsrfResourceClient( EndpointReferenceType destination, ISecurityProperties securityProperties )
169     {
170         this( destination, ANONYMOUS_EPR, securityProperties );
171         this.securityProperties = securityProperties;
172 
173         X500Principal serverIdentity = extractServerIdentity( destination );
174         if ( serverIdentity != null )
175         {
176             this.securityProperties.getProperties().put( SecurityConstants.X500_SERVER_IDENTITY,
177                                                          serverIdentity );
178         }
179     }
180 
181     private X500Principal extractServerIdentity( EndpointReferenceType epr )
182     {
183         X500Principal principal = null;
184 
185         if ( epr.isSetMetadata() )
186         {
187             XmlObject[] identity =
188                 epr.getMetadata().selectChildren( ServerIdentityDocument.type.getDocumentElementName() );
189             if ( identity.length > 0 )
190             {
191                 XmlString serverId = (XmlString) identity[0];
192                 principal = new X500Principal( serverId.getStringValue() );
193             }
194         }
195 
196         return principal;
197     }
198 
199     private static EndpointReferenceType convertMuseEndpoint( EndpointReference epr )
200     {
201         try
202         {
203             EndpointReferenceDocument converted =
204                 (EndpointReferenceDocument) XmlObject.Factory.parse( epr.toXML() );
205             return converted.getEndpointReference();
206         }
207         catch ( XmlException e )
208         {
209             String message = "Error while converting MUSE EPR to endpoint reference.";
210             throw new RuntimeException( message, e );
211         }
212     }
213 
214     private static EndpointReference convertEndpoint( EndpointReferenceType epr )
215     {
216         try
217         {
218             EndpointReferenceDocument doc = EndpointReferenceDocument.Factory.newInstance();
219             doc.setEndpointReference( epr );
220             return new EndpointReference( (Element) doc.getDomNode().getFirstChild() );
221         }
222         catch ( SoapFault e )
223         {
224             String message = "Error while converting endpoint reference to MUSE EPR.";
225             throw new RuntimeException( message, e );
226         }
227     }
228 
229     /**
230      * @return Returns the properties.
231      */
232     public Properties getProperties()
233     {
234         return soapClient.getProperties();
235     }
236 
237     /**
238      * @param properties
239      *            The properties to set.
240      */
241     public void setProperties( Properties properties )
242     {
243         soapClient.setProperties( properties );
244     }
245 
246     /**
247      * @return the securityProperties
248      */
249     public ISecurityProperties getSecurityProperties()
250     {
251         return securityProperties;
252     }
253 
254     /**
255      * @return Returns the endpoint reference for the service.
256      */
257     public EndpointReferenceType getEndpoint()
258     {
259         return convertMuseEndpoint( client.getEndpointReference() );
260     }
261 
262     /**
263      * @param qname
264      *            the name of the resource property to delete
265      * @throws SoapFault
266      *             indicates an error that occurred while deleting the resource property
267      * @see org.apache.muse.ws.resource.remote.WsResourceClient#deleteResourceProperty(javax.xml.namespace.QName)
268      */
269     public void deleteResourceProperty( QName qname ) throws SoapFault
270     {
271         client.deleteResourceProperty( qname );
272     }
273 
274     /**
275      * @throws SoapFault
276      *             indicates an error while destroying the WS resource
277      * 
278      * @see org.apache.muse.ws.resource.remote.WsResourceClient#destroy()
279      */
280     public void destroy() throws SoapFault
281     {
282         client.destroy();
283     }
284 
285     /**
286      * Retrieves multiple resource properties from a WSRF resource.
287      * 
288      * @param qnames
289      *            the QNames of the resource properties
290      * 
291      * @return the retrieved resource properties
292      * 
293      * @throws SoapFault
294      *             indicates an error while retrieving the resource properties
295      * 
296      * @see org.apache.muse.ws.resource.remote.WsResourceClient#getMultipleResourceProperties(javax.xml.namespace.QName[])
297      */
298     public Element[] getMultipleResourceProperties( QName[] qnames ) throws SoapFault
299     {
300         return client.getMultipleResourceProperties( qnames );
301     }
302 
303     /**
304      * Retrieves a single resource property from a WSRF resource.
305      * 
306      * @param qname
307      *            the QName of the resource property
308      * 
309      * @return the retrieved resource property
310      * 
311      * @throws SoapFault
312      *             indicates an error while retrieving the resource property
313      * 
314      * @see org.apache.muse.ws.resource.remote.WsResourceClient#getResourceProperty(javax.xml.namespace.QName)
315      */
316     public Element[] getResourceProperty( QName qname ) throws SoapFault
317     {
318         return client.getResourceProperty( qname );
319     }
320 
321     /**
322      * Retrieves a resource property document from a WSRF resource
323      * 
324      * @return the resource property document
325      * 
326      * @throws SoapFault
327      *             indicates an error while retrieving the resource properties document
328      * 
329      * @see org.apache.muse.ws.resource.remote.WsResourceClient#getResourcePropertyDocument()
330      */
331     public Element getResourcePropertyDocument() throws SoapFault
332     {
333         return client.getResourcePropertyDocument();
334     }
335 
336     /**
337      * @return if the client trace the messages or not
338      * 
339      * @see WsrfResourceClient#isUsingTrace()
340      */
341     public boolean isUsingTrace()
342     {
343         return client.isUsingTrace();
344     }
345 
346     /**
347      * Queries multiple resource properties from a WSRF resource.
348      * 
349      * @param query
350      *            the query
351      * 
352      * @param dialect
353      *            the query dialect (e.g. XPath)
354      * 
355      * @return the retrieved resource properties
356      * 
357      * @throws SoapFault
358      *             indicates an error while querying the resource properties
359      * 
360      * @see org.apache.muse.ws.resource.remote.WsResourceClient#queryResourceProperties(java.lang.String,
361      *      java.lang.String)
362      */
363     public Node[] queryResourceProperties( String query, String dialect ) throws SoapFault
364     {
365         return client.queryResourceProperties( query, dialect );
366     }
367 
368     /**
369      * Sets a resource property.
370      * 
371      * @param request
372      *            the request object
373      * @throws SoapFault
374      *             indicates an error while setting the resource property
375      * @see org.apache.muse.ws.resource.remote.WsResourceClient#setResourceProperties(org.apache.muse.ws.resource.properties.set.SetRequest)
376      */
377     public void setResourceProperties( SetRequest request ) throws SoapFault
378     {
379         client.setResourceProperties( request );
380     }
381 
382     /**
383      * Sets the termination time for a WSRF resource.
384      * 
385      * @param time
386      *            the termination time
387      * 
388      * @return the scheduled termination time
389      * 
390      * @throws SoapFault
391      *             indicates an error while setting the termination time
392      * 
393      * @see org.apache.muse.ws.resource.remote.WsResourceClient#setTerminationTime(java.util.Date)
394      */
395     public Date setTerminationTime( Date time ) throws SoapFault
396     {
397         return client.setTerminationTime( time );
398     }
399 
400     /**
401      * @param trace
402      *            toggles the trace capabilities
403      * 
404      * @see WsrfResourceClient#setTrace(boolean)
405      */
406     public void setTrace( boolean trace )
407     {
408         client.setTrace( trace );
409     }
410 
411     /**
412      * Updates a resource property.
413      * 
414      * @param qname
415      *            the name of the resource property to update
416      * @param values
417      *            the value(s) of the property
418      * @throws SoapFault
419      *             indicates an error while updating the resource property
420      * 
421      * @see org.apache.muse.ws.resource.remote.WsResourceClient#updateResourceProperty(javax.xml.namespace.QName,
422      *      java.lang.Object[])
423      */
424     public void updateResourceProperty( QName qname, Object[] values ) throws SoapFault
425     {
426         client.updateResourceProperty( qname, values );
427     }
428 
429     /**
430      * Invokes an action on a WSRF resource
431      * 
432      * @param action
433      *            the WSA action
434      * 
435      * @param soapBody
436      *            the SOAP body to send
437      * 
438      * @return the SOAP response
439      * 
440      * @throws SoapFault
441      *             indicates an error while invoking the SOAP client
442      * 
443      * @see org.apache.muse.core.AbstractResourceClient#invoke(java.lang.String, org.w3c.dom.Element)
444      */
445     public Element invoke( String action, Element soapBody ) throws SoapFault
446     {
447         return client.invoke( action, soapBody );
448     }
449 
450 }