src/share/jaxws_classes/com/sun/xml/internal/ws/server/SDDocumentImpl.java

Thu, 31 Aug 2017 15:18:52 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:18:52 +0800
changeset 637
9c07ef4934dd
parent 397
b99d7e355d4b
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.xml.internal.ws.server;
aoqi@0 27
aoqi@0 28 import com.sun.istack.internal.Nullable;
aoqi@0 29 import com.sun.xml.internal.ws.api.server.*;
aoqi@0 30 import com.sun.xml.internal.ws.api.streaming.XMLStreamWriterFactory;
aoqi@0 31 import com.sun.xml.internal.ws.streaming.XMLStreamReaderUtil;
aoqi@0 32 import com.sun.xml.internal.ws.wsdl.SDDocumentResolver;
aoqi@0 33 import com.sun.xml.internal.ws.util.RuntimeVersion;
aoqi@0 34 import com.sun.xml.internal.ws.util.xml.XMLStreamReaderToXMLStreamWriter;
aoqi@0 35 import com.sun.xml.internal.ws.wsdl.parser.ParserUtil;
aoqi@0 36 import com.sun.xml.internal.ws.wsdl.parser.WSDLConstants;
aoqi@0 37 import com.sun.xml.internal.ws.wsdl.writer.DocumentLocationResolver;
aoqi@0 38 import com.sun.xml.internal.ws.wsdl.writer.WSDLPatcher;
aoqi@0 39
aoqi@0 40 import javax.xml.namespace.QName;
aoqi@0 41 import javax.xml.stream.*;
aoqi@0 42 import javax.xml.ws.WebServiceException;
aoqi@0 43 import java.io.IOException;
aoqi@0 44 import java.io.OutputStream;
aoqi@0 45 import java.net.MalformedURLException;
aoqi@0 46 import java.net.URL;
aoqi@0 47 import java.util.HashSet;
aoqi@0 48 import java.util.List;
aoqi@0 49 import java.util.Set;
aoqi@0 50
aoqi@0 51 /**
aoqi@0 52 * {@link SDDocument} implmentation.
aoqi@0 53 *
aoqi@0 54 * <p>
aoqi@0 55 * This extends from {@link SDDocumentSource} so that
aoqi@0 56 * JAX-WS server runtime code can use {@link SDDocument}
aoqi@0 57 * as {@link SDDocumentSource}.
aoqi@0 58 *
aoqi@0 59 * @author Kohsuke Kawaguchi
aoqi@0 60 * @author Jitendra Kotamraju
aoqi@0 61 */
aoqi@0 62 public class SDDocumentImpl extends SDDocumentSource implements SDDocument {
aoqi@0 63
aoqi@0 64 private static final String NS_XSD = "http://www.w3.org/2001/XMLSchema";
aoqi@0 65 private static final QName SCHEMA_INCLUDE_QNAME = new QName(NS_XSD, "include");
aoqi@0 66 private static final QName SCHEMA_IMPORT_QNAME = new QName(NS_XSD, "import");
aoqi@0 67 private static final QName SCHEMA_REDEFINE_QNAME = new QName(NS_XSD, "redefine");
aoqi@0 68 private static final String VERSION_COMMENT =
aoqi@0 69 " Published by JAX-WS RI (http://jax-ws.java.net). RI's version is "+RuntimeVersion.VERSION+". ";
aoqi@0 70
aoqi@0 71 private final QName rootName;
aoqi@0 72 private final SDDocumentSource source;
aoqi@0 73
aoqi@0 74 /**
aoqi@0 75 * Set when {@link ServiceDefinitionImpl} is constructed.
aoqi@0 76 */
aoqi@0 77 @Nullable List<SDDocumentFilter> filters;
aoqi@0 78 @Nullable SDDocumentResolver sddocResolver;
aoqi@0 79
aoqi@0 80
aoqi@0 81 /**
aoqi@0 82 * The original system ID of this document.
aoqi@0 83 *
aoqi@0 84 * When this document contains relative references to other resources,
aoqi@0 85 * this field is used to find which {@link com.sun.xml.internal.ws.server.SDDocumentImpl} it refers to.
aoqi@0 86 *
aoqi@0 87 * Must not be null.
aoqi@0 88 */
aoqi@0 89 private final URL url;
aoqi@0 90 private final Set<String> imports;
aoqi@0 91
aoqi@0 92 /**
aoqi@0 93 * Creates {@link SDDocument} from {@link SDDocumentSource}.
aoqi@0 94 * @param src WSDL document infoset
aoqi@0 95 * @param serviceName wsdl:service name
aoqi@0 96 * @param portTypeName
aoqi@0 97 * The information about the port of {@link WSEndpoint} to which this document is built for.
aoqi@0 98 * These values are used to determine which document is the concrete and abstract WSDLs
aoqi@0 99 * for this endpoint.
aoqi@0 100 *
aoqi@0 101 * @return null
aoqi@0 102 * Always non-null.
aoqi@0 103 */
aoqi@0 104 public static SDDocumentImpl create(SDDocumentSource src, QName serviceName, QName portTypeName) {
aoqi@0 105 URL systemId = src.getSystemId();
aoqi@0 106
aoqi@0 107 try {
aoqi@0 108 // RuntimeWSDLParser parser = new RuntimeWSDLParser(null);
aoqi@0 109 XMLStreamReader reader = src.read();
aoqi@0 110 try {
aoqi@0 111 XMLStreamReaderUtil.nextElementContent(reader);
aoqi@0 112
aoqi@0 113 QName rootName = reader.getName();
aoqi@0 114 if(rootName.equals(WSDLConstants.QNAME_SCHEMA)) {
aoqi@0 115 String tns = ParserUtil.getMandatoryNonEmptyAttribute(reader, WSDLConstants.ATTR_TNS);
aoqi@0 116 Set<String> importedDocs = new HashSet<String>();
aoqi@0 117 while (XMLStreamReaderUtil.nextContent(reader) != XMLStreamConstants.END_DOCUMENT) {
aoqi@0 118 if (reader.getEventType() != XMLStreamConstants.START_ELEMENT)
aoqi@0 119 continue;
aoqi@0 120 QName name = reader.getName();
aoqi@0 121 if (SCHEMA_INCLUDE_QNAME.equals(name) || SCHEMA_IMPORT_QNAME.equals(name) ||
aoqi@0 122 SCHEMA_REDEFINE_QNAME.equals(name)) {
aoqi@0 123 String importedDoc = reader.getAttributeValue(null, "schemaLocation");
aoqi@0 124 if (importedDoc != null) {
aoqi@0 125 importedDocs.add(new URL(src.getSystemId(), importedDoc).toString());
aoqi@0 126 }
aoqi@0 127 }
aoqi@0 128 }
aoqi@0 129 return new SchemaImpl(rootName,systemId,src,tns,importedDocs);
aoqi@0 130 } else if (rootName.equals(WSDLConstants.QNAME_DEFINITIONS)) {
aoqi@0 131 String tns = ParserUtil.getMandatoryNonEmptyAttribute(reader, WSDLConstants.ATTR_TNS);
aoqi@0 132
aoqi@0 133 boolean hasPortType = false;
aoqi@0 134 boolean hasService = false;
aoqi@0 135 Set<String> importedDocs = new HashSet<String>();
aoqi@0 136 Set<QName> allServices = new HashSet<QName>();
aoqi@0 137
aoqi@0 138 // if WSDL, parse more
aoqi@0 139 while (XMLStreamReaderUtil.nextContent(reader) != XMLStreamConstants.END_DOCUMENT) {
aoqi@0 140 if(reader.getEventType() != XMLStreamConstants.START_ELEMENT)
aoqi@0 141 continue;
aoqi@0 142
aoqi@0 143 QName name = reader.getName();
aoqi@0 144 if (WSDLConstants.QNAME_PORT_TYPE.equals(name)) {
aoqi@0 145 String pn = ParserUtil.getMandatoryNonEmptyAttribute(reader, WSDLConstants.ATTR_NAME);
aoqi@0 146 if (portTypeName != null) {
aoqi@0 147 if(portTypeName.getLocalPart().equals(pn)&&portTypeName.getNamespaceURI().equals(tns)) {
aoqi@0 148 hasPortType = true;
aoqi@0 149 }
aoqi@0 150 }
aoqi@0 151 } else if (WSDLConstants.QNAME_SERVICE.equals(name)) {
aoqi@0 152 String sn = ParserUtil.getMandatoryNonEmptyAttribute(reader, WSDLConstants.ATTR_NAME);
aoqi@0 153 QName sqn = new QName(tns,sn);
aoqi@0 154 allServices.add(sqn);
aoqi@0 155 if(serviceName.equals(sqn)) {
aoqi@0 156 hasService = true;
aoqi@0 157 }
aoqi@0 158 } else if (WSDLConstants.QNAME_IMPORT.equals(name)) {
aoqi@0 159 String importedDoc = reader.getAttributeValue(null, "location");
aoqi@0 160 if (importedDoc != null) {
aoqi@0 161 importedDocs.add(new URL(src.getSystemId(), importedDoc).toString());
aoqi@0 162 }
aoqi@0 163 } else if (SCHEMA_INCLUDE_QNAME.equals(name) || SCHEMA_IMPORT_QNAME.equals(name) ||
aoqi@0 164 SCHEMA_REDEFINE_QNAME.equals(name)) {
aoqi@0 165 String importedDoc = reader.getAttributeValue(null, "schemaLocation");
aoqi@0 166 if (importedDoc != null) {
aoqi@0 167 importedDocs.add(new URL(src.getSystemId(), importedDoc).toString());
aoqi@0 168 }
aoqi@0 169 }
aoqi@0 170 }
aoqi@0 171 return new WSDLImpl(
aoqi@0 172 rootName,systemId,src,tns,hasPortType,hasService,importedDocs,allServices);
aoqi@0 173 } else {
aoqi@0 174 return new SDDocumentImpl(rootName,systemId,src);
aoqi@0 175 }
aoqi@0 176 } finally {
aoqi@0 177 reader.close();
aoqi@0 178 }
aoqi@0 179 } catch (WebServiceException e) {
aoqi@0 180 throw new ServerRtException("runtime.parser.wsdl", systemId,e);
aoqi@0 181 } catch (IOException e) {
aoqi@0 182 throw new ServerRtException("runtime.parser.wsdl", systemId,e);
aoqi@0 183 } catch (XMLStreamException e) {
aoqi@0 184 throw new ServerRtException("runtime.parser.wsdl", systemId,e);
aoqi@0 185 }
aoqi@0 186 }
aoqi@0 187
aoqi@0 188 protected SDDocumentImpl(QName rootName, URL url, SDDocumentSource source) {
aoqi@0 189 this(rootName, url, source, new HashSet<String>());
aoqi@0 190 }
aoqi@0 191
aoqi@0 192 protected SDDocumentImpl(QName rootName, URL url, SDDocumentSource source, Set<String> imports) {
aoqi@0 193 if (url == null) {
aoqi@0 194 throw new IllegalArgumentException("Cannot construct SDDocument with null URL.");
aoqi@0 195 }
aoqi@0 196 this.rootName = rootName;
aoqi@0 197 this.source = source;
aoqi@0 198 this.url = url;
aoqi@0 199 this.imports = imports;
aoqi@0 200 }
aoqi@0 201
aoqi@0 202 void setFilters(List<SDDocumentFilter> filters) {
aoqi@0 203 this.filters = filters;
aoqi@0 204 }
aoqi@0 205
aoqi@0 206 void setResolver(SDDocumentResolver sddocResolver) {
aoqi@0 207 this.sddocResolver = sddocResolver;
aoqi@0 208 }
aoqi@0 209
aoqi@0 210 public QName getRootName() {
aoqi@0 211 return rootName;
aoqi@0 212 }
aoqi@0 213
aoqi@0 214 public boolean isWSDL() {
aoqi@0 215 return false;
aoqi@0 216 }
aoqi@0 217
aoqi@0 218 public boolean isSchema() {
aoqi@0 219 return false;
aoqi@0 220 }
aoqi@0 221
aoqi@0 222 public URL getURL() {
aoqi@0 223 return url;
aoqi@0 224 }
aoqi@0 225
aoqi@0 226 public XMLStreamReader read(XMLInputFactory xif) throws IOException, XMLStreamException {
aoqi@0 227 return source.read(xif);
aoqi@0 228 }
aoqi@0 229
aoqi@0 230 public XMLStreamReader read() throws IOException, XMLStreamException {
aoqi@0 231 return source.read();
aoqi@0 232 }
aoqi@0 233
aoqi@0 234 public URL getSystemId() {
aoqi@0 235 return url;
aoqi@0 236 }
aoqi@0 237
aoqi@0 238 public Set<String> getImports() {
aoqi@0 239 return imports;
aoqi@0 240 }
aoqi@0 241
aoqi@0 242 public void writeTo(OutputStream os) throws IOException {
aoqi@0 243 XMLStreamWriter w = null;
aoqi@0 244 try {
aoqi@0 245 //generate the WSDL with utf-8 encoding and XML version 1.0
aoqi@0 246 w = XMLStreamWriterFactory.create(os, "UTF-8");
aoqi@0 247 w.writeStartDocument("UTF-8", "1.0");
aoqi@0 248 new XMLStreamReaderToXMLStreamWriter().bridge(source.read(), w);
aoqi@0 249 w.writeEndDocument();
aoqi@0 250 } catch (XMLStreamException e) {
aoqi@0 251 IOException ioe = new IOException(e.getMessage());
aoqi@0 252 ioe.initCause(e);
aoqi@0 253 throw ioe;
aoqi@0 254 } finally {
aoqi@0 255 try {
aoqi@0 256 if (w != null)
aoqi@0 257 w.close();
aoqi@0 258 } catch (XMLStreamException e) {
aoqi@0 259 IOException ioe = new IOException(e.getMessage());
aoqi@0 260 ioe.initCause(e);
aoqi@0 261 throw ioe;
aoqi@0 262 }
aoqi@0 263 }
aoqi@0 264 }
aoqi@0 265
aoqi@0 266
aoqi@0 267 public void writeTo(PortAddressResolver portAddressResolver, DocumentAddressResolver resolver, OutputStream os) throws IOException {
aoqi@0 268 XMLStreamWriter w = null;
aoqi@0 269 try {
aoqi@0 270 //generate the WSDL with utf-8 encoding and XML version 1.0
aoqi@0 271 w = XMLStreamWriterFactory.create(os, "UTF-8");
aoqi@0 272 w.writeStartDocument("UTF-8", "1.0");
aoqi@0 273 writeTo(portAddressResolver,resolver,w);
aoqi@0 274 w.writeEndDocument();
aoqi@0 275 } catch (XMLStreamException e) {
aoqi@0 276 IOException ioe = new IOException(e.getMessage());
aoqi@0 277 ioe.initCause(e);
aoqi@0 278 throw ioe;
aoqi@0 279 } finally {
aoqi@0 280 try {
aoqi@0 281 if (w != null)
aoqi@0 282 w.close();
aoqi@0 283 } catch (XMLStreamException e) {
aoqi@0 284 IOException ioe = new IOException(e.getMessage());
aoqi@0 285 ioe.initCause(e);
aoqi@0 286 throw ioe;
aoqi@0 287 }
aoqi@0 288 }
aoqi@0 289 }
aoqi@0 290
aoqi@0 291 public void writeTo(PortAddressResolver portAddressResolver, DocumentAddressResolver resolver, XMLStreamWriter out) throws XMLStreamException, IOException {
aoqi@0 292 if (filters != null) {
aoqi@0 293 for (SDDocumentFilter f : filters) {
aoqi@0 294 out = f.filter(this,out);
aoqi@0 295 }
aoqi@0 296 }
aoqi@0 297
aoqi@0 298 XMLStreamReader xsr = source.read();
aoqi@0 299 try {
aoqi@0 300 out.writeComment(VERSION_COMMENT);
aoqi@0 301 new WSDLPatcher(portAddressResolver, new DocumentLocationResolverImpl(resolver)).bridge(xsr,out);
aoqi@0 302 } finally {
aoqi@0 303 xsr.close();
aoqi@0 304 }
aoqi@0 305 }
aoqi@0 306
aoqi@0 307
aoqi@0 308 /**
aoqi@0 309 * {@link SDDocument.Schema} implementation.
aoqi@0 310 *
aoqi@0 311 * @author Kohsuke Kawaguchi
aoqi@0 312 */
aoqi@0 313 private static final class SchemaImpl extends SDDocumentImpl implements SDDocument.Schema {
aoqi@0 314 private final String targetNamespace;
aoqi@0 315
aoqi@0 316 public SchemaImpl(QName rootName, URL url, SDDocumentSource source, String targetNamespace,
aoqi@0 317 Set<String> imports) {
aoqi@0 318 super(rootName, url, source, imports);
aoqi@0 319 this.targetNamespace = targetNamespace;
aoqi@0 320 }
aoqi@0 321
aoqi@0 322 public String getTargetNamespace() {
aoqi@0 323 return targetNamespace;
aoqi@0 324 }
aoqi@0 325
aoqi@0 326 public boolean isSchema() {
aoqi@0 327 return true;
aoqi@0 328 }
aoqi@0 329 }
aoqi@0 330
aoqi@0 331
aoqi@0 332 private static final class WSDLImpl extends SDDocumentImpl implements SDDocument.WSDL {
aoqi@0 333 private final String targetNamespace;
aoqi@0 334 private final boolean hasPortType;
aoqi@0 335 private final boolean hasService;
aoqi@0 336 private final Set<QName> allServices;
aoqi@0 337
aoqi@0 338 public WSDLImpl(QName rootName, URL url, SDDocumentSource source, String targetNamespace, boolean hasPortType,
aoqi@0 339 boolean hasService, Set<String> imports,Set<QName> allServices) {
aoqi@0 340 super(rootName, url, source, imports);
aoqi@0 341 this.targetNamespace = targetNamespace;
aoqi@0 342 this.hasPortType = hasPortType;
aoqi@0 343 this.hasService = hasService;
aoqi@0 344 this.allServices = allServices;
aoqi@0 345 }
aoqi@0 346
aoqi@0 347 public String getTargetNamespace() {
aoqi@0 348 return targetNamespace;
aoqi@0 349 }
aoqi@0 350
aoqi@0 351 public boolean hasPortType() {
aoqi@0 352 return hasPortType;
aoqi@0 353 }
aoqi@0 354
aoqi@0 355 public boolean hasService() {
aoqi@0 356 return hasService;
aoqi@0 357 }
aoqi@0 358
aoqi@0 359 public Set<QName> getAllServices() {
aoqi@0 360 return allServices;
aoqi@0 361 }
aoqi@0 362
aoqi@0 363 public boolean isWSDL() {
aoqi@0 364 return true;
aoqi@0 365 }
aoqi@0 366 }
aoqi@0 367
aoqi@0 368 private class DocumentLocationResolverImpl implements DocumentLocationResolver {
aoqi@0 369 private DocumentAddressResolver delegate;
aoqi@0 370
aoqi@0 371 DocumentLocationResolverImpl(DocumentAddressResolver delegate) {
aoqi@0 372 this.delegate = delegate;
aoqi@0 373 }
aoqi@0 374
aoqi@0 375 public String getLocationFor(String namespaceURI, String systemId) {
aoqi@0 376 if (sddocResolver == null) {
aoqi@0 377 return systemId;
aoqi@0 378 }
aoqi@0 379 try {
aoqi@0 380 URL ref = new URL(getURL(), systemId);
aoqi@0 381 SDDocument refDoc = sddocResolver.resolve(ref.toExternalForm());
aoqi@0 382 if (refDoc == null)
aoqi@0 383 return systemId; // not something we know. just leave it as is.
aoqi@0 384
aoqi@0 385 return delegate.getRelativeAddressFor(SDDocumentImpl.this, refDoc);
aoqi@0 386 } catch (MalformedURLException mue) {
aoqi@0 387 return null;
aoqi@0 388 }
aoqi@0 389 }
aoqi@0 390 }
aoqi@0 391
aoqi@0 392 }

mercurial