Thu, 31 Aug 2017 15:18:52 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package com.sun.tools.internal.ws.processor.modeler.wsdl; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import com.sun.tools.internal.ws.processor.generator.Names; |
aoqi@0 | 29 | import static com.sun.tools.internal.ws.processor.modeler.wsdl.WSDLModelerBase.getExtensionOfType; |
aoqi@0 | 30 | import com.sun.tools.internal.ws.wscompile.ErrorReceiver; |
aoqi@0 | 31 | import com.sun.tools.internal.ws.wscompile.WsimportOptions; |
aoqi@0 | 32 | import com.sun.tools.internal.ws.wscompile.Options; |
aoqi@0 | 33 | import com.sun.tools.internal.ws.wsdl.document.*; |
aoqi@0 | 34 | import com.sun.tools.internal.ws.wsdl.document.jaxws.JAXWSBinding; |
aoqi@0 | 35 | import com.sun.tools.internal.ws.wsdl.document.schema.SchemaKinds; |
aoqi@0 | 36 | import com.sun.tools.internal.ws.wsdl.document.soap.SOAP12Binding; |
aoqi@0 | 37 | import com.sun.tools.internal.ws.wsdl.document.soap.SOAPBinding; |
aoqi@0 | 38 | import org.xml.sax.InputSource; |
aoqi@0 | 39 | |
aoqi@0 | 40 | import javax.xml.namespace.QName; |
aoqi@0 | 41 | import java.io.ByteArrayInputStream; |
aoqi@0 | 42 | import java.io.StringReader; |
aoqi@0 | 43 | import java.io.StringWriter; |
aoqi@0 | 44 | import java.nio.charset.StandardCharsets; |
aoqi@0 | 45 | import java.text.MessageFormat; |
aoqi@0 | 46 | import java.util.*; |
aoqi@0 | 47 | |
aoqi@0 | 48 | |
aoqi@0 | 49 | /** |
aoqi@0 | 50 | * Builds all possible pseudo schemas for async operation ResponseBean to feed to XJC. |
aoqi@0 | 51 | * |
aoqi@0 | 52 | * @author Vivek Pandey |
aoqi@0 | 53 | */ |
aoqi@0 | 54 | public class PseudoSchemaBuilder { |
aoqi@0 | 55 | |
aoqi@0 | 56 | private final StringWriter buf = new StringWriter(); |
aoqi@0 | 57 | private final WSDLDocument wsdlDocument; |
aoqi@0 | 58 | private WSDLModeler wsdlModeler; |
aoqi@0 | 59 | private final List<InputSource> schemas = new ArrayList<InputSource>(); |
aoqi@0 | 60 | private final HashMap<QName, Port> bindingNameToPortMap = new HashMap<QName, Port>(); |
aoqi@0 | 61 | private static final String w3ceprSchemaBinding = "<bindings\n" + |
aoqi@0 | 62 | " xmlns=\"http://java.sun.com/xml/ns/jaxb\"\n" + |
aoqi@0 | 63 | " xmlns:wsa=\"http://www.w3.org/2005/08/addressing\"\n" + |
aoqi@0 | 64 | " xmlns:xjc=\"http://java.sun.com/xml/ns/jaxb/xjc\"\n" + |
aoqi@0 | 65 | " version=\"2.1\">\n" + |
aoqi@0 | 66 | " \n" + |
aoqi@0 | 67 | " <bindings scd=\"x-schema::wsa\" if-exists=\"true\">\n" + |
aoqi@0 | 68 | //comment the following, otw JAXB won't generate ObjectFactory, classes from wsa schema. See JAX-WS-804 |
aoqi@0 | 69 | // " <schemaBindings map=\"false\" />\n" + |
aoqi@0 | 70 | " <bindings scd=\"wsa:EndpointReference\">\n" + |
aoqi@0 | 71 | " <class ref=\"javax.xml.ws.wsaddressing.W3CEndpointReference\" xjc:recursive=\"true\"/>\n" + |
aoqi@0 | 72 | " </bindings>\n" + |
aoqi@0 | 73 | " <bindings scd=\"~wsa:EndpointReferenceType\">\n" + |
aoqi@0 | 74 | " <class ref=\"javax.xml.ws.wsaddressing.W3CEndpointReference\" xjc:recursive=\"true\"/>\n" + |
aoqi@0 | 75 | " </bindings>\n" + |
aoqi@0 | 76 | " </bindings>\n" + |
aoqi@0 | 77 | "</bindings>"; |
aoqi@0 | 78 | |
aoqi@0 | 79 | private static final String memberSubmissionEPR = "<bindings\n" + |
aoqi@0 | 80 | " xmlns=\"http://java.sun.com/xml/ns/jaxb\"\n" + |
aoqi@0 | 81 | " xmlns:wsa=\"http://schemas.xmlsoap.org/ws/2004/08/addressing\"\n" + |
aoqi@0 | 82 | " version=\"2.1\">\n" + |
aoqi@0 | 83 | " \n" + |
aoqi@0 | 84 | " <bindings scd=\"x-schema::wsa\" if-exists=\"true\">\n" + |
aoqi@0 | 85 | //comment the following, otw JAXB won't generate ObjectFactory, classes from wsa schema. See JAX-WS-804 |
aoqi@0 | 86 | // " <schemaBindings map=\"false\" />\n" + |
aoqi@0 | 87 | " <bindings scd=\"wsa:EndpointReference\">\n" + |
aoqi@0 | 88 | " <class ref=\"com.sun.xml.internal.ws.developer.MemberSubmissionEndpointReference\"/>\n" + |
aoqi@0 | 89 | " </bindings>\n" + |
aoqi@0 | 90 | " <bindings scd=\"~wsa:EndpointReferenceType\">\n" + |
aoqi@0 | 91 | " <class ref=\"com.sun.xml.internal.ws.developer.MemberSubmissionEndpointReference\"/>\n" + |
aoqi@0 | 92 | " </bindings>\n" + |
aoqi@0 | 93 | " </bindings>\n" + |
aoqi@0 | 94 | "</bindings>"; |
aoqi@0 | 95 | |
aoqi@0 | 96 | private final static String sysId = "http://dummy.pseudo-schema#schema"; |
aoqi@0 | 97 | |
aoqi@0 | 98 | private WsimportOptions options; |
aoqi@0 | 99 | public static List<InputSource> build(WSDLModeler wsdlModeler, WsimportOptions options, ErrorReceiver errReceiver) { |
aoqi@0 | 100 | PseudoSchemaBuilder b = new PseudoSchemaBuilder(wsdlModeler.document); |
aoqi@0 | 101 | b.wsdlModeler = wsdlModeler; |
aoqi@0 | 102 | b.options = options; |
aoqi@0 | 103 | b.build(); |
aoqi@0 | 104 | int i; |
aoqi@0 | 105 | for(i = 0; i < b.schemas.size(); i++){ |
aoqi@0 | 106 | InputSource is = b.schemas.get(i); |
aoqi@0 | 107 | is.setSystemId(sysId+(i + 1)); |
aoqi@0 | 108 | } |
aoqi@0 | 109 | //add w3c EPR binding |
aoqi@0 | 110 | if(!(options.noAddressingBbinding) && options.target.isLaterThan(Options.Target.V2_1)){ |
aoqi@0 | 111 | InputSource is = new InputSource(new ByteArrayInputStream(w3ceprSchemaBinding.getBytes(StandardCharsets.UTF_8))); |
aoqi@0 | 112 | is.setSystemId(sysId+(++i +1)); |
aoqi@0 | 113 | b.schemas.add(is); |
aoqi@0 | 114 | } |
aoqi@0 | 115 | |
aoqi@0 | 116 | |
aoqi@0 | 117 | //TODO: uncomment after JAXB fixes the issue related to passing multiples of such bindings |
aoqi@0 | 118 | //add member submission EPR binding |
aoqi@0 | 119 | // InputSource is1 = new InputSource(new ByteArrayInputStream(memberSubmissionEPR.getBytes())); |
aoqi@0 | 120 | // is1.setSystemId(sysId+(++i + 1)); |
aoqi@0 | 121 | // b.schemas.add(is1); |
aoqi@0 | 122 | |
aoqi@0 | 123 | return b.schemas; |
aoqi@0 | 124 | } |
aoqi@0 | 125 | |
aoqi@0 | 126 | |
aoqi@0 | 127 | private PseudoSchemaBuilder(WSDLDocument _wsdl) { |
aoqi@0 | 128 | this.wsdlDocument = _wsdl; |
aoqi@0 | 129 | } |
aoqi@0 | 130 | |
aoqi@0 | 131 | private void build() { |
aoqi@0 | 132 | for(Iterator<Service> itr=wsdlDocument.getDefinitions().services(); itr.hasNext(); ) { |
aoqi@0 | 133 | build(itr.next()); |
aoqi@0 | 134 | } |
aoqi@0 | 135 | } |
aoqi@0 | 136 | |
aoqi@0 | 137 | private void build(Service service) { |
aoqi@0 | 138 | for( Iterator<Port> itr=service.ports(); itr.hasNext(); ) { |
aoqi@0 | 139 | build(itr.next() ); |
aoqi@0 | 140 | } |
aoqi@0 | 141 | } |
aoqi@0 | 142 | |
aoqi@0 | 143 | private void build(Port port) { |
aoqi@0 | 144 | if(wsdlModeler.isProvider(port)) |
aoqi@0 | 145 | return; |
aoqi@0 | 146 | Binding binding = port.resolveBinding(wsdlDocument); |
aoqi@0 | 147 | |
aoqi@0 | 148 | SOAPBinding soapBinding = |
aoqi@0 | 149 | (SOAPBinding)getExtensionOfType(binding, SOAPBinding.class); |
aoqi@0 | 150 | //lets try and see if its SOAP 1.2. dont worry about extension flag, its |
aoqi@0 | 151 | // handled much earlier |
aoqi@0 | 152 | if (soapBinding == null) { |
aoqi@0 | 153 | soapBinding = |
aoqi@0 | 154 | (SOAPBinding)getExtensionOfType(binding, SOAP12Binding.class); |
aoqi@0 | 155 | } |
aoqi@0 | 156 | if(soapBinding == null) |
aoqi@0 | 157 | return; |
aoqi@0 | 158 | PortType portType = binding.resolvePortType(wsdlDocument); |
aoqi@0 | 159 | |
aoqi@0 | 160 | QName bindingName = WSDLModelerBase.getQNameOf(binding); |
aoqi@0 | 161 | |
aoqi@0 | 162 | //we dont want to process the port bound to the binding processed earlier |
aoqi@0 | 163 | if(bindingNameToPortMap.containsKey(bindingName)) |
aoqi@0 | 164 | return; |
aoqi@0 | 165 | |
aoqi@0 | 166 | bindingNameToPortMap.put(bindingName, port); |
aoqi@0 | 167 | |
aoqi@0 | 168 | |
aoqi@0 | 169 | for(Iterator itr=binding.operations(); itr.hasNext();){ |
aoqi@0 | 170 | BindingOperation bindingOperation = (BindingOperation)itr.next(); |
aoqi@0 | 171 | |
aoqi@0 | 172 | // get only the bounded operations |
aoqi@0 | 173 | Set boundedOps = portType.getOperationsNamed(bindingOperation.getName()); |
aoqi@0 | 174 | if(boundedOps.size() != 1) |
aoqi@0 | 175 | continue; |
aoqi@0 | 176 | Operation operation = (Operation)boundedOps.iterator().next(); |
aoqi@0 | 177 | |
aoqi@0 | 178 | // No pseudo schema required for doc/lit |
aoqi@0 | 179 | if(wsdlModeler.isAsync(portType, operation)){ |
aoqi@0 | 180 | buildAsync(portType, operation, bindingOperation); |
aoqi@0 | 181 | } |
aoqi@0 | 182 | } |
aoqi@0 | 183 | } |
aoqi@0 | 184 | |
aoqi@0 | 185 | /** |
aoqi@0 | 186 | * @param portType |
aoqi@0 | 187 | * @param operation |
aoqi@0 | 188 | * @param bindingOperation |
aoqi@0 | 189 | */ |
aoqi@0 | 190 | private void buildAsync(PortType portType, Operation operation, BindingOperation bindingOperation) { |
aoqi@0 | 191 | String operationName = getCustomizedOperationName(operation);//operation.getName(); |
aoqi@0 | 192 | if(operationName == null) |
aoqi@0 | 193 | return; |
aoqi@0 | 194 | Message outputMessage = null; |
aoqi@0 | 195 | if(operation.getOutput() != null) |
aoqi@0 | 196 | outputMessage = operation.getOutput().resolveMessage(wsdlDocument); |
aoqi@0 | 197 | if(outputMessage != null){ |
aoqi@0 | 198 | List<MessagePart> allParts = new ArrayList<MessagePart>(outputMessage.getParts()); |
aoqi@0 | 199 | if(options != null && options.additionalHeaders) { |
aoqi@0 | 200 | List<MessagePart> addtionalHeaderParts = wsdlModeler.getAdditionHeaderParts(bindingOperation, outputMessage, false); |
aoqi@0 | 201 | allParts.addAll(addtionalHeaderParts); |
aoqi@0 | 202 | } |
aoqi@0 | 203 | if(allParts.size() > 1) |
aoqi@0 | 204 | build(getOperationName(operationName), allParts); |
aoqi@0 | 205 | } |
aoqi@0 | 206 | |
aoqi@0 | 207 | } |
aoqi@0 | 208 | |
aoqi@0 | 209 | private String getCustomizedOperationName(Operation operation) { |
aoqi@0 | 210 | JAXWSBinding jaxwsCustomization = (JAXWSBinding)getExtensionOfType(operation, JAXWSBinding.class); |
aoqi@0 | 211 | String operationName = (jaxwsCustomization != null)?((jaxwsCustomization.getMethodName() != null)?jaxwsCustomization.getMethodName().getName():null):null; |
aoqi@0 | 212 | if(operationName != null){ |
aoqi@0 | 213 | if(Names.isJavaReservedWord(operationName)){ |
aoqi@0 | 214 | return null; |
aoqi@0 | 215 | } |
aoqi@0 | 216 | |
aoqi@0 | 217 | return operationName; |
aoqi@0 | 218 | } |
aoqi@0 | 219 | return operation.getName(); |
aoqi@0 | 220 | } |
aoqi@0 | 221 | |
aoqi@0 | 222 | private void writeImports(QName elementName, List<MessagePart> parts){ |
aoqi@0 | 223 | Set<String> uris = new HashSet<String>(); |
aoqi@0 | 224 | for(MessagePart p:parts){ |
aoqi@0 | 225 | String ns = p.getDescriptor().getNamespaceURI(); |
aoqi@0 | 226 | if(!uris.contains(ns) && !ns.equals("http://www.w3.org/2001/XMLSchema") && !ns.equals(elementName.getNamespaceURI())){ |
aoqi@0 | 227 | print("<xs:import namespace=''{0}''/>", ns); |
aoqi@0 | 228 | uris.add(ns); |
aoqi@0 | 229 | } |
aoqi@0 | 230 | } |
aoqi@0 | 231 | } |
aoqi@0 | 232 | |
aoqi@0 | 233 | boolean asyncRespBeanBinding = false; |
aoqi@0 | 234 | private void build(QName elementName, List<MessagePart> allParts){ |
aoqi@0 | 235 | |
aoqi@0 | 236 | print( |
aoqi@0 | 237 | "<xs:schema xmlns:xs=''http://www.w3.org/2001/XMLSchema''" + |
aoqi@0 | 238 | " xmlns:jaxb=''http://java.sun.com/xml/ns/jaxb''" + |
aoqi@0 | 239 | " xmlns:xjc=''http://java.sun.com/xml/ns/jaxb/xjc''" + |
aoqi@0 | 240 | " jaxb:extensionBindingPrefixes=''xjc''" + |
aoqi@0 | 241 | " jaxb:version=''1.0''" + |
aoqi@0 | 242 | " targetNamespace=''{0}''>", |
aoqi@0 | 243 | elementName.getNamespaceURI()); |
aoqi@0 | 244 | |
aoqi@0 | 245 | writeImports(elementName, allParts); |
aoqi@0 | 246 | |
aoqi@0 | 247 | if(!asyncRespBeanBinding){ |
aoqi@0 | 248 | print( |
aoqi@0 | 249 | "<xs:annotation><xs:appinfo>" + |
aoqi@0 | 250 | " <jaxb:schemaBindings>" + |
aoqi@0 | 251 | " <jaxb:package name=''{0}'' />" + |
aoqi@0 | 252 | " </jaxb:schemaBindings>" + |
aoqi@0 | 253 | "</xs:appinfo></xs:annotation>", |
aoqi@0 | 254 | wsdlModeler.getJavaPackage() ); |
aoqi@0 | 255 | asyncRespBeanBinding = true; |
aoqi@0 | 256 | } |
aoqi@0 | 257 | |
aoqi@0 | 258 | print("<xs:element name=''{0}''>", elementName.getLocalPart()); |
aoqi@0 | 259 | print("<xs:complexType>"); |
aoqi@0 | 260 | print("<xs:sequence>"); |
aoqi@0 | 261 | |
aoqi@0 | 262 | |
aoqi@0 | 263 | for(MessagePart p:allParts) { |
aoqi@0 | 264 | //rpclit wsdl:part must reference schema type not element, also it must exclude headers and mime parts |
aoqi@0 | 265 | if(p.getDescriptorKind() == SchemaKinds.XSD_ELEMENT){ |
aoqi@0 | 266 | print("<xs:element ref=''types:{0}'' xmlns:types=''{1}''/>",p.getDescriptor().getLocalPart(), p.getDescriptor().getNamespaceURI()); |
aoqi@0 | 267 | }else{ |
aoqi@0 | 268 | print("<xs:element name=''{0}'' type=''{1}'' xmlns=''{2}'' />", |
aoqi@0 | 269 | p.getName(), |
aoqi@0 | 270 | p.getDescriptor().getLocalPart(), |
aoqi@0 | 271 | p.getDescriptor().getNamespaceURI() ); |
aoqi@0 | 272 | } |
aoqi@0 | 273 | } |
aoqi@0 | 274 | |
aoqi@0 | 275 | print("</xs:sequence>"); |
aoqi@0 | 276 | print("</xs:complexType>"); |
aoqi@0 | 277 | print("</xs:element>"); |
aoqi@0 | 278 | print("</xs:schema>"); |
aoqi@0 | 279 | |
aoqi@0 | 280 | // reset the StringWriter, so that next operation element could be written |
aoqi@0 | 281 | if(buf.toString().length() > 0){ |
aoqi@0 | 282 | //System.out.println("Response bean Schema for operation========> "+ elementName+"\n\n"+buf); |
aoqi@0 | 283 | InputSource is = new InputSource(new StringReader(buf.toString())); |
aoqi@0 | 284 | schemas.add(is); |
aoqi@0 | 285 | buf.getBuffer().setLength(0); |
aoqi@0 | 286 | } |
aoqi@0 | 287 | } |
aoqi@0 | 288 | |
aoqi@0 | 289 | private QName getOperationName(String operationName){ |
aoqi@0 | 290 | if(operationName == null) |
aoqi@0 | 291 | return null; |
aoqi@0 | 292 | // String namespaceURI = wsdlDocument.getDefinitions().getTargetNamespaceURI()+"?"+portType.getName()+"?" + operationName; |
aoqi@0 | 293 | String namespaceURI = ""; |
aoqi@0 | 294 | return new QName(namespaceURI, operationName+"Response"); |
aoqi@0 | 295 | } |
aoqi@0 | 296 | |
aoqi@0 | 297 | private void print( String msg ) { |
aoqi@0 | 298 | print( msg, new Object[0] ); |
aoqi@0 | 299 | } |
aoqi@0 | 300 | private void print( String msg, Object arg1 ) { |
aoqi@0 | 301 | print( msg, new Object[]{arg1} ); |
aoqi@0 | 302 | } |
aoqi@0 | 303 | private void print( String msg, Object arg1, Object arg2 ) { |
aoqi@0 | 304 | print( msg, new Object[]{arg1, arg2} ); |
aoqi@0 | 305 | } |
aoqi@0 | 306 | private void print( String msg, Object arg1, Object arg2, Object arg3 ) { |
aoqi@0 | 307 | print( msg, new Object[]{arg1,arg2,arg3} ); |
aoqi@0 | 308 | } |
aoqi@0 | 309 | private void print( String msg, Object[] args ) { |
aoqi@0 | 310 | buf.write(MessageFormat.format(msg,args)); |
aoqi@0 | 311 | buf.write('\n'); |
aoqi@0 | 312 | } |
aoqi@0 | 313 | |
aoqi@0 | 314 | } |