1 57 58 package org.apache.wsif.providers.soap.apachesoap; 59 60 import java.beans.BeanInfo ; 61 import java.beans.IntrospectionException ; 62 import java.beans.Introspector ; 63 import java.beans.PropertyDescriptor ; 64 import java.io.Serializable ; 65 import java.io.StringReader ; 66 import java.net.URL ; 67 import java.security.AccessController ; 68 import java.security.PrivilegedAction ; 69 import java.util.ArrayList ; 70 import java.util.HashMap ; 71 import java.util.Iterator ; 72 import java.util.List ; 73 import java.util.StringTokenizer ; 74 import java.util.Vector ; 75 76 import javax.wsdl.Definition; 77 import javax.wsdl.Input; 78 import javax.wsdl.Operation; 79 import javax.wsdl.Output; 80 import javax.wsdl.Part; 81 import javax.xml.namespace.QName ; 82 import javax.xml.parsers.DocumentBuilder ; 83 84 import org.apache.soap.Body; 85 import org.apache.soap.Constants; 86 import org.apache.soap.Envelope; 87 import org.apache.soap.Header; 88 import org.apache.soap.SOAPException; 89 import org.apache.soap.encoding.SOAPMappingRegistry; 90 import org.apache.soap.encoding.soapenc.BeanSerializer; 91 import org.apache.soap.messaging.Message; 92 import org.apache.soap.rpc.Call; 93 import org.apache.soap.rpc.Parameter; 94 import org.apache.soap.rpc.Response; 95 import org.apache.soap.rpc.SOAPContext; 96 import org.apache.soap.transport.SOAPTransport; 97 import org.apache.soap.transport.http.SOAPHTTPConnection; 98 import org.apache.soap.util.Bean; 99 import org.apache.soap.util.xml.Deserializer; 100 import org.apache.soap.util.xml.Serializer; 101 import org.apache.soap.util.xml.XMLParserUtils; 102 import org.apache.wsif.WSIFConstants; 103 import org.apache.wsif.WSIFCorrelationId; 104 import org.apache.wsif.WSIFException; 105 import org.apache.wsif.WSIFMessage; 106 import org.apache.wsif.WSIFOperation; 107 import org.apache.wsif.WSIFPort; 108 import org.apache.wsif.WSIFResponseHandler; 109 import org.apache.wsif.base.WSIFDefaultMessage; 110 import org.apache.wsif.base.WSIFDefaultOperation; 111 import org.apache.wsif.logging.MessageLogger; 112 import org.apache.wsif.logging.Trc; 113 import org.apache.wsif.providers.WSIFDynamicTypeMap; 114 import org.apache.wsif.util.WSIFProperties; 115 import org.apache.wsif.util.WSIFUtils; 116 import org.w3c.dom.Document ; 117 import org.w3c.dom.Element ; 118 import org.xml.sax.InputSource ; 119 120 130 public class WSIFOperation_ApacheSOAP 131 extends WSIFDefaultOperation 132 implements WSIFOperation, Serializable { 133 134 private static final long serialVersionUID = 1L; 135 136 transient protected String style = null; 137 138 transient protected WSIFPort_ApacheSOAP portInstance; 139 transient protected Operation operation; 140 transient protected Definition definition; 141 142 transient protected List partNames; 144 transient protected String [] names; 145 transient protected Class [] types; 146 transient protected String inputEncodingStyle = Constants.NS_URI_SOAP_ENC; 147 transient protected String inputNamespace; 148 149 transient protected Class returnType; 150 transient protected String actionUri; 151 152 transient protected boolean asyncOperation; 154 transient protected WSIFCorrelationId asyncRequestID; 155 protected WSIFResponseHandler responseHandler; 157 protected String outputEncodingStyle = Constants.NS_URI_SOAP_ENC; 158 protected String returnName; 159 protected boolean prepared = false; 160 protected WSIFDynamicTypeMap typeMap; 161 protected String inputUse = null; 162 protected String outputUse = null; 163 protected String partSerializerName = null; 164 protected ArrayList wsdlOutParams; 165 166 private static final String HTTP_PROXY_HOST_PROPERTY = 167 "http.proxyHost"; 168 private static final String HTTP_PROXY_PORT_PROPERTY = 169 "http.proxyPort"; 170 private static final String PROXY_EXCLUDES_PROPERTY = 171 "http.nonProxyHosts"; 172 173 177 public WSIFOperation_ApacheSOAP( 178 WSIFPort_ApacheSOAP pi, 179 Operation op, 180 WSIFDynamicTypeMap typeMap) 181 throws WSIFException { 182 Trc.entry(this, pi, op, typeMap); 183 184 this.typeMap = typeMap; 185 setDynamicWSIFPort(pi); 186 setOperation(op); 187 setDefinition(pi.getDefinition()); 188 189 if (Trc.ON) 190 Trc.exit(deep()); 191 } 192 193 197 public WSIFOperation_ApacheSOAP copy() throws WSIFException { 198 Trc.entry(this); 199 WSIFOperation_ApacheSOAP op = 200 new WSIFOperation_ApacheSOAP(portInstance, operation, typeMap); 201 202 op.setSoapActionURI(getSoapActionURI()); 203 op.setInputNamespace(getInputNamespace()); 204 op.setInputEncodingStyle(getInputEncodingStyle()); 205 op.setOutputEncodingStyle(getOutputEncodingStyle()); 206 op.setPartNames(getPartNames()); 207 op.setReturnName(getReturnName()); 208 op.setStyle(getStyle()); 209 op.setInputUse(getInputUse()); 210 op.setOutputUse(getOutputUse()); 211 op.setPartSerializerName(getPartSerializerName()); 212 op.setResponseHandler(getResponseHandler()); 213 op.setInputJmsProperties(getInputJmsProperties()); 214 op.setOutputJmsProperties(getOutputJmsProperties()); 215 op.setInputJmsPropertyValues(getInputJmsPropertyValues()); 216 217 if (Trc.ON) 218 Trc.exit(op.deep()); 219 return op; 220 } 221 222 227 public String getTargetNamespaceURI() { 228 Trc.entry(this); 229 Definition d = getDefinition(); 230 String s = (d == null) ? "" : d.getTargetNamespace(); 231 Trc.exit(s); 232 return s; 233 } 234 235 239 void prepare(WSIFMessage inputMessage, WSIFMessage outputMessage) 240 throws WSIFException { 241 Trc.entry(this, inputMessage, outputMessage); 242 HashMap mapOfUserTypes = portInstance.getLocalTypeMap(); 243 SOAPMappingRegistry smr = portInstance.getSOAPMappingRegistry(); 244 245 BeanSerializer beanSer = new BeanSerializer(); 247 PartSerializer partSer = null; 248 if (partSerializerName != null) { 249 try { 250 partSer = 251 (PartSerializer) Class 252 .forName( 253 partSerializerName, 254 true, 255 Thread.currentThread().getContextClassLoader()) 256 .newInstance(); 257 } catch (Throwable ignored) { 258 Trc.ignoredException(ignored); 259 } 260 } 261 262 Input input = operation.getInput(); 264 if (input != null) { 265 List parts; 266 if (partNames != null) { 267 parts = new Vector (); 268 for (Iterator i = partNames.iterator(); i.hasNext();) { 269 String partName = (String ) i.next(); 270 Part part = input.getMessage().getPart(partName); 271 if (part == null) { 272 throw new WSIFException( 273 "no input part named " 274 + partName 275 + " for binding operation " 276 + getName()); 277 } 278 parts.add(part); 279 } 280 } else { 281 parts = input.getMessage().getOrderedParts(null); 282 } 283 int count = parts.size(); 284 names = new String [count]; 285 types = new Class [count]; 286 287 for (int i = 0; i < count; ++i) { 289 Part part = (Part) parts.get(i); 290 names[i] = part.getName(); 291 QName partType = part.getTypeName(); 292 if (partType == null) 293 partType = part.getElementName(); 294 if (partType == null) { 295 throw new WSIFException( 296 "part " + names[i] + " must have type name declared"); 297 } 298 299 org.apache.soap.util.xml.QName qname = 300 new org.apache.soap.util.xml.QName( 301 partType.getNamespaceURI(), 302 partType.getLocalPart()); 303 try { 304 types[i] = (Class ) mapOfUserTypes.get(qname); 305 } catch (Throwable ignored) { 306 Trc.ignoredException(ignored); 307 } 308 if (types[i] == null) { 309 try { 310 types[i] = 311 (Class ) smr.queryJavaType( 312 qname, 313 inputEncodingStyle); 314 } catch (Throwable exn) { 315 if (types[i] == null) { 316 try { 317 String packageName = 318 WSIFUtils.getPackageNameFromNamespaceURI( 319 qname.getNamespaceURI()); 320 String className = 321 WSIFUtils.getJavaClassNameFromXMLName( 322 qname.getLocalPart()); 323 Class inputClass = null; 324 try { 325 inputClass = 326 Class.forName( 327 packageName + "." + className, 328 true, 329 Thread 330 .currentThread() 331 .getContextClassLoader()); 332 } catch (ClassNotFoundException exn5) { 333 inputClass = 334 Class.forName( 335 packageName 336 + "." 337 + className 338 + "Element", 339 true, 340 Thread 341 .currentThread() 342 .getContextClassLoader()); 343 } 344 types[i] = inputClass; 345 if ("literal".equals(inputUse)) { 346 smr.mapTypes("literal", qname, inputClass, partSer, partSer); 347 } else { 348 smr.mapTypes( 349 Constants.NS_URI_SOAP_ENC, 350 qname, 351 inputClass, 352 beanSer, 353 beanSer); 354 } 355 mapSubtypes(inputClass, beanSer, partSer, smr, inputUse); 356 } catch (ClassNotFoundException exn1) { 357 Trc.ignoredException(exn1); 358 } 359 } 360 } 361 362 } 363 } 364 } else { 365 names = new String [0]; 366 types = new Class [0]; 367 } 368 369 Output output = operation.getOutput(); 371 if (output != null) { 372 Part returnPart = null; 373 if (returnName != null) { 374 returnPart = output.getMessage().getPart(returnName); 375 if (returnPart == null) { 376 throw new WSIFException( 377 "no output part named " 378 + returnName 379 + " for bining operation " 380 + getName()); 381 } 382 } else { 383 List parts = output.getMessage().getOrderedParts(null); 384 if (parts.size() > 0) { 385 returnPart = (Part) parts.get(0); 386 returnName = returnPart.getName(); 387 } 388 } 389 390 if (returnPart != null) { 391 QName partType = returnPart.getTypeName(); 392 if (partType == null) 393 partType = returnPart.getElementName(); 394 395 org.apache.soap.util.xml.QName qname = 396 new org.apache.soap.util.xml.QName( 397 partType.getNamespaceURI(), 398 partType.getLocalPart()); 399 400 try { 401 returnType = (Class ) mapOfUserTypes.get(qname); 402 } catch (Throwable ignored) { 403 Trc.ignoredException(ignored); 404 } 405 406 if (returnType == null) { 407 try { 408 returnType = 409 (Class ) smr.queryJavaType( 410 qname, 411 outputEncodingStyle); 412 } catch (Throwable exn) { 413 if (returnType == null) { 414 try { 415 String packageName = 416 WSIFUtils.getPackageNameFromNamespaceURI( 417 qname.getNamespaceURI()); 418 String className = 419 WSIFUtils.getJavaClassNameFromXMLName( 420 qname.getLocalPart()); 421 try { 422 returnType = 423 Class.forName( 424 packageName + "." + className, 425 true, 426 Thread 427 .currentThread() 428 .getContextClassLoader()); 429 } catch (ClassNotFoundException exn5) { 430 returnType = 431 Class.forName( 432 packageName 433 + "." 434 + className 435 + "Element", 436 true, 437 Thread 438 .currentThread() 439 .getContextClassLoader()); 440 } 441 if ("literal".equals(outputUse)) { 442 smr.mapTypes( 443 "literal", 444 qname, 445 returnType, 446 partSer, 447 partSer); 448 } else { 449 smr.mapTypes( 450 Constants.NS_URI_SOAP_ENC, 451 qname, 452 returnType, 453 beanSer, 454 beanSer); 455 } 456 mapSubtypes(returnType, beanSer, partSer, smr, outputUse); 457 } catch (ClassNotFoundException exn1) { 458 Trc.ignoredException(exn1); 459 } 460 } 461 } 462 463 } 464 } 465 List parts = output.getMessage().getOrderedParts(null); 467 if (parts.size() > 1) { 468 ArrayList al = new ArrayList (); 469 for (int i = 1; i < parts.size(); i++) { 470 al.add(((Part) parts.get(i)).getName()); 471 } 472 setWSDLOutParams(al); 473 } 474 } 475 prepared = true; 476 Trc.exit(); 477 } 478 479 private void mapSubtypes( 480 Class javaType, 481 BeanSerializer beanSer, 482 PartSerializer partSer, 483 SOAPMappingRegistry smr, 484 String use) { 485 486 BeanInfo beanInfo = null; 487 try { 488 beanInfo = Introspector.getBeanInfo(javaType); 489 } catch (IntrospectionException e) { 490 Trc.ignoredException(e); 491 } 492 493 if (beanInfo != null) { 494 PropertyDescriptor [] properties = beanInfo.getPropertyDescriptors(); 495 if (properties != null) { 496 for (int i = 0; i < properties.length; i++) { 497 Class propType = properties[i].getPropertyType(); 498 try { 499 org.apache.soap.util.xml.QName qname = 500 smr.queryElementType(propType, inputEncodingStyle); 501 } catch (IllegalArgumentException exn) { 502 Trc.exception(exn); 503 if (!propType.equals(Class .class)) { 506 String packageName = 507 propType.getPackage().getName(); 508 String fullClassName = propType.getName(); 509 String className = 510 fullClassName.substring( 511 fullClassName.lastIndexOf(".") + 1); 512 org.apache.soap.util.xml.QName qname = 513 new org.apache.soap.util.xml.QName( 514 WSIFUtils.getXSDNamespaceFromPackageName( 515 packageName), 516 className); 517 518 if ("literal".equals(use)) { 519 smr.mapTypes( 520 "literal", 521 qname, 522 propType, 523 partSer, 524 partSer); 525 } else { 526 smr.mapTypes( 527 Constants.NS_URI_SOAP_ENC, 528 qname, 529 propType, 530 beanSer, 531 beanSer); 532 } 533 mapSubtypes(propType, beanSer, partSer, smr, use); 534 } 535 } 536 } 537 } 538 } 539 } 540 541 private void setupTypeMappings( 542 HashMap mapOfUserTypes, 543 SOAPMappingRegistry smr) 544 throws WSIFException { 545 546 if (portInstance == null) { 551 HashMap tempMap = new HashMap (); 552 WSIFPort_ApacheSOAP.prepareTypeMappings( 553 smr, 554 typeMap, 555 partSerializerName, 556 tempMap); 557 } 558 } 559 560 public boolean executeRequestResponseOperation( 561 WSIFMessage input, 562 WSIFMessage output, 563 WSIFMessage fault) 564 throws WSIFException { 565 566 Trc.entry(this, input, output, fault); 567 close(); 568 569 setAsyncOperation(false); 570 571 boolean succ; 572 if ("document".equals(style)) 573 succ = invokeRequestResponseOperationDocument(input, output, fault); 574 else 575 succ = invokeRequestResponseOperation(input, output, fault); 576 577 Trc.exit(succ); 578 return succ; 579 } 580 581 public void executeInputOnlyOperation(WSIFMessage input) 582 throws WSIFException { 583 584 Trc.entry(this, input); 585 setAsyncOperation(false); 586 invokeRequestResponseOperation(input, null, null); 587 Trc.exit(); 588 } 589 590 593 public boolean invokeRequestResponseOperation( 594 WSIFMessage input, 595 WSIFMessage output, 596 WSIFMessage fault) 597 throws WSIFException { 598 Trc.entry(this, input, output, fault); 599 if (!prepared) 600 prepare(input, output); 601 602 Call call = portInstance.getCall(); 603 604 call.setEncodingStyleURI(getInputEncodingStyle()); 605 606 SOAPTransport st = getTransport(); 607 if (st != null) { 608 call.setSOAPTransport(st); 609 if (st instanceof SOAPJMSConnection) { 610 SOAPJMSConnection sjt = (SOAPJMSConnection) st; 611 sjt.setSyncTimeout(WSIFProperties.getSyncTimeout()); 612 sjt.setAsyncTimeout(WSIFProperties.getAsyncTimeout()); 613 } 614 } 615 616 call.setTargetObjectURI(getInputNamespace()); 617 call.setMethodName(getName()); 618 619 if (inJmsPropVals != null && !inJmsPropVals.isEmpty()) { 620 checkForTimeoutProperties(st, inJmsPropVals); 621 ((SOAPJMSConnection) st).setJmsProperties(inJmsPropVals); 622 } 623 624 Vector params = new Vector (); 625 Object partInst; 626 for (int i = 0; i < names.length; ++i) { 627 try { 628 partInst = input.getObjectPart(names[i]); 629 } catch (WSIFException ex) { 630 Trc.exception(ex); 631 partInst = null; 632 } 633 Object value = partInst; 634 636 if (value != null 637 && !types[i].isPrimitive() 638 && !(types[i].isAssignableFrom(value.getClass()))) { 639 throw new WSIFException( 640 "value " 641 + value 642 + " has unexpected type " 643 + value.getClass() 644 + " instead of " 645 + types[i]); 646 } 647 648 if (inJmsProps.containsKey(names[i]) && st != null) { 649 String name = (String ) (inJmsProps.get(names[i])); 650 if (!timeoutProperty(st, name, value)) { 651 ((SOAPJMSConnection) st).setJmsProperty( 652 name, 653 value); 654 } 655 } else { 656 Parameter param = 657 new Parameter( 658 names[i], 659 types[i], 660 value, 661 inputEncodingStyle); 662 params.addElement(param); 663 } 664 } 665 666 call.setParams(params); 667 668 setTransportContext(st); 669 670 setCallContext(call); 671 672 Response resp; 674 boolean respOK = true; 675 URL locationUri = portInstance.getEndPoint(); 676 if ( locationUri != null && !isHostInNonProxyProperty( locationUri ) ) { 677 setSOAPProxy( st ); 678 } 679 680 Trc.event( 681 this, 682 "Invoking operation ", 683 getName(), 684 " on ", 685 locationUri, 686 " call object ", 687 call); 688 689 try { 690 resp = call.invoke(locationUri, getSoapActionURI()); 691 } catch (SOAPException e) { 692 Trc.exception(e); 693 694 MessageLogger.log("WSIF.0005E", "ApacheSOAP", getName()); 696 697 throw new WSIFException( 698 "SOAPException: " + e.getFaultCode() + e.getMessage(), 699 e); 700 } 701 702 Trc.event(this, "Operation returned ", resp); 703 704 706 if (!isAsyncOperation() && returnType != null) { 707 respOK = buildResponseMessages(resp, output, fault); 708 } 709 710 if (resp instanceof Response) { 711 Header soapHeader = resp.getHeader(); 712 addContextResponseSOAPHeaders(soapHeader); 713 } 714 715 Trc.exit(respOK); 716 return respOK; 717 } 718 719 public boolean invokeRequestResponseOperationDocument( 720 WSIFMessage input, 721 WSIFMessage output, 722 WSIFMessage fault) 723 throws WSIFException { 724 Trc.entry(this, input, output, fault); 725 726 if (!prepared) 727 prepare(input, output); 728 Envelope msgEnv = new Envelope(); 729 Body msgBody = new Body(); 730 Vector vect = new Vector (); 731 732 Iterator iterator = 733 operation.getInput().getMessage().getParts().keySet().iterator(); 734 while (iterator.hasNext()) { 735 String partName = (String ) iterator.next(); 736 Object part = input.getObjectPart(partName); 737 String encoding = null; 738 if ("literal".equals(inputUse)) { 739 encoding = "literal"; 741 } else { 742 encoding = this.inputEncodingStyle; 743 } 744 745 PartSerializer partSerializer = null; 746 Object o = 747 portInstance.getSOAPMappingRegistry().querySerializer( 748 part.getClass(), 749 inputUse); 750 if (o instanceof PartSerializer) { 751 PartSerializer tmp = (PartSerializer) o; 752 try { 753 partSerializer = 754 (PartSerializer) tmp.getClass().newInstance(); 755 } catch (InstantiationException e) { 756 Trc.ignoredException(e); 757 } catch (IllegalAccessException e) { 758 Trc.ignoredException(e); 759 } 760 partSerializer.setPart(part); 761 Part modelPart = 762 operation.getInput().getMessage().getPart(partName); 763 javax.xml.namespace.QName partType = modelPart.getTypeName(); 764 if (partType == null) 765 partType = modelPart.getElementName(); 766 partSerializer.setPartQName(partType); 767 } else { 769 partSerializer = new SOAPEncSerializerWrapper(); 770 partSerializer.setPart(part); 771 ( 772 ( 773 SOAPEncSerializerWrapper) partSerializer) 774 .setTargetSerializer( 775 (Serializer) o); 776 } 777 778 Bean bean = new Bean(partSerializer.getClass(), partSerializer); 779 vect.add(bean); 780 } 781 782 URL url = portInstance.getEndPoint(); 784 Envelope env = null; 785 msgBody.setBodyEntries(vect); 786 msgEnv.setBody(msgBody); 787 788 SOAPTransport st = getTransport(); 789 790 if (st instanceof SOAPJMSConnection) { 791 SOAPJMSConnection sjt = (SOAPJMSConnection) st; 792 sjt.setSyncTimeout(WSIFProperties.getSyncTimeout()); 793 sjt.setAsyncTimeout(WSIFProperties.getAsyncTimeout()); 794 } 795 796 if (inJmsPropVals != null && !inJmsPropVals.isEmpty()) { 797 checkForTimeoutProperties(st, inJmsPropVals); 798 ((SOAPJMSConnection) st).setJmsProperties(inJmsPropVals); 799 } 800 801 804 if ( url != null && !isHostInNonProxyProperty( url ) ) { 805 setSOAPProxy( st ); 806 } 807 808 try { 810 Message msg = new Message(); 811 if (st != null) 812 msg.setSOAPTransport(st); 813 814 Trc.event( 815 this, 816 "Invoking operation ", 817 getName(), 818 " url ", 819 url, 820 " soapaction ", 821 getSoapActionURI(), 822 " envelope ", 823 msgEnv, 824 " message ", 825 msg); 826 827 msg.send(url, getSoapActionURI(), msgEnv); 828 829 env = msg.receiveEnvelope(); 831 } catch (SOAPException exn) { 832 Trc.exception(exn); 833 WSIFException e = 834 new WSIFException("SOAP Exception: " + exn.getMessage()); 835 e.setTargetException(exn); 836 throw e; 837 } 838 839 Trc.event(this, "Returned from operation, envelope ", env); 840 841 Body retbody = env.getBody(); 842 java.util.Vector v = retbody.getBodyEntries(); 843 int index = 0; 844 845 String encoding = null; 846 if ("literal".equals(outputUse)) { 847 encoding = "literal"; 849 } else { 850 encoding = this.outputEncodingStyle; 851 } 852 853 iterator = 854 operation.getOutput().getMessage().getParts().keySet().iterator(); 855 while (iterator.hasNext()) { 856 Element element = (Element ) v.get(index++); 857 String partName = (String ) iterator.next(); 858 Part modelPart = 859 operation.getOutput().getMessage().getPart(partName); 860 javax.xml.namespace.QName partType = modelPart.getTypeName(); 861 if (partType == null) 862 partType = modelPart.getElementName(); 863 864 PartSerializer partSerializer = null; 865 Object o = 866 this.portInstance.getSOAPMappingRegistry().queryDeserializer( 867 new org.apache.soap.util.xml.QName( 868 partType.getNamespaceURI(), 869 partType.getLocalPart()), 870 encoding); 871 872 if (o instanceof PartSerializer) { 873 PartSerializer tmp = (PartSerializer) o; 874 try { 875 partSerializer = 876 (PartSerializer) tmp.getClass().newInstance(); 877 } catch (InstantiationException e) { 878 Trc.ignoredException(e); 879 } catch (IllegalAccessException e) { 880 Trc.ignoredException(e); 881 } 882 partSerializer.setPartQName(partType); 883 partSerializer.unmarshall(null, null, element, null, null); 884 Object retBean = partSerializer.getPart(); 885 output.setObjectPart(partName, retBean); 886 } else { 888 Bean bean = 889 ((Deserializer) o).unmarshall( 890 null, 891 null, 892 element, 893 null, 894 null); 895 Object retBean = bean.value; 896 } 897 } 898 899 Header soapHeader = env.getHeader(); 900 addContextResponseSOAPHeaders(soapHeader); 901 902 Trc.exit(true); 903 return true; 904 } 905 906 915 public WSIFCorrelationId executeRequestResponseAsync(WSIFMessage input) 916 throws WSIFException { 917 918 Trc.entry(this, input); 919 WSIFCorrelationId id = executeRequestResponseAsync(input, null); 920 Trc.exit(id); 921 return id; 922 923 } 924 925 936 public WSIFCorrelationId executeRequestResponseAsync( 937 WSIFMessage input, 938 WSIFResponseHandler handler) 939 throws WSIFException { 940 941 Trc.entry(this, input, handler); 942 close(); 943 944 if (!prepared) 945 prepare(input, null); 946 947 if (!portInstance.supportsAsync()) { 948 throw new WSIFException("asynchronous operations not available"); 949 } 950 951 if ("document".equals(style)) { 952 throw new WSIFException("docstyle asynchronous operations not implemented"); 953 } 954 955 setAsyncOperation(true); 956 setResponseHandler(handler); 957 SOAPJMSConnection transport = (SOAPJMSConnection) getTransport(); 958 transport.setWsifOperation(this); 959 transport.setAsyncOperation(true); 960 961 invokeRequestResponseOperation(input, null, null); 962 963 transport.setAsyncOperation(false); 964 WSIFCorrelationId id = getAsyncRequestID(); 965 Trc.exit(id); 966 return id; 967 968 } 969 970 983 public void fireAsyncResponse(Object response) throws WSIFException { 984 Trc.entry(this, response); 985 986 Response resp = deserialiseResponseObject(response); 987 988 WSIFMessage outMsg = createOutputMessage(); 989 WSIFMessage faultMsg = createFaultMessage(); 990 buildResponseMessages(resp, outMsg, faultMsg); 991 992 getResponseHandler().executeAsyncResponse(outMsg, faultMsg); 993 994 Trc.exit(outMsg); } 996 997 1018 public boolean processAsyncResponse( 1019 Object response, 1020 WSIFMessage output, 1021 WSIFMessage fault) 1022 throws WSIFException { 1023 Trc.entry(this, response, output, fault); 1024 1025 Response resp = deserialiseResponseObject(response); 1026 boolean ok = buildResponseMessages(resp, output, fault); 1027 1028 Trc.exit( ok ); 1029 return ok; 1030 } 1031 1032 1037 private Response deserialiseResponseObject(Object msg) 1038 throws WSIFException { 1039 1040 if (msg == null) { 1041 throw new WSIFException("null response to async send"); 1042 } 1043 1044 if (!(msg instanceof javax.jms.TextMessage )) { 1045 throw new WSIFException("response not a javax.jms.TextMessage"); 1046 } 1047 1048 HashMap mapOfUserTypes = new HashMap (); 1049 SOAPMappingRegistry smr = 1050 WSIFPort_ApacheSOAP.createSOAPMappingRegistry(new Call()); 1051 1052 setDynamicWSIFPort( null ); 1054 1055 setupTypeMappings(mapOfUserTypes, smr); 1056 1057 try { 1058 1059 javax.jms.TextMessage m = (javax.jms.TextMessage ) msg; 1060 String payloadStr = m.getText(); 1061 1062 SOAPContext respCtx = new SOAPContext(); 1064 respCtx.setRootPart(payloadStr, "text/xml"); 1065 1066 DocumentBuilder xdb = XMLParserUtils.getXMLDocBuilder(); 1068 Document respDoc = 1069 xdb.parse(new InputSource (new StringReader (payloadStr))); 1070 Element payload = null; 1071 1072 if (respDoc != null) { 1073 payload = respDoc.getDocumentElement(); 1074 } else { throw new SOAPException( 1076 Constants.FAULT_CODE_CLIENT, 1077 "Parsing error, response was:\n" + payloadStr); 1078 } 1079 1080 Envelope respEnv = Envelope.unmarshall(payload, respCtx); 1082 1083 Response resp = Response.extractFromEnvelope(respEnv, smr, respCtx); 1085 1086 return resp; 1087 1088 } catch (Exception ex) { 1089 Trc.exception(ex); 1090 throw new WSIFException(ex.getMessage()); 1091 } 1092 } 1093 1094 1097 private boolean buildResponseMessages( 1098 Response resp, 1099 WSIFMessage outMsg, 1100 WSIFMessage faultMsg) 1101 throws WSIFException { 1102 1103 boolean respOK; 1104 1105 if (resp == null) { 1106 throw new WSIFException("soap response is null"); 1107 } 1108 1109 if (resp.generatedFault()) { 1110 respOK = false; 1111 if (faultMsg != null) { 1112 org.apache.soap.Fault soapFault = resp.getFault(); 1113 faultMsg.setName(WSIFConstants.SOAP_FAULT_MSG_NAME); 1114 faultMsg.setObjectPart( 1115 WSIFConstants.SOAP_FAULT_OBJECT, 1116 soapFault); 1117 } 1118 } else { 1119 respOK = true; 1120 populateOutMsgReturnPart(resp, outMsg); 1121 populateOutMsgParts(resp, outMsg); 1122 } 1123 return respOK; 1124 } 1125 1126 1129 private void populateOutMsgReturnPart(Response resp, WSIFMessage outMsg) 1130 throws WSIFException { 1131 if (outMsg != null && returnName != null) { 1132 Parameter retValue = resp.getReturnValue(); 1133 if (retValue == null) { 1134 throw new WSIFException("return value not found in response message"); 1135 } 1136 Object result = retValue.getValue(); 1137 if (returnType != null) { if (!returnType.isPrimitive() 1139 && result != null 1140 && !(returnType.isAssignableFrom(result.getClass()))) { 1141 throw new WSIFException( 1142 "return value " 1143 + result 1144 + " has unexpected type " 1145 + result.getClass() 1146 + " instead of " 1147 + returnType); 1148 } 1149 } 1150 outMsg.setObjectPart(returnName, result); 1151 } 1152 } 1153 1154 1158 private void populateOutMsgParts(Response resp, WSIFMessage outMsg) 1159 throws WSIFException { 1160 if (outMsg != null) { 1161 Vector respParms = resp.getParams(); 1162 ArrayList wsdlOutParameters = getWSDLOutParams(); 1163 if (respParms != null) { 1164 Parameter p; 1165 for (Iterator i = respParms.iterator(); i.hasNext();) { 1166 p = (Parameter) i.next(); 1167 outMsg.setObjectPart(p.getName(), p.getValue()); 1168 wsdlOutParameters.remove(p.getName()); 1169 } 1170 } 1171 for (Iterator i = wsdlOutParameters.iterator(); i.hasNext();) { 1173 outMsg.setObjectPart((String ) i.next(), null); 1174 } 1175 } 1176 } 1177 1178 1181 public String getName() { 1182 Trc.entry(this); 1183 String s = operation.getName(); 1184 Trc.exit(s); 1185 return s; 1186 } 1187 1188 public String getSoapActionURI() { 1189 Trc.entry(this); 1190 Trc.exit(actionUri); 1191 return actionUri; 1192 } 1193 1194 public void setSoapActionURI(String value) { 1195 Trc.entry(this, value); 1196 actionUri = value; 1197 Trc.exit(); 1198 } 1199 1200 public String getInputNamespace() { 1201 Trc.entry(this); 1202 Trc.exit(inputNamespace); 1203 return inputNamespace; 1204 } 1205 1206 public void setInputNamespace(String value) { 1207 Trc.entry(this, value); 1208 inputNamespace = value; 1209 Trc.exit(); 1210 } 1211 1212 public String getInputEncodingStyle() { 1213 Trc.entry(this); 1214 Trc.exit(inputEncodingStyle); 1215 return inputEncodingStyle; 1216 } 1217 1218 public void setInputEncodingStyle(String value) { 1219 Trc.entry(this, value); 1220 inputEncodingStyle = value; 1221 Trc.exit(); 1222 } 1223 1224 public String getOutputEncodingStyle() { 1225 Trc.entry(this); 1226 Trc.exit(outputEncodingStyle); 1227 return outputEncodingStyle; 1228 } 1229 1230 public void setOutputEncodingStyle(String value) { 1231 Trc.entry(this, value); 1232 outputEncodingStyle = value; 1233 Trc.exit(); 1234 } 1235 1236 public List getPartNames() { 1237 Trc.entry(this); 1238 Trc.exit(partNames); 1239 return partNames; 1240 } 1241 1242 public void setPartNames(List value) { 1243 Trc.entry(this, value); 1244 partNames = value; 1245 Trc.exit(); 1246 } 1247 1248 public String getReturnName() { 1249 Trc.entry(this); 1250 Trc.exit(returnName); 1251 return returnName; 1252 } 1253 1254 public void setReturnName(String value) { 1255 Trc.entry(this, value); 1256 returnName = value; 1257 Trc.exit(); 1258 } 1259 1260 public Operation getOperation() { 1262 Trc.entry(this); 1263 Trc.exit(operation); 1264 return operation; 1265 } 1266 1267 public void setOperation(Operation value) { 1268 Trc.entry(this, value); 1269 operation = value; 1270 Trc.exit(); 1271 } 1272 1273 public Definition getDefinition() { 1274 Trc.entry(this); 1275 Trc.exit(definition); 1276 return definition; 1277 } 1278 1279 public void setDefinition(Definition value) { 1280 Trc.entry(this, value); 1281 definition = value; 1282 Trc.exit(); 1283 } 1284 1285 public WSIFPort_ApacheSOAP getDynamicWSIFPort() { 1287 Trc.entry(this); 1288 Trc.exit(portInstance); 1289 return portInstance; 1290 } 1291 1292 public void setDynamicWSIFPort(WSIFPort_ApacheSOAP value) { 1293 Trc.entry(this, value); 1294 portInstance = value; 1295 Trc.exit(); 1296 } 1297 1298 1302 public String getStyle() { 1303 Trc.entry(this); 1304 Trc.exit(style); 1305 return style; 1306 } 1307 1308 1314 WSIFResponseHandler getResponseHandler() { 1315 Trc.entry(this); 1316 Trc.exit(responseHandler); 1317 return responseHandler; 1318 } 1319 1320 1324 public SOAPTransport getTransport() { 1325 Trc.entry(this); 1326 SOAPTransport t = portInstance.getSOAPTransport(); 1327 Trc.exit(t); 1328 return t; 1329 } 1330 1331 1336 public WSIFCorrelationId getAsyncRequestID() { 1337 Trc.entry(this); 1338 Trc.exit(asyncRequestID); 1339 return asyncRequestID; 1340 } 1341 1342 1348 void setAsyncRequestID(WSIFCorrelationId asyncRequestID) { 1349 Trc.entry(this, asyncRequestID); 1350 this.asyncRequestID = asyncRequestID; 1351 Trc.exit(); 1352 } 1353 1354 1360 private void setAsyncOperation(boolean b) { 1361 asyncOperation = b; 1362 } 1363 1364 1368 public void setStyle(String style) { 1369 Trc.entry(this, style); 1370 this.style = style; 1371 Trc.exit(); 1372 } 1373 1374 1379 private void setResponseHandler(WSIFResponseHandler responseHandler) { 1380 this.responseHandler = responseHandler; 1381 } 1382 1383 1387 public String getInputUse() { 1388 Trc.entry(this); 1389 Trc.exit(inputUse); 1390 return inputUse; 1391 } 1392 1393 1397 public void setInputUse(String inputUse) { 1398 Trc.entry(this, inputUse); 1399 this.inputUse = inputUse; 1400 Trc.exit(); 1401 } 1402 1403 1407 public String getOutputUse() { 1408 Trc.entry(this); 1409 Trc.exit(outputUse); 1410 return outputUse; 1411 } 1412 1413 1417 public void setOutputUse(String outputUse) { 1418 Trc.entry(this, outputUse); 1419 this.outputUse = outputUse; 1420 Trc.exit(); 1421 } 1422 1423 1427 public String getPartSerializerName() { 1428 Trc.entry(this); 1429 Trc.exit(partSerializerName); 1430 return partSerializerName; 1431 } 1432 1433 private ArrayList getWSDLOutParams() { 1434 if (wsdlOutParams == null) { 1435 wsdlOutParams = new ArrayList (); 1436 } 1437 return wsdlOutParams; 1438 } 1439 1440 private void setWSDLOutParams(ArrayList al) { 1441 wsdlOutParams = al; 1442 } 1443 1444 public WSIFPort getWSIFPort() { 1445 Trc.entry(this); 1446 Trc.exit(portInstance); 1447 return portInstance; 1448 } 1449 1450 1456 public boolean isAsyncOperation() { 1457 Trc.entry(this); 1458 Trc.exit(asyncOperation); 1459 return asyncOperation; 1460 } 1461 1462 1466 public void setPartSerializerName(String partSerializerName) { 1467 Trc.entry(this, partSerializerName); 1468 this.partSerializerName = partSerializerName; 1469 Trc.exit(); 1470 } 1471 1472 1475 private void setJMSOutPropsInContext(SOAPTransport t) 1476 throws WSIFException { 1477 if (!(t instanceof SOAPJMSConnection)) { 1478 return; 1479 } 1480 HashMap props = ((SOAPJMSConnection) t).getJmsProperties(); 1481 if (props != null) { 1482 if (context == null) { 1483 context = new WSIFDefaultMessage(); 1484 } 1485 context.setParts(props); 1486 } 1487 } 1488 1489 1492 private void setTransportContext(SOAPTransport t) { 1493 if (context == null || !(t instanceof SOAPJMSConnection)) { 1494 return; 1495 } 1496 HashMap jmsProps = new HashMap (); 1497 for (Iterator i = context.getPartNames(); i.hasNext();) { 1498 try { 1499 String partName = (String ) i.next(); 1500 Object value = context.getObjectPart(partName); 1501 if (!timeoutProperty(t, partName, value)) { 1502 if (partName.startsWith(WSIFConstants.CONTEXT_JMS_PREFIX)) { 1503 jmsProps.put( 1504 partName.substring( 1505 WSIFConstants.CONTEXT_JMS_PREFIX.length()), 1506 value); 1507 } 1508 } 1509 } catch (WSIFException ex) { 1510 Trc.ignoredException(ex); 1511 } 1512 } 1513 if (jmsProps.size() > 0) { 1514 ((SOAPJMSConnection) t).setJmsProperties(jmsProps); 1515 } 1516 } 1517 1518 1522 private void setCallContext(Call call) { 1523 Object o; 1524 String name; 1525 1526 call.setHeader(null); 1528 addHTTPHeader(call, WSIFConstants.CONTEXT_HTTP_USER, null); 1529 addHTTPHeader(call, WSIFConstants.CONTEXT_HTTP_PSWD, null); 1530 1531 if (context == null) { 1532 return; 1533 } 1534 1535 name = WSIFConstants.CONTEXT_HTTP_USER; 1536 try { 1537 o = context.getObjectPart(name); 1538 if (o instanceof String ) { 1539 addHTTPHeader(call, name, (String ) o); 1540 } 1541 } catch (WSIFException ex) { 1542 Trc.ignoredException(ex); 1543 addHTTPHeader(call, name, null); 1544 } 1545 1546 name = WSIFConstants.CONTEXT_HTTP_PSWD; 1547 try { 1548 o = context.getObjectPart(name); 1549 if (o instanceof String ) { 1550 addHTTPHeader(call, name, (String ) o); 1551 } 1552 } catch (WSIFException ex) { 1553 Trc.ignoredException(ex); 1554 addHTTPHeader(call, name, null); 1555 } 1556 1557 try { 1558 name = WSIFConstants.CONTEXT_REQUEST_SOAP_HEADERS; 1559 o = context.getObjectPart(name); 1560 if (o instanceof List ) { 1561 addSOAPHeader(call, name, (List ) o); 1562 } 1563 } catch (WSIFException ex) { 1564 Trc.ignoredException(ex); 1565 } 1566 } 1567 1568 1571 private void addSOAPHeader(Call call, String name, List soapHeaders) { 1572 Header h = new Header(); 1573 h.setHeaderEntries(new Vector (soapHeaders)); 1574 call.setHeader(h); 1575 } 1576 1577 1582 private void addHTTPHeader(Call call, String name, String value) { 1583 SOAPTransport st = call.getSOAPTransport(); 1584 if (st instanceof SOAPHTTPConnection) { 1585 SOAPHTTPConnection httpTransport = (SOAPHTTPConnection) st; 1586 if (name.equals(WSIFConstants.CONTEXT_HTTP_USER)) { 1587 httpTransport.setUserName(value); 1588 } else if (name.equals(WSIFConstants.CONTEXT_HTTP_PSWD)) { 1589 httpTransport.setPassword(value); 1590 } 1591 } 1592 } 1593 1594 1597 private void addContextResponseSOAPHeaders(Header soapHeader) 1598 throws WSIFException { 1599 1600 if( soapHeader != null ) { 1601 Vector headerEntries = soapHeader.getHeaderEntries(); 1602 if (headerEntries != null) { 1603 ArrayList headers = new ArrayList (); 1604 for(Iterator i = headerEntries.iterator(); i.hasNext(); ) { 1605 Object o = i.next(); 1606 if (o instanceof Element ) { 1607 headers.add(o); 1608 } else { 1609 Trc.event("unexpected response SOAP header type: ", o); 1610 } 1611 } 1612 if (headers.size() > 0) { 1613 WSIFMessage context = getContext(); 1614 context.setObjectPart( 1615 WSIFConstants.CONTEXT_RESPONSE_SOAP_HEADERS, 1616 headers); 1617 setContext(context); 1618 } 1619 } 1620 } 1621 } 1622 1623 private boolean isHostInNonProxyProperty(URL u) { 1624 if ( u != null ) { 1625 String excludeList = 1626 (String ) AccessController.doPrivileged(new PrivilegedAction () { 1627 public Object run() { 1628 return System.getProperty( PROXY_EXCLUDES_PROPERTY ); 1629 } 1630 }); 1631 if ( excludeList != null ) { 1632 StringTokenizer st = new StringTokenizer ( excludeList, "|" ); 1633 while ( st.hasMoreTokens() ) { 1634 String xhost = st.nextToken().trim(); 1635 if ( WSIFUtils.wildcardCompare( xhost, u.getHost(), '*' ) ) { 1636 return true; 1637 } 1638 } 1639 } 1640 } 1641 return false; 1642 } 1643 1644 1649 private void setSOAPProxy(SOAPTransport st) { 1650 if (st instanceof SOAPHTTPConnection) { 1651 SOAPHTTPConnection shttpc = (SOAPHTTPConnection) st; 1652 String proxyHost = 1653 (String ) AccessController.doPrivileged(new PrivilegedAction () { 1654 public Object run() { 1655 return System.getProperty( HTTP_PROXY_HOST_PROPERTY ); 1656 } 1657 }); 1658 String proxyPort = 1659 (String ) AccessController.doPrivileged(new PrivilegedAction () { 1660 public Object run() { 1661 return System.getProperty( HTTP_PROXY_PORT_PROPERTY ); 1662 } 1663 }); 1664 if ( proxyHost != null && proxyHost.length() > 0 ) { 1665 shttpc.setProxyHost( proxyHost ); 1666 if ( proxyPort != null && proxyPort.length() > 0 ) { 1667 try { 1668 int port = Integer.parseInt( proxyPort ); 1669 shttpc.setProxyPort( port ); 1670 } catch (NumberFormatException ex) { 1671 Trc.ignoredException(ex); 1672 } 1673 } 1674 } 1675 } 1676 } 1677 1678 private void checkForTimeoutProperties(SOAPTransport st, HashMap inJmsPropVals) { 1679 if (inJmsPropVals != null) { 1680 for (Iterator i = inJmsPropVals.keySet().iterator(); i.hasNext();) { 1681 String name = (String ) i.next(); 1682 Object value = inJmsPropVals.get(name); 1683 if (timeoutProperty(st, name, value)) { 1684 i.remove(); 1685 } 1686 } 1687 } 1688 } 1689 1690 private boolean timeoutProperty( 1691 SOAPTransport t, 1692 String propertyName, 1693 Object value) { 1694 Trc.entry(this, t, propertyName, value); 1695 boolean isTimeoutProperty = false; 1696 if (t != null 1697 && t instanceof SOAPJMSConnection) { 1698 SOAPJMSConnection st = (SOAPJMSConnection) t; 1699 try { 1700 if (WSIFConstants.WSIF_PROP_SYNC_TIMEOUT.equals(propertyName)) { 1701 isTimeoutProperty = true; 1702 long syncTimeout = Long.parseLong(value.toString()); 1703 st.setSyncTimeout(syncTimeout); 1704 Trc.event(this, "overridding syncTimeout to " + syncTimeout); 1705 } else if ( 1706 WSIFConstants.WSIF_PROP_ASYNC_TIMEOUT.equals(propertyName)) { 1707 isTimeoutProperty = true; 1708 long asyncTimeout = Long.parseLong(value.toString()); 1709 st.setAsyncTimeout(asyncTimeout); 1710 Trc.event(this, "overridding asyncTimeout to " + asyncTimeout); 1711 } 1712 } catch (NumberFormatException ex) { 1713 Trc.ignoredException(ex); 1714 } 1715 } 1716 Trc.exit(isTimeoutProperty); 1717 return isTimeoutProperty; 1718 } 1719 1720 public String deep() { 1721 String buff = ""; 1722 try { 1723 buff = new String (super.toString() + ":\n"); 1724 buff += " style:" + style; 1725 buff += " portInstance:" + portInstance; 1726 buff += " operation:" + Trc.brief(operation); 1727 buff += " definition:" + Trc.brief(definition); 1728 buff += " context:" + context; 1729 buff += " partNames:" + partNames; 1730 buff += Trc.brief("names", names); 1731 buff += Trc.brief("types", types); 1732 buff += " inputEncodingStyle:" + inputEncodingStyle; 1733 buff += " inputNamespace:" + inputNamespace; 1734 buff += " returnType:" + returnType; 1735 buff += " actionUri:" + actionUri; 1736 buff += " inJmsProps:" + inJmsProps; 1737 buff += " outJmsProps:" + outJmsProps; 1738 buff += " inJmsPropVals:" + inJmsPropVals; 1739 buff += " asyncOperation:" + asyncOperation; 1740 buff += " asyncRequestID:" + asyncRequestID; 1741 buff += " responseHandler:" + responseHandler; 1742 buff += " outputEncodingStyle:" + outputEncodingStyle; 1743 buff += " returnName:" + returnName; 1744 buff += " prepared:" + prepared; 1745 buff += " typeMap:" + typeMap; 1746 buff += " inputUse:" + inputUse; 1747 buff += " outputUse:" + outputUse; 1748 buff += " partSerializerName:" + partSerializerName; 1749 } catch (Exception e) { 1750 Trc.exceptionInTrace(e); 1751 } 1752 return buff; 1753 } 1754} | Popular Tags |