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

changeset 368
0989ad8c0860
parent 286
f50545b5e2f1
child 397
b99d7e355d4b
     1.1 --- a/src/share/jaxws_classes/com/sun/xml/internal/ws/wsdl/writer/WSDLGenerator.java	Thu Apr 04 19:05:24 2013 -0700
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/ws/wsdl/writer/WSDLGenerator.java	Tue Apr 09 14:51:13 2013 +0100
     1.3 @@ -1,5 +1,5 @@
     1.4  /*
     1.5 - * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
     1.6 + * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
     1.7   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.8   *
     1.9   * This code is free software; you can redistribute it and/or modify it
    1.10 @@ -28,6 +28,8 @@
    1.11  
    1.12  import static com.sun.xml.internal.bind.v2.schemagen.Util.*;
    1.13  
    1.14 +import com.oracle.webservices.internal.api.databinding.WSDLResolver;
    1.15 +
    1.16  import com.sun.xml.internal.txw2.TXW;
    1.17  import com.sun.xml.internal.txw2.TypedXmlWriter;
    1.18  import com.sun.xml.internal.txw2.output.ResultFactory;
    1.19 @@ -48,6 +50,7 @@
    1.20  import com.sun.xml.internal.ws.model.JavaMethodImpl;
    1.21  import com.sun.xml.internal.ws.model.ParameterImpl;
    1.22  import com.sun.xml.internal.ws.model.WrapperParameter;
    1.23 +import com.sun.xml.internal.ws.util.xml.XmlUtil;
    1.24  import com.sun.xml.internal.ws.wsdl.parser.SOAPConstants;
    1.25  import com.sun.xml.internal.ws.wsdl.parser.WSDLConstants;
    1.26  import com.sun.xml.internal.ws.wsdl.writer.document.Binding;
    1.27 @@ -68,11 +71,18 @@
    1.28  import com.sun.xml.internal.ws.wsdl.writer.document.soap.Header;
    1.29  import com.sun.xml.internal.ws.wsdl.writer.document.soap.SOAPAddress;
    1.30  import com.sun.xml.internal.ws.wsdl.writer.document.soap.SOAPFault;
    1.31 +import com.sun.xml.internal.ws.wsdl.writer.document.xsd.Schema;
    1.32  import com.sun.xml.internal.ws.spi.db.BindingContext;
    1.33  import com.sun.xml.internal.ws.spi.db.BindingHelper;
    1.34 +import com.sun.xml.internal.ws.spi.db.TypeInfo;
    1.35 +import com.sun.xml.internal.ws.spi.db.WrapperComposite;
    1.36  import com.sun.xml.internal.ws.util.RuntimeVersion;
    1.37  import com.sun.xml.internal.ws.policy.jaxws.PolicyWSDLGeneratorExtension;
    1.38  import com.sun.xml.internal.ws.encoding.soap.streaming.SOAPNamespaceConstants;
    1.39 +import com.sun.xml.internal.bind.v2.schemagen.xmlschema.Element;
    1.40 +import com.sun.xml.internal.bind.v2.schemagen.xmlschema.ComplexType;
    1.41 +import com.sun.xml.internal.bind.v2.schemagen.xmlschema.ExplicitGroup;
    1.42 +import com.sun.xml.internal.bind.v2.schemagen.xmlschema.LocalElement;
    1.43  
    1.44  import javax.jws.soap.SOAPBinding.Style;
    1.45  import javax.jws.soap.SOAPBinding.Use;
    1.46 @@ -95,6 +105,7 @@
    1.47  import java.net.URI;
    1.48  import java.net.URISyntaxException;
    1.49  import java.util.ArrayList;
    1.50 +import java.util.HashMap;
    1.51  import java.util.HashSet;
    1.52  import java.util.Iterator;
    1.53  import java.util.List;
    1.54 @@ -198,6 +209,7 @@
    1.55      private final Class implType;
    1.56  
    1.57      private boolean inlineSchemas;      // TODO
    1.58 +    private final boolean disableSecureXmlProcessing;
    1.59  
    1.60      /**
    1.61       * Creates the WSDLGenerator
    1.62 @@ -209,6 +221,22 @@
    1.63       */
    1.64      public WSDLGenerator(AbstractSEIModelImpl model, WSDLResolver wsdlResolver, WSBinding binding, Container container,
    1.65                           Class implType, boolean inlineSchemas, WSDLGeneratorExtension... extensions) {
    1.66 +        this(model, wsdlResolver, binding, container, implType, inlineSchemas, false, extensions);
    1.67 +    }
    1.68 +
    1.69 +    /**
    1.70 +     * Creates the WSDLGenerator
    1.71 +     * @param model The {@link AbstractSEIModelImpl} used to generate the WSDL
    1.72 +     * @param wsdlResolver The {@link WSDLResolver} to use resovle names while generating the WSDL
    1.73 +     * @param binding specifies which {@link javax.xml.ws.BindingType} to generate
    1.74 +     * @param disableSecureXmlProcessing specifies whether to disable the secure xml processing feature
    1.75 +     * @param extensions an array {@link WSDLGeneratorExtension} that will
    1.76 +     * be invoked to generate WSDL extensions
    1.77 +     */
    1.78 +    public WSDLGenerator(AbstractSEIModelImpl model, WSDLResolver wsdlResolver, WSBinding binding, Container container,
    1.79 +                         Class implType, boolean inlineSchemas, boolean disableSecureXmlProcessing,
    1.80 +                         WSDLGeneratorExtension... extensions) {
    1.81 +
    1.82          this.model = model;
    1.83          resolver = new JAXWSOutputSchemaResolver();
    1.84          this.wsdlResolver = wsdlResolver;
    1.85 @@ -217,6 +245,7 @@
    1.86          this.implType = implType;
    1.87          extensionHandlers = new ArrayList<WSDLGeneratorExtension>();
    1.88          this.inlineSchemas = inlineSchemas;
    1.89 +        this.disableSecureXmlProcessing = disableSecureXmlProcessing;
    1.90  
    1.91          // register handlers for default extensions
    1.92          register(new W3CAddressingWSDLGeneratorExtension());
    1.93 @@ -306,48 +335,59 @@
    1.94              this.serializer = serializer;
    1.95          }
    1.96  
    1.97 +        @Override
    1.98          public void startDocument() {
    1.99              serializer.startDocument();
   1.100              comment(new StringBuilder(VERSION_COMMENT));
   1.101              text(new StringBuilder("\n"));
   1.102          }
   1.103  
   1.104 +        @Override
   1.105          public void beginStartTag(String uri, String localName, String prefix) {
   1.106              serializer.beginStartTag(uri, localName, prefix);
   1.107          }
   1.108  
   1.109 +        @Override
   1.110          public void writeAttribute(String uri, String localName, String prefix, StringBuilder value) {
   1.111              serializer.writeAttribute(uri, localName, prefix, value);
   1.112          }
   1.113  
   1.114 +        @Override
   1.115          public void writeXmlns(String prefix, String uri) {
   1.116              serializer.writeXmlns(prefix, uri);
   1.117          }
   1.118  
   1.119 +        @Override
   1.120          public void endStartTag(String uri, String localName, String prefix) {
   1.121              serializer.endStartTag(uri, localName, prefix);
   1.122          }
   1.123  
   1.124 +        @Override
   1.125          public void endTag() {
   1.126              serializer.endTag();
   1.127          }
   1.128  
   1.129 +        @Override
   1.130          public void text(StringBuilder text) {
   1.131              serializer.text(text);
   1.132          }
   1.133  
   1.134 +        @Override
   1.135          public void cdata(StringBuilder text) {
   1.136              serializer.cdata(text);
   1.137          }
   1.138  
   1.139 +        @Override
   1.140          public void comment(StringBuilder comment) {
   1.141              serializer.comment(comment);
   1.142          }
   1.143  
   1.144 +        @Override
   1.145          public void endDocument() {
   1.146              serializer.endDocument();
   1.147          }
   1.148  
   1.149 +        @Override
   1.150          public void flush() {
   1.151              serializer.flush();
   1.152          }
   1.153 @@ -419,12 +459,11 @@
   1.154                  model.getBindingContext().generateSchema(resolver);
   1.155              } catch (IOException e) {
   1.156                  // TODO locallize and wrap this
   1.157 -                e.printStackTrace();
   1.158                  throw new WebServiceException(e.getMessage());
   1.159              }
   1.160          }
   1.161          if (resolver.nonGlassfishSchemas != null) {
   1.162 -            TransformerFactory tf = TransformerFactory.newInstance();
   1.163 +            TransformerFactory tf = XmlUtil.newTransformerFactory(!disableSecureXmlProcessing);
   1.164              try {
   1.165                  Transformer t = tf.newTransformer();
   1.166                  for (DOMResult xsd : resolver.nonGlassfishSchemas) {
   1.167 @@ -433,13 +472,69 @@
   1.168                      t.transform(new DOMSource(doc.getDocumentElement()), sax);
   1.169                  }
   1.170              } catch (TransformerConfigurationException e) {
   1.171 -                e.printStackTrace();
   1.172                  throw new WebServiceException(e.getMessage(), e);
   1.173              } catch (TransformerException e) {
   1.174 -                e.printStackTrace();
   1.175                  throw new WebServiceException(e.getMessage(), e);
   1.176              }
   1.177          }
   1.178 +        generateWrappers();
   1.179 +    }
   1.180 +
   1.181 +    void generateWrappers() {
   1.182 +        List<WrapperParameter> wrappers = new ArrayList<WrapperParameter>();
   1.183 +        for (JavaMethodImpl method : model.getJavaMethods()) {
   1.184 +            if(method.getBinding().isRpcLit()) continue;
   1.185 +            for (ParameterImpl p : method.getRequestParameters()) {
   1.186 +                if (p instanceof WrapperParameter) {
   1.187 +                    if (WrapperComposite.class.equals((((WrapperParameter)p).getTypeInfo().type))) {
   1.188 +                        wrappers.add((WrapperParameter)p);
   1.189 +                    }
   1.190 +                }
   1.191 +            }
   1.192 +            for (ParameterImpl p : method.getResponseParameters()) {
   1.193 +                if (p instanceof WrapperParameter) {
   1.194 +                    if (WrapperComposite.class.equals((((WrapperParameter)p).getTypeInfo().type))) {
   1.195 +                        wrappers.add((WrapperParameter)p);
   1.196 +                    }
   1.197 +                }
   1.198 +            }
   1.199 +        }
   1.200 +        if (wrappers.isEmpty()) return;
   1.201 +        HashMap<String, Schema> xsds = new HashMap<String, Schema>();
   1.202 +        for(WrapperParameter wp : wrappers) {
   1.203 +            String tns = wp.getName().getNamespaceURI();
   1.204 +            Schema xsd = xsds.get(tns);
   1.205 +            if (xsd == null) {
   1.206 +                xsd = types.schema();
   1.207 +                xsd.targetNamespace(tns);
   1.208 +                xsds.put(tns, xsd);
   1.209 +            }
   1.210 +            Element e =  xsd._element(Element.class);
   1.211 +            e._attribute("name", wp.getName().getLocalPart());
   1.212 +            e.type(wp.getName());
   1.213 +            ComplexType ct =  xsd._element(ComplexType.class);
   1.214 +            ct._attribute("name", wp.getName().getLocalPart());
   1.215 +            ExplicitGroup sq = ct.sequence();
   1.216 +            for (ParameterImpl p : wp.getWrapperChildren() ) {
   1.217 +                if (p.getBinding().isBody()) {
   1.218 +                    LocalElement le = sq.element();
   1.219 +                    le._attribute("name", p.getName().getLocalPart());
   1.220 +                    TypeInfo typeInfo = p.getItemType();
   1.221 +                    boolean repeatedElement = false;
   1.222 +                    if (typeInfo == null) {
   1.223 +                        typeInfo = p.getTypeInfo();
   1.224 +                    } else {
   1.225 +                        repeatedElement = true;
   1.226 +                    }
   1.227 +                    QName type = model.getBindingContext().getTypeName(typeInfo);
   1.228 +                    le.type(type);
   1.229 +                    if (repeatedElement) {
   1.230 +                        le.minOccurs(0);
   1.231 +                        le.maxOccurs("unbounded");
   1.232 +                    }
   1.233 +                }
   1.234 +            }
   1.235 +        }
   1.236      }
   1.237  
   1.238      /**
   1.239 @@ -484,15 +579,8 @@
   1.240              }
   1.241          }
   1.242          if (method.getMEP() != MEP.ONE_WAY) {
   1.243 -//            message = portDefinitions.message().name(method.getOperation().getName().getLocalPart()+RESPONSE);
   1.244              message = portDefinitions.message().name(method.getResponseMessageName());
   1.245              extension.addOutputMessageExtension(message, method);
   1.246 -            if (unwrappable) {
   1.247 -                for (ParameterImpl param : method.getResponseParameters()) {
   1.248 -                    if (isHeaderParameter(param))
   1.249 -                        unwrappable = false;
   1.250 -                }
   1.251 -            }
   1.252  
   1.253              for (ParameterImpl param : method.getResponseParameters()) {
   1.254                  if (isDoclit) {
   1.255 @@ -548,6 +636,8 @@
   1.256                  case ONE_WAY:
   1.257                      generateInputMessage(operation, method);
   1.258                      break;
   1.259 +                default:
   1.260 +                    break;
   1.261              }
   1.262              // faults
   1.263              for (CheckedExceptionImpl exception : method.getCheckedExceptions()) {
   1.264 @@ -601,7 +691,7 @@
   1.265       */
   1.266      protected void generateRpcParameterOrder(Operation operation, JavaMethodImpl method) {
   1.267          String partName;
   1.268 -        StringBuffer paramOrder = new StringBuffer();
   1.269 +        StringBuilder paramOrder = new StringBuilder();
   1.270          Set<String> partNames = new HashSet<String>();
   1.271          List<ParameterImpl> sortedParams = sortMethodParameters(method);
   1.272          int i = 0;
   1.273 @@ -629,10 +719,10 @@
   1.274       */
   1.275      protected void generateDocumentParameterOrder(Operation operation, JavaMethodImpl method) {
   1.276          String partName;
   1.277 -        StringBuffer paramOrder = new StringBuffer();
   1.278 +        StringBuilder paramOrder = new StringBuilder();
   1.279          Set<String> partNames = new HashSet<String>();
   1.280          List<ParameterImpl> sortedParams = sortMethodParameters(method);
   1.281 -        boolean isWrapperStyle = isWrapperStyle(method);
   1.282 +//        boolean isWrapperStyle = isWrapperStyle(method);
   1.283          int i = 0;
   1.284          for (ParameterImpl parameter : sortedParams) {
   1.285  //            System.out.println("param: "+parameter.getIndex()+" name: "+parameter.getName().getLocalPart());
   1.286 @@ -697,7 +787,7 @@
   1.287              paramSet.addAll(method.getResponseParameters());
   1.288          }
   1.289          Iterator<ParameterImpl> params = paramSet.iterator();
   1.290 -        if (paramSet.size() == 0)
   1.291 +        if (paramSet.isEmpty())
   1.292              return sortedParams;
   1.293          ParameterImpl param = params.next();
   1.294          sortedParams.add(param);
   1.295 @@ -744,23 +834,23 @@
   1.296       * Generates the Binding section of the WSDL
   1.297       */
   1.298      protected void generateBinding() {
   1.299 -        Binding binding = serviceDefinitions.binding().name(model.getBoundPortTypeName().getLocalPart());
   1.300 -        extension.addBindingExtension(binding);
   1.301 -        binding.type(model.getPortTypeName());
   1.302 +        Binding newBinding = serviceDefinitions.binding().name(model.getBoundPortTypeName().getLocalPart());
   1.303 +        extension.addBindingExtension(newBinding);
   1.304 +        newBinding.type(model.getPortTypeName());
   1.305          boolean first = true;
   1.306          for (JavaMethodImpl method : model.getJavaMethods()) {
   1.307              if (first) {
   1.308                  SOAPBinding sBinding = method.getBinding();
   1.309                  SOAPVersion soapVersion = sBinding.getSOAPVersion();
   1.310                  if (soapVersion == SOAPVersion.SOAP_12) {
   1.311 -                    com.sun.xml.internal.ws.wsdl.writer.document.soap12.SOAPBinding soapBinding = binding.soap12Binding();
   1.312 +                    com.sun.xml.internal.ws.wsdl.writer.document.soap12.SOAPBinding soapBinding = newBinding.soap12Binding();
   1.313                      soapBinding.transport(this.binding.getBindingId().getTransport());
   1.314                      if (sBinding.getStyle().equals(Style.DOCUMENT))
   1.315                          soapBinding.style(DOCUMENT);
   1.316                      else
   1.317                          soapBinding.style(RPC);
   1.318                  } else {
   1.319 -                    com.sun.xml.internal.ws.wsdl.writer.document.soap.SOAPBinding soapBinding = binding.soapBinding();
   1.320 +                    com.sun.xml.internal.ws.wsdl.writer.document.soap.SOAPBinding soapBinding = newBinding.soapBinding();
   1.321                      soapBinding.transport(this.binding.getBindingId().getTransport());
   1.322                      if (sBinding.getStyle().equals(Style.DOCUMENT))
   1.323                          soapBinding.style(DOCUMENT);
   1.324 @@ -770,9 +860,9 @@
   1.325                  first = false;
   1.326              }
   1.327              if (this.binding.getBindingId().getSOAPVersion() == SOAPVersion.SOAP_12)
   1.328 -                generateSOAP12BindingOperation(method, binding);
   1.329 +                generateSOAP12BindingOperation(method, newBinding);
   1.330              else
   1.331 -                generateBindingOperation(method, binding);
   1.332 +                generateBindingOperation(method, newBinding);
   1.333          }
   1.334      }
   1.335  
   1.336 @@ -798,7 +888,7 @@
   1.337                  if (bodyParams.size() > 0) {
   1.338                      ParameterImpl param = bodyParams.iterator().next();
   1.339                      if (isRpc) {
   1.340 -                        StringBuffer parts = new StringBuffer();
   1.341 +                        StringBuilder parts = new StringBuilder();
   1.342                          int i = 0;
   1.343                          for (ParameterImpl parameter : ((WrapperParameter) param).getWrapperChildren()) {
   1.344                              if (i++ > 0)
   1.345 @@ -823,34 +913,33 @@
   1.346          }
   1.347  
   1.348          if (method.getMEP() != MEP.ONE_WAY) {
   1.349 -            boolean unwrappable = headerParams.size() == 0;
   1.350              // output
   1.351              bodyParams.clear();
   1.352              headerParams.clear();
   1.353              splitParameters(bodyParams, headerParams, method.getResponseParameters());
   1.354 -            unwrappable = unwrappable ? headerParams.size() == 0 : unwrappable;
   1.355              TypedXmlWriter output = operation.output();
   1.356              extension.addBindingOperationOutputExtension(output, method);
   1.357              body = output._element(Body.class);
   1.358              body.use(LITERAL);
   1.359              if (headerParams.size() > 0) {
   1.360 -                String parts = "";
   1.361 +                StringBuilder parts = new StringBuilder();
   1.362                  if (bodyParams.size() > 0) {
   1.363                      ParameterImpl param = bodyParams.iterator().hasNext() ? bodyParams.iterator().next() : null;
   1.364                      if (param != null) {
   1.365                          if (isRpc) {
   1.366                              int i = 0;
   1.367                              for (ParameterImpl parameter : ((WrapperParameter) param).getWrapperChildren()) {
   1.368 -                                if (i++ > 0)
   1.369 -                                    parts += " ";
   1.370 -                                parts += parameter.getPartName();
   1.371 +                                if (i++ > 0) {
   1.372 +                                    parts.append(" ");
   1.373 +                                }
   1.374 +                                parts.append(parameter.getPartName());
   1.375                              }
   1.376                          } else {
   1.377 -                            parts = param.getPartName();
   1.378 +                            parts = new StringBuilder(param.getPartName());
   1.379                          }
   1.380                      }
   1.381                  }
   1.382 -                body.parts(parts);
   1.383 +                body.parts(parts.toString());
   1.384                  QName responseMessage = new QName(targetNamespace, method.getResponseMessageName());
   1.385                  generateSOAPHeaders(output, headerParams, responseMessage);
   1.386              }
   1.387 @@ -892,7 +981,7 @@
   1.388                  if (bodyParams.size() > 0) {
   1.389                      ParameterImpl param = bodyParams.iterator().next();
   1.390                      if (isRpc) {
   1.391 -                        StringBuffer parts = new StringBuffer();
   1.392 +                        StringBuilder parts = new StringBuilder();
   1.393                          int i = 0;
   1.394                          for (ParameterImpl parameter : ((WrapperParameter) param).getWrapperChildren()) {
   1.395                              if (i++ > 0)
   1.396 @@ -918,11 +1007,9 @@
   1.397  
   1.398          if (method.getMEP() != MEP.ONE_WAY) {
   1.399              // output
   1.400 -            boolean unwrappable = headerParams.size() == 0;
   1.401              bodyParams.clear();
   1.402              headerParams.clear();
   1.403              splitParameters(bodyParams, headerParams, method.getResponseParameters());
   1.404 -            unwrappable = unwrappable ? headerParams.size() == 0 : unwrappable;
   1.405              TypedXmlWriter output = operation.output();
   1.406              extension.addBindingOperationOutputExtension(output, method);
   1.407              body = output._element(com.sun.xml.internal.ws.wsdl.writer.document.soap12.Body.class);
   1.408 @@ -931,14 +1018,15 @@
   1.409                  if (bodyParams.size() > 0) {
   1.410                      ParameterImpl param = bodyParams.iterator().next();
   1.411                      if (isRpc) {
   1.412 -                        String parts = "";
   1.413 +                        StringBuilder parts = new StringBuilder();
   1.414                          int i = 0;
   1.415                          for (ParameterImpl parameter : ((WrapperParameter) param).getWrapperChildren()) {
   1.416 -                            if (i++ > 0)
   1.417 -                                parts += " ";
   1.418 -                            parts += parameter.getPartName();
   1.419 +                            if (i++ > 0) {
   1.420 +                                parts.append(" ");
   1.421 +                            }
   1.422 +                            parts.append(parameter.getPartName());
   1.423                          }
   1.424 -                        body.parts(parts);
   1.425 +                        body.parts(parts.toString());
   1.426                      } else {
   1.427                          body.parts(param.getPartName());
   1.428                      }
   1.429 @@ -1003,7 +1091,7 @@
   1.430          Port port = service.port().name(portQName.getLocalPart());
   1.431          port.binding(model.getBoundPortTypeName());
   1.432          extension.addPortExtension(port);
   1.433 -        if (model.getJavaMethods().size() == 0)
   1.434 +        if (model.getJavaMethods().isEmpty())
   1.435              return;
   1.436  
   1.437          if (this.binding.getBindingId().getSOAPVersion() == SOAPVersion.SOAP_12) {
   1.438 @@ -1133,7 +1221,7 @@
   1.439  
   1.440              if (relPath == null)
   1.441                  return uri; // recursion found no commonality in the two uris at all
   1.442 -            StringBuffer relUri = new StringBuffer();
   1.443 +            StringBuilder relUri = new StringBuilder();
   1.444              relUri.append(relPath);
   1.445              if (theUri.getQuery() != null)
   1.446                  relUri.append('?').append(theUri.getQuery());
   1.447 @@ -1172,6 +1260,7 @@
   1.448           * @return the {@link Result} for JAXB to generate the schema into
   1.449           * @throws java.io.IOException thrown if on IO error occurs
   1.450           */
   1.451 +        @Override
   1.452          public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException {
   1.453              return inlineSchemas
   1.454                      ? ((nonGlassfishSchemas != null) ? nonGlassfishSchemaResult(namespaceUri, suggestedFileName) : createInlineSchema(namespaceUri, suggestedFileName))

mercurial