1 55 56 package org.jboss.axis.wsdl.fromJava; 57 58 import com.ibm.wsdl.BindingFaultImpl; 59 import com.ibm.wsdl.extensions.soap.SOAPAddressImpl; 60 import com.ibm.wsdl.extensions.soap.SOAPBindingImpl; 61 import com.ibm.wsdl.extensions.soap.SOAPBodyImpl; 62 import com.ibm.wsdl.extensions.soap.SOAPOperationImpl; 63 import org.jboss.axis.AxisFault; 64 import org.jboss.axis.Constants; 65 import org.jboss.axis.description.FaultDesc; 66 import org.jboss.axis.description.OperationDesc; 67 import org.jboss.axis.description.ParameterDesc; 68 import org.jboss.axis.description.ServiceDesc; 69 import org.jboss.axis.encoding.DefaultTypeMappingImpl; 70 import org.jboss.axis.encoding.TypeMapping; 71 import org.jboss.axis.enums.Style; 72 import org.jboss.axis.enums.Use; 73 import org.jboss.axis.utils.ClassUtils; 74 import org.jboss.axis.utils.JavaUtils; 75 import org.jboss.axis.utils.XMLUtils; 76 import org.w3c.dom.Document ; 77 import org.w3c.dom.Element ; 78 import org.xml.sax.SAXException ; 79 80 import javax.wsdl.Binding; 81 import javax.wsdl.BindingFault; 82 import javax.wsdl.BindingInput; 83 import javax.wsdl.BindingOperation; 84 import javax.wsdl.BindingOutput; 85 import javax.wsdl.Definition; 86 import javax.wsdl.Fault; 87 import javax.wsdl.Import; 88 import javax.wsdl.Input; 89 import javax.wsdl.Message; 90 import javax.wsdl.Operation; 91 import javax.wsdl.Output; 92 import javax.wsdl.Part; 93 import javax.wsdl.Port; 94 import javax.wsdl.PortType; 95 import javax.wsdl.Service; 96 import javax.wsdl.WSDLException; 97 import javax.wsdl.extensions.ExtensibilityElement; 98 import javax.wsdl.extensions.soap.SOAPAddress; 99 import javax.wsdl.extensions.soap.SOAPBinding; 100 import javax.wsdl.extensions.soap.SOAPBody; 101 import javax.wsdl.extensions.soap.SOAPFault; 102 import javax.wsdl.extensions.soap.SOAPOperation; 103 import javax.wsdl.factory.WSDLFactory; 104 import javax.xml.namespace.QName ; 105 import javax.xml.parsers.ParserConfigurationException ; 106 import java.io.File ; 107 import java.io.FileOutputStream ; 108 import java.io.IOException ; 109 import java.io.StringWriter ; 110 import java.util.ArrayList ; 111 import java.util.HashMap ; 112 import java.util.Iterator ; 113 import java.util.Map ; 114 import java.util.StringTokenizer ; 115 import java.util.Vector ; 116 117 125 public class Emitter 126 { 127 public static final int MODE_ALL = 0; 129 public static final int MODE_INTERFACE = 1; 130 public static final int MODE_IMPLEMENTATION = 2; 131 132 private Class cls; 133 private Class [] extraClasses; private Class implCls; private Vector allowedMethods = null; private Vector disallowedMethods = null; private ArrayList stopClasses = new ArrayList (); private boolean useInheritedMethods = false; 139 private String intfNS; 140 private String implNS; 141 private String inputSchema; 142 private String inputWSDL; 143 private String locationUrl; 144 private String importUrl; 145 private String servicePortName; 146 private String serviceElementName; 147 private String targetService = null; 148 private String description; 149 private Style style = Style.RPC; 150 private Use use = null; private TypeMapping tm = null; private TypeMapping defaultTM = null; private Namespaces namespaces; 154 private Map exceptionMsg = null; 155 156 private ArrayList encodingList; 157 protected Types types; 158 private String clsName; 159 private String portTypeName; 160 private String bindingName; 161 162 private ServiceDesc serviceDesc; 163 private ServiceDesc serviceDesc2; 164 private String soapAction = "DEFAULT"; 165 166 170 public static final int MODE_RPC = 0; 171 174 public static final int MODE_DOCUMENT = 1; 175 178 public static final int MODE_DOC_WRAPPED = 2; 179 180 185 public Emitter() 186 { 187 namespaces = new Namespaces(); 188 exceptionMsg = new HashMap (); 189 } 190 191 201 public void emit(String filename1, String filename2) 202 throws IOException , WSDLException, 203 SAXException , ParserConfigurationException 204 { 205 Definition intf = getIntfWSDL(); 207 Definition impl = getImplWSDL(); 208 209 if (filename1 == null) 211 { 212 filename1 = getServicePortName() + "_interface.wsdl"; 213 } 214 if (filename2 == null) 215 { 216 filename2 = getServicePortName() + "_implementation.wsdl"; 217 } 218 219 for (int i = 0; extraClasses != null && i < extraClasses.length; i++) 220 { 221 types.writeTypeForPart(extraClasses[i], null); 222 } 223 Document doc = WSDLFactory.newInstance(). 226 newWSDLWriter().getDocument(intf); 227 types.insertTypesFragment(doc); 228 prettyDocumentToFile(doc, filename1); 229 230 doc = WSDLFactory.newInstance().newWSDLWriter().getDocument(impl); 232 prettyDocumentToFile(doc, filename2); 233 } 234 235 244 public void emit(String filename) 245 throws IOException , WSDLException, 246 SAXException , ParserConfigurationException 247 { 248 emit(filename, MODE_ALL); 249 } 250 251 265 public Document emit(int mode) 266 throws IOException , WSDLException, 267 SAXException , ParserConfigurationException 268 { 269 Document doc = null; 270 Definition def = null; 271 switch (mode) 272 { 273 case MODE_ALL: 274 def = getWSDL(); 275 for (int i = 0; extraClasses != null && i < extraClasses.length; i++) 276 { 277 types.writeTypeForPart(extraClasses[i], null); 278 } 279 doc = WSDLFactory.newInstance(). 281 newWSDLWriter().getDocument(def); 282 types.insertTypesFragment(doc); 283 break; 284 case MODE_INTERFACE: 285 def = getIntfWSDL(); 286 for (int i = 0; extraClasses != null && i < extraClasses.length; i++) 287 { 288 types.writeTypeForPart(extraClasses[i], null); 289 } 290 doc = WSDLFactory.newInstance(). 292 newWSDLWriter().getDocument(def); 293 types.insertTypesFragment(doc); 294 break; 295 case MODE_IMPLEMENTATION: 296 def = getImplWSDL(); 297 doc = WSDLFactory.newInstance(). 298 newWSDLWriter().getDocument(def); 299 break; 300 } 301 302 return doc; 304 } 305 306 320 public String emitToString(int mode) 321 throws IOException , WSDLException, 322 SAXException , ParserConfigurationException 323 { 324 Document doc = emit(mode); 325 StringWriter sw = new StringWriter (); 326 XMLUtils.PrettyDocumentToWriter(doc, sw); 327 return sw.toString(); 328 } 329 330 344 public void emit(String filename, int mode) 345 throws IOException , WSDLException, 346 SAXException , ParserConfigurationException 347 { 348 Document doc = emit(mode); 349 350 if (filename == null) 352 { 353 filename = getServicePortName(); 354 switch (mode) 355 { 356 case MODE_ALL: 357 filename += ".wsdl"; 358 break; 359 case MODE_INTERFACE: 360 filename += "_interface.wsdl"; 361 break; 362 case MODE_IMPLEMENTATION: 363 filename += "_implementation.wsdl"; 364 break; 365 } 366 } 367 368 prettyDocumentToFile(doc, filename); 369 } 370 371 381 public Definition getWSDL() 382 throws IOException , WSDLException, 383 SAXException , ParserConfigurationException 384 { 385 init(MODE_ALL); 387 388 Definition def = createDefinition(); 390 391 writeDefinitions(def, intfNS); 393 394 types = createTypes(def); 396 397 Binding binding = writeBinding(def, true); 399 writePortType(def, binding); 400 writeService(def, binding); 401 return def; 402 } 403 404 414 public Definition getIntfWSDL() 415 throws IOException , WSDLException, 416 SAXException , ParserConfigurationException 417 { 418 init(MODE_INTERFACE); 420 421 Definition def = createDefinition(); 423 424 writeDefinitions(def, intfNS); 426 427 types = createTypes(def); 429 430 Binding binding = writeBinding(def, true); 432 writePortType(def, binding); 433 return def; 434 } 435 436 446 public Definition getImplWSDL() 447 throws IOException , WSDLException, 448 SAXException , ParserConfigurationException 449 { 450 init(MODE_IMPLEMENTATION); 452 453 Definition def = createDefinition(); 455 456 writeDefinitions(def, implNS); 458 writeImport(def, intfNS, importUrl); 459 460 Binding binding = writeBinding(def, false); writeService(def, binding); 463 return def; 464 } 465 466 470 protected void init(int mode) 471 { 472 473 if (use == null) 475 { 476 if (style == Style.RPC) 477 { 478 use = Use.ENCODED; 479 } 480 else 481 { 482 use = Use.LITERAL; 483 } 484 } 485 486 if (defaultTM == null) 488 { 489 defaultTM = DefaultTypeMappingImpl.getSingleton(); 490 } 491 492 if (serviceDesc == null) 494 { 495 serviceDesc = new ServiceDesc(); 496 serviceDesc.setImplClass(cls); 497 498 if (tm != null) 501 { 502 serviceDesc.setTypeMapping(tm); 503 } 504 else 505 { 506 serviceDesc.setTypeMapping(defaultTM); 507 } 508 509 serviceDesc.setStopClasses(stopClasses); 510 serviceDesc.setAllowedMethods(allowedMethods); 511 serviceDesc.setDisallowedMethods(disallowedMethods); 512 serviceDesc.setStyle(style); 513 514 if (implCls != null && 519 implCls != cls && 520 serviceDesc2 == null) 521 { 522 serviceDesc2 = new ServiceDesc(); 523 serviceDesc2.setImplClass(implCls); 524 525 if (tm != null) 528 { 529 serviceDesc2.setTypeMapping(tm); 530 } 531 else 532 { 533 serviceDesc2.setTypeMapping(defaultTM); 534 } 535 serviceDesc2.setStopClasses(stopClasses); 536 serviceDesc2.setAllowedMethods(allowedMethods); 537 serviceDesc2.setDisallowedMethods(disallowedMethods); 538 serviceDesc2.setStyle(style); 539 } 540 } 541 542 if (encodingList == null) 543 { 544 clsName = cls.getName(); 545 clsName = clsName.substring(clsName.lastIndexOf('.') + 1); 546 547 if (getPortTypeName() == null) 549 { 550 setPortTypeName(clsName); 551 } 552 553 if (getServiceElementName() == null) 555 { 556 setServiceElementName(getPortTypeName() + "Service"); 557 } 558 559 if (getServicePortName() == null) 561 { 562 String name = getLocationUrl(); 563 if (name != null) 564 { 565 if (name.lastIndexOf('/') > 0) 566 { 567 name = name.substring(name.lastIndexOf('/') + 1); 568 } 569 else if (name.lastIndexOf('\\') > 0) 570 { 571 name = name.substring(name.lastIndexOf('\\') + 1); 572 } 573 else 574 { 575 name = null; 576 } 577 if (name != null && name.endsWith(".jws")) 579 { 580 name = name.substring(0, 581 (name.length() - ".jws".length())); 582 } 583 } 584 if (name == null || name.equals("")) 585 { 586 name = clsName; 587 } 588 setServicePortName(name); 589 } 590 591 if (getBindingName() == null) 593 { 594 setBindingName(getServicePortName() + "SoapBinding"); 595 } 596 597 encodingList = new ArrayList (); 598 encodingList.add(Constants.URI_DEFAULT_SOAP_ENC); 599 600 if (intfNS == null) 601 { 602 Package pkg = cls.getPackage(); 603 intfNS = namespaces.getCreate(pkg == null ? null : pkg.getName()); 604 } 605 if (implNS == null) 608 { 609 if (mode == MODE_ALL) 610 { 611 implNS = intfNS; 612 } 613 else 614 { 615 implNS = intfNS + "-impl"; 616 } 617 } 618 619 serviceDesc.setDefaultNamespace(intfNS); 621 if (serviceDesc2 != null) 622 { 623 serviceDesc2.setDefaultNamespace(implNS); 624 } 625 626 if (cls != null) 627 { 628 namespaces.put(cls.getName(), intfNS, "intf"); 629 } 630 namespaces.putPrefix(implNS, "impl"); 631 } 632 } 633 634 635 641 protected Definition createDefinition() 642 throws WSDLException, SAXException , IOException , 643 ParserConfigurationException 644 { 645 Definition def; 646 if (inputWSDL == null) 647 { 648 def = WSDLFactory.newInstance().newDefinition(); 649 } 650 else 651 { 652 javax.wsdl.xml.WSDLReader reader = 653 WSDLFactory.newInstance().newWSDLReader(); 654 Document doc = XMLUtils.newDocument(inputWSDL); 655 def = reader.readWSDL(null, doc); 656 def.setTypes(null); 659 } 660 return def; 661 } 662 663 protected static TypeMapping standardTypes = 664 (TypeMapping)new org.jboss.axis.encoding.TypeMappingRegistryImpl().getTypeMapping(null); 665 666 667 673 protected Types createTypes(Definition def) 674 throws IOException , WSDLException, SAXException , 675 ParserConfigurationException 676 { 677 types = new Types(def, tm, defaultTM, namespaces, 678 intfNS, stopClasses, serviceDesc); 679 if (inputWSDL != null) 680 { 681 types.loadInputTypes(inputWSDL); 682 } 683 if (inputSchema != null) 684 { 685 types.loadInputSchema(inputSchema); 686 } 687 688 if (tm != null) 689 { 690 Class [] mappedTypes = tm.getAllClasses(); 691 for (int i = 0; i < mappedTypes.length; i++) 692 { 693 Class mappedType = mappedTypes[i]; 694 QName name = tm.getTypeQName(mappedType); 695 699 if (standardTypes.getSerializer(mappedType) == null) 700 { 701 types.writeTypeForPart(mappedType, name); 702 } 703 } 704 } 705 706 return types; 707 } 708 709 710 716 protected void writeDefinitions(Definition def, String tns) 717 { 718 def.setTargetNamespace(tns); 719 720 def.addNamespace("intf", intfNS); 721 def.addNamespace("impl", implNS); 722 723 def.addNamespace(Constants.NS_PREFIX_WSDL_SOAP, 724 Constants.URI_WSDL11_SOAP); 725 namespaces.putPrefix(Constants.URI_WSDL11_SOAP, 726 Constants.NS_PREFIX_WSDL_SOAP); 727 728 def.addNamespace(Constants.NS_PREFIX_WSDL, 729 Constants.NS_URI_WSDL11); 730 namespaces.putPrefix(Constants.NS_URI_WSDL11, 731 Constants.NS_PREFIX_WSDL); 732 733 def.addNamespace(Constants.NS_PREFIX_SOAP_ENC, 734 Constants.URI_DEFAULT_SOAP_ENC); 735 namespaces.putPrefix(Constants.URI_DEFAULT_SOAP_ENC, 736 Constants.NS_PREFIX_SOAP_ENC); 737 738 def.addNamespace(Constants.NS_PREFIX_SCHEMA_XSD, 739 Constants.URI_DEFAULT_SCHEMA_XSD); 740 namespaces.putPrefix(Constants.URI_DEFAULT_SCHEMA_XSD, 741 Constants.NS_PREFIX_SCHEMA_XSD); 742 743 def.addNamespace(Constants.NS_PREFIX_XMLSOAP, 744 Constants.NS_URI_XMLSOAP); 745 namespaces.putPrefix(Constants.NS_URI_XMLSOAP, 746 Constants.NS_PREFIX_XMLSOAP); 747 } 748 749 756 protected void writeImport(Definition def, String tns, String loc) 757 { 758 Import imp = def.createImport(); 759 760 imp.setNamespaceURI(tns); 761 if (loc != null && !loc.equals("")) 762 imp.setLocationURI(loc); 763 def.addImport(imp); 764 } 765 766 772 protected Binding writeBinding(Definition def, boolean add) 773 { 774 QName bindingQName = 775 new QName (intfNS, getBindingName()); 776 777 Binding binding = def.getBinding(bindingQName); 779 if (binding != null) 780 { 781 return binding; 782 } 783 784 binding = def.createBinding(); 786 binding.setUndefined(false); 787 binding.setQName(bindingQName); 788 789 SOAPBinding soapBinding = new SOAPBindingImpl(); 790 String styleStr = (style == Style.RPC) ? "rpc" : "document"; 791 soapBinding.setStyle(styleStr); 792 soapBinding.setTransportURI(Constants.URI_SOAP11_HTTP); 793 794 binding.addExtensibilityElement(soapBinding); 795 796 if (add) 797 { 798 def.addBinding(binding); 799 } 800 return binding; 801 } 802 803 809 protected void writeService(Definition def, Binding binding) 810 { 811 812 QName serviceElementQName = 813 new QName (implNS, 814 getServiceElementName()); 815 816 Service service = def.getService(serviceElementQName); 818 if (service == null) 819 { 820 service = def.createService(); 821 service.setQName(serviceElementQName); 822 def.addService(service); 823 } 824 825 Port port = def.createPort(); 827 port.setBinding(binding); 828 port.setName(getServicePortName()); 830 831 SOAPAddress addr = new SOAPAddressImpl(); 832 addr.setLocationURI(locationUrl); 833 834 port.addExtensibilityElement(addr); 835 836 service.addPort(port); 837 } 838 839 847 protected void writePortType(Definition def, Binding binding) 848 throws WSDLException, AxisFault 849 { 850 851 QName portTypeQName = new QName (intfNS, getPortTypeName()); 852 853 PortType portType = def.getPortType(portTypeQName); 855 boolean newPortType = false; 856 if (portType == null) 857 { 858 portType = def.createPortType(); 859 portType.setUndefined(false); 860 portType.setQName(portTypeQName); 861 newPortType = true; 862 } 863 else if (binding.getBindingOperations().size() > 0) 864 { 865 return; 868 } 869 870 ArrayList operations = serviceDesc.getOperations(); 872 for (Iterator i = operations.iterator(); i.hasNext();) 873 { 874 OperationDesc thisOper = (OperationDesc)i.next(); 875 876 BindingOperation bindingOper = writeOperation(def, 877 binding, 878 thisOper); 879 Operation oper = bindingOper.getOperation(); 880 881 OperationDesc messageOper = thisOper; 882 if (serviceDesc2 != null) 883 { 884 OperationDesc[] operArray = 891 serviceDesc2.getOperationsByName(thisOper.getName()); 892 boolean found = false; 893 if (operArray != null) 894 { 895 for (int j = 0; 896 j < operArray.length && !found; 897 j++) 898 { 899 OperationDesc tryOper = operArray[j]; 900 if (tryOper.getParameters().size() == 901 thisOper.getParameters().size()) 902 { 903 boolean parmsMatch = true; 904 for (int k = 0; 905 k < thisOper.getParameters().size() && parmsMatch; 906 k++) 907 { 908 if (tryOper.getParameter(k).getMode() != 909 thisOper.getParameter(k).getMode() || 910 (!tryOper.getParameter(k).getJavaType(). 911 equals(thisOper.getParameter(k).getJavaType()))) 912 { 913 parmsMatch = false; 914 } 915 } 916 if (parmsMatch) 917 { 918 messageOper = tryOper; 919 found = true; 920 } 921 } 922 } 923 } 924 } 925 926 writeMessages(def, oper, messageOper, 927 bindingOper); 928 if (newPortType) 929 { 930 portType.addOperation(oper); 931 } 932 } 933 934 if (newPortType) 935 { 936 def.addPortType(portType); 937 } 938 939 binding.setPortType(portType); 940 } 941 942 952 protected void writeMessages(Definition def, 953 Operation oper, 954 OperationDesc desc, 955 BindingOperation bindingOper) 956 throws WSDLException, AxisFault 957 { 958 Input input = def.createInput(); 959 960 Message msg = writeRequestMessage(def, desc); 961 input.setMessage(msg); 962 963 String name = msg.getQName().getLocalPart(); 967 input.setName(name); 968 bindingOper.getBindingInput().setName(name); 969 970 oper.setInput(input); 971 def.addMessage(msg); 972 973 msg = writeResponseMessage(def, desc); 974 Output output = def.createOutput(); 975 output.setMessage(msg); 976 977 name = msg.getQName().getLocalPart(); 981 output.setName(name); 982 bindingOper.getBindingOutput().setName(name); 983 984 oper.setOutput(output); 985 def.addMessage(msg); 986 987 ArrayList exceptions = desc.getFaults(); 988 989 for (int i = 0; exceptions != null && i < exceptions.size(); i++) 990 { 991 FaultDesc faultDesc = (FaultDesc)exceptions.get(i); 992 msg = writeFaultMessage(def, faultDesc); 993 994 Fault fault = def.createFault(); 996 fault.setMessage(msg); 997 fault.setName(faultDesc.getName()); 998 oper.addFault(fault); 999 1000 BindingFault bFault = def.createBindingFault(); 1002 bFault.setName(faultDesc.getName()); 1003 SOAPFault soapFault = writeSOAPFault(faultDesc); 1004 bFault.addExtensibilityElement(soapFault); 1005 bindingOper.addBindingFault(bFault); 1006 1007 if (def.getMessage(msg.getQName()) == null) 1009 { 1010 def.addMessage(msg); 1011 } 1012 } 1013 1014 ArrayList parameters = desc.getParameters(); 1016 Vector names = new Vector (); 1017 for (int i = 0; i < parameters.size(); i++) 1018 { 1019 ParameterDesc param = (ParameterDesc)parameters.get(i); 1020 names.add(param.getName()); 1021 } 1022 1023 if (names.size() > 0) 1024 { 1025 if (style == Style.WRAPPED) 1026 { 1027 names.clear(); 1028 } 1029 oper.setParameterOrdering(names); 1030 } 1031 } 1032 1033 1039 protected BindingOperation writeOperation(Definition def, 1040 Binding binding, 1041 OperationDesc desc) 1042 { 1043 Operation oper = def.createOperation(); 1044 oper.setName(desc.getName()); 1045 oper.setUndefined(false); 1046 return writeBindingOperation(def, binding, oper, desc); 1047 } 1048 1049 1056 protected BindingOperation writeBindingOperation(Definition def, 1057 Binding binding, 1058 Operation oper, 1059 OperationDesc desc) 1060 { 1061 BindingOperation bindingOper = def.createBindingOperation(); 1062 BindingInput bindingInput = def.createBindingInput(); 1063 BindingOutput bindingOutput = def.createBindingOutput(); 1064 1065 bindingOper.setName(oper.getName()); 1066 bindingOper.setOperation(oper); 1067 1068 SOAPOperation soapOper = new SOAPOperationImpl(); 1069 1070 1071 String soapAction = ""; 1076 if (getSoapAction().equals("OPERATION")) 1077 { 1078 soapAction = oper.getName(); 1079 } 1080 else if (getSoapAction().equals("NONE")) 1081 { 1082 soapAction = ""; 1083 } 1084 else 1085 { 1086 soapAction = desc.getSoapAction(); 1087 if (soapAction == null) 1088 { 1089 soapAction = ""; 1090 } 1091 } 1092 soapOper.setSoapActionURI(soapAction); 1093 1094 1098 bindingOper.addExtensibilityElement(soapOper); 1099 1100 ExtensibilityElement input = null; 1102 input = writeSOAPBody(desc.getElementQName()); 1103 bindingInput.addExtensibilityElement(input); 1104 1105 ExtensibilityElement output = null; 1107 output = writeSOAPBody(desc.getReturnQName()); 1108 1109 bindingOutput.addExtensibilityElement(output); 1110 1111 bindingOper.setBindingInput(bindingInput); 1113 bindingOper.setBindingOutput(bindingOutput); 1114 1115 ArrayList faultList = desc.getFaults(); 1117 if (faultList != null) 1118 { 1119 for (Iterator it = faultList.iterator(); it.hasNext();) 1120 { 1121 FaultDesc faultDesc = (FaultDesc)it.next(); 1122 ExtensibilityElement soapFault = writeSOAPFault(faultDesc); 1124 BindingFault bindingFault = new BindingFaultImpl(); 1126 bindingFault.setName(faultDesc.getName()); 1127 bindingFault.addExtensibilityElement(soapFault); 1128 bindingOper.addBindingFault(bindingFault); 1129 } 1130 } 1131 1132 binding.addBindingOperation(bindingOper); 1133 1134 return bindingOper; 1135 } 1136 1137 protected ExtensibilityElement writeSOAPBody(QName operQName) 1138 { 1139 SOAPBody soapBody = new SOAPBodyImpl(); 1140 if (use == Use.ENCODED) 1142 { 1143 soapBody.setUse("encoded"); 1144 soapBody.setEncodingStyles(encodingList); 1145 } 1146 else 1147 { 1148 soapBody.setUse("literal"); 1149 } 1150 if (targetService == null) 1151 soapBody.setNamespaceURI(intfNS); 1152 else 1153 soapBody.setNamespaceURI(targetService); 1154 if (operQName != null && 1155 !operQName.getNamespaceURI().equals("")) 1156 { 1157 soapBody.setNamespaceURI(operQName.getNamespaceURI()); 1158 } 1159 return soapBody; 1160 } 1162 protected SOAPFault writeSOAPFault(FaultDesc faultDesc) 1163 { 1164 SOAPFault soapFault = new com.ibm.wsdl.extensions.soap.SOAPFaultImpl(); 1165 if (use != Use.ENCODED) 1166 { 1167 soapFault.setUse("literal"); 1168 } 1170 else 1171 { 1172 soapFault.setUse("encoded"); 1173 soapFault.setEncodingStyles(encodingList); 1174 1175 QName faultQName = faultDesc.getQName(); 1178 if (faultQName != null && 1179 !faultQName.getNamespaceURI().equals("")) 1180 { 1181 soapFault.setNamespaceURI(faultQName.getNamespaceURI()); 1182 } 1183 else 1184 { 1185 if (targetService == null) 1186 { 1187 soapFault.setNamespaceURI(intfNS); 1188 } 1189 else 1190 { 1191 soapFault.setNamespaceURI(targetService); 1192 } 1193 } 1194 } 1195 return soapFault; 1196 } 1198 1199 1206 protected Message writeRequestMessage(Definition def, 1207 OperationDesc oper) 1208 throws WSDLException, AxisFault 1209 { 1210 Message msg = def.createMessage(); 1211 1212 QName qName = createMessageName(def, oper.getName() + "Request"); 1213 1214 msg.setQName(qName); 1215 msg.setUndefined(false); 1216 1217 if (oper.getStyle() == Style.MESSAGE) 1218 { 1219 QName qname = oper.getElementQName(); 1223 Element el = types.createElementDecl(qname.getLocalPart(), 1224 Object .class, 1225 Constants.XSD_ANYTYPE, 1226 false, false); 1227 types.writeSchemaElement(qname, el); 1228 1229 Part part = def.createPart(); 1230 part.setName("part"); 1231 part.setElementName(qname); 1232 msg.addPart(part); 1233 } 1234 else if (oper.getStyle() == Style.WRAPPED) 1235 { 1236 writeWrapperPart(def, msg, oper, true); 1240 } 1241 else 1242 { 1243 ArrayList parameters = oper.getParameters(); 1245 for (int i = 0; i < parameters.size(); i++) 1246 { 1247 ParameterDesc parameter = (ParameterDesc)parameters.get(i); 1248 writePartToMessage(def, msg, true, parameter); 1249 } 1250 } 1251 1252 return msg; 1253 } 1254 1255 protected QName getRequestQName(OperationDesc oper) 1256 { 1257 QName qname = oper.getElementQName(); 1258 if (qname == null) 1259 { 1260 qname = new QName (oper.getName()); 1261 } 1262 return qname; 1263 } 1264 1265 protected QName getResponseQName(OperationDesc oper) 1266 { 1267 QName qname = oper.getElementQName(); 1268 if (qname == null) 1269 { 1270 return new QName (oper.getName() + "Response"); 1271 } 1272 return new QName (qname.getNamespaceURI(), 1273 qname.getLocalPart() + "Response"); 1274 } 1275 1276 1283 public void writeWrapperPart(Definition def, Message msg, 1284 OperationDesc oper, boolean request) 1285 throws AxisFault 1286 { 1287 QName qname = request ? getRequestQName(oper) : getResponseQName(oper); 1288 boolean hasParams = false; 1289 if (request) 1290 { 1291 hasParams = (oper.getNumInParams() > 0); 1292 } 1293 else 1294 { 1295 if (oper.getReturnClass() != void.class) 1296 { 1297 hasParams = true; 1298 } 1299 else 1300 { 1301 hasParams = (oper.getNumOutParams() > 0); 1302 } 1303 } 1304 1305 Element sequence = types.writeWrapperElement(qname, request, hasParams); 1307 1308 if (sequence != null) 1311 { 1312 ArrayList parameters = request ? oper.getAllInParams() : 1313 oper.getAllOutParams(); 1314 if (!request) 1315 { 1316 String retName; 1317 if (oper.getReturnQName() == null) 1318 { 1319 retName = oper.getName() + "Response"; 1320 } 1321 else 1322 { 1323 retName = oper.getReturnQName().getLocalPart(); 1324 } 1325 types.writeWrappedParameter(sequence, retName, 1326 oper.getReturnType(), 1327 oper.getReturnClass()); 1328 } 1329 for (int i = 0; i < parameters.size(); i++) 1330 { 1331 ParameterDesc parameter = (ParameterDesc)parameters.get(i); 1332 types.writeWrappedParameter(sequence, 1333 parameter.getName(), parameter.getTypeQName(), 1335 parameter.getJavaType()); 1336 } 1337 } 1338 1339 Part part = def.createPart(); 1341 part.setName("parameters"); part.setElementName(qname); 1343 msg.addPart(part); 1344 } 1345 1346 1353 protected Message writeResponseMessage(Definition def, 1354 OperationDesc desc) 1355 throws WSDLException, AxisFault 1356 { 1357 Message msg = def.createMessage(); 1358 1359 QName qName = createMessageName(def, desc.getName() + "Response"); 1360 1361 msg.setQName(qName); 1362 msg.setUndefined(false); 1363 1364 if (desc.getStyle() == Style.WRAPPED) 1365 { 1366 writeWrapperPart(def, msg, desc, false); 1367 } 1368 else 1369 { 1370 ParameterDesc retParam = new ParameterDesc(); 1372 if (desc.getReturnQName() == null) 1373 { 1374 String ns = ""; 1375 if (desc.getStyle() != Style.RPC) 1376 { 1377 ns = getServiceDesc().getDefaultNamespace(); 1378 if (ns == null || "".equals(ns)) 1379 { 1380 ns = "http://ws.apache.org/axis/defaultNS"; 1381 } 1382 } 1383 retParam.setQName(new QName (ns, desc.getName() + "Response")); 1384 } 1385 else 1386 { 1387 retParam.setQName(desc.getReturnQName()); 1388 } 1389 retParam.setTypeQName(desc.getReturnType()); 1390 retParam.setMode(ParameterDesc.OUT); 1391 retParam.setIsReturn(true); 1392 retParam.setJavaType(desc.getReturnClass()); 1393 writePartToMessage(def, msg, false, retParam); 1394 1395 ArrayList parameters = desc.getAllOutParams(); 1396 for (Iterator i = parameters.iterator(); i.hasNext();) 1397 { 1398 ParameterDesc param = (ParameterDesc)i.next(); 1399 writePartToMessage(def, msg, false, param); 1400 } 1401 } 1402 return msg; 1403 } 1404 1405 1413 protected Message writeFaultMessage(Definition def, 1414 FaultDesc exception) 1415 throws WSDLException, AxisFault 1416 { 1417 1418 String pkgAndClsName = exception.getClassName(); 1419 String clsName = pkgAndClsName.substring(pkgAndClsName.lastIndexOf('.') + 1, 1420 pkgAndClsName.length()); 1421 1422 exception.setName(clsName); 1424 1425 1428 Message msg = (Message)exceptionMsg.get(pkgAndClsName); 1429 1430 if (msg == null) 1431 { 1432 msg = def.createMessage(); 1433 QName qName = createMessageName(def, clsName); 1434 1435 msg.setQName(qName); 1436 msg.setUndefined(false); 1437 1438 ArrayList parameters = exception.getParameters(); 1439 if (parameters != null) 1440 { 1441 for (int i = 0; i < parameters.size(); i++) 1442 { 1443 ParameterDesc parameter = (ParameterDesc)parameters.get(i); 1444 writePartToMessage(def, msg, true, parameter); 1445 } 1446 } 1447 exceptionMsg.put(pkgAndClsName, msg); 1448 } 1449 1450 return msg; 1451 1452 } 1453 1454 1465 public String writePartToMessage(Definition def, 1466 Message msg, 1467 boolean request, 1468 ParameterDesc param) throws WSDLException, AxisFault 1469 { 1470 if (param == null || 1472 param.getJavaType() == java.lang.Void.TYPE) 1473 return null; 1474 1475 if (request && 1478 param.getMode() == ParameterDesc.OUT) 1479 { 1480 return null; 1481 } 1482 if (!request && 1483 param.getMode() == ParameterDesc.IN) 1484 { 1485 return null; 1486 } 1487 1488 Part part = def.createPart(); 1490 1491 Class javaType = param.getJavaType(); 1497 if (param.getMode() != ParameterDesc.IN && 1498 param.getIsReturn() == false) 1499 { 1500 javaType = JavaUtils.getHolderValueType(javaType); 1501 } 1502 1503 if (use == Use.ENCODED || style == Style.RPC) 1504 { 1505 QName typeQName = param.getTypeQName(); 1509 if (javaType != null) 1510 typeQName = types.writeTypeForPart(javaType, 1511 typeQName); 1512 if (typeQName != null) 1514 { 1515 part.setName(param.getName()); 1516 part.setTypeName(typeQName); 1517 msg.addPart(part); 1518 } 1519 } 1520 else if (use == Use.LITERAL) 1521 { 1522 QName qname = param.getQName(); 1526 Element el = types.createElementDecl(qname.getLocalPart(), 1527 param.getJavaType(), 1528 param.getTypeQName(), 1529 false, false); 1530 types.writeSchemaElement(qname, el); 1531 1532 part.setName(param.getName()); 1533 part.setElementName(qname); 1534 msg.addPart(part); 1535 } 1536 return param.getName(); 1537 } 1538 1539 1542 protected QName createMessageName(Definition def, String methodName) 1543 { 1544 1545 QName qName = new QName (intfNS, methodName); 1546 1547 int messageNumber = 1; 1549 while (def.getMessage(qName) != null) 1550 { 1551 StringBuffer namebuf = new StringBuffer (methodName); 1552 namebuf.append(messageNumber); 1553 qName = new QName (intfNS, namebuf.toString()); 1554 messageNumber++; 1555 } 1556 return qName; 1557 } 1558 1559 1566 protected void prettyDocumentToFile(Document doc, String filename) 1567 throws IOException 1568 { 1569 FileOutputStream fos = new FileOutputStream (new File (filename)); 1570 XMLUtils.PrettyDocumentToStream(doc, fos); 1571 fos.close(); 1572 } 1573 1574 1576 1581 public Class getCls() 1582 { 1583 return cls; 1584 } 1585 1586 1591 public void setCls(Class cls) 1592 { 1593 this.cls = cls; 1594 } 1595 1596 1601 public void setClsSmart(Class cls, String location) 1602 { 1603 1604 if (cls == null || location == null) 1605 return; 1606 1607 if (location.lastIndexOf('/') > 0) 1609 { 1610 location = 1611 location.substring(location.lastIndexOf('/') + 1); 1612 } 1613 else if (location.lastIndexOf('\\') > 0) 1614 { 1615 location = 1616 location.substring(location.lastIndexOf('\\') + 1); 1617 } 1618 1619 java.lang.reflect.Constructor [] constructors = 1621 cls.getDeclaredConstructors(); 1622 Class intf = null; 1623 for (int i = 0; i < constructors.length && intf == null; i++) 1624 { 1625 Class [] parms = constructors[i].getParameterTypes(); 1626 if (parms.length == 1 && 1630 parms[0].isInterface() && 1631 parms[0].getName() != null && 1632 Types.getLocalNameFromFullName(parms[0].getName()).equals(location)) 1633 { 1634 intf = parms[0]; 1635 } 1636 } 1637 if (intf != null) 1638 { 1639 setCls(intf); 1640 if (implCls == null) 1641 { 1642 setImplCls(cls); 1643 } 1644 } 1645 else 1646 setCls(cls); 1647 } 1648 1649 1654 public void setCls(String className) throws ClassNotFoundException 1655 { 1656 cls = ClassUtils.forName(className); 1657 } 1658 1659 1664 public Class getImplCls() 1665 { 1666 return implCls; 1667 } 1668 1669 1674 public void setImplCls(Class implCls) 1675 { 1676 this.implCls = implCls; 1677 } 1678 1679 1684 public void setImplCls(String className) 1685 { 1686 try 1687 { 1688 implCls = ClassUtils.forName(className); 1689 } 1690 catch (Exception ex) 1691 { 1692 ex.printStackTrace(); 1693 } 1694 } 1695 1696 1701 public String getIntfNamespace() 1702 { 1703 return intfNS; 1704 } 1705 1706 1711 public void setIntfNamespace(String ns) 1712 { 1713 this.intfNS = ns; 1714 } 1715 1716 1721 public String getImplNamespace() 1722 { 1723 return implNS; 1724 } 1725 1726 1731 public void setImplNamespace(String ns) 1732 { 1733 this.implNS = ns; 1734 } 1735 1736 1741 public Vector getAllowedMethods() 1742 { 1743 return allowedMethods; 1744 } 1745 1746 1749 public void setAllowedMethods(String text) 1750 { 1751 if (text != null) 1752 { 1753 StringTokenizer tokenizer = new StringTokenizer (text, " ,+"); 1754 if (allowedMethods == null) 1755 { 1756 allowedMethods = new Vector (); 1757 } 1758 while (tokenizer.hasMoreTokens()) 1759 { 1760 allowedMethods.add(tokenizer.nextToken()); 1761 } 1762 } 1763 } 1764 1765 1770 public void setAllowedMethods(Vector allowedMethods) 1771 { 1772 if (this.allowedMethods == null) 1773 { 1774 this.allowedMethods = new Vector (); 1775 } 1776 this.allowedMethods.addAll(allowedMethods); 1777 } 1778 1779 1782 public boolean getUseInheritedMethods() 1783 { 1784 return useInheritedMethods; 1785 } 1786 1787 1790 public void setUseInheritedMethods(boolean useInheritedMethods) 1791 { 1792 this.useInheritedMethods = useInheritedMethods; 1793 } 1794 1795 1800 public void setDisallowedMethods(Vector disallowedMethods) 1801 { 1802 if (this.disallowedMethods == null) 1803 { 1804 this.disallowedMethods = new Vector (); 1805 } 1806 this.disallowedMethods.addAll(disallowedMethods); 1807 } 1808 1809 1814 public void setDisallowedMethods(String text) 1815 { 1816 if (text != null) 1817 { 1818 StringTokenizer tokenizer = new StringTokenizer (text, " ,+"); 1819 if (disallowedMethods == null) 1820 { 1821 disallowedMethods = new Vector (); 1822 } 1823 disallowedMethods = new Vector (); 1824 while (tokenizer.hasMoreTokens()) 1825 { 1826 disallowedMethods.add(tokenizer.nextToken()); 1827 } 1828 } 1829 } 1830 1831 1834 public Vector getDisallowedMethods() 1835 { 1836 return disallowedMethods; 1837 } 1838 1839 1845 public void setStopClasses(ArrayList stopClasses) 1846 { 1847 if (this.stopClasses == null) 1848 { 1849 this.stopClasses = new ArrayList (); 1850 } 1851 this.stopClasses.addAll(stopClasses); 1852 } 1853 1854 1860 public void setStopClasses(String text) 1861 { 1862 if (text != null) 1863 { 1864 StringTokenizer tokenizer = new StringTokenizer (text, " ,+"); 1865 if (stopClasses == null) 1866 { 1867 stopClasses = new ArrayList (); 1868 } 1869 while (tokenizer.hasMoreTokens()) 1870 { 1871 stopClasses.add(tokenizer.nextToken()); 1872 } 1873 } 1874 } 1875 1876 1879 public ArrayList getStopClasses() 1880 { 1881 return stopClasses; 1882 } 1883 1884 1889 public Map getNamespaceMap() 1890 { 1891 return namespaces; 1892 } 1893 1894 1899 public void setNamespaceMap(Map map) 1900 { 1901 if (map != null) 1902 namespaces.putAll(map); 1903 } 1904 1905 1910 public String getInputWSDL() 1911 { 1912 return inputWSDL; 1913 } 1914 1915 1920 public void setInputWSDL(String inputWSDL) 1921 { 1922 this.inputWSDL = inputWSDL; 1923 } 1924 1925 1928 public String getInputSchema() 1929 { 1930 return inputSchema; 1931 } 1932 1933 1938 public void setInputSchema(String inputSchema) 1939 { 1940 this.inputSchema = inputSchema; 1941 } 1942 1943 1948 public String getLocationUrl() 1949 { 1950 return locationUrl; 1951 } 1952 1953 1958 public void setLocationUrl(String locationUrl) 1959 { 1960 this.locationUrl = locationUrl; 1961 } 1962 1963 1968 public String getImportUrl() 1969 { 1970 return importUrl; 1971 } 1972 1973 1980 public void setImportUrl(String importUrl) 1981 { 1982 this.importUrl = importUrl; 1983 } 1984 1985 1990 public String getServicePortName() 1991 { 1992 return servicePortName; 1993 } 1994 1995 2000 public void setServicePortName(String servicePortName) 2001 { 2002 this.servicePortName = servicePortName; 2003 } 2004 2005 2010 public String getServiceElementName() 2011 { 2012 return serviceElementName; 2013 } 2014 2015 2020 public void setServiceElementName(String serviceElementName) 2021 { 2022 this.serviceElementName = serviceElementName; 2023 } 2024 2025 2030 public String getPortTypeName() 2031 { 2032 return portTypeName; 2033 } 2034 2035 2040 public void setPortTypeName(String portTypeName) 2041 { 2042 this.portTypeName = portTypeName; 2043 } 2044 2045 2050 public String getBindingName() 2051 { 2052 return bindingName; 2053 } 2054 2055 2060 public void setBindingName(String bindingName) 2061 { 2062 this.bindingName = bindingName; 2063 } 2064 2065 2070 public String getTargetService() 2071 { 2072 return targetService; 2073 } 2074 2075 2080 public void setTargetService(String targetService) 2081 { 2082 this.targetService = targetService; 2083 } 2084 2085 2090 public String getDescription() 2091 { 2092 return description; 2093 } 2094 2095 2100 public void setDescription(String description) 2101 { 2102 this.description = description; 2103 } 2104 2105 2110 public String getSoapAction() 2111 { 2112 return soapAction; 2113 } 2114 2115 2120 public void setSoapAction(String value) 2121 { 2122 soapAction = value; 2123 } 2124 2125 2130 public TypeMapping getTypeMapping() 2131 { 2132 return tm; 2133 } 2134 2135 2140 public void setTypeMapping(TypeMapping tm) 2141 { 2142 this.tm = tm; 2143 } 2144 2145 2150 public TypeMapping getDefaultTypeMapping() 2151 { 2152 return defaultTM; 2153 } 2154 2155 2160 public void setDefaultTypeMapping(TypeMapping defaultTM) 2161 { 2162 this.defaultTM = defaultTM; 2163 } 2164 2165 2166 2171 public Style getStyle() 2172 { 2173 return style; 2174 } 2175 2176 2187 public void setStyle(String value) 2188 { 2189 setStyle(Style.getStyle(value)); 2190 } 2191 2192 2197 public void setStyle(Style value) 2198 { 2199 style = value; 2200 if (style.equals(Style.WRAPPED)) 2201 { 2202 setUse(Use.LITERAL); 2203 } 2204 } 2205 2206 2211 public Use getUse() 2212 { 2213 return use; 2214 } 2215 2216 2226 public void setUse(String value) 2227 { 2228 use = Use.getUse(value); 2229 } 2230 2231 2236 public void setUse(Use value) 2237 { 2238 use = value; 2239 } 2240 2241 2246 public void setMode(int mode) 2247 { 2248 if (mode == MODE_RPC) 2249 { 2250 setStyle(Style.RPC); 2251 setUse(Use.ENCODED); 2252 } 2253 else if (mode == MODE_DOCUMENT) 2254 { 2255 setStyle(Style.DOCUMENT); 2256 setUse(Use.LITERAL); 2257 } 2258 else if (mode == MODE_DOC_WRAPPED) 2259 { 2260 setStyle(Style.WRAPPED); 2261 setUse(Use.LITERAL); 2262 } 2263 } 2264 2265 2271 public int getMode() 2272 { 2273 if (style == Style.RPC) 2274 { 2275 return MODE_RPC; 2276 } 2277 else if (style == Style.DOCUMENT) 2278 { 2279 return MODE_DOCUMENT; 2280 } 2281 else if (style == Style.WRAPPED) 2282 { 2283 return MODE_DOC_WRAPPED; 2284 } 2285 return -1; 2286 } 2287 2288 public ServiceDesc getServiceDesc() 2289 { 2290 return serviceDesc; 2291 } 2292 2293 public void setServiceDesc(ServiceDesc serviceDesc) 2294 { 2295 this.serviceDesc = serviceDesc; 2296 } 2297 2298 2301 public Class [] getExtraClasses() 2302 { 2303 return extraClasses; 2304 } 2305 2306 2310 public void setExtraClasses(Class [] extraClasses) 2311 { 2312 this.extraClasses = extraClasses; 2313 } 2314 2315 2320 public void setExtraClasses(String text) throws ClassNotFoundException 2321 { 2322 ArrayList clsList = new ArrayList (); 2323 if (text != null) 2324 { 2325 StringTokenizer tokenizer = new StringTokenizer (text, " ,"); 2326 while (tokenizer.hasMoreTokens()) 2327 { 2328 String clsName = tokenizer.nextToken(); 2329 Class cls = ClassUtils.forName(clsName); 2331 clsList.add(cls); 2332 } 2333 } 2334 Class [] ec; 2336 if (extraClasses != null) 2337 { 2338 ec = new Class [clsList.size() + extraClasses.length]; 2339 for (int i = 0; i < extraClasses.length; i++) 2341 { 2342 Class c = extraClasses[i]; 2343 ec[i] = c; 2344 } 2345 } 2346 else 2347 { 2348 ec = new Class [clsList.size()]; 2349 } 2350 for (int i = 0; i < clsList.size(); i++) 2352 { 2353 Class c = (Class )clsList.get(i); 2354 ec[i] = c; 2355 } 2356 this.extraClasses = ec; 2358 } 2359 2360} 2361 | Popular Tags |