| 1 package org.objectweb.celtix.bus.ws.addressing; 2 3 4 5 import java.lang.reflect.Method ; 6 import java.util.UUID ; 7 import java.util.logging.Level ; 8 import java.util.logging.Logger ; 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 48 public final class ContextUtils { 49 50 public static final ObjectFactory WSA_OBJECT_FACTORY = new ObjectFactory(); 51 52 private static final String WS_ADDRESSING_PACKAGE = 53 EndpointReferenceType.class.getPackage().getName(); 54 private static final Logger LOG = LogUtils.getL7dLogger(ContextUtils.class); 55 56 private static final String TO_PROPERTY = 57 "org.objectweb.celtix.ws.addressing.to"; 58 private static final String REPLYTO_PROPERTY = 59 "org.objectweb.celtix.ws.addressing.replyto"; 60 private static final String USING_PROPERTY = 61 "org.objectweb.celtix.ws.addressing.using"; 62 63 66 private static final String URN_UUID = "urn:uuid:"; 67 68 69 private static JAXBContext jaxbContext; 70 71 74 private static final String MAP_FAULT_NAME_PROPERTY = 75 "org.objectweb.celtix.ws.addressing.map.fault.name"; 76 77 80 private static final String MAP_FAULT_REASON_PROPERTY = 81 "org.objectweb.celtix.ws.addressing.map.fault.reason"; 82 83 86 private ContextUtils() { 87 } 88 89 95 public static boolean isOutbound(MessageContext context) { 96 Boolean outbound = (Boolean )context.get(MESSAGE_OUTBOUND_PROPERTY); 97 return outbound != null && outbound.booleanValue(); 98 } 99 100 107 public static boolean isRequestor(MessageContext context) { 108 Boolean requestor = (Boolean )context.get(REQUESTOR_ROLE_PROPERTY); 109 return requestor != null && requestor.booleanValue(); 110 } 111 112 118 public static boolean isOneway(MessageContext context) { 119 Boolean oneway = (Boolean )context.get(ONEWAY_MESSAGE_TF); 120 return oneway != null && oneway.booleanValue(); 121 } 122 123 134 public static String 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 155 public static String getCorrelationIDProperty(boolean isOutbound) { 156 return isOutbound ? CORRELATION_OUT : CORRELATION_IN; 157 } 158 159 160 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 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 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 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 229 public static AddressingPropertiesImpl retrieveMAPs( 230 MessageContext context, 231 boolean isProviderContext, 232 boolean isOutbound) { 233 boolean isRequestor = ContextUtils.isRequestor(context); 234 String 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 257 public static AttributedURIType getAttributedURI(String uri) { 258 AttributedURIType attributedURI = 259 WSA_OBJECT_FACTORY.createAttributedURIType(); 260 attributedURI.setValue(uri); 261 return attributedURI; 262 } 263 264 270 public static RelatesToType getRelatesTo(String uri) { 271 RelatesToType relatesTo = 272 WSA_OBJECT_FACTORY.createRelatesToType(); 273 relatesTo.setValue(uri); 274 return relatesTo; 275 } 276 277 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 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 315 public static void rebaseTransport(AddressingProperties inMAPs, 316 MessageContext context, 317 ServerBinding serverBinding, 318 ServerTransport serverTransport) { 319 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 e) { 340 LOG.log(Level.WARNING, "SERVER_TRANSPORT_REBASE_FAILURE_MSG", e); 341 } 342 } 343 } 344 345 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 362 public static boolean retrieveUsingAddressing(MessageContext context) { 363 Boolean override = (Boolean )context.get(USING_PROPERTY); 364 return override != null && override.booleanValue(); 365 } 366 367 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 385 public static EndpointReferenceType retrieveTo(MessageContext context) { 386 397 return (EndpointReferenceType)context.get(TO_PROPERTY); 398 } 399 400 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 418 public static EndpointReferenceType retrieveReplyTo(MessageContext context) { 419 434 return (EndpointReferenceType)context.get(REPLYTO_PROPERTY); 435 } 436 437 443 public static void storeMAPFaultName(String 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 456 public static String retrieveMAPFaultName(MessageContext context) { 457 return (String )context.get(MAP_FAULT_NAME_PROPERTY); 458 } 459 460 466 public static void storeMAPFaultReason(String 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 479 public static String retrieveMAPFaultReason(MessageContext context) { 480 return (String )context.get(MAP_FAULT_REASON_PROPERTY); 481 } 482 483 490 public static void storeCorrelationID(RelatesToType id, 491 boolean isOutbound, 492 MessageContext context) { 493 storeCorrelationID(id.getValue(), isOutbound, context); 494 } 495 496 503 public static void storeCorrelationID(AttributedURIType id, 504 boolean isOutbound, 505 MessageContext context) { 506 storeCorrelationID(id.getValue(), isOutbound, context); 507 } 508 509 516 protected static void storeCorrelationID(String 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 531 public static String retrieveCorrelationID(MessageContext context, 532 boolean isOutbound) { 533 return (String )context.get(getCorrelationIDProperty(isOutbound)); 534 } 535 536 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 556 public static void setJAXBContext(JAXBContext ctx) throws JAXBException { 557 synchronized (ContextUtils.class) { 558 jaxbContext = ctx; 559 } 560 } 561 562 563 566 public static String generateUUID() { 567 return URN_UUID + UUID.randomUUID(); 568 } 569 570 576 public static AttributedURIType getAction(MessageContext context) { 577 String action = null; 578 LOG.fine("Determining action"); 581 Throwable fault = 582 (Throwable )context.get(ObjectMessageContext.METHOD_FAULT); 583 Method method = (Method )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 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 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 649 private static String getAction(String targetNamespace, 650 Method method, 651 String localName, 652 boolean isFault) { 653 String 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 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 |