1 10 11 package com.sun.enterprise.jbi.serviceengine.util.soap; 12 13 import org.w3c.dom.*; 14 15 import com.sun.enterprise.deployment.ServiceReferenceDescriptor; 16 import com.sun.enterprise.deployment.ServiceRefPortInfo; 17 import com.sun.enterprise.jbi.serviceengine.ServiceEngineException; 18 import com.sun.enterprise.jbi.serviceengine.comm.MessageSender; 19 import com.sun.enterprise.jbi.serviceengine.core.JavaEEServiceEngineContext; 20 import com.sun.enterprise.jbi.serviceengine.core.ServiceEngineEndpoint; 21 import com.sun.enterprise.jbi.serviceengine.core.EndpointRegistry; 22 import com.sun.jbi.wsdl11wrapper.*; 23 import com.sun.logging.LogDomains; 24 import javax.jbi.messaging.*; 25 import javax.jbi.messaging.Fault; 26 import javax.jbi.servicedesc.ServiceEndpoint; 27 import javax.xml.namespace.QName ; 28 import javax.xml.soap.SOAPBody ; 29 import javax.xml.soap.SOAPEnvelope ; 30 import javax.xml.soap.SOAPMessage ; 31 import javax.xml.soap.SOAPPart ; 32 import javax.xml.transform.*; 33 import javax.xml.transform.dom.*; 34 import javax.wsdl.*; 35 import javax.wsdl.factory.*; 36 import java.util.logging.Logger ; 37 import java.util.logging.Level ; 38 import java.util.concurrent.ConcurrentHashMap ; 39 40 45 public class MessageExchangeHelper { 46 47 private static Logger logger = 48 LogDomains.getLogger(LogDomains.SERVER_LOGGER); 49 50 private MessageExchange messageExchange; 51 private ServiceRefPortInfo portInfo; 52 53 private static ConcurrentHashMap wsdlCache = new ConcurrentHashMap (11,0.75f,4); 55 56 private static String WSDL11 = "http://schemas.xmlsoap.org/wsdl/"; 57 58 59 public MessageExchangeHelper() { 60 } 61 62 public void setMessageExchange(MessageExchange messageExchange) { 63 this.messageExchange = messageExchange; 64 } 65 66 67 public SOAPMessage denormalizeMessage(boolean inFlag) throws ServiceEngineException { 68 validateMessageExchange(); 69 NormalizedMessage normalizedMsg = null; 70 71 if(inFlag) { 72 if(isInOutMessageExchange()) { 73 InOut inOutExchange = (InOut)messageExchange; 74 normalizedMsg = inOutExchange.getInMessage(); 75 76 } else { 77 InOnly inOnlyExchange = (InOnly)messageExchange; 78 normalizedMsg = inOnlyExchange.getInMessage(); 79 } 80 } else { 81 normalizedMsg = (messageExchange.getFault()!=null)? 83 messageExchange.getFault() : ((InOut)messageExchange).getOutMessage(); 84 } 86 87 QName operationQName = messageExchange.getOperation(); 88 String pattern = messageExchange.getPattern().toString(); 89 90 Operation operation = new Operation(operationQName.getLocalPart(), pattern); 91 93 MessageDenormalizerImpl d = new MessageDenormalizerImpl(); 94 SOAPWrapper wrapper; 95 if(messageExchange.getFault()!=null) { 96 wrapper = d.denormalizeFaultMessage((Fault)normalizedMsg); 98 } else { 99 unWrapMessage(normalizedMsg); 100 wrapper = d.denormalizeMessage(normalizedMsg, operation, !inFlag); 101 } 102 SOAPMessage message = wrapper.getMessage(); 103 printSOAPMessage( "Denormalizing in ? "+ inFlag + "message :" , message) ; 104 return message; 105 106 } 107 108 public boolean isInOutMessageExchange() { 109 return messageExchange instanceof InOut; 110 } 111 112 public void normalizeMessage(SOAPMessage soapMessage, boolean inFlag) throws ServiceEngineException { 113 validateMessageExchange(); 114 if(soapMessage != null) { 115 116 117 printSOAPMessage( "normalizing in ? "+ inFlag + "message :" , soapMessage) ; 118 NormalizedMessage normalizedMsg = null; 119 Operation operation = null; 120 try { 121 122 boolean isFault = (soapMessage.getSOAPBody().getFault() != null); 123 normalizedMsg = 124 (isFault)? messageExchange.createFault() : messageExchange.createMessage(); 125 126 if(isFault) { 128 SOAPWrapper wrapper = new SOAPWrapper(soapMessage); 130 MessageNormalizerImpl normalizer = new MessageNormalizerImpl(); 131 normalizer.normalizeFaultMessage(wrapper, normalizedMsg); 132 } else { 133 normalizeAndWrapMessage(normalizedMsg, soapMessage); 135 } 136 137 if(isFault) 138 messageExchange.setFault((javax.jbi.messaging.Fault)normalizedMsg); 139 else if(inFlag) { 140 if(isInOutMessageExchange()) { 141 ((InOut)messageExchange).setInMessage(normalizedMsg); 142 } else { 143 ((InOnly)messageExchange).setInMessage(normalizedMsg); 144 } 145 } else ((InOut)messageExchange).setOutMessage(normalizedMsg); 147 } catch(Exception e) { 148 e.printStackTrace(); 149 } 150 151 } 152 153 } 154 155 private void unWrapMessage(NormalizedMessage normalizedMsg) { 156 try { 157 String wsdlPath = null; 158 String endpointName = null; 159 QName serviceName = null; 160 Definition mDefinition = null; 161 EndpointMetaData emd = null; 162 163 ServiceEndpoint serviceEndpoint = messageExchange.getEndpoint(); 164 if(serviceEndpoint != null) { 165 EndpointRegistry endpointRegistry = EndpointRegistry.getInstance(); 166 endpointName = serviceEndpoint.getEndpointName(); 167 serviceName = serviceEndpoint.getServiceName(); 168 ServiceEngineEndpoint serviceEngineEndpoint = endpointRegistry.get( 169 serviceName, endpointName); 170 if(serviceEngineEndpoint != null) 171 wsdlPath = serviceEngineEndpoint.getWsdlPath(); 172 } 173 if(wsdlPath == null && portInfo != null) { 174 ServiceReferenceDescriptor serviceRef = portInfo.getServiceReference(); 175 serviceName = serviceRef.getServiceName(); 177 endpointName = portInfo.hasWsdlPort() ? portInfo.getWsdlPort().getLocalPart() : portInfo.getName(); 178 if(serviceRef.getWsdlFileUrl() != null && serviceRef.getWsdlFileUrl().getProtocol().equals("file")) 179 wsdlPath = serviceRef.getWsdlFileUrl().getPath(); 180 else 181 wsdlPath = serviceRef.getWsdlFileUri(); 182 } 183 184 190 191 emd = (EndpointMetaData)wsdlCache.get(wsdlPath); 192 if(emd == null) { 193 if(logger.isLoggable(Level.FINEST)) 194 logger.log(Level.FINEST," Now fresh add to cache"); 195 try { 196 mDefinition = getWsdlDefinition(wsdlPath); 197 } catch (Exception e) { 198 logger.log(Level.WARNING, e.getMessage(), e); 200 } 201 emd = new EndpointMetaData(mDefinition); 202 wsdlCache.put(wsdlPath,emd); 203 if(logger.isLoggable(Level.FINEST)) 204 logger.log(Level.FINEST," Now fresh add to cache done"); 205 } 206 mDefinition = emd.getDefinition(); 207 if(isWsdl11(mDefinition)) { 208 Wsdl11WrapperHelper helper = new Wsdl11WrapperHelper(mDefinition); 209 Source source = normalizedMsg.getContent(); 210 String operationName = messageExchange.getOperation().getLocalPart(); 211 boolean isProvider = messageExchange.getRole().equals(MessageExchange.Role.PROVIDER); 212 Document unwrappedDoc = helper.unwrapMessage(source, 213 serviceName, 214 endpointName, 215 operationName, 216 isProvider); 217 normalizedMsg.setContent(new DOMSource(unwrappedDoc)); 218 } 219 }catch(Exception e) { 220 e.printStackTrace(); 221 } 222 } 223 224 private void normalizeAndWrapMessage(NormalizedMessage normalizedMsg, 225 SOAPMessage soapMessage) { 226 try { 227 String wsdlPath = null; 228 String endpointName = null; 229 QName serviceName = null; 230 Definition mDefinition = null; 231 EndpointMetaData emd = null; 232 ServiceEndpoint serviceEndpoint = messageExchange.getEndpoint(); 233 if(serviceEndpoint != null) { 234 EndpointRegistry endpointRegistry = EndpointRegistry.getInstance(); 235 endpointName = serviceEndpoint.getEndpointName(); 236 serviceName = serviceEndpoint.getServiceName(); 237 ServiceEngineEndpoint serviceEngineEndpoint = endpointRegistry.get( 238 serviceName, endpointName); 239 if(serviceEngineEndpoint != null) 240 wsdlPath = serviceEngineEndpoint.getWsdlPath(); 241 } 242 if(wsdlPath == null && portInfo != null) { 243 ServiceReferenceDescriptor serviceRef = portInfo.getServiceReference(); 244 serviceName = serviceRef.getServiceName(); 246 endpointName = portInfo.hasWsdlPort() ? portInfo.getWsdlPort().getLocalPart() : portInfo.getName(); 247 if(serviceRef.getWsdlFileUrl() != null && serviceRef.getWsdlFileUrl().getProtocol().equals("file")) 248 wsdlPath = serviceRef.getWsdlFileUrl().getPath(); 249 else 250 wsdlPath = serviceRef.getWsdlFileUri(); 251 } 252 258 emd = (EndpointMetaData)wsdlCache.get(wsdlPath); 259 if(emd == null) { 260 if(logger.isLoggable(Level.FINEST)) 261 logger.log(Level.FINEST," wrap : Now fresh add to cache"); 262 mDefinition = getWsdlDefinition(wsdlPath); 263 emd= new EndpointMetaData(mDefinition,serviceName,endpointName); 264 emd.resolve(); 265 wsdlCache.put(wsdlPath,emd); 266 if(logger.isLoggable(Level.FINEST)) 267 logger.log(Level.FINEST,"wrap : Now fresh add to cache done"); 268 } 269 270 mDefinition = emd.getDefinition(); 271 Wsdl11WrapperHelper helper = new Wsdl11WrapperHelper(mDefinition); 272 if(isWsdl11(mDefinition)) { 273 String operationName = null; 275 Operation operation = null; 276 if (messageExchange.getOperation() == null) { 277 operationName = emd.getOperationMetaData(soapMessage).getOperationName(); 278 if (logger.isLoggable(Level.FINEST)) 279 logger.finest("Operation Name is :" + operationName); 280 QName opQName = new QName ( 281 messageExchange.getService().getNamespaceURI(), 282 operationName); 283 messageExchange.setOperation(opQName); 284 operation = getOperation(soapMessage, 285 serviceName, operationName); 286 } else { 287 operationName = messageExchange.getOperation().getLocalPart(); 288 operation = new Operation(operationName, 289 messageExchange.getPattern().toString()); 290 } 291 292 SOAPWrapper wrapper = new SOAPWrapper(soapMessage); 293 MessageNormalizerImpl normalizer = new MessageNormalizerImpl(); 294 normalizer.normalizeMessage(wrapper, normalizedMsg, operation); 295 Document wrappedDoc = null; 296 Source source = normalizedMsg.getContent(); 297 boolean isProvider = messageExchange.getRole().equals(MessageExchange.Role.PROVIDER); 298 wrappedDoc = helper.wrapMessage(source, serviceName, endpointName, operationName,!isProvider); 299 300 normalizedMsg.setContent(new DOMSource(wrappedDoc)); 301 } 302 }catch(Exception e) { 303 e.printStackTrace(); 304 } 305 306 } 307 308 private Definition getWsdlDefinition(String wsdlPath) throws Exception { 309 javax.wsdl.factory.WSDLFactory mFactory = WSDLFactory.newInstance(); 310 javax.wsdl.xml.WSDLReader mReader = mFactory.newWSDLReader(); 311 Definition mDefinition = mReader.readWSDL(wsdlPath); 312 return mDefinition; 313 } 314 315 private boolean isWsdl11(Definition mDefinition) 316 throws Wsdl11WrapperHelperException { 317 try 318 { 319 if (mDefinition != null) { 320 String xmlns = mDefinition.getNamespace (""); 321 if (xmlns.trim ().equals(WSDL11)) { 322 return true; 323 } 324 } 325 } 326 catch (Exception e) 327 { 328 throw new Wsdl11WrapperHelperException("Cannot get version", e); 329 } 330 return false; 331 } 332 333 public MessageExchange getMessageExchange() { 334 return messageExchange; 335 } 336 337 public void initializeMessageExchange(ServiceRefPortInfo portInfo, boolean oneWay) 338 throws ServiceEngineException { 339 try { 340 this.portInfo = portInfo; 341 QName svcQName = portInfo.getServiceReference().getServiceName(); 342 DeliveryChannel channel = 343 JavaEEServiceEngineContext.getInstance(). getDeliveryChannel(); 344 MessageExchangeFactory factory = 346 channel.createExchangeFactoryForService(svcQName); 347 348 MessageExchange msgExchange = null; 349 NormalizedMessage inMsg = null; 350 351 if(oneWay) { 352 InOnly inMessageExchange = factory.createInOnlyExchange(); 353 inMsg = inMessageExchange.createMessage(); 354 inMessageExchange.setInMessage(inMsg); 355 msgExchange = inMessageExchange; 356 } else { 357 InOut inOutMessageExchange = factory.createInOutExchange(); 358 inMsg = inOutMessageExchange.createMessage(); 359 inOutMessageExchange.setInMessage(inMsg); 360 msgExchange = inOutMessageExchange; 361 } 362 msgExchange.setService(svcQName); 363 setMessageExchange(msgExchange); 364 } catch(Exception e) { 365 throw new ServiceEngineException(e); 366 } 367 } 368 369 public void initializeMessageExchange(ServiceRefPortInfo portInfo, SOAPMessage soapMessage) 370 throws ServiceEngineException { 371 try { 372 this.portInfo = portInfo; 373 QName svcQName = portInfo.getServiceReference().getServiceName(); 374 DeliveryChannel channel = 375 JavaEEServiceEngineContext.getInstance(). getDeliveryChannel(); 376 MessageExchangeFactory factory = 378 channel.createExchangeFactoryForService(svcQName); 379 380 MessageExchange msgExchange = null; 381 NormalizedMessage inMsg = null; 382 QName opQName = null; 383 384 boolean oneWay = false; 386 String wsdlPath = null; 387 String endpointName = null; 388 QName serviceName = null; 389 Definition mDefinition = null; 390 EndpointMetaData emd = null; 391 392 ServiceReferenceDescriptor serviceRef = portInfo.getServiceReference(); 393 serviceName = serviceRef.getServiceName(); 395 endpointName = portInfo.hasWsdlPort() ? portInfo.getWsdlPort().getLocalPart() : portInfo.getName(); 396 if(serviceRef.getWsdlFileUrl() != null && serviceRef.getWsdlFileUrl().getProtocol().equals("file")) 397 wsdlPath = serviceRef.getWsdlFileUrl().getPath(); 398 else 399 wsdlPath = serviceRef.getWsdlFileUri(); 400 401 407 emd = (EndpointMetaData)wsdlCache.get(wsdlPath); 408 if(emd == null) { 409 if(logger.isLoggable(Level.FINEST)) 410 logger.log(Level.FINEST," wrap : Now fresh add to cache"); 411 mDefinition = getWsdlDefinition(wsdlPath); 412 emd= new EndpointMetaData(mDefinition,serviceName,endpointName); 413 emd.resolve(); 414 wsdlCache.put(wsdlPath,emd); 415 if(logger.isLoggable(Level.FINEST)) 416 logger.log(Level.FINEST,"wrap : Now fresh add to cache done"); 417 } 418 419 mDefinition = emd.getDefinition(); 420 Wsdl11WrapperHelper helper = new Wsdl11WrapperHelper(mDefinition); 421 if(isWsdl11(mDefinition)) { 422 EndpointMetaData.OperationMetaData operationMetaData = emd.getOperationMetaData(soapMessage); 423 oneWay = operationMetaData.isOneWay(); 424 String operationName = operationMetaData.getOperationName(); 425 opQName = new QName (svcQName.getNamespaceURI(), operationName); 426 logger.log(Level.FINE, "IsOneWay(" + operationName + ") = " + oneWay); 427 } 428 430 if(oneWay) { 431 InOnly inMessageExchange = factory.createInOnlyExchange(); 432 inMsg = inMessageExchange.createMessage(); 433 inMessageExchange.setInMessage(inMsg); 434 msgExchange = inMessageExchange; 435 } else { 436 InOut inOutMessageExchange = factory.createInOutExchange(); 437 inMsg = inOutMessageExchange.createMessage(); 438 inOutMessageExchange.setInMessage(inMsg); 439 msgExchange = inOutMessageExchange; 440 } 441 442 msgExchange.setService(svcQName); 443 msgExchange.setOperation(opQName); 444 setMessageExchange(msgExchange); 445 } catch(Exception e) { 446 e.printStackTrace(); 447 throw new ServiceEngineException(e); 448 } 449 } 450 451 public void handleException(Exception exception) { 452 try { 453 if((messageExchange instanceof InOut) || 454 (messageExchange instanceof RobustInOnly)){ 455 normalizeException(exception); 456 } else if (messageExchange instanceof InOnly) { 457 messageExchange.setStatus(ExchangeStatus.ERROR); 458 } 459 dispatchMessage(); 460 } catch(Exception e) { 461 logger.log(Level.SEVERE, e.getMessage()); 462 } 463 } 464 465 public void handleResponse(SOAPMessage response, boolean flag) { 466 try { 467 if(messageExchange instanceof InOut) { 468 normalizeMessage(response, flag); 469 } else if((messageExchange instanceof InOnly ) || 470 (messageExchange instanceof RobustInOnly)) { 471 messageExchange.setStatus(ExchangeStatus.DONE); 472 } 473 dispatchMessage(); 474 } catch(Exception e) { 475 logger.log(Level.SEVERE, e.getMessage()); 476 } 477 } 478 479 public void normalizeException(Exception exception) { 480 if(exception != null) { 481 try { 482 MessageDenormalizerImpl d = new MessageDenormalizerImpl(); 483 SOAPWrapper soapWrapper = d.denormalizeMessage(exception); 484 normalizeMessage(soapWrapper.getMessage(), false); 485 486 } catch(Exception e) { 487 e.printStackTrace(); 488 } 489 } 490 } 491 492 public void dispatchMessage() throws ServiceEngineException { 493 if(messageExchange != null) { 494 MessageSender messageSender = new MessageSender(); 495 messageSender.setMessageExchange(messageExchange); 496 messageSender.send(); 497 ServiceEngineException exception = messageSender.getException(); 498 if(exception != null) 499 throw exception; 500 } 501 } 502 503 private void validateMessageExchange() throws ServiceEngineException { 504 if(messageExchange == null) 505 throw new ServiceEngineException("MessageExchange not set, use setMessageExchange()"); 506 } 507 508 509 private Operation getOperation(SOAPMessage soapMessage, QName svcQName, 510 String opName) throws ServiceEngineException { 511 try { 512 SOAPPart sp = soapMessage.getSOAPPart(); 515 SOAPEnvelope env = sp.getEnvelope(); 516 SOAPBody body = env.getBody(); 517 org.w3c.dom.Node firstChild = body.getFirstChild(); 519 String namespacePrefix = firstChild.getPrefix(); 520 521 String svcNamespace = svcQName.getNamespaceURI(); 524 525 String namespaceURI = null; 528 if(namespacePrefix != null) 529 namespaceURI = env.getNamespaceURI(namespacePrefix); 530 else 531 namespaceURI = svcNamespace; 532 533 QName opQName = new QName (svcNamespace, opName); 534 535 Operation operation = new Operation(opName, "in-out"); 537 operation.setStyle("rpc"); 539 operation.setInputNamespace(namespaceURI); 540 operation.setOutputNamespace(namespaceURI); 541 return operation; 542 543 } catch(Exception e) { 544 e.printStackTrace(); 545 546 throw new ServiceEngineException(e.getMessage()); 547 } 548 } 549 550 551 protected void printSOAPMessage(String message, SOAPMessage soapMessage) { 552 try { 553 if(logger.isLoggable(Level.FINE)) { 554 System.out.print(message); 555 soapMessage.writeTo(System.out); 556 } 557 }catch(Exception e) { 558 System.out.println(e.getMessage()); 559 } 560 } 561 562 protected void printSOAPContent(String message, NormalizedMessage normalizedMessage) { 563 if(logger.isLoggable(Level.FINE)) { 564 if(normalizedMessage != null) { 565 javax.xml.transform.Source source = normalizedMessage.getContent() ; 566 if(source != null) { 567 try { 568 javax.xml.transform.Transformer transformer = javax.xml.transform.TransformerFactory.newInstance().newTransformer(); 569 System.out.print(message); 570 javax.xml.transform.stream.StreamResult result = new javax.xml.transform.stream.StreamResult (System.out); 571 transformer.transform(source, result); 572 } catch(Exception e) { 573 e.printStackTrace(); 574 } 575 } 576 577 } 578 } 579 } 580 581 } 582 | Popular Tags |