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.wsrf.impl;
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.security.ISecurityProperties;
52 import org.ogf.graap.wsag.api.security.SecurityConstants;
53 import org.ogf.graap.wsag.client.api.RemoteClient;
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 extends RemoteClient
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 final 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 Properties properties, ISecurityProperties securityProperties )
145 {
146 super( destination, properties, securityProperties );
147
148 if ( ( useDefaultSOAPClient ) && ( defaultSOAPClient != null ) )
149 {
150 soapClient = defaultSOAPClient;
151 }
152 else
153 {
154 soapClient = new Axis2SoapClient( properties, securityProperties );
155 }
156
157 client = new WsResourceClient( convertEndpoint( destination ), convertEndpoint( source ), soapClient );
158 }
159
160 /**
161 * Creates a WSRFResourceClient with the given endpoint and the provided security properties.
162 *
163 * @param destination
164 * the WSRF resource endpoint
165 *
166 * @param securityProperties
167 * the security properties to use
168 *
169 */
170 public WsrfResourceClient( EndpointReferenceType destination, Properties properties,
171 ISecurityProperties securityProperties )
172 {
173 this( destination, ANONYMOUS_EPR, properties, securityProperties );
174 this.securityProperties = securityProperties;
175
176 X500Principal serverIdentity = extractServerIdentity( destination );
177 if ( serverIdentity != null )
178 {
179 this.securityProperties.getProperties().put( SecurityConstants.X500_SERVER_IDENTITY,
180 serverIdentity );
181 }
182 }
183
184 private X500Principal extractServerIdentity( EndpointReferenceType epr )
185 {
186 X500Principal principal = null;
187
188 if ( epr.isSetMetadata() )
189 {
190 XmlObject[] identity =
191 epr.getMetadata().selectChildren( ServerIdentityDocument.type.getDocumentElementName() );
192 if ( identity.length > 0 )
193 {
194 XmlString serverId = (XmlString) identity[0];
195 principal = new X500Principal( serverId.getStringValue() );
196 }
197 }
198
199 return principal;
200 }
201
202 private static EndpointReferenceType convertMuseEndpoint( EndpointReference epr )
203 {
204 try
205 {
206 EndpointReferenceDocument converted =
207 (EndpointReferenceDocument) XmlObject.Factory.parse( epr.toXML() );
208 return converted.getEndpointReference();
209 }
210 catch ( XmlException e )
211 {
212 String message = "Error while converting MUSE EPR to endpoint reference.";
213 throw new RuntimeException( message, e );
214 }
215 }
216
217 private static EndpointReference convertEndpoint( EndpointReferenceType epr )
218 {
219 try
220 {
221 EndpointReferenceDocument doc = EndpointReferenceDocument.Factory.newInstance();
222 doc.setEndpointReference( epr );
223 return new EndpointReference( (Element) doc.getDomNode().getFirstChild() );
224 }
225 catch ( SoapFault e )
226 {
227 String message = "Error while converting endpoint reference to MUSE EPR.";
228 throw new RuntimeException( message, e );
229 }
230 }
231
232 /**
233 * @return Returns the properties.
234 */
235 @Override
236 public Properties getProperties()
237 {
238 return soapClient.getProperties();
239 }
240
241 /**
242 * @param properties
243 * The properties to set.
244 */
245 @Override
246 public void setProperties( Properties properties )
247 {
248 soapClient.setProperties( properties );
249 }
250
251 /**
252 * @return the securityProperties
253 */
254 @Override
255 public ISecurityProperties getSecurityProperties()
256 {
257 return securityProperties;
258 }
259
260 /**
261 * @return Returns the endpoint reference for the service.
262 */
263 public EndpointReferenceType getEndpoint()
264 {
265 return convertMuseEndpoint( client.getEndpointReference() );
266 }
267
268 /**
269 * @param qname
270 * the name of the resource property to delete
271 * @throws SoapFault
272 * indicates an error that occurred while deleting the resource property
273 * @see org.apache.muse.ws.resource.remote.WsResourceClient#deleteResourceProperty(javax.xml.namespace.QName)
274 */
275 public void deleteResourceProperty( QName qname ) throws SoapFault
276 {
277 client.deleteResourceProperty( qname );
278 }
279
280 /**
281 * @throws SoapFault
282 * indicates an error while destroying the WS resource
283 *
284 * @see org.apache.muse.ws.resource.remote.WsResourceClient#destroy()
285 */
286 public void destroy() throws SoapFault
287 {
288 client.destroy();
289 }
290
291 /**
292 * Retrieves multiple resource properties from a WSRF resource.
293 *
294 * @param qnames
295 * the QNames of the resource properties
296 *
297 * @return the retrieved resource properties
298 *
299 * @throws SoapFault
300 * indicates an error while retrieving the resource properties
301 *
302 * @see org.apache.muse.ws.resource.remote.WsResourceClient#getMultipleResourceProperties(javax.xml.namespace.QName[])
303 */
304 public Element[] getMultipleResourceProperties( QName[] qnames ) throws SoapFault
305 {
306 return client.getMultipleResourceProperties( qnames );
307 }
308
309 /**
310 * Retrieves a single resource property from a WSRF resource.
311 *
312 * @param qname
313 * the QName of the resource property
314 *
315 * @return the retrieved resource property
316 *
317 * @throws SoapFault
318 * indicates an error while retrieving the resource property
319 *
320 * @see org.apache.muse.ws.resource.remote.WsResourceClient#getResourceProperty(javax.xml.namespace.QName)
321 */
322 public Element[] getResourceProperty( QName qname ) throws SoapFault
323 {
324 return client.getResourceProperty( qname );
325 }
326
327 /**
328 * Retrieves a resource property document from a WSRF resource
329 *
330 * @return the resource property document
331 *
332 * @throws SoapFault
333 * indicates an error while retrieving the resource properties document
334 *
335 * @see org.apache.muse.ws.resource.remote.WsResourceClient#getResourcePropertyDocument()
336 */
337 public Element getResourcePropertyDocument() throws SoapFault
338 {
339 return client.getResourcePropertyDocument();
340 }
341
342 /**
343 * @return if the client trace the messages or not
344 *
345 * @see WsrfResourceClient#isUsingTrace()
346 */
347 @Override
348 public boolean isUsingTrace()
349 {
350 return client.isUsingTrace();
351 }
352
353 /**
354 * Queries multiple resource properties from a WSRF resource.
355 *
356 * @param query
357 * the query
358 *
359 * @param dialect
360 * the query dialect (e.g. XPath)
361 *
362 * @return the retrieved resource properties
363 *
364 * @throws SoapFault
365 * indicates an error while querying the resource properties
366 *
367 * @see org.apache.muse.ws.resource.remote.WsResourceClient#queryResourceProperties(java.lang.String,
368 * java.lang.String)
369 */
370 public Node[] queryResourceProperties( String query, String dialect ) throws SoapFault
371 {
372 return client.queryResourceProperties( query, dialect );
373 }
374
375 /**
376 * Sets a resource property.
377 *
378 * @param request
379 * the request object
380 * @throws SoapFault
381 * indicates an error while setting the resource property
382 * @see org.apache.muse.ws.resource.remote.WsResourceClient#setResourceProperties(org.apache.muse.ws.resource.properties.set.SetRequest)
383 */
384 public void setResourceProperties( SetRequest request ) throws SoapFault
385 {
386 client.setResourceProperties( request );
387 }
388
389 /**
390 * Sets the termination time for a WSRF resource.
391 *
392 * @param time
393 * the termination time
394 *
395 * @return the scheduled termination time
396 *
397 * @throws SoapFault
398 * indicates an error while setting the termination time
399 *
400 * @see org.apache.muse.ws.resource.remote.WsResourceClient#setTerminationTime(java.util.Date)
401 */
402 public Date setTerminationTime( Date time ) throws SoapFault
403 {
404 return client.setTerminationTime( time );
405 }
406
407 /**
408 * @param trace
409 * toggles the trace capabilities
410 *
411 * @see WsrfResourceClient#setTrace(boolean)
412 */
413 @Override
414 public void setTrace( boolean trace )
415 {
416 client.setTrace( trace );
417 }
418
419 /**
420 * Updates a resource property.
421 *
422 * @param qname
423 * the name of the resource property to update
424 * @param values
425 * the value(s) of the property
426 * @throws SoapFault
427 * indicates an error while updating the resource property
428 *
429 * @see org.apache.muse.ws.resource.remote.WsResourceClient#updateResourceProperty(javax.xml.namespace.QName,
430 * java.lang.Object[])
431 */
432 public void updateResourceProperty( QName qname, Object[] values ) throws SoapFault
433 {
434 client.updateResourceProperty( qname, values );
435 }
436
437 /**
438 * Invokes an action on a WSRF resource
439 *
440 * @param action
441 * the WSA action
442 *
443 * @param soapBody
444 * the SOAP body to send
445 *
446 * @return the SOAP response
447 *
448 * @throws SoapFault
449 * indicates an error while invoking the SOAP client
450 *
451 * @see org.apache.muse.core.AbstractResourceClient#invoke(java.lang.String, org.w3c.dom.Element)
452 */
453 public Element invoke( String action, Element soapBody ) throws SoapFault
454 {
455 return client.invoke( action, soapBody );
456 }
457
458 }