KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > bus > ws > addressing > ContextUtils


1 package org.objectweb.celtix.bus.ws.addressing;
2
3
4
5 import java.lang.reflect.Method JavaDoc;
6 import java.util.UUID JavaDoc;
7 import java.util.logging.Level JavaDoc;
8 import java.util.logging.Logger JavaDoc;
9
10 import javax.jws.WebMethod;
11 import javax.jws.WebService;
12 import javax.xml.bind.JAXBContext;
13 import javax.xml.bind.JAXBException;
14 import javax.xml.ws.RequestWrapper;
15 import javax.xml.ws.ResponseWrapper;
16 import javax.xml.ws.WebFault;
17 import javax.xml.ws.handler.MessageContext;
18 import static javax.xml.ws.handler.MessageContext.MESSAGE_OUTBOUND_PROPERTY;
19
20 import org.objectweb.celtix.bindings.BindingContextUtils;
21 import org.objectweb.celtix.bindings.DataBindingCallback;
22 import org.objectweb.celtix.bindings.ServerBinding;
23 import org.objectweb.celtix.bus.jaxws.JAXBDataBindingCallback;
24 import org.objectweb.celtix.common.logging.LogUtils;
25 import org.objectweb.celtix.context.ObjectMessageContext;
26 import org.objectweb.celtix.context.OutputStreamMessageContext;
27 import org.objectweb.celtix.transports.ServerTransport;
28 import org.objectweb.celtix.ws.addressing.AddressingProperties;
29 import org.objectweb.celtix.ws.addressing.AttributedURIType;
30 import org.objectweb.celtix.ws.addressing.EndpointReferenceType;
31 import org.objectweb.celtix.ws.addressing.ObjectFactory;
32 import org.objectweb.celtix.ws.addressing.RelatesToType;
33
34 import static org.objectweb.celtix.context.ObjectMessageContext.CORRELATION_IN;
35 import static org.objectweb.celtix.context.ObjectMessageContext.CORRELATION_OUT;
36 import static org.objectweb.celtix.context.ObjectMessageContext.REQUESTOR_ROLE_PROPERTY;
37 import static org.objectweb.celtix.context.OutputStreamMessageContext.ONEWAY_MESSAGE_TF;
38 import static org.objectweb.celtix.ws.addressing.JAXWSAConstants.CLIENT_ADDRESSING_PROPERTIES;
39 import static org.objectweb.celtix.ws.addressing.JAXWSAConstants.CLIENT_ADDRESSING_PROPERTIES_INBOUND;
40 import static org.objectweb.celtix.ws.addressing.JAXWSAConstants.CLIENT_ADDRESSING_PROPERTIES_OUTBOUND;
41 import static org.objectweb.celtix.ws.addressing.JAXWSAConstants.SERVER_ADDRESSING_PROPERTIES_INBOUND;
42 import static org.objectweb.celtix.ws.addressing.JAXWSAConstants.SERVER_ADDRESSING_PROPERTIES_OUTBOUND;
43
44
45 /**
46  * Holder for utility methods relating to contexts.
47  */

48 public final class ContextUtils {
49
50     public static final ObjectFactory WSA_OBJECT_FACTORY = new ObjectFactory();
51
52     private static final String JavaDoc WS_ADDRESSING_PACKAGE =
53         EndpointReferenceType.class.getPackage().getName();
54     private static final Logger JavaDoc LOG = LogUtils.getL7dLogger(ContextUtils.class);
55     
56     private static final String JavaDoc TO_PROPERTY =
57         "org.objectweb.celtix.ws.addressing.to";
58     private static final String JavaDoc REPLYTO_PROPERTY =
59         "org.objectweb.celtix.ws.addressing.replyto";
60     private static final String JavaDoc USING_PROPERTY =
61         "org.objectweb.celtix.ws.addressing.using";
62     
63     /**
64      * Used to fabricate a Uniform Resource Name from a UUID string
65      */

66     private static final String JavaDoc URN_UUID = "urn:uuid:";
67
68
69     private static JAXBContext jaxbContext;
70      
71     /**
72      * Used by MAPAggregator to cache bad MAP fault name
73      */

74     private static final String JavaDoc MAP_FAULT_NAME_PROPERTY =
75         "org.objectweb.celtix.ws.addressing.map.fault.name";
76
77     /**
78      * Used by MAPAggregator to cache bad MAP fault reason
79      */

80     private static final String JavaDoc MAP_FAULT_REASON_PROPERTY =
81         "org.objectweb.celtix.ws.addressing.map.fault.reason";
82  
83    /**
84     * Prevents instantiation.
85     */

86     private ContextUtils() {
87     }
88
89    /**
90     * Determine if context indicates message is outbound.
91     *
92     * @param context the current MessageContext
93     * @return true iff the message direction is outbound
94     */

95     public static boolean isOutbound(MessageContext context) {
96         Boolean JavaDoc outbound = (Boolean JavaDoc)context.get(MESSAGE_OUTBOUND_PROPERTY);
97         return outbound != null && outbound.booleanValue();
98     }
99
100    /**
101     * Determine if context indicates current messaging role is that of
102     * requestor.
103     *
104     * @param context the current MessageContext
105     * @return true iff the current messaging role is that of requestor
106     */

107     public static boolean isRequestor(MessageContext context) {
108         Boolean JavaDoc requestor = (Boolean JavaDoc)context.get(REQUESTOR_ROLE_PROPERTY);
109         return requestor != null && requestor.booleanValue();
110     }
111
112     /**
113      * Determine if context indicates current invocation is oneway.
114      *
115      * @param context the current MessageContext
116      * @return true iff the current invocation is oneway
117      */

118     public static boolean isOneway(MessageContext context) {
119         Boolean JavaDoc oneway = (Boolean JavaDoc)context.get(ONEWAY_MESSAGE_TF);
120         return oneway != null && oneway.booleanValue();
121     }
122
123     /**
124      * Get appropriate context property name for message addressing properties.
125      *
126      * @param isProviderContext true if the binding provider request context
127      * available to the client application as opposed to the message context
128      * visible to handlers
129      * @param isRequestor true iff the current messaging role is that of
130      * requestor
131      * @param isOutbound true iff the message is outbound
132      * @return the property name to use when caching the MAPs in the context
133      */

134     public static String JavaDoc getMAPProperty(boolean isRequestor,
135                                         boolean isProviderContext,
136                                         boolean isOutbound) {
137         return isRequestor
138                 ? isProviderContext
139                  ? CLIENT_ADDRESSING_PROPERTIES
140                  : isOutbound
141                    ? CLIENT_ADDRESSING_PROPERTIES_OUTBOUND
142                    : CLIENT_ADDRESSING_PROPERTIES_INBOUND
143                : isOutbound
144                  ? SERVER_ADDRESSING_PROPERTIES_OUTBOUND
145                  : SERVER_ADDRESSING_PROPERTIES_INBOUND;
146     }
147     
148     /**
149      * Get appropriate context property name for correlation ID.
150      *
151      * @param isOutbound true iff the message is outbound
152      * @return the property name to use when caching the
153      * correlation ID in the context
154      */

155     public static String JavaDoc getCorrelationIDProperty(boolean isOutbound) {
156         return isOutbound ? CORRELATION_OUT : CORRELATION_IN;
157     }
158
159
160     /**
161      * Store MAPs in the context.
162      *
163      * @param context the message context
164      * @param isOutbound true iff the message is outbound
165      */

166     public static void storeMAPs(AddressingProperties maps,
167                                  MessageContext context,
168                                  boolean isOutbound) {
169         storeMAPs(maps, context, isOutbound, isRequestor(context), true, false);
170     }
171
172     /**
173      * Store MAPs in the context.
174      *
175      * @param maps the MAPs to store
176      * @param context the message context
177      * @param isOutbound true iff the message is outbound
178      * @param isRequestor true iff the current messaging role is that of
179      * requestor
180      * @param handler true if HANDLER scope, APPLICATION scope otherwise
181      */

182     public static void storeMAPs(AddressingProperties maps,
183                                  MessageContext context,
184                                  boolean isOutbound,
185                                  boolean isRequestor,
186                                  boolean handler) {
187         storeMAPs(maps, context, isOutbound, isRequestor, handler, false);
188     }
189     
190     /**
191      * Store MAPs in the context.
192      *
193      * @param maps the MAPs to store
194      * @param context the message context
195      * @param isOutbound true iff the message is outbound
196      * @param isRequestor true iff the current messaging role is that of
197      * requestor
198      * @param handler true if HANDLER scope, APPLICATION scope otherwise
199      * @param isProviderContext true if the binding provider request context
200      */

201     public static void storeMAPs(AddressingProperties maps,
202                                  MessageContext context,
203                                  boolean isOutbound,
204                                  boolean isRequestor,
205                                  boolean handler,
206                                  boolean isProviderContext) {
207         if (maps != null) {
208             String JavaDoc mapProperty = getMAPProperty(isRequestor, isProviderContext, isOutbound);
209             LOG.log(Level.INFO,
210                     "associating MAPs with context property {0}",
211                     mapProperty);
212             context.put(mapProperty, maps);
213             context.setScope(mapProperty,
214                              handler
215                              ? MessageContext.Scope.HANDLER
216                              : MessageContext.Scope.APPLICATION);
217         }
218     }
219
220
221     /**
222      * @param context the message context
223      * @param isProviderContext true if the binding provider request context
224      * available to the client application as opposed to the message context
225      * visible to handlers
226      * @param isOutbound true iff the message is outbound
227      * @return the current addressing properties
228      */

229     public static AddressingPropertiesImpl retrieveMAPs(
230                                                    MessageContext context,
231                                                    boolean isProviderContext,
232                                                    boolean isOutbound) {
233         boolean isRequestor = ContextUtils.isRequestor(context);
234         String JavaDoc mapProperty =
235             ContextUtils.getMAPProperty(isProviderContext,
236                                         isRequestor,
237                                         isOutbound);
238         LOG.log(Level.INFO,
239                 "retrieving MAPs from context property {0}",
240                 mapProperty);
241         AddressingPropertiesImpl maps =
242             (AddressingPropertiesImpl)context.get(mapProperty);
243         if (maps != null) {
244             LOG.log(Level.INFO, "current MAPs {0}", maps);
245         } else if (!isProviderContext) {
246             LOG.warning("MAPS_RETRIEVAL_FAILURE_MSG");
247         }
248         return maps;
249     }
250
251     /**
252      * Helper method to get an attributed URI.
253      *
254      * @param uri the URI
255      * @return an AttributedURIType encapsulating the URI
256      */

257     public static AttributedURIType getAttributedURI(String JavaDoc uri) {
258         AttributedURIType attributedURI =
259             WSA_OBJECT_FACTORY.createAttributedURIType();
260         attributedURI.setValue(uri);
261         return attributedURI;
262     }
263
264     /**
265      * Helper method to get a RealtesTo instance.
266      *
267      * @param uri the related URI
268      * @return a RelatesToType encapsulating the URI
269      */

270     public static RelatesToType getRelatesTo(String JavaDoc uri) {
271         RelatesToType relatesTo =
272             WSA_OBJECT_FACTORY.createRelatesToType();
273         relatesTo.setValue(uri);
274         return relatesTo;
275     }
276
277     /**
278      * Helper method to determine if an EPR address is generic (either null,
279      * none or anonymous).
280      *
281      * @param ref the EPR under test
282      * @return true iff the address is generic
283      */

284     public static boolean isGenericAddress(EndpointReferenceType ref) {
285         return ref == null
286                || ref.getAddress() == null
287                || Names.WSA_ANONYMOUS_ADDRESS.equals(ref.getAddress().getValue())
288                || Names.WSA_NONE_ADDRESS.equals(ref.getAddress().getValue());
289     }
290
291     /**
292      * Helper method to determine if an MAPs Action is empty (a null action
293      * is considered empty, whereas a zero length action suppresses
294      * the propogation of the Action property).
295      *
296      * @param ref the MAPs Action under test
297      * @return true iff the Action is empty
298      */

299     public static boolean hasEmptyAction(AddressingProperties maps) {
300         boolean empty = maps.getAction() == null;
301         if (maps.getAction() != null
302             && maps.getAction().getValue().length() == 0) {
303             maps.setAction(null);
304             empty = false;
305         }
306         return empty;
307     }
308
309     /**
310      * Rebase server transport on replyTo
311      *
312      * @param inMAPs the incoming MAPs
313      * @param context the message context
314      */

315     public static void rebaseTransport(AddressingProperties inMAPs,
316                                        MessageContext context,
317                                        ServerBinding serverBinding,
318                                        ServerTransport serverTransport) {
319         // ensure there is a MAPs instance available for the outbound
320
// partial response that contains appropriate To and ReplyTo
321
// properties (i.e. anonymous & none respectively)
322
AddressingPropertiesImpl maps = new AddressingPropertiesImpl();
323         maps.setTo(ContextUtils.getAttributedURI(Names.WSA_ANONYMOUS_ADDRESS));
324         maps.setReplyTo(WSA_OBJECT_FACTORY.createEndpointReferenceType());
325         maps.getReplyTo().setAddress(getAttributedURI(Names.WSA_NONE_ADDRESS));
326         maps.setAction(getAttributedURI(""));
327         maps.exposeAs(inMAPs.getNamespaceURI());
328         storeMAPs(maps, context, true, true, true, true);
329
330         if (serverTransport != null && serverBinding != null) {
331             try {
332                 OutputStreamMessageContext outputContext =
333                     serverTransport.rebase(context, inMAPs.getReplyTo());
334                 if (outputContext != null) {
335                     serverBinding.partialResponse(outputContext,
336                                                   getDataBindingCallback());
337                 }
338                 BindingContextUtils.storeDecoupledResponse(context, true);
339             } catch (Exception JavaDoc e) {
340                 LOG.log(Level.WARNING, "SERVER_TRANSPORT_REBASE_FAILURE_MSG", e);
341             }
342         }
343     }
344
345     /**
346      * Store UsingAddressing override flag in the context
347      *
348      * @param override true if UsingAddressing should be overridden
349      * @param context the message context
350      */

351     public static void storeUsingAddressing(boolean override, MessageContext context) {
352         context.put(USING_PROPERTY, Boolean.valueOf(override));
353         context.setScope(USING_PROPERTY, MessageContext.Scope.APPLICATION);
354     }
355     
356     /**
357      * Retrieve UsingAddressing override flag from the context
358      *
359      * @param override true if UsingAddressing should be overridden
360      * @param context the message context
361      */

362     public static boolean retrieveUsingAddressing(MessageContext context) {
363         Boolean JavaDoc override = (Boolean JavaDoc)context.get(USING_PROPERTY);
364         return override != null && override.booleanValue();
365     }
366
367     /**
368      * Store To EPR in the context
369      *
370      * @param to the To EPR
371      * @param context the message context
372      */

373     public static void storeTo(EndpointReferenceType to,
374                                MessageContext context) {
375         context.put(TO_PROPERTY, to);
376         context.setScope(TO_PROPERTY, MessageContext.Scope.APPLICATION);
377     }
378     
379     /**
380      * Retrieve To EPR from the context.
381      *
382      * @param context the message context
383      * @returned the retrieved EPR
384      */

385     public static EndpointReferenceType retrieveTo(MessageContext context) {
386         /*
387         // required?
388         ClientTransport transport = BindingContextUtils.retrieveClientTransport(context);
389         EndpointReferenceType to = null;
390         if (transport != null) {
391             to = transport.getTargetEndpoint();
392         } else {
393             to = (EndpointReferenceType)context.get(TO_PROPERTY);
394         }
395         return to;
396         */

397         return (EndpointReferenceType)context.get(TO_PROPERTY);
398     }
399     
400     /**
401      * Store ReplyTo EPR in the context
402      *
403      * @param replyTo the ReplyTo EPR
404      * @param context the message context
405      */

406     public static void storeReplyTo(EndpointReferenceType replyTo,
407                                        MessageContext context) {
408         context.put(REPLYTO_PROPERTY, replyTo);
409         context.setScope(REPLYTO_PROPERTY, MessageContext.Scope.APPLICATION);
410     }
411
412     /**
413      * Retrieve ReplyTo EPR from the context.
414      *
415      * @param context the message context
416      * @returned the retrieved EPR
417      */

418     public static EndpointReferenceType retrieveReplyTo(MessageContext context) {
419         /*
420         // required?
421         ClientTransport transport = BindingContextUtils.retrieveClientTransport(context);
422         EndpointReferenceType replyTo = null;
423         if (transport != null) {
424             try {
425                 replyTo = transport.getDecoupledEndpoint();
426             } catch (IOException ioe) {
427                 // ignore
428             }
429         } else {
430             replyTo = (EndpointReferenceType)context.get(REPLYTO_PROPERTY);
431         }
432         return replyTo;
433         */

434         return (EndpointReferenceType)context.get(REPLYTO_PROPERTY);
435     }
436
437     /**
438      * Store bad MAP fault name in the context.
439      *
440      * @param faultName the fault name to store
441      * @param context the message context
442      */

443     public static void storeMAPFaultName(String JavaDoc faultName,
444                                          MessageContext context) {
445         context.put(MAP_FAULT_NAME_PROPERTY, faultName);
446         context.setScope(MAP_FAULT_NAME_PROPERTY,
447                          MessageContext.Scope.HANDLER);
448     }
449
450     /**
451      * Retrieve MAP fault name from the context.
452      *
453      * @param context the message context
454      * @returned the retrieved fault name
455      */

456     public static String JavaDoc retrieveMAPFaultName(MessageContext context) {
457         return (String JavaDoc)context.get(MAP_FAULT_NAME_PROPERTY);
458     }
459
460     /**
461      * Store MAP fault reason in the context.
462      *
463      * @param reason the fault reason to store
464      * @param context the message context
465      */

466     public static void storeMAPFaultReason(String JavaDoc reason,
467                                            MessageContext context) {
468         context.put(MAP_FAULT_REASON_PROPERTY, reason);
469         context.setScope(MAP_FAULT_REASON_PROPERTY,
470                          MessageContext.Scope.HANDLER);
471     }
472
473     /**
474      * Retrieve MAP fault reason from the context.
475      *
476      * @param context the message context
477      * @returned the retrieved fault reason
478      */

479     public static String JavaDoc retrieveMAPFaultReason(MessageContext context) {
480         return (String JavaDoc)context.get(MAP_FAULT_REASON_PROPERTY);
481     }
482
483     /**
484      * Store correlation ID in the context
485      *
486      * @param id the correlation ID
487      * @param isOutbound true if message is outbound
488      * @param context the message context
489      */

490     public static void storeCorrelationID(RelatesToType id,
491                                           boolean isOutbound,
492                                           MessageContext context) {
493         storeCorrelationID(id.getValue(), isOutbound, context);
494     }
495     
496     /**
497      * Store correlation ID in the context
498      *
499      * @param id the correlation ID
500      * @param isOutbound true if message is outbound
501      * @param context the message context
502      */

503     public static void storeCorrelationID(AttributedURIType id,
504                                           boolean isOutbound,
505                                           MessageContext context) {
506         storeCorrelationID(id.getValue(), isOutbound, context);
507     }
508     
509     /**
510      * Store correlation ID in the context
511      *
512      * @param id the correlation ID
513      * @param isOutbound true if message is outbound
514      * @param context the message context
515      */

516     protected static void storeCorrelationID(String JavaDoc id,
517                                            boolean isOutbound,
518                                            MessageContext context) {
519         context.put(getCorrelationIDProperty(isOutbound), id);
520         context.setScope(getCorrelationIDProperty(isOutbound),
521                          MessageContext.Scope.APPLICATION);
522     }
523     
524     /**
525      * Retrieve correlation ID from the context.
526      *
527      * @param context the message context
528      * @param isOutbound true if message is outbound
529      * @returned the retrieved correlation ID
530      */

531     public static String JavaDoc retrieveCorrelationID(MessageContext context,
532                                                boolean isOutbound) {
533         return (String JavaDoc)context.get(getCorrelationIDProperty(isOutbound));
534     }
535     
536     /**
537      * Retrieve a JAXBContext for marshalling and unmarshalling JAXB generated
538      * types.
539      *
540      * @return a JAXBContext
541      */

542     public static JAXBContext getJAXBContext() throws JAXBException {
543         synchronized (ContextUtils.class) {
544             if (jaxbContext == null) {
545                 jaxbContext = JAXBContext.newInstance(WS_ADDRESSING_PACKAGE);
546             }
547         }
548         return jaxbContext;
549     }
550
551     /**
552      * Set the encapsulated JAXBContext (used by unit tests).
553      *
554      * @param ctx JAXBContext
555      */

556     public static void setJAXBContext(JAXBContext ctx) throws JAXBException {
557         synchronized (ContextUtils.class) {
558             jaxbContext = ctx;
559         }
560     }
561     
562     
563     /**
564      * @return a generated UUID
565      */

566     public static String JavaDoc generateUUID() {
567         return URN_UUID + UUID.randomUUID();
568     }
569     
570     /**
571      * Construct the Action URI.
572      *
573      * @param context the message context
574      * @return the Action URI
575      */

576     public static AttributedURIType getAction(MessageContext context) {
577         String JavaDoc action = null;
578         // REVISIT: add support for @{Fault}Action annotation (generated
579
// from the wsaw:Action WSDL element)
580
LOG.fine("Determining action");
581         Throwable JavaDoc fault =
582             (Throwable JavaDoc)context.get(ObjectMessageContext.METHOD_FAULT);
583         Method JavaDoc method = (Method JavaDoc)context.get(ObjectMessageContext.METHOD_OBJ);
584         LOG.fine("method: " + method + ", fault: " + fault);
585         if (method != null) {
586             if (fault != null) {
587                 WebFault webFault = fault.getClass().getAnnotation(WebFault.class);
588                 action = getAction(webFault.targetNamespace(),
589                                    method,
590                                    webFault.name(),
591                                    true);
592             } else {
593                 if (ContextUtils.isRequestor(context)) {
594                     RequestWrapper requestWrapper =
595                         method.getAnnotation(RequestWrapper.class);
596                     if (requestWrapper != null) {
597                         action = getAction(requestWrapper.targetNamespace(),
598                                            method,
599                                            requestWrapper.localName(),
600                                            false);
601                     } else {
602                         //TODO: What if the WSDL is RPC-Literal encoded.
603
// We need to get action out of available annotations?
604
//
605

606                         WebService wsAnnotation = method.getDeclaringClass().getAnnotation(WebService.class);
607                         WebMethod wmAnnotation = method.getAnnotation(WebMethod.class);
608                         
609                         action = getAction(wsAnnotation.targetNamespace(),
610                                            method,
611                                            wmAnnotation.operationName(),
612                                            false);
613                     }
614                         
615                 } else {
616                     ResponseWrapper responseWrapper =
617                         method.getAnnotation(ResponseWrapper.class);
618                     if (responseWrapper != null) {
619                         action = getAction(responseWrapper.targetNamespace(),
620                                            method,
621                                            responseWrapper.localName(),
622                                           false);
623                     } else {
624                        //RPC-Literal case.
625
WebService wsAnnotation = method.getDeclaringClass().getAnnotation(WebService.class);
626                         WebMethod wmAnnotation = method.getAnnotation(WebMethod.class);
627                         
628                         action = getAction(wsAnnotation.targetNamespace(),
629                                            method,
630                                            wmAnnotation.operationName(),
631                                            false);
632                     }
633                 }
634             }
635         }
636         return action != null ? getAttributedURI(action) : null;
637     }
638         
639
640     /**
641      * Construct the Action string.
642      *
643      * @param targetNamespace the target namespace
644      * @param method the method
645      * @param localName the local name
646      * @param isFault true if a fault
647      * @return action string
648      */

649     private static String JavaDoc getAction(String JavaDoc targetNamespace,
650                                     Method JavaDoc method,
651                                     String JavaDoc localName,
652                                     boolean isFault) {
653         String JavaDoc action = null;
654         action = targetNamespace;
655         action += Names.WSA_ACTION_DELIMITER;
656         action += method.getDeclaringClass().getSimpleName();
657         if (isFault) {
658             action += method.getName();
659             action += Names.WSA_FAULT_DELIMITER;
660         }
661         action += Names.WSA_ACTION_DELIMITER;
662         action += localName;
663         return action;
664     }
665
666     /**
667      * Get a DataBindingCallback (for use with an outgoing partial response).
668      *
669      * @return a DataBindingCallback
670      */

671     private static DataBindingCallback getDataBindingCallback()
672         throws JAXBException {
673         return new JAXBDataBindingCallback(null,
674                                            DataBindingCallback.Mode.PARTS,
675                                            getJAXBContext());
676     }
677 }
678
679
680
681
682
683
684
685
686
687
688
689
Popular Tags