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

Tue, 06 Mar 2012 16:09:35 -0800

author
ohair
date
Tue, 06 Mar 2012 16:09:35 -0800
changeset 286
f50545b5e2f1
child 368
0989ad8c0860
permissions
-rw-r--r--

7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom

ohair@286 1 /*
ohair@286 2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
ohair@286 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286 4 *
ohair@286 5 * This code is free software; you can redistribute it and/or modify it
ohair@286 6 * under the terms of the GNU General Public License version 2 only, as
ohair@286 7 * published by the Free Software Foundation. Oracle designates this
ohair@286 8 * particular file as subject to the "Classpath" exception as provided
ohair@286 9 * by Oracle in the LICENSE file that accompanied this code.
ohair@286 10 *
ohair@286 11 * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286 14 * version 2 for more details (a copy is included in the LICENSE file that
ohair@286 15 * accompanied this code).
ohair@286 16 *
ohair@286 17 * You should have received a copy of the GNU General Public License version
ohair@286 18 * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286 20 *
ohair@286 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286 22 * or visit www.oracle.com if you need additional information or have any
ohair@286 23 * questions.
ohair@286 24 */
ohair@286 25
ohair@286 26 package com.sun.xml.internal.ws.wsdl.writer;
ohair@286 27
ohair@286 28
ohair@286 29 import static com.sun.xml.internal.bind.v2.schemagen.Util.*;
ohair@286 30
ohair@286 31 import com.sun.xml.internal.txw2.TXW;
ohair@286 32 import com.sun.xml.internal.txw2.TypedXmlWriter;
ohair@286 33 import com.sun.xml.internal.txw2.output.ResultFactory;
ohair@286 34 import com.sun.xml.internal.txw2.output.XmlSerializer;
ohair@286 35 import com.sun.xml.internal.txw2.output.TXWResult;
ohair@286 36 import com.sun.xml.internal.ws.api.SOAPVersion;
ohair@286 37 import com.sun.xml.internal.ws.api.WSBinding;
ohair@286 38 import com.sun.xml.internal.ws.api.model.JavaMethod;
ohair@286 39 import com.sun.xml.internal.ws.api.model.MEP;
ohair@286 40 import com.sun.xml.internal.ws.api.model.ParameterBinding;
ohair@286 41 import com.sun.xml.internal.ws.api.model.SEIModel;
ohair@286 42 import com.sun.xml.internal.ws.api.model.soap.SOAPBinding;
ohair@286 43 import com.sun.xml.internal.ws.api.server.Container;
ohair@286 44 import com.sun.xml.internal.ws.api.wsdl.writer.WSDLGeneratorExtension;
ohair@286 45 import com.sun.xml.internal.ws.api.wsdl.writer.WSDLGenExtnContext;
ohair@286 46 import com.sun.xml.internal.ws.model.AbstractSEIModelImpl;
ohair@286 47 import com.sun.xml.internal.ws.model.CheckedExceptionImpl;
ohair@286 48 import com.sun.xml.internal.ws.model.JavaMethodImpl;
ohair@286 49 import com.sun.xml.internal.ws.model.ParameterImpl;
ohair@286 50 import com.sun.xml.internal.ws.model.WrapperParameter;
ohair@286 51 import com.sun.xml.internal.ws.wsdl.parser.SOAPConstants;
ohair@286 52 import com.sun.xml.internal.ws.wsdl.parser.WSDLConstants;
ohair@286 53 import com.sun.xml.internal.ws.wsdl.writer.document.Binding;
ohair@286 54 import com.sun.xml.internal.ws.wsdl.writer.document.BindingOperationType;
ohair@286 55 import com.sun.xml.internal.ws.wsdl.writer.document.Definitions;
ohair@286 56 import com.sun.xml.internal.ws.wsdl.writer.document.Fault;
ohair@286 57 import com.sun.xml.internal.ws.wsdl.writer.document.FaultType;
ohair@286 58 import com.sun.xml.internal.ws.wsdl.writer.document.Import;
ohair@286 59 import com.sun.xml.internal.ws.wsdl.writer.document.Message;
ohair@286 60 import com.sun.xml.internal.ws.wsdl.writer.document.Operation;
ohair@286 61 import com.sun.xml.internal.ws.wsdl.writer.document.ParamType;
ohair@286 62 import com.sun.xml.internal.ws.wsdl.writer.document.Port;
ohair@286 63 import com.sun.xml.internal.ws.wsdl.writer.document.PortType;
ohair@286 64 import com.sun.xml.internal.ws.wsdl.writer.document.Service;
ohair@286 65 import com.sun.xml.internal.ws.wsdl.writer.document.Types;
ohair@286 66 import com.sun.xml.internal.ws.wsdl.writer.document.soap.Body;
ohair@286 67 import com.sun.xml.internal.ws.wsdl.writer.document.soap.BodyType;
ohair@286 68 import com.sun.xml.internal.ws.wsdl.writer.document.soap.Header;
ohair@286 69 import com.sun.xml.internal.ws.wsdl.writer.document.soap.SOAPAddress;
ohair@286 70 import com.sun.xml.internal.ws.wsdl.writer.document.soap.SOAPFault;
ohair@286 71 import com.sun.xml.internal.ws.spi.db.BindingContext;
ohair@286 72 import com.sun.xml.internal.ws.spi.db.BindingHelper;
ohair@286 73 import com.sun.xml.internal.ws.util.RuntimeVersion;
ohair@286 74 import com.sun.xml.internal.ws.policy.jaxws.PolicyWSDLGeneratorExtension;
ohair@286 75 import com.sun.xml.internal.ws.encoding.soap.streaming.SOAPNamespaceConstants;
ohair@286 76
ohair@286 77 import javax.jws.soap.SOAPBinding.Style;
ohair@286 78 import javax.jws.soap.SOAPBinding.Use;
ohair@286 79 import javax.xml.bind.SchemaOutputResolver;
ohair@286 80 import javax.xml.namespace.QName;
ohair@286 81 import javax.xml.transform.Result;
ohair@286 82 import javax.xml.transform.Transformer;
ohair@286 83 import javax.xml.transform.TransformerConfigurationException;
ohair@286 84 import javax.xml.transform.TransformerException;
ohair@286 85 import javax.xml.transform.TransformerFactory;
ohair@286 86 import javax.xml.transform.dom.DOMResult;
ohair@286 87 import javax.xml.transform.dom.DOMSource;
ohair@286 88 import javax.xml.transform.sax.SAXResult;
ohair@286 89 import javax.xml.ws.Holder;
ohair@286 90 import javax.xml.ws.WebServiceException;
ohair@286 91
ohair@286 92 import org.w3c.dom.Document;
ohair@286 93
ohair@286 94 import java.io.IOException;
ohair@286 95 import java.net.URI;
ohair@286 96 import java.net.URISyntaxException;
ohair@286 97 import java.util.ArrayList;
ohair@286 98 import java.util.HashSet;
ohair@286 99 import java.util.Iterator;
ohair@286 100 import java.util.List;
ohair@286 101 import java.util.Set;
ohair@286 102
ohair@286 103
ohair@286 104 /**
ohair@286 105 * Class used to generate WSDLs from a {@link SEIModel}.
ohair@286 106 *
ohair@286 107 * @author WS Development Team
ohair@286 108 */
ohair@286 109 public class WSDLGenerator {
ohair@286 110 private JAXWSOutputSchemaResolver resolver;
ohair@286 111 private WSDLResolver wsdlResolver = null;
ohair@286 112 private AbstractSEIModelImpl model;
ohair@286 113 private Definitions serviceDefinitions;
ohair@286 114 private Definitions portDefinitions;
ohair@286 115 private Types types;
ohair@286 116 /**
ohair@286 117 * Constant String for ".wsdl"
ohair@286 118 */
ohair@286 119 private static final String DOT_WSDL = ".wsdl";
ohair@286 120 /**
ohair@286 121 * Constant String appended to response message names
ohair@286 122 */
ohair@286 123 private static final String RESPONSE = "Response";
ohair@286 124 /**
ohair@286 125 * constant String used for part name for wrapped request messages
ohair@286 126 */
ohair@286 127 private static final String PARAMETERS = "parameters";
ohair@286 128 /**
ohair@286 129 * the part name for unwrappable response messages
ohair@286 130 */
ohair@286 131 private static final String RESULT = "parameters";
ohair@286 132 /**
ohair@286 133 * the part name for response messages that are not unwrappable
ohair@286 134 */
ohair@286 135 private static final String UNWRAPPABLE_RESULT = "result";
ohair@286 136 /**
ohair@286 137 * The WSDL namespace
ohair@286 138 */
ohair@286 139 private static final String WSDL_NAMESPACE = WSDLConstants.NS_WSDL;
ohair@286 140
ohair@286 141 /**
ohair@286 142 * the XSD namespace
ohair@286 143 */
ohair@286 144 private static final String XSD_NAMESPACE = SOAPNamespaceConstants.XSD;
ohair@286 145 /**
ohair@286 146 * the namespace prefix to use for the XSD namespace
ohair@286 147 */
ohair@286 148 private static final String XSD_PREFIX = "xsd";
ohair@286 149 /**
ohair@286 150 * The SOAP 1.1 namespace
ohair@286 151 */
ohair@286 152 private static final String SOAP11_NAMESPACE = SOAPConstants.NS_WSDL_SOAP;
ohair@286 153 /**
ohair@286 154 * The SOAP 1.2 namespace
ohair@286 155 */
ohair@286 156 private static final String SOAP12_NAMESPACE = SOAPConstants.NS_WSDL_SOAP12;
ohair@286 157 /**
ohair@286 158 * The namespace prefix to use for the SOAP 1.1 namespace
ohair@286 159 */
ohair@286 160 private static final String SOAP_PREFIX = "soap";
ohair@286 161 /**
ohair@286 162 * The namespace prefix to use for the SOAP 1.2 namespace
ohair@286 163 */
ohair@286 164 private static final String SOAP12_PREFIX = "soap12";
ohair@286 165 /**
ohair@286 166 * The namespace prefix to use for the targetNamespace
ohair@286 167 */
ohair@286 168 private static final String TNS_PREFIX = "tns";
ohair@286 169
ohair@286 170 /**
ohair@286 171 * Constant String "document" used to specify <code>document</code> style
ohair@286 172 * soapBindings
ohair@286 173 */
ohair@286 174 private static final String DOCUMENT = "document";
ohair@286 175 /**
ohair@286 176 * Constant String "rpc" used to specify <code>rpc</code> style
ohair@286 177 * soapBindings
ohair@286 178 */
ohair@286 179 private static final String RPC = "rpc";
ohair@286 180 /**
ohair@286 181 * Constant String "literal" used to create <code>literal</code> use binddings
ohair@286 182 */
ohair@286 183 private static final String LITERAL = "literal";
ohair@286 184 /**
ohair@286 185 * Constant String to flag the URL to replace at runtime for the endpoint
ohair@286 186 */
ohair@286 187 private static final String REPLACE_WITH_ACTUAL_URL = "REPLACE_WITH_ACTUAL_URL";
ohair@286 188 private Set<QName> processedExceptions = new HashSet<QName>();
ohair@286 189 private WSBinding binding;
ohair@286 190 private String wsdlLocation;
ohair@286 191 private String portWSDLID;
ohair@286 192 private String schemaPrefix;
ohair@286 193 private WSDLGeneratorExtension extension;
ohair@286 194 List<WSDLGeneratorExtension> extensionHandlers;
ohair@286 195
ohair@286 196 private String endpointAddress = REPLACE_WITH_ACTUAL_URL;
ohair@286 197 private Container container;
ohair@286 198 private final Class implType;
ohair@286 199
ohair@286 200 private boolean inlineSchemas; // TODO
ohair@286 201
ohair@286 202 /**
ohair@286 203 * Creates the WSDLGenerator
ohair@286 204 * @param model The {@link AbstractSEIModelImpl} used to generate the WSDL
ohair@286 205 * @param wsdlResolver The {@link WSDLResolver} to use resovle names while generating the WSDL
ohair@286 206 * @param binding specifies which {@link javax.xml.ws.BindingType} to generate
ohair@286 207 * @param extensions an array {@link WSDLGeneratorExtension} that will
ohair@286 208 * be invoked to generate WSDL extensions
ohair@286 209 */
ohair@286 210 public WSDLGenerator(AbstractSEIModelImpl model, WSDLResolver wsdlResolver, WSBinding binding, Container container,
ohair@286 211 Class implType, boolean inlineSchemas, WSDLGeneratorExtension... extensions) {
ohair@286 212 this.model = model;
ohair@286 213 resolver = new JAXWSOutputSchemaResolver();
ohair@286 214 this.wsdlResolver = wsdlResolver;
ohair@286 215 this.binding = binding;
ohair@286 216 this.container = container;
ohair@286 217 this.implType = implType;
ohair@286 218 extensionHandlers = new ArrayList<WSDLGeneratorExtension>();
ohair@286 219 this.inlineSchemas = inlineSchemas;
ohair@286 220
ohair@286 221 // register handlers for default extensions
ohair@286 222 register(new W3CAddressingWSDLGeneratorExtension());
ohair@286 223 register(new W3CAddressingMetadataWSDLGeneratorExtension());
ohair@286 224 register(new PolicyWSDLGeneratorExtension());
ohair@286 225
ohair@286 226 if (container != null) { // on server
ohair@286 227 WSDLGeneratorExtension[] wsdlGeneratorExtensions = container.getSPI(WSDLGeneratorExtension[].class);
ohair@286 228 if (wsdlGeneratorExtensions != null) {
ohair@286 229 for (WSDLGeneratorExtension wsdlGeneratorExtension : wsdlGeneratorExtensions) {
ohair@286 230 register(wsdlGeneratorExtension);
ohair@286 231 }
ohair@286 232 }
ohair@286 233 }
ohair@286 234
ohair@286 235 for (WSDLGeneratorExtension w : extensions)
ohair@286 236 register(w);
ohair@286 237
ohair@286 238 this.extension = new WSDLGeneratorExtensionFacade(extensionHandlers.toArray(new WSDLGeneratorExtension[0]));
ohair@286 239 }
ohair@286 240
ohair@286 241 /**
ohair@286 242 * Sets the endpoint address string to be written.
ohair@286 243 * Defaults to {@link #REPLACE_WITH_ACTUAL_URL}.
ohair@286 244 *
ohair@286 245 * @param address wsdl:port/soap:address/[@location] value
ohair@286 246 */
ohair@286 247 public void setEndpointAddress(String address) {
ohair@286 248 this.endpointAddress = address;
ohair@286 249 }
ohair@286 250
ohair@286 251 protected String mangleName(String name) {
ohair@286 252 return BindingHelper.mangleNameToClassName(name);
ohair@286 253 }
ohair@286 254
ohair@286 255 /**
ohair@286 256 * Performes the actual WSDL generation
ohair@286 257 */
ohair@286 258 public void doGeneration() {
ohair@286 259 XmlSerializer serviceWriter;
ohair@286 260 XmlSerializer portWriter = null;
ohair@286 261 String fileName = mangleName(model.getServiceQName().getLocalPart());
ohair@286 262 Result result = wsdlResolver.getWSDL(fileName + DOT_WSDL);
ohair@286 263 wsdlLocation = result.getSystemId();
ohair@286 264 serviceWriter = new CommentFilter(ResultFactory.createSerializer(result));
ohair@286 265 if (model.getServiceQName().getNamespaceURI().equals(model.getTargetNamespace())) {
ohair@286 266 portWriter = serviceWriter;
ohair@286 267 schemaPrefix = fileName + "_";
ohair@286 268 } else {
ohair@286 269 String wsdlName = mangleName(model.getPortTypeName().getLocalPart());
ohair@286 270 if (wsdlName.equals(fileName))
ohair@286 271 wsdlName += "PortType";
ohair@286 272 Holder<String> absWSDLName = new Holder<String>();
ohair@286 273 absWSDLName.value = wsdlName + DOT_WSDL;
ohair@286 274 result = wsdlResolver.getAbstractWSDL(absWSDLName);
ohair@286 275
ohair@286 276 if (result != null) {
ohair@286 277 portWSDLID = result.getSystemId();
ohair@286 278 if (portWSDLID.equals(wsdlLocation)) {
ohair@286 279 portWriter = serviceWriter;
ohair@286 280 } else {
ohair@286 281 portWriter = new CommentFilter(ResultFactory.createSerializer(result));
ohair@286 282 }
ohair@286 283 } else {
ohair@286 284 portWSDLID = absWSDLName.value;
ohair@286 285 }
ohair@286 286 schemaPrefix = new java.io.File(portWSDLID).getName();
ohair@286 287 int idx = schemaPrefix.lastIndexOf('.');
ohair@286 288 if (idx > 0)
ohair@286 289 schemaPrefix = schemaPrefix.substring(0, idx);
ohair@286 290 schemaPrefix = mangleName(schemaPrefix) + "_";
ohair@286 291 }
ohair@286 292 generateDocument(serviceWriter, portWriter);
ohair@286 293 }
ohair@286 294
ohair@286 295 /**
ohair@286 296 * Writing directly to XmlSerializer is a problem, since it doesn't suppress
ohair@286 297 * xml declaration. Creating filter so that comment is written before TXW writes
ohair@286 298 * anything in the WSDL.
ohair@286 299 */
ohair@286 300 private static class CommentFilter implements XmlSerializer {
ohair@286 301 final XmlSerializer serializer;
ohair@286 302 private static final String VERSION_COMMENT =
ohair@286 303 " Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is " + RuntimeVersion.VERSION + ". ";
ohair@286 304
ohair@286 305 CommentFilter(XmlSerializer serializer) {
ohair@286 306 this.serializer = serializer;
ohair@286 307 }
ohair@286 308
ohair@286 309 public void startDocument() {
ohair@286 310 serializer.startDocument();
ohair@286 311 comment(new StringBuilder(VERSION_COMMENT));
ohair@286 312 text(new StringBuilder("\n"));
ohair@286 313 }
ohair@286 314
ohair@286 315 public void beginStartTag(String uri, String localName, String prefix) {
ohair@286 316 serializer.beginStartTag(uri, localName, prefix);
ohair@286 317 }
ohair@286 318
ohair@286 319 public void writeAttribute(String uri, String localName, String prefix, StringBuilder value) {
ohair@286 320 serializer.writeAttribute(uri, localName, prefix, value);
ohair@286 321 }
ohair@286 322
ohair@286 323 public void writeXmlns(String prefix, String uri) {
ohair@286 324 serializer.writeXmlns(prefix, uri);
ohair@286 325 }
ohair@286 326
ohair@286 327 public void endStartTag(String uri, String localName, String prefix) {
ohair@286 328 serializer.endStartTag(uri, localName, prefix);
ohair@286 329 }
ohair@286 330
ohair@286 331 public void endTag() {
ohair@286 332 serializer.endTag();
ohair@286 333 }
ohair@286 334
ohair@286 335 public void text(StringBuilder text) {
ohair@286 336 serializer.text(text);
ohair@286 337 }
ohair@286 338
ohair@286 339 public void cdata(StringBuilder text) {
ohair@286 340 serializer.cdata(text);
ohair@286 341 }
ohair@286 342
ohair@286 343 public void comment(StringBuilder comment) {
ohair@286 344 serializer.comment(comment);
ohair@286 345 }
ohair@286 346
ohair@286 347 public void endDocument() {
ohair@286 348 serializer.endDocument();
ohair@286 349 }
ohair@286 350
ohair@286 351 public void flush() {
ohair@286 352 serializer.flush();
ohair@286 353 }
ohair@286 354
ohair@286 355 }
ohair@286 356
ohair@286 357 private void generateDocument(XmlSerializer serviceStream, XmlSerializer portStream) {
ohair@286 358 serviceDefinitions = TXW.create(Definitions.class, serviceStream);
ohair@286 359 serviceDefinitions._namespace(WSDL_NAMESPACE, "");//WSDL_PREFIX);
ohair@286 360 serviceDefinitions._namespace(XSD_NAMESPACE, XSD_PREFIX);
ohair@286 361 serviceDefinitions.targetNamespace(model.getServiceQName().getNamespaceURI());
ohair@286 362 serviceDefinitions._namespace(model.getServiceQName().getNamespaceURI(), TNS_PREFIX);
ohair@286 363 if (binding.getSOAPVersion() == SOAPVersion.SOAP_12)
ohair@286 364 serviceDefinitions._namespace(SOAP12_NAMESPACE, SOAP12_PREFIX);
ohair@286 365 else
ohair@286 366 serviceDefinitions._namespace(SOAP11_NAMESPACE, SOAP_PREFIX);
ohair@286 367 serviceDefinitions.name(model.getServiceQName().getLocalPart());
ohair@286 368 WSDLGenExtnContext serviceCtx = new WSDLGenExtnContext(serviceDefinitions, model, binding, container, implType);
ohair@286 369 extension.start(serviceCtx);
ohair@286 370 if (serviceStream != portStream && portStream != null) {
ohair@286 371 // generate an abstract and concrete wsdl
ohair@286 372 portDefinitions = TXW.create(Definitions.class, portStream);
ohair@286 373 portDefinitions._namespace(WSDL_NAMESPACE, "");//WSDL_PREFIX);
ohair@286 374 portDefinitions._namespace(XSD_NAMESPACE, XSD_PREFIX);
ohair@286 375 if (model.getTargetNamespace() != null) {
ohair@286 376 portDefinitions.targetNamespace(model.getTargetNamespace());
ohair@286 377 portDefinitions._namespace(model.getTargetNamespace(), TNS_PREFIX);
ohair@286 378 }
ohair@286 379
ohair@286 380 String schemaLoc = relativize(portWSDLID, wsdlLocation);
ohair@286 381 Import _import = serviceDefinitions._import().namespace(model.getTargetNamespace());
ohair@286 382 _import.location(schemaLoc);
ohair@286 383 } else if (portStream != null) {
ohair@286 384 // abstract and concrete are the same
ohair@286 385 portDefinitions = serviceDefinitions;
ohair@286 386 } else {
ohair@286 387 // import a provided abstract wsdl
ohair@286 388 String schemaLoc = relativize(portWSDLID, wsdlLocation);
ohair@286 389 Import _import = serviceDefinitions._import().namespace(model.getTargetNamespace());
ohair@286 390 _import.location(schemaLoc);
ohair@286 391 }
ohair@286 392 extension.addDefinitionsExtension(serviceDefinitions);
ohair@286 393
ohair@286 394 if (portDefinitions != null) {
ohair@286 395 generateTypes();
ohair@286 396 generateMessages();
ohair@286 397 generatePortType();
ohair@286 398 }
ohair@286 399 generateBinding();
ohair@286 400 generateService();
ohair@286 401 //Give a chance to WSDLGeneratorExtensions to write stuff before closing </wsdl:defintions>
ohair@286 402 extension.end(serviceCtx);
ohair@286 403 serviceDefinitions.commit();
ohair@286 404 if (portDefinitions != null && portDefinitions != serviceDefinitions)
ohair@286 405 portDefinitions.commit();
ohair@286 406 }
ohair@286 407
ohair@286 408
ohair@286 409 /**
ohair@286 410 * Generates the types section of the WSDL
ohair@286 411 */
ohair@286 412 protected void generateTypes() {
ohair@286 413 types = portDefinitions.types();
ohair@286 414 if (model.getBindingContext() != null) {
ohair@286 415 if (inlineSchemas && model.getBindingContext().getClass().getName().indexOf("glassfish") == -1) {
ohair@286 416 resolver.nonGlassfishSchemas = new ArrayList<DOMResult>();
ohair@286 417 }
ohair@286 418 try {
ohair@286 419 model.getBindingContext().generateSchema(resolver);
ohair@286 420 } catch (IOException e) {
ohair@286 421 // TODO locallize and wrap this
ohair@286 422 e.printStackTrace();
ohair@286 423 throw new WebServiceException(e.getMessage());
ohair@286 424 }
ohair@286 425 }
ohair@286 426 if (resolver.nonGlassfishSchemas != null) {
ohair@286 427 TransformerFactory tf = TransformerFactory.newInstance();
ohair@286 428 try {
ohair@286 429 Transformer t = tf.newTransformer();
ohair@286 430 for (DOMResult xsd : resolver.nonGlassfishSchemas) {
ohair@286 431 Document doc = (Document) xsd.getNode();
ohair@286 432 SAXResult sax = new SAXResult(new TXWContentHandler(types));
ohair@286 433 t.transform(new DOMSource(doc.getDocumentElement()), sax);
ohair@286 434 }
ohair@286 435 } catch (TransformerConfigurationException e) {
ohair@286 436 e.printStackTrace();
ohair@286 437 throw new WebServiceException(e.getMessage(), e);
ohair@286 438 } catch (TransformerException e) {
ohair@286 439 e.printStackTrace();
ohair@286 440 throw new WebServiceException(e.getMessage(), e);
ohair@286 441 }
ohair@286 442 }
ohair@286 443 }
ohair@286 444
ohair@286 445 /**
ohair@286 446 * Generates the WSDL messages
ohair@286 447 */
ohair@286 448 protected void generateMessages() {
ohair@286 449 for (JavaMethodImpl method : model.getJavaMethods()) {
ohair@286 450 generateSOAPMessages(method, method.getBinding());
ohair@286 451 }
ohair@286 452 }
ohair@286 453
ohair@286 454 /**
ohair@286 455 * Generates messages for a SOAPBinding
ohair@286 456 * @param method The {@link JavaMethod} to generate messages for
ohair@286 457 * @param binding The {@link com.sun.xml.internal.ws.api.model.soap.SOAPBinding} to add the generated messages to
ohair@286 458 */
ohair@286 459 protected void generateSOAPMessages(JavaMethodImpl method, com.sun.xml.internal.ws.api.model.soap.SOAPBinding binding) {
ohair@286 460 boolean isDoclit = binding.isDocLit();
ohair@286 461 // Message message = portDefinitions.message().name(method.getOperation().getName().getLocalPart());
ohair@286 462 Message message = portDefinitions.message().name(method.getRequestMessageName());
ohair@286 463 extension.addInputMessageExtension(message, method);
ohair@286 464 com.sun.xml.internal.ws.wsdl.writer.document.Part part;
ohair@286 465 BindingContext jaxbContext = model.getBindingContext();
ohair@286 466 boolean unwrappable = true;
ohair@286 467 for (ParameterImpl param : method.getRequestParameters()) {
ohair@286 468 if (isDoclit) {
ohair@286 469 if (isHeaderParameter(param))
ohair@286 470 unwrappable = false;
ohair@286 471
ohair@286 472 part = message.part().name(param.getPartName());
ohair@286 473 part.element(param.getName());
ohair@286 474 } else {
ohair@286 475 if (param.isWrapperStyle()) {
ohair@286 476 for (ParameterImpl childParam : ((WrapperParameter) param).getWrapperChildren()) {
ohair@286 477 part = message.part().name(childParam.getPartName());
ohair@286 478 part.type(jaxbContext.getTypeName(childParam.getXMLBridge().getTypeInfo()));
ohair@286 479 }
ohair@286 480 } else {
ohair@286 481 part = message.part().name(param.getPartName());
ohair@286 482 part.element(param.getName());
ohair@286 483 }
ohair@286 484 }
ohair@286 485 }
ohair@286 486 if (method.getMEP() != MEP.ONE_WAY) {
ohair@286 487 // message = portDefinitions.message().name(method.getOperation().getName().getLocalPart()+RESPONSE);
ohair@286 488 message = portDefinitions.message().name(method.getResponseMessageName());
ohair@286 489 extension.addOutputMessageExtension(message, method);
ohair@286 490 if (unwrappable) {
ohair@286 491 for (ParameterImpl param : method.getResponseParameters()) {
ohair@286 492 if (isHeaderParameter(param))
ohair@286 493 unwrappable = false;
ohair@286 494 }
ohair@286 495 }
ohair@286 496
ohair@286 497 for (ParameterImpl param : method.getResponseParameters()) {
ohair@286 498 if (isDoclit) {
ohair@286 499 part = message.part().name(param.getPartName());
ohair@286 500 part.element(param.getName());
ohair@286 501
ohair@286 502 } else {
ohair@286 503 if (param.isWrapperStyle()) {
ohair@286 504 for (ParameterImpl childParam : ((WrapperParameter) param).getWrapperChildren()) {
ohair@286 505 part = message.part().name(childParam.getPartName());
ohair@286 506 part.type(jaxbContext.getTypeName(childParam.getXMLBridge().getTypeInfo()));
ohair@286 507 }
ohair@286 508 } else {
ohair@286 509 part = message.part().name(param.getPartName());
ohair@286 510 part.element(param.getName());
ohair@286 511 }
ohair@286 512 }
ohair@286 513 }
ohair@286 514 }
ohair@286 515 for (CheckedExceptionImpl exception : method.getCheckedExceptions()) {
ohair@286 516 QName tagName = exception.getDetailType().tagName;
ohair@286 517 String messageName = exception.getMessageName();
ohair@286 518 QName messageQName = new QName(model.getTargetNamespace(), messageName);
ohair@286 519 if (processedExceptions.contains(messageQName))
ohair@286 520 continue;
ohair@286 521 message = portDefinitions.message().name(messageName);
ohair@286 522
ohair@286 523 extension.addFaultMessageExtension(message, method, exception);
ohair@286 524 part = message.part().name("fault");//tagName.getLocalPart());
ohair@286 525 part.element(tagName);
ohair@286 526 processedExceptions.add(messageQName);
ohair@286 527 }
ohair@286 528 }
ohair@286 529
ohair@286 530 /**
ohair@286 531 * Generates the WSDL portType
ohair@286 532 */
ohair@286 533 protected void generatePortType() {
ohair@286 534
ohair@286 535 PortType portType = portDefinitions.portType().name(model.getPortTypeName().getLocalPart());
ohair@286 536 extension.addPortTypeExtension(portType);
ohair@286 537 for (JavaMethodImpl method : model.getJavaMethods()) {
ohair@286 538 Operation operation = portType.operation().name(method.getOperationName());
ohair@286 539 generateParameterOrder(operation, method);
ohair@286 540 extension.addOperationExtension(operation, method);
ohair@286 541 switch (method.getMEP()) {
ohair@286 542 case REQUEST_RESPONSE:
ohair@286 543 // input message
ohair@286 544 generateInputMessage(operation, method);
ohair@286 545 // output message
ohair@286 546 generateOutputMessage(operation, method);
ohair@286 547 break;
ohair@286 548 case ONE_WAY:
ohair@286 549 generateInputMessage(operation, method);
ohair@286 550 break;
ohair@286 551 }
ohair@286 552 // faults
ohair@286 553 for (CheckedExceptionImpl exception : method.getCheckedExceptions()) {
ohair@286 554 QName messageName = new QName(model.getTargetNamespace(), exception.getMessageName());
ohair@286 555 FaultType paramType = operation.fault().message(messageName).name(exception.getMessageName());
ohair@286 556 extension.addOperationFaultExtension(paramType, method, exception);
ohair@286 557 }
ohair@286 558 }
ohair@286 559 }
ohair@286 560
ohair@286 561 /**
ohair@286 562 * Determines if the <CODE>method</CODE> is wrapper style
ohair@286 563 * @param method The {@link JavaMethod} to check if it is wrapper style
ohair@286 564 * @return true if the method is wrapper style, otherwise, false.
ohair@286 565 */
ohair@286 566 protected boolean isWrapperStyle(JavaMethodImpl method) {
ohair@286 567 if (method.getRequestParameters().size() > 0) {
ohair@286 568 ParameterImpl param = method.getRequestParameters().iterator().next();
ohair@286 569 return param.isWrapperStyle();
ohair@286 570 }
ohair@286 571 return false;
ohair@286 572 }
ohair@286 573
ohair@286 574 /**
ohair@286 575 * Determines if a {@link JavaMethod} is rpc/literal
ohair@286 576 * @param method The method to check
ohair@286 577 * @return true if method is rpc/literal, otherwise, false
ohair@286 578 */
ohair@286 579 protected boolean isRpcLit(JavaMethodImpl method) {
ohair@286 580 return method.getBinding().getStyle() == Style.RPC;
ohair@286 581 }
ohair@286 582
ohair@286 583 /**
ohair@286 584 * Generates the parameterOrder for a PortType operation
ohair@286 585 * @param operation The operation to generate the parameterOrder for
ohair@286 586 * @param method The {@link JavaMethod} to generate the parameterOrder from
ohair@286 587 */
ohair@286 588 protected void generateParameterOrder(Operation operation, JavaMethodImpl method) {
ohair@286 589 if (method.getMEP() == MEP.ONE_WAY)
ohair@286 590 return;
ohair@286 591 if (isRpcLit(method))
ohair@286 592 generateRpcParameterOrder(operation, method);
ohair@286 593 else
ohair@286 594 generateDocumentParameterOrder(operation, method);
ohair@286 595 }
ohair@286 596
ohair@286 597 /**
ohair@286 598 * Generates the parameterOrder for a PortType operation
ohair@286 599 * @param operation the operation to generate the parameterOrder for
ohair@286 600 * @param method the {@link JavaMethod} to generate the parameterOrder from
ohair@286 601 */
ohair@286 602 protected void generateRpcParameterOrder(Operation operation, JavaMethodImpl method) {
ohair@286 603 String partName;
ohair@286 604 StringBuffer paramOrder = new StringBuffer();
ohair@286 605 Set<String> partNames = new HashSet<String>();
ohair@286 606 List<ParameterImpl> sortedParams = sortMethodParameters(method);
ohair@286 607 int i = 0;
ohair@286 608 for (ParameterImpl parameter : sortedParams) {
ohair@286 609 if (parameter.getIndex() >= 0) {
ohair@286 610 partName = parameter.getPartName();
ohair@286 611 if (!partNames.contains(partName)) {
ohair@286 612 if (i++ > 0)
ohair@286 613 paramOrder.append(' ');
ohair@286 614 paramOrder.append(partName);
ohair@286 615 partNames.add(partName);
ohair@286 616 }
ohair@286 617 }
ohair@286 618 }
ohair@286 619 if (i > 1) {
ohair@286 620 operation.parameterOrder(paramOrder.toString());
ohair@286 621 }
ohair@286 622 }
ohair@286 623
ohair@286 624
ohair@286 625 /**
ohair@286 626 * Generates the parameterOrder for a PortType operation
ohair@286 627 * @param operation the operation to generate the parameterOrder for
ohair@286 628 * @param method the {@link JavaMethod} to generate the parameterOrder from
ohair@286 629 */
ohair@286 630 protected void generateDocumentParameterOrder(Operation operation, JavaMethodImpl method) {
ohair@286 631 String partName;
ohair@286 632 StringBuffer paramOrder = new StringBuffer();
ohair@286 633 Set<String> partNames = new HashSet<String>();
ohair@286 634 List<ParameterImpl> sortedParams = sortMethodParameters(method);
ohair@286 635 boolean isWrapperStyle = isWrapperStyle(method);
ohair@286 636 int i = 0;
ohair@286 637 for (ParameterImpl parameter : sortedParams) {
ohair@286 638 // System.out.println("param: "+parameter.getIndex()+" name: "+parameter.getName().getLocalPart());
ohair@286 639 if (parameter.getIndex() < 0)
ohair@286 640 continue;
ohair@286 641
ohair@286 642 // This should be safe change. if it affects compatibility,
ohair@286 643 // remove the following single statement and uncomment the code in block below.
ohair@286 644 partName = parameter.getPartName();
ohair@286 645 /*
ohair@286 646 if (isWrapperStyle && isBodyParameter(parameter)) {
ohair@286 647 System.out.println("isWrapper and is body");
ohair@286 648 if (method.getRequestParameters().contains(parameter))
ohair@286 649 partName = PARAMETERS;
ohair@286 650 else {
ohair@286 651 //Rama: don't understand this logic "Response" below,
ohair@286 652
ohair@286 653 // really make sure this is a wrapper style wsdl we are creating
ohair@286 654 partName = RESPONSE;
ohair@286 655 }
ohair@286 656 } else {
ohair@286 657 partName = parameter.getPartName();
ohair@286 658 }*/
ohair@286 659
ohair@286 660 if (!partNames.contains(partName)) {
ohair@286 661 if (i++ > 0)
ohair@286 662 paramOrder.append(' ');
ohair@286 663 paramOrder.append(partName);
ohair@286 664 partNames.add(partName);
ohair@286 665 }
ohair@286 666 }
ohair@286 667 if (i > 1) {
ohair@286 668 operation.parameterOrder(paramOrder.toString());
ohair@286 669 }
ohair@286 670 }
ohair@286 671
ohair@286 672 /**
ohair@286 673 * Sorts the parameters for the method by their position
ohair@286 674 * @param method the {@link JavaMethod} used to sort the parameters
ohair@286 675 * @return the sorted {@link List} of parameters
ohair@286 676 */
ohair@286 677 protected List<ParameterImpl> sortMethodParameters(JavaMethodImpl method) {
ohair@286 678 Set<ParameterImpl> paramSet = new HashSet<ParameterImpl>();
ohair@286 679 List<ParameterImpl> sortedParams = new ArrayList<ParameterImpl>();
ohair@286 680 if (isRpcLit(method)) {
ohair@286 681 for (ParameterImpl param : method.getRequestParameters()) {
ohair@286 682 if (param instanceof WrapperParameter) {
ohair@286 683 paramSet.addAll(((WrapperParameter) param).getWrapperChildren());
ohair@286 684 } else {
ohair@286 685 paramSet.add(param);
ohair@286 686 }
ohair@286 687 }
ohair@286 688 for (ParameterImpl param : method.getResponseParameters()) {
ohair@286 689 if (param instanceof WrapperParameter) {
ohair@286 690 paramSet.addAll(((WrapperParameter) param).getWrapperChildren());
ohair@286 691 } else {
ohair@286 692 paramSet.add(param);
ohair@286 693 }
ohair@286 694 }
ohair@286 695 } else {
ohair@286 696 paramSet.addAll(method.getRequestParameters());
ohair@286 697 paramSet.addAll(method.getResponseParameters());
ohair@286 698 }
ohair@286 699 Iterator<ParameterImpl> params = paramSet.iterator();
ohair@286 700 if (paramSet.size() == 0)
ohair@286 701 return sortedParams;
ohair@286 702 ParameterImpl param = params.next();
ohair@286 703 sortedParams.add(param);
ohair@286 704 ParameterImpl sortedParam;
ohair@286 705 int pos;
ohair@286 706 for (int i = 1; i < paramSet.size(); i++) {
ohair@286 707 param = params.next();
ohair@286 708 for (pos = 0; pos < i; pos++) {
ohair@286 709 sortedParam = sortedParams.get(pos);
ohair@286 710 if (param.getIndex() == sortedParam.getIndex() &&
ohair@286 711 param instanceof WrapperParameter)
ohair@286 712 break;
ohair@286 713 if (param.getIndex() < sortedParam.getIndex()) {
ohair@286 714 break;
ohair@286 715 }
ohair@286 716 }
ohair@286 717 sortedParams.add(pos, param);
ohair@286 718 }
ohair@286 719 return sortedParams;
ohair@286 720 }
ohair@286 721
ohair@286 722 /**
ohair@286 723 * Determines if a parameter is associated with the message Body
ohair@286 724 * @param parameter the parameter to check
ohair@286 725 * @return true if the parameter is a <code>body</code> parameter
ohair@286 726 */
ohair@286 727 protected boolean isBodyParameter(ParameterImpl parameter) {
ohair@286 728 ParameterBinding paramBinding = parameter.getBinding();
ohair@286 729 return paramBinding.isBody();
ohair@286 730 }
ohair@286 731
ohair@286 732 protected boolean isHeaderParameter(ParameterImpl parameter) {
ohair@286 733 ParameterBinding paramBinding = parameter.getBinding();
ohair@286 734 return paramBinding.isHeader();
ohair@286 735 }
ohair@286 736
ohair@286 737 protected boolean isAttachmentParameter(ParameterImpl parameter) {
ohair@286 738 ParameterBinding paramBinding = parameter.getBinding();
ohair@286 739 return paramBinding.isAttachment();
ohair@286 740 }
ohair@286 741
ohair@286 742
ohair@286 743 /**
ohair@286 744 * Generates the Binding section of the WSDL
ohair@286 745 */
ohair@286 746 protected void generateBinding() {
ohair@286 747 Binding binding = serviceDefinitions.binding().name(model.getBoundPortTypeName().getLocalPart());
ohair@286 748 extension.addBindingExtension(binding);
ohair@286 749 binding.type(model.getPortTypeName());
ohair@286 750 boolean first = true;
ohair@286 751 for (JavaMethodImpl method : model.getJavaMethods()) {
ohair@286 752 if (first) {
ohair@286 753 SOAPBinding sBinding = method.getBinding();
ohair@286 754 SOAPVersion soapVersion = sBinding.getSOAPVersion();
ohair@286 755 if (soapVersion == SOAPVersion.SOAP_12) {
ohair@286 756 com.sun.xml.internal.ws.wsdl.writer.document.soap12.SOAPBinding soapBinding = binding.soap12Binding();
ohair@286 757 soapBinding.transport(this.binding.getBindingId().getTransport());
ohair@286 758 if (sBinding.getStyle().equals(Style.DOCUMENT))
ohair@286 759 soapBinding.style(DOCUMENT);
ohair@286 760 else
ohair@286 761 soapBinding.style(RPC);
ohair@286 762 } else {
ohair@286 763 com.sun.xml.internal.ws.wsdl.writer.document.soap.SOAPBinding soapBinding = binding.soapBinding();
ohair@286 764 soapBinding.transport(this.binding.getBindingId().getTransport());
ohair@286 765 if (sBinding.getStyle().equals(Style.DOCUMENT))
ohair@286 766 soapBinding.style(DOCUMENT);
ohair@286 767 else
ohair@286 768 soapBinding.style(RPC);
ohair@286 769 }
ohair@286 770 first = false;
ohair@286 771 }
ohair@286 772 if (this.binding.getBindingId().getSOAPVersion() == SOAPVersion.SOAP_12)
ohair@286 773 generateSOAP12BindingOperation(method, binding);
ohair@286 774 else
ohair@286 775 generateBindingOperation(method, binding);
ohair@286 776 }
ohair@286 777 }
ohair@286 778
ohair@286 779 protected void generateBindingOperation(JavaMethodImpl method, Binding binding) {
ohair@286 780 BindingOperationType operation = binding.operation().name(method.getOperationName());
ohair@286 781 extension.addBindingOperationExtension(operation, method);
ohair@286 782 String targetNamespace = model.getTargetNamespace();
ohair@286 783 QName requestMessage = new QName(targetNamespace, method.getOperationName());
ohair@286 784 List<ParameterImpl> bodyParams = new ArrayList<ParameterImpl>();
ohair@286 785 List<ParameterImpl> headerParams = new ArrayList<ParameterImpl>();
ohair@286 786 splitParameters(bodyParams, headerParams, method.getRequestParameters());
ohair@286 787 SOAPBinding soapBinding = method.getBinding();
ohair@286 788 operation.soapOperation().soapAction(soapBinding.getSOAPAction());
ohair@286 789
ohair@286 790 // input
ohair@286 791 TypedXmlWriter input = operation.input();
ohair@286 792 extension.addBindingOperationInputExtension(input, method);
ohair@286 793 BodyType body = input._element(Body.class);
ohair@286 794 boolean isRpc = soapBinding.getStyle().equals(Style.RPC);
ohair@286 795 if (soapBinding.getUse() == Use.LITERAL) {
ohair@286 796 body.use(LITERAL);
ohair@286 797 if (headerParams.size() > 0) {
ohair@286 798 if (bodyParams.size() > 0) {
ohair@286 799 ParameterImpl param = bodyParams.iterator().next();
ohair@286 800 if (isRpc) {
ohair@286 801 StringBuffer parts = new StringBuffer();
ohair@286 802 int i = 0;
ohair@286 803 for (ParameterImpl parameter : ((WrapperParameter) param).getWrapperChildren()) {
ohair@286 804 if (i++ > 0)
ohair@286 805 parts.append(' ');
ohair@286 806 parts.append(parameter.getPartName());
ohair@286 807 }
ohair@286 808 body.parts(parts.toString());
ohair@286 809 } else {
ohair@286 810 body.parts(param.getPartName());
ohair@286 811 }
ohair@286 812 } else {
ohair@286 813 body.parts("");
ohair@286 814 }
ohair@286 815 generateSOAPHeaders(input, headerParams, requestMessage);
ohair@286 816 }
ohair@286 817 if (isRpc) {
ohair@286 818 body.namespace(method.getRequestParameters().iterator().next().getName().getNamespaceURI());
ohair@286 819 }
ohair@286 820 } else {
ohair@286 821 // TODO localize this
ohair@286 822 throw new WebServiceException("encoded use is not supported");
ohair@286 823 }
ohair@286 824
ohair@286 825 if (method.getMEP() != MEP.ONE_WAY) {
ohair@286 826 boolean unwrappable = headerParams.size() == 0;
ohair@286 827 // output
ohair@286 828 bodyParams.clear();
ohair@286 829 headerParams.clear();
ohair@286 830 splitParameters(bodyParams, headerParams, method.getResponseParameters());
ohair@286 831 unwrappable = unwrappable ? headerParams.size() == 0 : unwrappable;
ohair@286 832 TypedXmlWriter output = operation.output();
ohair@286 833 extension.addBindingOperationOutputExtension(output, method);
ohair@286 834 body = output._element(Body.class);
ohair@286 835 body.use(LITERAL);
ohair@286 836 if (headerParams.size() > 0) {
ohair@286 837 String parts = "";
ohair@286 838 if (bodyParams.size() > 0) {
ohair@286 839 ParameterImpl param = bodyParams.iterator().hasNext() ? bodyParams.iterator().next() : null;
ohair@286 840 if (param != null) {
ohair@286 841 if (isRpc) {
ohair@286 842 int i = 0;
ohair@286 843 for (ParameterImpl parameter : ((WrapperParameter) param).getWrapperChildren()) {
ohair@286 844 if (i++ > 0)
ohair@286 845 parts += " ";
ohair@286 846 parts += parameter.getPartName();
ohair@286 847 }
ohair@286 848 } else {
ohair@286 849 parts = param.getPartName();
ohair@286 850 }
ohair@286 851 }
ohair@286 852 }
ohair@286 853 body.parts(parts);
ohair@286 854 QName responseMessage = new QName(targetNamespace, method.getResponseMessageName());
ohair@286 855 generateSOAPHeaders(output, headerParams, responseMessage);
ohair@286 856 }
ohair@286 857 if (isRpc) {
ohair@286 858 body.namespace(method.getRequestParameters().iterator().next().getName().getNamespaceURI());
ohair@286 859 }
ohair@286 860 }
ohair@286 861 for (CheckedExceptionImpl exception : method.getCheckedExceptions()) {
ohair@286 862 Fault fault = operation.fault().name(exception.getMessageName());
ohair@286 863 extension.addBindingOperationFaultExtension(fault, method, exception);
ohair@286 864 SOAPFault soapFault = fault._element(SOAPFault.class).name(exception.getMessageName());
ohair@286 865 soapFault.use(LITERAL);
ohair@286 866 }
ohair@286 867 }
ohair@286 868
ohair@286 869 protected void generateSOAP12BindingOperation(JavaMethodImpl method, Binding binding) {
ohair@286 870 BindingOperationType operation = binding.operation().name(method.getOperationName());
ohair@286 871 extension.addBindingOperationExtension(operation, method);
ohair@286 872 String targetNamespace = model.getTargetNamespace();
ohair@286 873 QName requestMessage = new QName(targetNamespace, method.getOperationName());
ohair@286 874 ArrayList<ParameterImpl> bodyParams = new ArrayList<ParameterImpl>();
ohair@286 875 ArrayList<ParameterImpl> headerParams = new ArrayList<ParameterImpl>();
ohair@286 876 splitParameters(bodyParams, headerParams, method.getRequestParameters());
ohair@286 877 SOAPBinding soapBinding = method.getBinding();
ohair@286 878
ohair@286 879 String soapAction = soapBinding.getSOAPAction();
ohair@286 880 if (soapAction != null) {
ohair@286 881 operation.soap12Operation().soapAction(soapAction);
ohair@286 882 }
ohair@286 883
ohair@286 884 // input
ohair@286 885 TypedXmlWriter input = operation.input();
ohair@286 886 extension.addBindingOperationInputExtension(input, method);
ohair@286 887 com.sun.xml.internal.ws.wsdl.writer.document.soap12.BodyType body = input._element(com.sun.xml.internal.ws.wsdl.writer.document.soap12.Body.class);
ohair@286 888 boolean isRpc = soapBinding.getStyle().equals(Style.RPC);
ohair@286 889 if (soapBinding.getUse().equals(Use.LITERAL)) {
ohair@286 890 body.use(LITERAL);
ohair@286 891 if (headerParams.size() > 0) {
ohair@286 892 if (bodyParams.size() > 0) {
ohair@286 893 ParameterImpl param = bodyParams.iterator().next();
ohair@286 894 if (isRpc) {
ohair@286 895 StringBuffer parts = new StringBuffer();
ohair@286 896 int i = 0;
ohair@286 897 for (ParameterImpl parameter : ((WrapperParameter) param).getWrapperChildren()) {
ohair@286 898 if (i++ > 0)
ohair@286 899 parts.append(' ');
ohair@286 900 parts.append(parameter.getPartName());
ohair@286 901 }
ohair@286 902 body.parts(parts.toString());
ohair@286 903 } else {
ohair@286 904 body.parts(param.getPartName());
ohair@286 905 }
ohair@286 906 } else {
ohair@286 907 body.parts("");
ohair@286 908 }
ohair@286 909 generateSOAP12Headers(input, headerParams, requestMessage);
ohair@286 910 }
ohair@286 911 if (isRpc) {
ohair@286 912 body.namespace(method.getRequestParameters().iterator().next().getName().getNamespaceURI());
ohair@286 913 }
ohair@286 914 } else {
ohair@286 915 // TODO localize this
ohair@286 916 throw new WebServiceException("encoded use is not supported");
ohair@286 917 }
ohair@286 918
ohair@286 919 if (method.getMEP() != MEP.ONE_WAY) {
ohair@286 920 // output
ohair@286 921 boolean unwrappable = headerParams.size() == 0;
ohair@286 922 bodyParams.clear();
ohair@286 923 headerParams.clear();
ohair@286 924 splitParameters(bodyParams, headerParams, method.getResponseParameters());
ohair@286 925 unwrappable = unwrappable ? headerParams.size() == 0 : unwrappable;
ohair@286 926 TypedXmlWriter output = operation.output();
ohair@286 927 extension.addBindingOperationOutputExtension(output, method);
ohair@286 928 body = output._element(com.sun.xml.internal.ws.wsdl.writer.document.soap12.Body.class);
ohair@286 929 body.use(LITERAL);
ohair@286 930 if (headerParams.size() > 0) {
ohair@286 931 if (bodyParams.size() > 0) {
ohair@286 932 ParameterImpl param = bodyParams.iterator().next();
ohair@286 933 if (isRpc) {
ohair@286 934 String parts = "";
ohair@286 935 int i = 0;
ohair@286 936 for (ParameterImpl parameter : ((WrapperParameter) param).getWrapperChildren()) {
ohair@286 937 if (i++ > 0)
ohair@286 938 parts += " ";
ohair@286 939 parts += parameter.getPartName();
ohair@286 940 }
ohair@286 941 body.parts(parts);
ohair@286 942 } else {
ohair@286 943 body.parts(param.getPartName());
ohair@286 944 }
ohair@286 945 } else {
ohair@286 946 body.parts("");
ohair@286 947 }
ohair@286 948 QName responseMessage = new QName(targetNamespace, method.getResponseMessageName());
ohair@286 949 generateSOAP12Headers(output, headerParams, responseMessage);
ohair@286 950 }
ohair@286 951 if (isRpc) {
ohair@286 952 body.namespace(method.getRequestParameters().iterator().next().getName().getNamespaceURI());
ohair@286 953 }
ohair@286 954 }
ohair@286 955 for (CheckedExceptionImpl exception : method.getCheckedExceptions()) {
ohair@286 956 Fault fault = operation.fault().name(exception.getMessageName());
ohair@286 957 extension.addBindingOperationFaultExtension(fault, method, exception);
ohair@286 958 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());
ohair@286 959 soapFault.use(LITERAL);
ohair@286 960 }
ohair@286 961 }
ohair@286 962
ohair@286 963 protected void splitParameters(List<ParameterImpl> bodyParams, List<ParameterImpl> headerParams, List<ParameterImpl> params) {
ohair@286 964 for (ParameterImpl parameter : params) {
ohair@286 965 if (isBodyParameter(parameter)) {
ohair@286 966 bodyParams.add(parameter);
ohair@286 967 } else {
ohair@286 968 headerParams.add(parameter);
ohair@286 969 }
ohair@286 970 }
ohair@286 971 }
ohair@286 972
ohair@286 973 protected void generateSOAPHeaders(TypedXmlWriter writer, List<ParameterImpl> parameters, QName message) {
ohair@286 974
ohair@286 975 for (ParameterImpl headerParam : parameters) {
ohair@286 976 Header header = writer._element(Header.class);
ohair@286 977 header.message(message);
ohair@286 978 header.part(headerParam.getPartName());
ohair@286 979 header.use(LITERAL);
ohair@286 980 }
ohair@286 981 }
ohair@286 982
ohair@286 983 protected void generateSOAP12Headers(TypedXmlWriter writer, List<ParameterImpl> parameters, QName message) {
ohair@286 984
ohair@286 985 for (ParameterImpl headerParam : parameters) {
ohair@286 986 com.sun.xml.internal.ws.wsdl.writer.document.soap12.Header header = writer._element(com.sun.xml.internal.ws.wsdl.writer.document.soap12.Header.class);
ohair@286 987 header.message(message);
ohair@286 988
ohair@286 989
ohair@286 990 header.part(headerParam.getPartName());
ohair@286 991 header.use(LITERAL);
ohair@286 992 }
ohair@286 993 }
ohair@286 994
ohair@286 995 /**
ohair@286 996 * Generates the Service section of the WSDL
ohair@286 997 */
ohair@286 998 protected void generateService() {
ohair@286 999 QName portQName = model.getPortName();
ohair@286 1000 QName serviceQName = model.getServiceQName();
ohair@286 1001 Service service = serviceDefinitions.service().name(serviceQName.getLocalPart());
ohair@286 1002 extension.addServiceExtension(service);
ohair@286 1003 Port port = service.port().name(portQName.getLocalPart());
ohair@286 1004 port.binding(model.getBoundPortTypeName());
ohair@286 1005 extension.addPortExtension(port);
ohair@286 1006 if (model.getJavaMethods().size() == 0)
ohair@286 1007 return;
ohair@286 1008
ohair@286 1009 if (this.binding.getBindingId().getSOAPVersion() == SOAPVersion.SOAP_12) {
ohair@286 1010 com.sun.xml.internal.ws.wsdl.writer.document.soap12.SOAPAddress address = port._element(com.sun.xml.internal.ws.wsdl.writer.document.soap12.SOAPAddress.class);
ohair@286 1011 address.location(endpointAddress);
ohair@286 1012 } else {
ohair@286 1013 SOAPAddress address = port._element(SOAPAddress.class);
ohair@286 1014 address.location(endpointAddress);
ohair@286 1015 }
ohair@286 1016 }
ohair@286 1017
ohair@286 1018 protected void generateInputMessage(Operation operation, JavaMethodImpl method) {
ohair@286 1019 ParamType paramType = operation.input();
ohair@286 1020 extension.addOperationInputExtension(paramType, method);
ohair@286 1021 // paramType.message(method.getOperation().getName());
ohair@286 1022 paramType.message(new QName(model.getTargetNamespace(), method.getRequestMessageName()));
ohair@286 1023 }
ohair@286 1024
ohair@286 1025 protected void generateOutputMessage(Operation operation, JavaMethodImpl method) {
ohair@286 1026 ParamType paramType = operation.output();
ohair@286 1027 extension.addOperationOutputExtension(paramType, method);
ohair@286 1028 // paramType.message(new QName(model.getTargetNamespace(), method.getOperation().getLocalName()+RESPONSE));
ohair@286 1029 paramType.message(new QName(model.getTargetNamespace(), method.getResponseMessageName()));
ohair@286 1030 }
ohair@286 1031
ohair@286 1032 /**
ohair@286 1033 * Creates the {@link Result} object used by JAXB to generate a schema for the
ohair@286 1034 * namesapceUri namespace.
ohair@286 1035 * @param namespaceUri The namespace for the schema being generated
ohair@286 1036 * @param suggestedFileName the JAXB suggested file name for the schema file
ohair@286 1037 * @return the {@link Result} for JAXB to generate the schema into
ohair@286 1038 * @throws java.io.IOException thrown if on IO error occurs
ohair@286 1039 */
ohair@286 1040 public Result createOutputFile(String namespaceUri, String suggestedFileName) throws IOException {
ohair@286 1041 Result result;
ohair@286 1042 if (namespaceUri == null) {
ohair@286 1043 return null;
ohair@286 1044 }
ohair@286 1045
ohair@286 1046 Holder<String> fileNameHolder = new Holder<String>();
ohair@286 1047 fileNameHolder.value = schemaPrefix + suggestedFileName;
ohair@286 1048 result = wsdlResolver.getSchemaOutput(namespaceUri, fileNameHolder);
ohair@286 1049 // System.out.println("schema file: "+fileNameHolder.value);
ohair@286 1050 // System.out.println("result: "+result);
ohair@286 1051 String schemaLoc;
ohair@286 1052 if (result == null)
ohair@286 1053 schemaLoc = fileNameHolder.value;
ohair@286 1054 else
ohair@286 1055 schemaLoc = relativize(result.getSystemId(), wsdlLocation);
ohair@286 1056 boolean isEmptyNs = namespaceUri.trim().equals("");
ohair@286 1057 if (!isEmptyNs) {
ohair@286 1058 com.sun.xml.internal.ws.wsdl.writer.document.xsd.Import _import = types.schema()._import();
ohair@286 1059 _import.namespace(namespaceUri);
ohair@286 1060 _import.schemaLocation(schemaLoc);
ohair@286 1061 }
ohair@286 1062 return result;
ohair@286 1063 }
ohair@286 1064
ohair@286 1065 private Result createInlineSchema(String namespaceUri, String suggestedFileName) throws IOException {
ohair@286 1066 Result result;
ohair@286 1067 if (namespaceUri.equals("")) {
ohair@286 1068 return null;
ohair@286 1069 }
ohair@286 1070
ohair@286 1071 // Holder<String> fileNameHolder = new Holder<String>();
ohair@286 1072 // fileNameHolder.value = schemaPrefix+suggestedFileName;
ohair@286 1073 // result = wsdlResolver.getSchemaOutput(namespaceUri, fileNameHolder);
ohair@286 1074 // if (result == null) {
ohair@286 1075 // // JAXB doesn't have to generate it, a schema is already available
ohair@286 1076 // com.sun.xml.internal.ws.wsdl.writer.document.xsd.Import _import = types.schema()._import().namespace(namespaceUri);
ohair@286 1077 // _import.schemaLocation(fileNameHolder.value);
ohair@286 1078 // } else {
ohair@286 1079 // Let JAXB write the schema directly into wsdl's TypedXmlWriter
ohair@286 1080 result = new TXWResult(types);
ohair@286 1081 result.setSystemId("");
ohair@286 1082 // }
ohair@286 1083 return result;
ohair@286 1084 }
ohair@286 1085
ohair@286 1086 /**
ohair@286 1087 * Relativizes a URI by using another URI (base URI.)
ohair@286 1088 *
ohair@286 1089 * <p>
ohair@286 1090 * For example, {@code relative("http://www.sun.com/abc/def","http://www.sun.com/pqr/stu") => "../abc/def"}
ohair@286 1091 *
ohair@286 1092 * <p>
ohair@286 1093 * This method only works on hierarchical URI's, not opaque URI's (refer to the
ohair@286 1094 * <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/net/URI.html">java.net.URI</a>
ohair@286 1095 * javadoc for complete definitions of these terms.
ohair@286 1096 *
ohair@286 1097 * <p>
ohair@286 1098 * This method will not normalize the relative URI.
ohair@286 1099 * @param uri the URI to relativize
ohair@286 1100 *
ohair@286 1101 *
ohair@286 1102 * @param baseUri the base URI to use for the relativization
ohair@286 1103 * @return the relative URI or the original URI if a relative one could not be computed
ohair@286 1104 */
ohair@286 1105 protected static String relativize(String uri, String baseUri) {
ohair@286 1106 try {
ohair@286 1107 assert uri != null;
ohair@286 1108
ohair@286 1109 if (baseUri == null) return uri;
ohair@286 1110
ohair@286 1111 URI theUri = new URI(escapeURI(uri));
ohair@286 1112 URI theBaseUri = new URI(escapeURI(baseUri));
ohair@286 1113
ohair@286 1114 if (theUri.isOpaque() || theBaseUri.isOpaque())
ohair@286 1115 return uri;
ohair@286 1116
ohair@286 1117 if (!equalsIgnoreCase(theUri.getScheme(), theBaseUri.getScheme()) ||
ohair@286 1118 !equal(theUri.getAuthority(), theBaseUri.getAuthority()))
ohair@286 1119 return uri;
ohair@286 1120
ohair@286 1121 String uriPath = theUri.getPath();
ohair@286 1122 String basePath = theBaseUri.getPath();
ohair@286 1123
ohair@286 1124 // normalize base path
ohair@286 1125 if (!basePath.endsWith("/")) {
ohair@286 1126 basePath = normalizeUriPath(basePath);
ohair@286 1127 }
ohair@286 1128
ohair@286 1129 if (uriPath.equals(basePath))
ohair@286 1130 return ".";
ohair@286 1131
ohair@286 1132 String relPath = calculateRelativePath(uriPath, basePath);
ohair@286 1133
ohair@286 1134 if (relPath == null)
ohair@286 1135 return uri; // recursion found no commonality in the two uris at all
ohair@286 1136 StringBuffer relUri = new StringBuffer();
ohair@286 1137 relUri.append(relPath);
ohair@286 1138 if (theUri.getQuery() != null)
ohair@286 1139 relUri.append('?').append(theUri.getQuery());
ohair@286 1140 if (theUri.getFragment() != null)
ohair@286 1141 relUri.append('#').append(theUri.getFragment());
ohair@286 1142
ohair@286 1143 return relUri.toString();
ohair@286 1144 } catch (URISyntaxException e) {
ohair@286 1145 throw new InternalError("Error escaping one of these uris:\n\t" + uri + "\n\t" + baseUri);
ohair@286 1146 }
ohair@286 1147 }
ohair@286 1148
ohair@286 1149 private static String calculateRelativePath(String uri, String base) {
ohair@286 1150 if (base == null) {
ohair@286 1151 return null;
ohair@286 1152 }
ohair@286 1153 if (uri.startsWith(base)) {
ohair@286 1154 return uri.substring(base.length());
ohair@286 1155 } else {
ohair@286 1156 return "../" + calculateRelativePath(uri, getParentUriPath(base));
ohair@286 1157 }
ohair@286 1158 }
ohair@286 1159
ohair@286 1160
ohair@286 1161 /**
ohair@286 1162 * Implements the SchemaOutputResolver used by JAXB to
ohair@286 1163 */
ohair@286 1164 protected class JAXWSOutputSchemaResolver extends SchemaOutputResolver {
ohair@286 1165 ArrayList<DOMResult> nonGlassfishSchemas = null;
ohair@286 1166
ohair@286 1167 /**
ohair@286 1168 * Creates the {@link Result} object used by JAXB to generate a schema for the
ohair@286 1169 * namesapceUri namespace.
ohair@286 1170 * @param namespaceUri The namespace for the schema being generated
ohair@286 1171 * @param suggestedFileName the JAXB suggested file name for the schema file
ohair@286 1172 * @return the {@link Result} for JAXB to generate the schema into
ohair@286 1173 * @throws java.io.IOException thrown if on IO error occurs
ohair@286 1174 */
ohair@286 1175 public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException {
ohair@286 1176 return inlineSchemas
ohair@286 1177 ? ((nonGlassfishSchemas != null) ? nonGlassfishSchemaResult(namespaceUri, suggestedFileName) : createInlineSchema(namespaceUri, suggestedFileName))
ohair@286 1178 // ? createInlineSchema(namespaceUri, suggestedFileName)
ohair@286 1179 : createOutputFile(namespaceUri, suggestedFileName);
ohair@286 1180 }
ohair@286 1181
ohair@286 1182 private Result nonGlassfishSchemaResult(String namespaceUri, String suggestedFileName) throws IOException {
ohair@286 1183 DOMResult result = new DOMResult();
ohair@286 1184 result.setSystemId("");
ohair@286 1185 nonGlassfishSchemas.add(result);
ohair@286 1186 return result;
ohair@286 1187 }
ohair@286 1188 }
ohair@286 1189
ohair@286 1190 private void register(WSDLGeneratorExtension h) {
ohair@286 1191 extensionHandlers.add(h);
ohair@286 1192 }
ohair@286 1193 }

mercurial