src/share/jaxws_classes/com/sun/xml/internal/ws/wsdl/writer/WSDLGenerator.java

changeset 286
f50545b5e2f1
child 368
0989ad8c0860
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/ws/wsdl/writer/WSDLGenerator.java	Tue Mar 06 16:09:35 2012 -0800
     1.3 @@ -0,0 +1,1193 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.xml.internal.ws.wsdl.writer;
    1.30 +
    1.31 +
    1.32 +import static com.sun.xml.internal.bind.v2.schemagen.Util.*;
    1.33 +
    1.34 +import com.sun.xml.internal.txw2.TXW;
    1.35 +import com.sun.xml.internal.txw2.TypedXmlWriter;
    1.36 +import com.sun.xml.internal.txw2.output.ResultFactory;
    1.37 +import com.sun.xml.internal.txw2.output.XmlSerializer;
    1.38 +import com.sun.xml.internal.txw2.output.TXWResult;
    1.39 +import com.sun.xml.internal.ws.api.SOAPVersion;
    1.40 +import com.sun.xml.internal.ws.api.WSBinding;
    1.41 +import com.sun.xml.internal.ws.api.model.JavaMethod;
    1.42 +import com.sun.xml.internal.ws.api.model.MEP;
    1.43 +import com.sun.xml.internal.ws.api.model.ParameterBinding;
    1.44 +import com.sun.xml.internal.ws.api.model.SEIModel;
    1.45 +import com.sun.xml.internal.ws.api.model.soap.SOAPBinding;
    1.46 +import com.sun.xml.internal.ws.api.server.Container;
    1.47 +import com.sun.xml.internal.ws.api.wsdl.writer.WSDLGeneratorExtension;
    1.48 +import com.sun.xml.internal.ws.api.wsdl.writer.WSDLGenExtnContext;
    1.49 +import com.sun.xml.internal.ws.model.AbstractSEIModelImpl;
    1.50 +import com.sun.xml.internal.ws.model.CheckedExceptionImpl;
    1.51 +import com.sun.xml.internal.ws.model.JavaMethodImpl;
    1.52 +import com.sun.xml.internal.ws.model.ParameterImpl;
    1.53 +import com.sun.xml.internal.ws.model.WrapperParameter;
    1.54 +import com.sun.xml.internal.ws.wsdl.parser.SOAPConstants;
    1.55 +import com.sun.xml.internal.ws.wsdl.parser.WSDLConstants;
    1.56 +import com.sun.xml.internal.ws.wsdl.writer.document.Binding;
    1.57 +import com.sun.xml.internal.ws.wsdl.writer.document.BindingOperationType;
    1.58 +import com.sun.xml.internal.ws.wsdl.writer.document.Definitions;
    1.59 +import com.sun.xml.internal.ws.wsdl.writer.document.Fault;
    1.60 +import com.sun.xml.internal.ws.wsdl.writer.document.FaultType;
    1.61 +import com.sun.xml.internal.ws.wsdl.writer.document.Import;
    1.62 +import com.sun.xml.internal.ws.wsdl.writer.document.Message;
    1.63 +import com.sun.xml.internal.ws.wsdl.writer.document.Operation;
    1.64 +import com.sun.xml.internal.ws.wsdl.writer.document.ParamType;
    1.65 +import com.sun.xml.internal.ws.wsdl.writer.document.Port;
    1.66 +import com.sun.xml.internal.ws.wsdl.writer.document.PortType;
    1.67 +import com.sun.xml.internal.ws.wsdl.writer.document.Service;
    1.68 +import com.sun.xml.internal.ws.wsdl.writer.document.Types;
    1.69 +import com.sun.xml.internal.ws.wsdl.writer.document.soap.Body;
    1.70 +import com.sun.xml.internal.ws.wsdl.writer.document.soap.BodyType;
    1.71 +import com.sun.xml.internal.ws.wsdl.writer.document.soap.Header;
    1.72 +import com.sun.xml.internal.ws.wsdl.writer.document.soap.SOAPAddress;
    1.73 +import com.sun.xml.internal.ws.wsdl.writer.document.soap.SOAPFault;
    1.74 +import com.sun.xml.internal.ws.spi.db.BindingContext;
    1.75 +import com.sun.xml.internal.ws.spi.db.BindingHelper;
    1.76 +import com.sun.xml.internal.ws.util.RuntimeVersion;
    1.77 +import com.sun.xml.internal.ws.policy.jaxws.PolicyWSDLGeneratorExtension;
    1.78 +import com.sun.xml.internal.ws.encoding.soap.streaming.SOAPNamespaceConstants;
    1.79 +
    1.80 +import javax.jws.soap.SOAPBinding.Style;
    1.81 +import javax.jws.soap.SOAPBinding.Use;
    1.82 +import javax.xml.bind.SchemaOutputResolver;
    1.83 +import javax.xml.namespace.QName;
    1.84 +import javax.xml.transform.Result;
    1.85 +import javax.xml.transform.Transformer;
    1.86 +import javax.xml.transform.TransformerConfigurationException;
    1.87 +import javax.xml.transform.TransformerException;
    1.88 +import javax.xml.transform.TransformerFactory;
    1.89 +import javax.xml.transform.dom.DOMResult;
    1.90 +import javax.xml.transform.dom.DOMSource;
    1.91 +import javax.xml.transform.sax.SAXResult;
    1.92 +import javax.xml.ws.Holder;
    1.93 +import javax.xml.ws.WebServiceException;
    1.94 +
    1.95 +import org.w3c.dom.Document;
    1.96 +
    1.97 +import java.io.IOException;
    1.98 +import java.net.URI;
    1.99 +import java.net.URISyntaxException;
   1.100 +import java.util.ArrayList;
   1.101 +import java.util.HashSet;
   1.102 +import java.util.Iterator;
   1.103 +import java.util.List;
   1.104 +import java.util.Set;
   1.105 +
   1.106 +
   1.107 +/**
   1.108 + * Class used to generate WSDLs from a {@link SEIModel}.
   1.109 + *
   1.110 + * @author WS Development Team
   1.111 + */
   1.112 +public class WSDLGenerator {
   1.113 +    private JAXWSOutputSchemaResolver resolver;
   1.114 +    private WSDLResolver wsdlResolver = null;
   1.115 +    private AbstractSEIModelImpl model;
   1.116 +    private Definitions serviceDefinitions;
   1.117 +    private Definitions portDefinitions;
   1.118 +    private Types types;
   1.119 +    /**
   1.120 +     * Constant String for ".wsdl"
   1.121 +     */
   1.122 +    private static final String DOT_WSDL = ".wsdl";
   1.123 +    /**
   1.124 +     * Constant String appended to response message names
   1.125 +     */
   1.126 +    private static final String RESPONSE = "Response";
   1.127 +    /**
   1.128 +     * constant String used for part name for wrapped request messages
   1.129 +     */
   1.130 +    private static final String PARAMETERS = "parameters";
   1.131 +    /**
   1.132 +     * the part name for unwrappable response messages
   1.133 +     */
   1.134 +    private static final String RESULT = "parameters";
   1.135 +    /**
   1.136 +     * the part name for response messages that are not unwrappable
   1.137 +     */
   1.138 +    private static final String UNWRAPPABLE_RESULT = "result";
   1.139 +    /**
   1.140 +     * The WSDL namespace
   1.141 +     */
   1.142 +    private static final String WSDL_NAMESPACE = WSDLConstants.NS_WSDL;
   1.143 +
   1.144 +    /**
   1.145 +     * the XSD namespace
   1.146 +     */
   1.147 +    private static final String XSD_NAMESPACE = SOAPNamespaceConstants.XSD;
   1.148 +    /**
   1.149 +     * the namespace prefix to use for the XSD namespace
   1.150 +     */
   1.151 +    private static final String XSD_PREFIX = "xsd";
   1.152 +    /**
   1.153 +     * The SOAP 1.1 namespace
   1.154 +     */
   1.155 +    private static final String SOAP11_NAMESPACE = SOAPConstants.NS_WSDL_SOAP;
   1.156 +    /**
   1.157 +     * The SOAP 1.2 namespace
   1.158 +     */
   1.159 +    private static final String SOAP12_NAMESPACE = SOAPConstants.NS_WSDL_SOAP12;
   1.160 +    /**
   1.161 +     * The namespace prefix to use for the SOAP 1.1 namespace
   1.162 +     */
   1.163 +    private static final String SOAP_PREFIX = "soap";
   1.164 +    /**
   1.165 +     * The namespace prefix to use for the SOAP 1.2 namespace
   1.166 +     */
   1.167 +    private static final String SOAP12_PREFIX = "soap12";
   1.168 +    /**
   1.169 +     * The namespace prefix to use for the targetNamespace
   1.170 +     */
   1.171 +    private static final String TNS_PREFIX = "tns";
   1.172 +
   1.173 +    /**
   1.174 +     * Constant String "document" used to specify <code>document</code> style
   1.175 +     * soapBindings
   1.176 +     */
   1.177 +    private static final String DOCUMENT = "document";
   1.178 +    /**
   1.179 +     * Constant String "rpc" used to specify <code>rpc</code> style
   1.180 +     * soapBindings
   1.181 +     */
   1.182 +    private static final String RPC = "rpc";
   1.183 +    /**
   1.184 +     * Constant String "literal" used to create <code>literal</code> use binddings
   1.185 +     */
   1.186 +    private static final String LITERAL = "literal";
   1.187 +    /**
   1.188 +     * Constant String to flag the URL to replace at runtime for the endpoint
   1.189 +     */
   1.190 +    private static final String REPLACE_WITH_ACTUAL_URL = "REPLACE_WITH_ACTUAL_URL";
   1.191 +    private Set<QName> processedExceptions = new HashSet<QName>();
   1.192 +    private WSBinding binding;
   1.193 +    private String wsdlLocation;
   1.194 +    private String portWSDLID;
   1.195 +    private String schemaPrefix;
   1.196 +    private WSDLGeneratorExtension extension;
   1.197 +    List<WSDLGeneratorExtension> extensionHandlers;
   1.198 +
   1.199 +    private String endpointAddress = REPLACE_WITH_ACTUAL_URL;
   1.200 +    private Container container;
   1.201 +    private final Class implType;
   1.202 +
   1.203 +    private boolean inlineSchemas;      // TODO
   1.204 +
   1.205 +    /**
   1.206 +     * Creates the WSDLGenerator
   1.207 +     * @param model The {@link AbstractSEIModelImpl} used to generate the WSDL
   1.208 +     * @param wsdlResolver The {@link WSDLResolver} to use resovle names while generating the WSDL
   1.209 +     * @param binding specifies which {@link javax.xml.ws.BindingType} to generate
   1.210 +     * @param extensions an array {@link WSDLGeneratorExtension} that will
   1.211 +     * be invoked to generate WSDL extensions
   1.212 +     */
   1.213 +    public WSDLGenerator(AbstractSEIModelImpl model, WSDLResolver wsdlResolver, WSBinding binding, Container container,
   1.214 +                         Class implType, boolean inlineSchemas, WSDLGeneratorExtension... extensions) {
   1.215 +        this.model = model;
   1.216 +        resolver = new JAXWSOutputSchemaResolver();
   1.217 +        this.wsdlResolver = wsdlResolver;
   1.218 +        this.binding = binding;
   1.219 +        this.container = container;
   1.220 +        this.implType = implType;
   1.221 +        extensionHandlers = new ArrayList<WSDLGeneratorExtension>();
   1.222 +        this.inlineSchemas = inlineSchemas;
   1.223 +
   1.224 +        // register handlers for default extensions
   1.225 +        register(new W3CAddressingWSDLGeneratorExtension());
   1.226 +        register(new W3CAddressingMetadataWSDLGeneratorExtension());
   1.227 +        register(new PolicyWSDLGeneratorExtension());
   1.228 +
   1.229 +        if (container != null) { // on server
   1.230 +            WSDLGeneratorExtension[] wsdlGeneratorExtensions = container.getSPI(WSDLGeneratorExtension[].class);
   1.231 +            if (wsdlGeneratorExtensions != null) {
   1.232 +                for (WSDLGeneratorExtension wsdlGeneratorExtension : wsdlGeneratorExtensions) {
   1.233 +                    register(wsdlGeneratorExtension);
   1.234 +                }
   1.235 +            }
   1.236 +        }
   1.237 +
   1.238 +        for (WSDLGeneratorExtension w : extensions)
   1.239 +            register(w);
   1.240 +
   1.241 +        this.extension = new WSDLGeneratorExtensionFacade(extensionHandlers.toArray(new WSDLGeneratorExtension[0]));
   1.242 +    }
   1.243 +
   1.244 +    /**
   1.245 +     * Sets the endpoint address string to be written.
   1.246 +     * Defaults to {@link #REPLACE_WITH_ACTUAL_URL}.
   1.247 +     *
   1.248 +     * @param address wsdl:port/soap:address/[@location] value
   1.249 +     */
   1.250 +    public void setEndpointAddress(String address) {
   1.251 +        this.endpointAddress = address;
   1.252 +    }
   1.253 +
   1.254 +    protected String mangleName(String name) {
   1.255 +        return BindingHelper.mangleNameToClassName(name);
   1.256 +    }
   1.257 +
   1.258 +    /**
   1.259 +     * Performes the actual WSDL generation
   1.260 +     */
   1.261 +    public void doGeneration() {
   1.262 +        XmlSerializer serviceWriter;
   1.263 +        XmlSerializer portWriter = null;
   1.264 +        String fileName = mangleName(model.getServiceQName().getLocalPart());
   1.265 +        Result result = wsdlResolver.getWSDL(fileName + DOT_WSDL);
   1.266 +        wsdlLocation = result.getSystemId();
   1.267 +        serviceWriter = new CommentFilter(ResultFactory.createSerializer(result));
   1.268 +        if (model.getServiceQName().getNamespaceURI().equals(model.getTargetNamespace())) {
   1.269 +            portWriter = serviceWriter;
   1.270 +            schemaPrefix = fileName + "_";
   1.271 +        } else {
   1.272 +            String wsdlName = mangleName(model.getPortTypeName().getLocalPart());
   1.273 +            if (wsdlName.equals(fileName))
   1.274 +                wsdlName += "PortType";
   1.275 +            Holder<String> absWSDLName = new Holder<String>();
   1.276 +            absWSDLName.value = wsdlName + DOT_WSDL;
   1.277 +            result = wsdlResolver.getAbstractWSDL(absWSDLName);
   1.278 +
   1.279 +            if (result != null) {
   1.280 +                portWSDLID = result.getSystemId();
   1.281 +                if (portWSDLID.equals(wsdlLocation)) {
   1.282 +                    portWriter = serviceWriter;
   1.283 +                } else {
   1.284 +                    portWriter = new CommentFilter(ResultFactory.createSerializer(result));
   1.285 +                }
   1.286 +            } else {
   1.287 +                portWSDLID = absWSDLName.value;
   1.288 +            }
   1.289 +            schemaPrefix = new java.io.File(portWSDLID).getName();
   1.290 +            int idx = schemaPrefix.lastIndexOf('.');
   1.291 +            if (idx > 0)
   1.292 +                schemaPrefix = schemaPrefix.substring(0, idx);
   1.293 +            schemaPrefix = mangleName(schemaPrefix) + "_";
   1.294 +        }
   1.295 +        generateDocument(serviceWriter, portWriter);
   1.296 +    }
   1.297 +
   1.298 +    /**
   1.299 +     * Writing directly to XmlSerializer is a problem, since it doesn't suppress
   1.300 +     * xml declaration. Creating filter so that comment is written before TXW writes
   1.301 +     * anything in the WSDL.
   1.302 +     */
   1.303 +    private static class CommentFilter implements XmlSerializer {
   1.304 +        final XmlSerializer serializer;
   1.305 +        private static final String VERSION_COMMENT =
   1.306 +                " Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is " + RuntimeVersion.VERSION + ". ";
   1.307 +
   1.308 +        CommentFilter(XmlSerializer serializer) {
   1.309 +            this.serializer = serializer;
   1.310 +        }
   1.311 +
   1.312 +        public void startDocument() {
   1.313 +            serializer.startDocument();
   1.314 +            comment(new StringBuilder(VERSION_COMMENT));
   1.315 +            text(new StringBuilder("\n"));
   1.316 +        }
   1.317 +
   1.318 +        public void beginStartTag(String uri, String localName, String prefix) {
   1.319 +            serializer.beginStartTag(uri, localName, prefix);
   1.320 +        }
   1.321 +
   1.322 +        public void writeAttribute(String uri, String localName, String prefix, StringBuilder value) {
   1.323 +            serializer.writeAttribute(uri, localName, prefix, value);
   1.324 +        }
   1.325 +
   1.326 +        public void writeXmlns(String prefix, String uri) {
   1.327 +            serializer.writeXmlns(prefix, uri);
   1.328 +        }
   1.329 +
   1.330 +        public void endStartTag(String uri, String localName, String prefix) {
   1.331 +            serializer.endStartTag(uri, localName, prefix);
   1.332 +        }
   1.333 +
   1.334 +        public void endTag() {
   1.335 +            serializer.endTag();
   1.336 +        }
   1.337 +
   1.338 +        public void text(StringBuilder text) {
   1.339 +            serializer.text(text);
   1.340 +        }
   1.341 +
   1.342 +        public void cdata(StringBuilder text) {
   1.343 +            serializer.cdata(text);
   1.344 +        }
   1.345 +
   1.346 +        public void comment(StringBuilder comment) {
   1.347 +            serializer.comment(comment);
   1.348 +        }
   1.349 +
   1.350 +        public void endDocument() {
   1.351 +            serializer.endDocument();
   1.352 +        }
   1.353 +
   1.354 +        public void flush() {
   1.355 +            serializer.flush();
   1.356 +        }
   1.357 +
   1.358 +    }
   1.359 +
   1.360 +    private void generateDocument(XmlSerializer serviceStream, XmlSerializer portStream) {
   1.361 +        serviceDefinitions = TXW.create(Definitions.class, serviceStream);
   1.362 +        serviceDefinitions._namespace(WSDL_NAMESPACE, "");//WSDL_PREFIX);
   1.363 +        serviceDefinitions._namespace(XSD_NAMESPACE, XSD_PREFIX);
   1.364 +        serviceDefinitions.targetNamespace(model.getServiceQName().getNamespaceURI());
   1.365 +        serviceDefinitions._namespace(model.getServiceQName().getNamespaceURI(), TNS_PREFIX);
   1.366 +        if (binding.getSOAPVersion() == SOAPVersion.SOAP_12)
   1.367 +            serviceDefinitions._namespace(SOAP12_NAMESPACE, SOAP12_PREFIX);
   1.368 +        else
   1.369 +            serviceDefinitions._namespace(SOAP11_NAMESPACE, SOAP_PREFIX);
   1.370 +        serviceDefinitions.name(model.getServiceQName().getLocalPart());
   1.371 +        WSDLGenExtnContext serviceCtx = new WSDLGenExtnContext(serviceDefinitions, model, binding, container, implType);
   1.372 +        extension.start(serviceCtx);
   1.373 +        if (serviceStream != portStream && portStream != null) {
   1.374 +            // generate an abstract and concrete wsdl
   1.375 +            portDefinitions = TXW.create(Definitions.class, portStream);
   1.376 +            portDefinitions._namespace(WSDL_NAMESPACE, "");//WSDL_PREFIX);
   1.377 +            portDefinitions._namespace(XSD_NAMESPACE, XSD_PREFIX);
   1.378 +            if (model.getTargetNamespace() != null) {
   1.379 +                portDefinitions.targetNamespace(model.getTargetNamespace());
   1.380 +                portDefinitions._namespace(model.getTargetNamespace(), TNS_PREFIX);
   1.381 +            }
   1.382 +
   1.383 +            String schemaLoc = relativize(portWSDLID, wsdlLocation);
   1.384 +            Import _import = serviceDefinitions._import().namespace(model.getTargetNamespace());
   1.385 +            _import.location(schemaLoc);
   1.386 +        } else if (portStream != null) {
   1.387 +            // abstract and concrete are the same
   1.388 +            portDefinitions = serviceDefinitions;
   1.389 +        } else {
   1.390 +            // import a provided abstract wsdl
   1.391 +            String schemaLoc = relativize(portWSDLID, wsdlLocation);
   1.392 +            Import _import = serviceDefinitions._import().namespace(model.getTargetNamespace());
   1.393 +            _import.location(schemaLoc);
   1.394 +        }
   1.395 +        extension.addDefinitionsExtension(serviceDefinitions);
   1.396 +
   1.397 +        if (portDefinitions != null) {
   1.398 +            generateTypes();
   1.399 +            generateMessages();
   1.400 +            generatePortType();
   1.401 +        }
   1.402 +        generateBinding();
   1.403 +        generateService();
   1.404 +        //Give a chance to WSDLGeneratorExtensions to write stuff before closing </wsdl:defintions>
   1.405 +        extension.end(serviceCtx);
   1.406 +        serviceDefinitions.commit();
   1.407 +        if (portDefinitions != null && portDefinitions != serviceDefinitions)
   1.408 +            portDefinitions.commit();
   1.409 +    }
   1.410 +
   1.411 +
   1.412 +    /**
   1.413 +     * Generates the types section of the WSDL
   1.414 +     */
   1.415 +    protected void generateTypes() {
   1.416 +        types = portDefinitions.types();
   1.417 +        if (model.getBindingContext() != null) {
   1.418 +            if (inlineSchemas && model.getBindingContext().getClass().getName().indexOf("glassfish") == -1) {
   1.419 +                resolver.nonGlassfishSchemas = new ArrayList<DOMResult>();
   1.420 +            }
   1.421 +            try {
   1.422 +                model.getBindingContext().generateSchema(resolver);
   1.423 +            } catch (IOException e) {
   1.424 +                // TODO locallize and wrap this
   1.425 +                e.printStackTrace();
   1.426 +                throw new WebServiceException(e.getMessage());
   1.427 +            }
   1.428 +        }
   1.429 +        if (resolver.nonGlassfishSchemas != null) {
   1.430 +            TransformerFactory tf = TransformerFactory.newInstance();
   1.431 +            try {
   1.432 +                Transformer t = tf.newTransformer();
   1.433 +                for (DOMResult xsd : resolver.nonGlassfishSchemas) {
   1.434 +                    Document doc = (Document) xsd.getNode();
   1.435 +                    SAXResult sax = new SAXResult(new TXWContentHandler(types));
   1.436 +                    t.transform(new DOMSource(doc.getDocumentElement()), sax);
   1.437 +                }
   1.438 +            } catch (TransformerConfigurationException e) {
   1.439 +                e.printStackTrace();
   1.440 +                throw new WebServiceException(e.getMessage(), e);
   1.441 +            } catch (TransformerException e) {
   1.442 +                e.printStackTrace();
   1.443 +                throw new WebServiceException(e.getMessage(), e);
   1.444 +            }
   1.445 +        }
   1.446 +    }
   1.447 +
   1.448 +    /**
   1.449 +     * Generates the WSDL messages
   1.450 +     */
   1.451 +    protected void generateMessages() {
   1.452 +        for (JavaMethodImpl method : model.getJavaMethods()) {
   1.453 +            generateSOAPMessages(method, method.getBinding());
   1.454 +        }
   1.455 +    }
   1.456 +
   1.457 +    /**
   1.458 +     * Generates messages for a SOAPBinding
   1.459 +     * @param method The {@link JavaMethod} to generate messages for
   1.460 +     * @param binding The {@link com.sun.xml.internal.ws.api.model.soap.SOAPBinding} to add the generated messages to
   1.461 +     */
   1.462 +    protected void generateSOAPMessages(JavaMethodImpl method, com.sun.xml.internal.ws.api.model.soap.SOAPBinding binding) {
   1.463 +        boolean isDoclit = binding.isDocLit();
   1.464 +//        Message message = portDefinitions.message().name(method.getOperation().getName().getLocalPart());
   1.465 +        Message message = portDefinitions.message().name(method.getRequestMessageName());
   1.466 +        extension.addInputMessageExtension(message, method);
   1.467 +        com.sun.xml.internal.ws.wsdl.writer.document.Part part;
   1.468 +        BindingContext jaxbContext = model.getBindingContext();
   1.469 +        boolean unwrappable = true;
   1.470 +        for (ParameterImpl param : method.getRequestParameters()) {
   1.471 +            if (isDoclit) {
   1.472 +                if (isHeaderParameter(param))
   1.473 +                    unwrappable = false;
   1.474 +
   1.475 +                part = message.part().name(param.getPartName());
   1.476 +                part.element(param.getName());
   1.477 +            } else {
   1.478 +                if (param.isWrapperStyle()) {
   1.479 +                    for (ParameterImpl childParam : ((WrapperParameter) param).getWrapperChildren()) {
   1.480 +                        part = message.part().name(childParam.getPartName());
   1.481 +                        part.type(jaxbContext.getTypeName(childParam.getXMLBridge().getTypeInfo()));
   1.482 +                    }
   1.483 +                } else {
   1.484 +                    part = message.part().name(param.getPartName());
   1.485 +                    part.element(param.getName());
   1.486 +                }
   1.487 +            }
   1.488 +        }
   1.489 +        if (method.getMEP() != MEP.ONE_WAY) {
   1.490 +//            message = portDefinitions.message().name(method.getOperation().getName().getLocalPart()+RESPONSE);
   1.491 +            message = portDefinitions.message().name(method.getResponseMessageName());
   1.492 +            extension.addOutputMessageExtension(message, method);
   1.493 +            if (unwrappable) {
   1.494 +                for (ParameterImpl param : method.getResponseParameters()) {
   1.495 +                    if (isHeaderParameter(param))
   1.496 +                        unwrappable = false;
   1.497 +                }
   1.498 +            }
   1.499 +
   1.500 +            for (ParameterImpl param : method.getResponseParameters()) {
   1.501 +                if (isDoclit) {
   1.502 +                    part = message.part().name(param.getPartName());
   1.503 +                    part.element(param.getName());
   1.504 +
   1.505 +                } else {
   1.506 +                    if (param.isWrapperStyle()) {
   1.507 +                        for (ParameterImpl childParam : ((WrapperParameter) param).getWrapperChildren()) {
   1.508 +                            part = message.part().name(childParam.getPartName());
   1.509 +                            part.type(jaxbContext.getTypeName(childParam.getXMLBridge().getTypeInfo()));
   1.510 +                        }
   1.511 +                    } else {
   1.512 +                        part = message.part().name(param.getPartName());
   1.513 +                        part.element(param.getName());
   1.514 +                    }
   1.515 +                }
   1.516 +            }
   1.517 +        }
   1.518 +        for (CheckedExceptionImpl exception : method.getCheckedExceptions()) {
   1.519 +            QName tagName = exception.getDetailType().tagName;
   1.520 +            String messageName = exception.getMessageName();
   1.521 +            QName messageQName = new QName(model.getTargetNamespace(), messageName);
   1.522 +            if (processedExceptions.contains(messageQName))
   1.523 +                continue;
   1.524 +            message = portDefinitions.message().name(messageName);
   1.525 +
   1.526 +            extension.addFaultMessageExtension(message, method, exception);
   1.527 +            part = message.part().name("fault");//tagName.getLocalPart());
   1.528 +            part.element(tagName);
   1.529 +            processedExceptions.add(messageQName);
   1.530 +        }
   1.531 +    }
   1.532 +
   1.533 +    /**
   1.534 +     * Generates the WSDL portType
   1.535 +     */
   1.536 +    protected void generatePortType() {
   1.537 +
   1.538 +        PortType portType = portDefinitions.portType().name(model.getPortTypeName().getLocalPart());
   1.539 +        extension.addPortTypeExtension(portType);
   1.540 +        for (JavaMethodImpl method : model.getJavaMethods()) {
   1.541 +            Operation operation = portType.operation().name(method.getOperationName());
   1.542 +            generateParameterOrder(operation, method);
   1.543 +            extension.addOperationExtension(operation, method);
   1.544 +            switch (method.getMEP()) {
   1.545 +                case REQUEST_RESPONSE:
   1.546 +                    // input message
   1.547 +                    generateInputMessage(operation, method);
   1.548 +                    // output message
   1.549 +                    generateOutputMessage(operation, method);
   1.550 +                    break;
   1.551 +                case ONE_WAY:
   1.552 +                    generateInputMessage(operation, method);
   1.553 +                    break;
   1.554 +            }
   1.555 +            // faults
   1.556 +            for (CheckedExceptionImpl exception : method.getCheckedExceptions()) {
   1.557 +                QName messageName = new QName(model.getTargetNamespace(), exception.getMessageName());
   1.558 +                FaultType paramType = operation.fault().message(messageName).name(exception.getMessageName());
   1.559 +                extension.addOperationFaultExtension(paramType, method, exception);
   1.560 +            }
   1.561 +        }
   1.562 +    }
   1.563 +
   1.564 +    /**
   1.565 +     * Determines if the <CODE>method</CODE> is wrapper style
   1.566 +     * @param method The {@link JavaMethod} to check if it is wrapper style
   1.567 +     * @return true if the method is wrapper style, otherwise, false.
   1.568 +     */
   1.569 +    protected boolean isWrapperStyle(JavaMethodImpl method) {
   1.570 +        if (method.getRequestParameters().size() > 0) {
   1.571 +            ParameterImpl param = method.getRequestParameters().iterator().next();
   1.572 +            return param.isWrapperStyle();
   1.573 +        }
   1.574 +        return false;
   1.575 +    }
   1.576 +
   1.577 +    /**
   1.578 +     * Determines if a {@link JavaMethod} is rpc/literal
   1.579 +     * @param method The method to check
   1.580 +     * @return true if method is rpc/literal, otherwise, false
   1.581 +     */
   1.582 +    protected boolean isRpcLit(JavaMethodImpl method) {
   1.583 +        return method.getBinding().getStyle() == Style.RPC;
   1.584 +    }
   1.585 +
   1.586 +    /**
   1.587 +     * Generates the parameterOrder for a PortType operation
   1.588 +     * @param operation The operation to generate the parameterOrder for
   1.589 +     * @param method The {@link JavaMethod} to generate the parameterOrder from
   1.590 +     */
   1.591 +    protected void generateParameterOrder(Operation operation, JavaMethodImpl method) {
   1.592 +        if (method.getMEP() == MEP.ONE_WAY)
   1.593 +            return;
   1.594 +        if (isRpcLit(method))
   1.595 +            generateRpcParameterOrder(operation, method);
   1.596 +        else
   1.597 +            generateDocumentParameterOrder(operation, method);
   1.598 +    }
   1.599 +
   1.600 +    /**
   1.601 +     * Generates the parameterOrder for a PortType operation
   1.602 +     * @param operation the operation to generate the parameterOrder for
   1.603 +     * @param method the {@link JavaMethod} to generate the parameterOrder from
   1.604 +     */
   1.605 +    protected void generateRpcParameterOrder(Operation operation, JavaMethodImpl method) {
   1.606 +        String partName;
   1.607 +        StringBuffer paramOrder = new StringBuffer();
   1.608 +        Set<String> partNames = new HashSet<String>();
   1.609 +        List<ParameterImpl> sortedParams = sortMethodParameters(method);
   1.610 +        int i = 0;
   1.611 +        for (ParameterImpl parameter : sortedParams) {
   1.612 +            if (parameter.getIndex() >= 0) {
   1.613 +                partName = parameter.getPartName();
   1.614 +                if (!partNames.contains(partName)) {
   1.615 +                    if (i++ > 0)
   1.616 +                        paramOrder.append(' ');
   1.617 +                    paramOrder.append(partName);
   1.618 +                    partNames.add(partName);
   1.619 +                }
   1.620 +            }
   1.621 +        }
   1.622 +        if (i > 1) {
   1.623 +            operation.parameterOrder(paramOrder.toString());
   1.624 +        }
   1.625 +    }
   1.626 +
   1.627 +
   1.628 +    /**
   1.629 +     * Generates the parameterOrder for a PortType operation
   1.630 +     * @param operation the operation to generate the parameterOrder for
   1.631 +     * @param method the {@link JavaMethod} to generate the parameterOrder from
   1.632 +     */
   1.633 +    protected void generateDocumentParameterOrder(Operation operation, JavaMethodImpl method) {
   1.634 +        String partName;
   1.635 +        StringBuffer paramOrder = new StringBuffer();
   1.636 +        Set<String> partNames = new HashSet<String>();
   1.637 +        List<ParameterImpl> sortedParams = sortMethodParameters(method);
   1.638 +        boolean isWrapperStyle = isWrapperStyle(method);
   1.639 +        int i = 0;
   1.640 +        for (ParameterImpl parameter : sortedParams) {
   1.641 +//            System.out.println("param: "+parameter.getIndex()+" name: "+parameter.getName().getLocalPart());
   1.642 +            if (parameter.getIndex() < 0)
   1.643 +                continue;
   1.644 +
   1.645 +            // This should be safe change. if it affects compatibility,
   1.646 +            // remove the following single statement and uncomment the code in block below.
   1.647 +            partName = parameter.getPartName();
   1.648 +            /*
   1.649 +            if (isWrapperStyle && isBodyParameter(parameter)) {
   1.650 +               System.out.println("isWrapper and is body");
   1.651 +                if (method.getRequestParameters().contains(parameter))
   1.652 +                    partName = PARAMETERS;
   1.653 +                else {
   1.654 +                    //Rama: don't understand this logic "Response" below,
   1.655 +
   1.656 +                    // really make sure this is a wrapper style wsdl we are creating
   1.657 +                    partName = RESPONSE;
   1.658 +                }
   1.659 +            } else {
   1.660 +               partName = parameter.getPartName();
   1.661 +            }*/
   1.662 +
   1.663 +            if (!partNames.contains(partName)) {
   1.664 +                if (i++ > 0)
   1.665 +                    paramOrder.append(' ');
   1.666 +                paramOrder.append(partName);
   1.667 +                partNames.add(partName);
   1.668 +            }
   1.669 +        }
   1.670 +        if (i > 1) {
   1.671 +            operation.parameterOrder(paramOrder.toString());
   1.672 +        }
   1.673 +    }
   1.674 +
   1.675 +    /**
   1.676 +     * Sorts the parameters for the method by their position
   1.677 +     * @param method the {@link JavaMethod} used to sort the parameters
   1.678 +     * @return the sorted {@link List} of parameters
   1.679 +     */
   1.680 +    protected List<ParameterImpl> sortMethodParameters(JavaMethodImpl method) {
   1.681 +        Set<ParameterImpl> paramSet = new HashSet<ParameterImpl>();
   1.682 +        List<ParameterImpl> sortedParams = new ArrayList<ParameterImpl>();
   1.683 +        if (isRpcLit(method)) {
   1.684 +            for (ParameterImpl param : method.getRequestParameters()) {
   1.685 +                if (param instanceof WrapperParameter) {
   1.686 +                    paramSet.addAll(((WrapperParameter) param).getWrapperChildren());
   1.687 +                } else {
   1.688 +                    paramSet.add(param);
   1.689 +                }
   1.690 +            }
   1.691 +            for (ParameterImpl param : method.getResponseParameters()) {
   1.692 +                if (param instanceof WrapperParameter) {
   1.693 +                    paramSet.addAll(((WrapperParameter) param).getWrapperChildren());
   1.694 +                } else {
   1.695 +                    paramSet.add(param);
   1.696 +                }
   1.697 +            }
   1.698 +        } else {
   1.699 +            paramSet.addAll(method.getRequestParameters());
   1.700 +            paramSet.addAll(method.getResponseParameters());
   1.701 +        }
   1.702 +        Iterator<ParameterImpl> params = paramSet.iterator();
   1.703 +        if (paramSet.size() == 0)
   1.704 +            return sortedParams;
   1.705 +        ParameterImpl param = params.next();
   1.706 +        sortedParams.add(param);
   1.707 +        ParameterImpl sortedParam;
   1.708 +        int pos;
   1.709 +        for (int i = 1; i < paramSet.size(); i++) {
   1.710 +            param = params.next();
   1.711 +            for (pos = 0; pos < i; pos++) {
   1.712 +                sortedParam = sortedParams.get(pos);
   1.713 +                if (param.getIndex() == sortedParam.getIndex() &&
   1.714 +                        param instanceof WrapperParameter)
   1.715 +                    break;
   1.716 +                if (param.getIndex() < sortedParam.getIndex()) {
   1.717 +                    break;
   1.718 +                }
   1.719 +            }
   1.720 +            sortedParams.add(pos, param);
   1.721 +        }
   1.722 +        return sortedParams;
   1.723 +    }
   1.724 +
   1.725 +    /**
   1.726 +     * Determines if a parameter is associated with the message Body
   1.727 +     * @param parameter the parameter to check
   1.728 +     * @return true if the parameter is a <code>body</code> parameter
   1.729 +     */
   1.730 +    protected boolean isBodyParameter(ParameterImpl parameter) {
   1.731 +        ParameterBinding paramBinding = parameter.getBinding();
   1.732 +        return paramBinding.isBody();
   1.733 +    }
   1.734 +
   1.735 +    protected boolean isHeaderParameter(ParameterImpl parameter) {
   1.736 +        ParameterBinding paramBinding = parameter.getBinding();
   1.737 +        return paramBinding.isHeader();
   1.738 +    }
   1.739 +
   1.740 +    protected boolean isAttachmentParameter(ParameterImpl parameter) {
   1.741 +        ParameterBinding paramBinding = parameter.getBinding();
   1.742 +        return paramBinding.isAttachment();
   1.743 +    }
   1.744 +
   1.745 +
   1.746 +    /**
   1.747 +     * Generates the Binding section of the WSDL
   1.748 +     */
   1.749 +    protected void generateBinding() {
   1.750 +        Binding binding = serviceDefinitions.binding().name(model.getBoundPortTypeName().getLocalPart());
   1.751 +        extension.addBindingExtension(binding);
   1.752 +        binding.type(model.getPortTypeName());
   1.753 +        boolean first = true;
   1.754 +        for (JavaMethodImpl method : model.getJavaMethods()) {
   1.755 +            if (first) {
   1.756 +                SOAPBinding sBinding = method.getBinding();
   1.757 +                SOAPVersion soapVersion = sBinding.getSOAPVersion();
   1.758 +                if (soapVersion == SOAPVersion.SOAP_12) {
   1.759 +                    com.sun.xml.internal.ws.wsdl.writer.document.soap12.SOAPBinding soapBinding = binding.soap12Binding();
   1.760 +                    soapBinding.transport(this.binding.getBindingId().getTransport());
   1.761 +                    if (sBinding.getStyle().equals(Style.DOCUMENT))
   1.762 +                        soapBinding.style(DOCUMENT);
   1.763 +                    else
   1.764 +                        soapBinding.style(RPC);
   1.765 +                } else {
   1.766 +                    com.sun.xml.internal.ws.wsdl.writer.document.soap.SOAPBinding soapBinding = binding.soapBinding();
   1.767 +                    soapBinding.transport(this.binding.getBindingId().getTransport());
   1.768 +                    if (sBinding.getStyle().equals(Style.DOCUMENT))
   1.769 +                        soapBinding.style(DOCUMENT);
   1.770 +                    else
   1.771 +                        soapBinding.style(RPC);
   1.772 +                }
   1.773 +                first = false;
   1.774 +            }
   1.775 +            if (this.binding.getBindingId().getSOAPVersion() == SOAPVersion.SOAP_12)
   1.776 +                generateSOAP12BindingOperation(method, binding);
   1.777 +            else
   1.778 +                generateBindingOperation(method, binding);
   1.779 +        }
   1.780 +    }
   1.781 +
   1.782 +    protected void generateBindingOperation(JavaMethodImpl method, Binding binding) {
   1.783 +        BindingOperationType operation = binding.operation().name(method.getOperationName());
   1.784 +        extension.addBindingOperationExtension(operation, method);
   1.785 +        String targetNamespace = model.getTargetNamespace();
   1.786 +        QName requestMessage = new QName(targetNamespace, method.getOperationName());
   1.787 +        List<ParameterImpl> bodyParams = new ArrayList<ParameterImpl>();
   1.788 +        List<ParameterImpl> headerParams = new ArrayList<ParameterImpl>();
   1.789 +        splitParameters(bodyParams, headerParams, method.getRequestParameters());
   1.790 +        SOAPBinding soapBinding = method.getBinding();
   1.791 +        operation.soapOperation().soapAction(soapBinding.getSOAPAction());
   1.792 +
   1.793 +        // input
   1.794 +        TypedXmlWriter input = operation.input();
   1.795 +        extension.addBindingOperationInputExtension(input, method);
   1.796 +        BodyType body = input._element(Body.class);
   1.797 +        boolean isRpc = soapBinding.getStyle().equals(Style.RPC);
   1.798 +        if (soapBinding.getUse() == Use.LITERAL) {
   1.799 +            body.use(LITERAL);
   1.800 +            if (headerParams.size() > 0) {
   1.801 +                if (bodyParams.size() > 0) {
   1.802 +                    ParameterImpl param = bodyParams.iterator().next();
   1.803 +                    if (isRpc) {
   1.804 +                        StringBuffer parts = new StringBuffer();
   1.805 +                        int i = 0;
   1.806 +                        for (ParameterImpl parameter : ((WrapperParameter) param).getWrapperChildren()) {
   1.807 +                            if (i++ > 0)
   1.808 +                                parts.append(' ');
   1.809 +                            parts.append(parameter.getPartName());
   1.810 +                        }
   1.811 +                        body.parts(parts.toString());
   1.812 +                    } else {
   1.813 +                        body.parts(param.getPartName());
   1.814 +                    }
   1.815 +                } else {
   1.816 +                    body.parts("");
   1.817 +                }
   1.818 +                generateSOAPHeaders(input, headerParams, requestMessage);
   1.819 +            }
   1.820 +            if (isRpc) {
   1.821 +                body.namespace(method.getRequestParameters().iterator().next().getName().getNamespaceURI());
   1.822 +            }
   1.823 +        } else {
   1.824 +            // TODO localize this
   1.825 +            throw new WebServiceException("encoded use is not supported");
   1.826 +        }
   1.827 +
   1.828 +        if (method.getMEP() != MEP.ONE_WAY) {
   1.829 +            boolean unwrappable = headerParams.size() == 0;
   1.830 +            // output
   1.831 +            bodyParams.clear();
   1.832 +            headerParams.clear();
   1.833 +            splitParameters(bodyParams, headerParams, method.getResponseParameters());
   1.834 +            unwrappable = unwrappable ? headerParams.size() == 0 : unwrappable;
   1.835 +            TypedXmlWriter output = operation.output();
   1.836 +            extension.addBindingOperationOutputExtension(output, method);
   1.837 +            body = output._element(Body.class);
   1.838 +            body.use(LITERAL);
   1.839 +            if (headerParams.size() > 0) {
   1.840 +                String parts = "";
   1.841 +                if (bodyParams.size() > 0) {
   1.842 +                    ParameterImpl param = bodyParams.iterator().hasNext() ? bodyParams.iterator().next() : null;
   1.843 +                    if (param != null) {
   1.844 +                        if (isRpc) {
   1.845 +                            int i = 0;
   1.846 +                            for (ParameterImpl parameter : ((WrapperParameter) param).getWrapperChildren()) {
   1.847 +                                if (i++ > 0)
   1.848 +                                    parts += " ";
   1.849 +                                parts += parameter.getPartName();
   1.850 +                            }
   1.851 +                        } else {
   1.852 +                            parts = param.getPartName();
   1.853 +                        }
   1.854 +                    }
   1.855 +                }
   1.856 +                body.parts(parts);
   1.857 +                QName responseMessage = new QName(targetNamespace, method.getResponseMessageName());
   1.858 +                generateSOAPHeaders(output, headerParams, responseMessage);
   1.859 +            }
   1.860 +            if (isRpc) {
   1.861 +                body.namespace(method.getRequestParameters().iterator().next().getName().getNamespaceURI());
   1.862 +            }
   1.863 +        }
   1.864 +        for (CheckedExceptionImpl exception : method.getCheckedExceptions()) {
   1.865 +            Fault fault = operation.fault().name(exception.getMessageName());
   1.866 +            extension.addBindingOperationFaultExtension(fault, method, exception);
   1.867 +            SOAPFault soapFault = fault._element(SOAPFault.class).name(exception.getMessageName());
   1.868 +            soapFault.use(LITERAL);
   1.869 +        }
   1.870 +    }
   1.871 +
   1.872 +    protected void generateSOAP12BindingOperation(JavaMethodImpl method, Binding binding) {
   1.873 +        BindingOperationType operation = binding.operation().name(method.getOperationName());
   1.874 +        extension.addBindingOperationExtension(operation, method);
   1.875 +        String targetNamespace = model.getTargetNamespace();
   1.876 +        QName requestMessage = new QName(targetNamespace, method.getOperationName());
   1.877 +        ArrayList<ParameterImpl> bodyParams = new ArrayList<ParameterImpl>();
   1.878 +        ArrayList<ParameterImpl> headerParams = new ArrayList<ParameterImpl>();
   1.879 +        splitParameters(bodyParams, headerParams, method.getRequestParameters());
   1.880 +        SOAPBinding soapBinding = method.getBinding();
   1.881 +
   1.882 +        String soapAction = soapBinding.getSOAPAction();
   1.883 +        if (soapAction != null) {
   1.884 +            operation.soap12Operation().soapAction(soapAction);
   1.885 +        }
   1.886 +
   1.887 +        // input
   1.888 +        TypedXmlWriter input = operation.input();
   1.889 +        extension.addBindingOperationInputExtension(input, method);
   1.890 +        com.sun.xml.internal.ws.wsdl.writer.document.soap12.BodyType body = input._element(com.sun.xml.internal.ws.wsdl.writer.document.soap12.Body.class);
   1.891 +        boolean isRpc = soapBinding.getStyle().equals(Style.RPC);
   1.892 +        if (soapBinding.getUse().equals(Use.LITERAL)) {
   1.893 +            body.use(LITERAL);
   1.894 +            if (headerParams.size() > 0) {
   1.895 +                if (bodyParams.size() > 0) {
   1.896 +                    ParameterImpl param = bodyParams.iterator().next();
   1.897 +                    if (isRpc) {
   1.898 +                        StringBuffer parts = new StringBuffer();
   1.899 +                        int i = 0;
   1.900 +                        for (ParameterImpl parameter : ((WrapperParameter) param).getWrapperChildren()) {
   1.901 +                            if (i++ > 0)
   1.902 +                                parts.append(' ');
   1.903 +                            parts.append(parameter.getPartName());
   1.904 +                        }
   1.905 +                        body.parts(parts.toString());
   1.906 +                    } else {
   1.907 +                        body.parts(param.getPartName());
   1.908 +                    }
   1.909 +                } else {
   1.910 +                    body.parts("");
   1.911 +                }
   1.912 +                generateSOAP12Headers(input, headerParams, requestMessage);
   1.913 +            }
   1.914 +            if (isRpc) {
   1.915 +                body.namespace(method.getRequestParameters().iterator().next().getName().getNamespaceURI());
   1.916 +            }
   1.917 +        } else {
   1.918 +            // TODO localize this
   1.919 +            throw new WebServiceException("encoded use is not supported");
   1.920 +        }
   1.921 +
   1.922 +        if (method.getMEP() != MEP.ONE_WAY) {
   1.923 +            // output
   1.924 +            boolean unwrappable = headerParams.size() == 0;
   1.925 +            bodyParams.clear();
   1.926 +            headerParams.clear();
   1.927 +            splitParameters(bodyParams, headerParams, method.getResponseParameters());
   1.928 +            unwrappable = unwrappable ? headerParams.size() == 0 : unwrappable;
   1.929 +            TypedXmlWriter output = operation.output();
   1.930 +            extension.addBindingOperationOutputExtension(output, method);
   1.931 +            body = output._element(com.sun.xml.internal.ws.wsdl.writer.document.soap12.Body.class);
   1.932 +            body.use(LITERAL);
   1.933 +            if (headerParams.size() > 0) {
   1.934 +                if (bodyParams.size() > 0) {
   1.935 +                    ParameterImpl param = bodyParams.iterator().next();
   1.936 +                    if (isRpc) {
   1.937 +                        String parts = "";
   1.938 +                        int i = 0;
   1.939 +                        for (ParameterImpl parameter : ((WrapperParameter) param).getWrapperChildren()) {
   1.940 +                            if (i++ > 0)
   1.941 +                                parts += " ";
   1.942 +                            parts += parameter.getPartName();
   1.943 +                        }
   1.944 +                        body.parts(parts);
   1.945 +                    } else {
   1.946 +                        body.parts(param.getPartName());
   1.947 +                    }
   1.948 +                } else {
   1.949 +                    body.parts("");
   1.950 +                }
   1.951 +                QName responseMessage = new QName(targetNamespace, method.getResponseMessageName());
   1.952 +                generateSOAP12Headers(output, headerParams, responseMessage);
   1.953 +            }
   1.954 +            if (isRpc) {
   1.955 +                body.namespace(method.getRequestParameters().iterator().next().getName().getNamespaceURI());
   1.956 +            }
   1.957 +        }
   1.958 +        for (CheckedExceptionImpl exception : method.getCheckedExceptions()) {
   1.959 +            Fault fault = operation.fault().name(exception.getMessageName());
   1.960 +            extension.addBindingOperationFaultExtension(fault, method, exception);
   1.961 +            com.sun.xml.internal.ws.wsdl.writer.document.soap12.SOAPFault soapFault = fault._element(com.sun.xml.internal.ws.wsdl.writer.document.soap12.SOAPFault.class).name(exception.getMessageName());
   1.962 +            soapFault.use(LITERAL);
   1.963 +        }
   1.964 +    }
   1.965 +
   1.966 +    protected void splitParameters(List<ParameterImpl> bodyParams, List<ParameterImpl> headerParams, List<ParameterImpl> params) {
   1.967 +        for (ParameterImpl parameter : params) {
   1.968 +            if (isBodyParameter(parameter)) {
   1.969 +                bodyParams.add(parameter);
   1.970 +            } else {
   1.971 +                headerParams.add(parameter);
   1.972 +            }
   1.973 +        }
   1.974 +    }
   1.975 +
   1.976 +    protected void generateSOAPHeaders(TypedXmlWriter writer, List<ParameterImpl> parameters, QName message) {
   1.977 +
   1.978 +        for (ParameterImpl headerParam : parameters) {
   1.979 +            Header header = writer._element(Header.class);
   1.980 +            header.message(message);
   1.981 +            header.part(headerParam.getPartName());
   1.982 +            header.use(LITERAL);
   1.983 +        }
   1.984 +    }
   1.985 +
   1.986 +    protected void generateSOAP12Headers(TypedXmlWriter writer, List<ParameterImpl> parameters, QName message) {
   1.987 +
   1.988 +        for (ParameterImpl headerParam : parameters) {
   1.989 +            com.sun.xml.internal.ws.wsdl.writer.document.soap12.Header header = writer._element(com.sun.xml.internal.ws.wsdl.writer.document.soap12.Header.class);
   1.990 +            header.message(message);
   1.991 +
   1.992 +
   1.993 +            header.part(headerParam.getPartName());
   1.994 +            header.use(LITERAL);
   1.995 +        }
   1.996 +    }
   1.997 +
   1.998 +    /**
   1.999 +     * Generates the Service section of the WSDL
  1.1000 +     */
  1.1001 +    protected void generateService() {
  1.1002 +        QName portQName = model.getPortName();
  1.1003 +        QName serviceQName = model.getServiceQName();
  1.1004 +        Service service = serviceDefinitions.service().name(serviceQName.getLocalPart());
  1.1005 +        extension.addServiceExtension(service);
  1.1006 +        Port port = service.port().name(portQName.getLocalPart());
  1.1007 +        port.binding(model.getBoundPortTypeName());
  1.1008 +        extension.addPortExtension(port);
  1.1009 +        if (model.getJavaMethods().size() == 0)
  1.1010 +            return;
  1.1011 +
  1.1012 +        if (this.binding.getBindingId().getSOAPVersion() == SOAPVersion.SOAP_12) {
  1.1013 +            com.sun.xml.internal.ws.wsdl.writer.document.soap12.SOAPAddress address = port._element(com.sun.xml.internal.ws.wsdl.writer.document.soap12.SOAPAddress.class);
  1.1014 +            address.location(endpointAddress);
  1.1015 +        } else {
  1.1016 +            SOAPAddress address = port._element(SOAPAddress.class);
  1.1017 +            address.location(endpointAddress);
  1.1018 +        }
  1.1019 +    }
  1.1020 +
  1.1021 +    protected void generateInputMessage(Operation operation, JavaMethodImpl method) {
  1.1022 +        ParamType paramType = operation.input();
  1.1023 +        extension.addOperationInputExtension(paramType, method);
  1.1024 +//        paramType.message(method.getOperation().getName());
  1.1025 +        paramType.message(new QName(model.getTargetNamespace(), method.getRequestMessageName()));
  1.1026 +    }
  1.1027 +
  1.1028 +    protected void generateOutputMessage(Operation operation, JavaMethodImpl method) {
  1.1029 +        ParamType paramType = operation.output();
  1.1030 +        extension.addOperationOutputExtension(paramType, method);
  1.1031 +//        paramType.message(new QName(model.getTargetNamespace(), method.getOperation().getLocalName()+RESPONSE));
  1.1032 +        paramType.message(new QName(model.getTargetNamespace(), method.getResponseMessageName()));
  1.1033 +    }
  1.1034 +
  1.1035 +    /**
  1.1036 +     * Creates the {@link Result} object used by JAXB to generate a schema for the
  1.1037 +     * namesapceUri namespace.
  1.1038 +     * @param namespaceUri The namespace for the schema being generated
  1.1039 +     * @param suggestedFileName the JAXB suggested file name for the schema file
  1.1040 +     * @return the {@link Result} for JAXB to generate the schema into
  1.1041 +     * @throws java.io.IOException thrown if on IO error occurs
  1.1042 +     */
  1.1043 +    public Result createOutputFile(String namespaceUri, String suggestedFileName) throws IOException {
  1.1044 +        Result result;
  1.1045 +        if (namespaceUri == null) {
  1.1046 +            return null;
  1.1047 +        }
  1.1048 +
  1.1049 +        Holder<String> fileNameHolder = new Holder<String>();
  1.1050 +        fileNameHolder.value = schemaPrefix + suggestedFileName;
  1.1051 +        result = wsdlResolver.getSchemaOutput(namespaceUri, fileNameHolder);
  1.1052 +//        System.out.println("schema file: "+fileNameHolder.value);
  1.1053 +//        System.out.println("result: "+result);
  1.1054 +        String schemaLoc;
  1.1055 +        if (result == null)
  1.1056 +            schemaLoc = fileNameHolder.value;
  1.1057 +        else
  1.1058 +            schemaLoc = relativize(result.getSystemId(), wsdlLocation);
  1.1059 +        boolean isEmptyNs = namespaceUri.trim().equals("");
  1.1060 +        if (!isEmptyNs) {
  1.1061 +            com.sun.xml.internal.ws.wsdl.writer.document.xsd.Import _import = types.schema()._import();
  1.1062 +            _import.namespace(namespaceUri);
  1.1063 +            _import.schemaLocation(schemaLoc);
  1.1064 +        }
  1.1065 +        return result;
  1.1066 +    }
  1.1067 +
  1.1068 +    private Result createInlineSchema(String namespaceUri, String suggestedFileName) throws IOException {
  1.1069 +        Result result;
  1.1070 +        if (namespaceUri.equals("")) {
  1.1071 +            return null;
  1.1072 +        }
  1.1073 +
  1.1074 +//        Holder<String> fileNameHolder = new Holder<String>();
  1.1075 +//        fileNameHolder.value = schemaPrefix+suggestedFileName;
  1.1076 +//        result = wsdlResolver.getSchemaOutput(namespaceUri, fileNameHolder);
  1.1077 +//        if (result == null) {
  1.1078 +//            // JAXB doesn't have to generate it, a schema is already available
  1.1079 +//            com.sun.xml.internal.ws.wsdl.writer.document.xsd.Import _import = types.schema()._import().namespace(namespaceUri);
  1.1080 +//            _import.schemaLocation(fileNameHolder.value);
  1.1081 +//        } else {
  1.1082 +        // Let JAXB write the schema directly into wsdl's TypedXmlWriter
  1.1083 +        result = new TXWResult(types);
  1.1084 +        result.setSystemId("");
  1.1085 +//        }
  1.1086 +        return result;
  1.1087 +    }
  1.1088 +
  1.1089 +    /**
  1.1090 +     * Relativizes a URI by using another URI (base URI.)
  1.1091 +     *
  1.1092 +     * <p>
  1.1093 +     * For example, {@code relative("http://www.sun.com/abc/def","http://www.sun.com/pqr/stu") => "../abc/def"}
  1.1094 +     *
  1.1095 +     * <p>
  1.1096 +     * This method only works on hierarchical URI's, not opaque URI's (refer to the
  1.1097 +     * <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/net/URI.html">java.net.URI</a>
  1.1098 +     * javadoc for complete definitions of these terms.
  1.1099 +     *
  1.1100 +     * <p>
  1.1101 +     * This method will not normalize the relative URI.
  1.1102 +     * @param uri the URI to relativize
  1.1103 +     *
  1.1104 +     *
  1.1105 +     * @param baseUri the base URI to use for the relativization
  1.1106 +     * @return the relative URI or the original URI if a relative one could not be computed
  1.1107 +     */
  1.1108 +    protected static String relativize(String uri, String baseUri) {
  1.1109 +        try {
  1.1110 +            assert uri != null;
  1.1111 +
  1.1112 +            if (baseUri == null) return uri;
  1.1113 +
  1.1114 +            URI theUri = new URI(escapeURI(uri));
  1.1115 +            URI theBaseUri = new URI(escapeURI(baseUri));
  1.1116 +
  1.1117 +            if (theUri.isOpaque() || theBaseUri.isOpaque())
  1.1118 +                return uri;
  1.1119 +
  1.1120 +            if (!equalsIgnoreCase(theUri.getScheme(), theBaseUri.getScheme()) ||
  1.1121 +                    !equal(theUri.getAuthority(), theBaseUri.getAuthority()))
  1.1122 +                return uri;
  1.1123 +
  1.1124 +            String uriPath = theUri.getPath();
  1.1125 +            String basePath = theBaseUri.getPath();
  1.1126 +
  1.1127 +            // normalize base path
  1.1128 +            if (!basePath.endsWith("/")) {
  1.1129 +                basePath = normalizeUriPath(basePath);
  1.1130 +            }
  1.1131 +
  1.1132 +            if (uriPath.equals(basePath))
  1.1133 +                return ".";
  1.1134 +
  1.1135 +            String relPath = calculateRelativePath(uriPath, basePath);
  1.1136 +
  1.1137 +            if (relPath == null)
  1.1138 +                return uri; // recursion found no commonality in the two uris at all
  1.1139 +            StringBuffer relUri = new StringBuffer();
  1.1140 +            relUri.append(relPath);
  1.1141 +            if (theUri.getQuery() != null)
  1.1142 +                relUri.append('?').append(theUri.getQuery());
  1.1143 +            if (theUri.getFragment() != null)
  1.1144 +                relUri.append('#').append(theUri.getFragment());
  1.1145 +
  1.1146 +            return relUri.toString();
  1.1147 +        } catch (URISyntaxException e) {
  1.1148 +            throw new InternalError("Error escaping one of these uris:\n\t" + uri + "\n\t" + baseUri);
  1.1149 +        }
  1.1150 +    }
  1.1151 +
  1.1152 +    private static String calculateRelativePath(String uri, String base) {
  1.1153 +        if (base == null) {
  1.1154 +            return null;
  1.1155 +        }
  1.1156 +        if (uri.startsWith(base)) {
  1.1157 +            return uri.substring(base.length());
  1.1158 +        } else {
  1.1159 +            return "../" + calculateRelativePath(uri, getParentUriPath(base));
  1.1160 +        }
  1.1161 +    }
  1.1162 +
  1.1163 +
  1.1164 +    /**
  1.1165 +     * Implements the SchemaOutputResolver used by JAXB to
  1.1166 +     */
  1.1167 +    protected class JAXWSOutputSchemaResolver extends SchemaOutputResolver {
  1.1168 +        ArrayList<DOMResult> nonGlassfishSchemas = null;
  1.1169 +
  1.1170 +        /**
  1.1171 +         * Creates the {@link Result} object used by JAXB to generate a schema for the
  1.1172 +         * namesapceUri namespace.
  1.1173 +         * @param namespaceUri The namespace for the schema being generated
  1.1174 +         * @param suggestedFileName the JAXB suggested file name for the schema file
  1.1175 +         * @return the {@link Result} for JAXB to generate the schema into
  1.1176 +         * @throws java.io.IOException thrown if on IO error occurs
  1.1177 +         */
  1.1178 +        public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException {
  1.1179 +            return inlineSchemas
  1.1180 +                    ? ((nonGlassfishSchemas != null) ? nonGlassfishSchemaResult(namespaceUri, suggestedFileName) : createInlineSchema(namespaceUri, suggestedFileName))
  1.1181 +//                    ? createInlineSchema(namespaceUri, suggestedFileName)
  1.1182 +                    : createOutputFile(namespaceUri, suggestedFileName);
  1.1183 +        }
  1.1184 +
  1.1185 +        private Result nonGlassfishSchemaResult(String namespaceUri, String suggestedFileName) throws IOException {
  1.1186 +            DOMResult result = new DOMResult();
  1.1187 +            result.setSystemId("");
  1.1188 +            nonGlassfishSchemas.add(result);
  1.1189 +            return result;
  1.1190 +        }
  1.1191 +    }
  1.1192 +
  1.1193 +    private void register(WSDLGeneratorExtension h) {
  1.1194 +        extensionHandlers.add(h);
  1.1195 +    }
  1.1196 +}

mercurial