1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/ws/server/SDDocumentImpl.java Tue Mar 06 16:09:35 2012 -0800 1.3 @@ -0,0 +1,392 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.xml.internal.ws.server; 1.30 + 1.31 +import com.sun.istack.internal.Nullable; 1.32 +import com.sun.xml.internal.ws.api.server.*; 1.33 +import com.sun.xml.internal.ws.api.streaming.XMLStreamWriterFactory; 1.34 +import com.sun.xml.internal.ws.streaming.XMLStreamReaderUtil; 1.35 +import com.sun.xml.internal.ws.wsdl.SDDocumentResolver; 1.36 +import com.sun.xml.internal.ws.util.RuntimeVersion; 1.37 +import com.sun.xml.internal.ws.util.xml.XMLStreamReaderToXMLStreamWriter; 1.38 +import com.sun.xml.internal.ws.wsdl.parser.ParserUtil; 1.39 +import com.sun.xml.internal.ws.wsdl.parser.WSDLConstants; 1.40 +import com.sun.xml.internal.ws.wsdl.writer.DocumentLocationResolver; 1.41 +import com.sun.xml.internal.ws.wsdl.writer.WSDLPatcher; 1.42 + 1.43 +import javax.xml.namespace.QName; 1.44 +import javax.xml.stream.*; 1.45 +import javax.xml.ws.WebServiceException; 1.46 +import java.io.IOException; 1.47 +import java.io.OutputStream; 1.48 +import java.net.MalformedURLException; 1.49 +import java.net.URL; 1.50 +import java.util.HashSet; 1.51 +import java.util.List; 1.52 +import java.util.Set; 1.53 + 1.54 +/** 1.55 + * {@link SDDocument} implmentation. 1.56 + * 1.57 + * <p> 1.58 + * This extends from {@link SDDocumentSource} so that 1.59 + * JAX-WS server runtime code can use {@link SDDocument} 1.60 + * as {@link SDDocumentSource}. 1.61 + * 1.62 + * @author Kohsuke Kawaguchi 1.63 + * @author Jitendra Kotamraju 1.64 + */ 1.65 +public class SDDocumentImpl extends SDDocumentSource implements SDDocument { 1.66 + 1.67 + private static final String NS_XSD = "http://www.w3.org/2001/XMLSchema"; 1.68 + private static final QName SCHEMA_INCLUDE_QNAME = new QName(NS_XSD, "include"); 1.69 + private static final QName SCHEMA_IMPORT_QNAME = new QName(NS_XSD, "import"); 1.70 + private static final QName SCHEMA_REDEFINE_QNAME = new QName(NS_XSD, "redefine"); 1.71 + private static final String VERSION_COMMENT = 1.72 + " Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is "+RuntimeVersion.VERSION+". "; 1.73 + 1.74 + private final QName rootName; 1.75 + private final SDDocumentSource source; 1.76 + 1.77 + /** 1.78 + * Set when {@link ServiceDefinitionImpl} is constructed. 1.79 + */ 1.80 + @Nullable List<SDDocumentFilter> filters; 1.81 + @Nullable SDDocumentResolver sddocResolver; 1.82 + 1.83 + 1.84 + /** 1.85 + * The original system ID of this document. 1.86 + * 1.87 + * When this document contains relative references to other resources, 1.88 + * this field is used to find which {@link com.sun.xml.internal.ws.server.SDDocumentImpl} it refers to. 1.89 + * 1.90 + * Must not be null. 1.91 + */ 1.92 + private final URL url; 1.93 + private final Set<String> imports; 1.94 + 1.95 + /** 1.96 + * Creates {@link SDDocument} from {@link SDDocumentSource}. 1.97 + * @param src WSDL document infoset 1.98 + * @param serviceName wsdl:service name 1.99 + * @param portTypeName 1.100 + * The information about the port of {@link WSEndpoint} to which this document is built for. 1.101 + * These values are used to determine which document is the concrete and abstract WSDLs 1.102 + * for this endpoint. 1.103 + * 1.104 + * @return null 1.105 + * Always non-null. 1.106 + */ 1.107 + public static SDDocumentImpl create(SDDocumentSource src, QName serviceName, QName portTypeName) { 1.108 + URL systemId = src.getSystemId(); 1.109 + 1.110 + try { 1.111 + // RuntimeWSDLParser parser = new RuntimeWSDLParser(null); 1.112 + XMLStreamReader reader = src.read(); 1.113 + try { 1.114 + XMLStreamReaderUtil.nextElementContent(reader); 1.115 + 1.116 + QName rootName = reader.getName(); 1.117 + if(rootName.equals(WSDLConstants.QNAME_SCHEMA)) { 1.118 + String tns = ParserUtil.getMandatoryNonEmptyAttribute(reader, WSDLConstants.ATTR_TNS); 1.119 + Set<String> importedDocs = new HashSet<String>(); 1.120 + while (XMLStreamReaderUtil.nextContent(reader) != XMLStreamConstants.END_DOCUMENT) { 1.121 + if (reader.getEventType() != XMLStreamConstants.START_ELEMENT) 1.122 + continue; 1.123 + QName name = reader.getName(); 1.124 + if (SCHEMA_INCLUDE_QNAME.equals(name) || SCHEMA_IMPORT_QNAME.equals(name) || 1.125 + SCHEMA_REDEFINE_QNAME.equals(name)) { 1.126 + String importedDoc = reader.getAttributeValue(null, "schemaLocation"); 1.127 + if (importedDoc != null) { 1.128 + importedDocs.add(new URL(src.getSystemId(), importedDoc).toString()); 1.129 + } 1.130 + } 1.131 + } 1.132 + return new SchemaImpl(rootName,systemId,src,tns,importedDocs); 1.133 + } else if (rootName.equals(WSDLConstants.QNAME_DEFINITIONS)) { 1.134 + String tns = ParserUtil.getMandatoryNonEmptyAttribute(reader, WSDLConstants.ATTR_TNS); 1.135 + 1.136 + boolean hasPortType = false; 1.137 + boolean hasService = false; 1.138 + Set<String> importedDocs = new HashSet<String>(); 1.139 + Set<QName> allServices = new HashSet<QName>(); 1.140 + 1.141 + // if WSDL, parse more 1.142 + while (XMLStreamReaderUtil.nextContent(reader) != XMLStreamConstants.END_DOCUMENT) { 1.143 + if(reader.getEventType() != XMLStreamConstants.START_ELEMENT) 1.144 + continue; 1.145 + 1.146 + QName name = reader.getName(); 1.147 + if (WSDLConstants.QNAME_PORT_TYPE.equals(name)) { 1.148 + String pn = ParserUtil.getMandatoryNonEmptyAttribute(reader, WSDLConstants.ATTR_NAME); 1.149 + if (portTypeName != null) { 1.150 + if(portTypeName.getLocalPart().equals(pn)&&portTypeName.getNamespaceURI().equals(tns)) { 1.151 + hasPortType = true; 1.152 + } 1.153 + } 1.154 + } else if (WSDLConstants.QNAME_SERVICE.equals(name)) { 1.155 + String sn = ParserUtil.getMandatoryNonEmptyAttribute(reader, WSDLConstants.ATTR_NAME); 1.156 + QName sqn = new QName(tns,sn); 1.157 + allServices.add(sqn); 1.158 + if(serviceName.equals(sqn)) { 1.159 + hasService = true; 1.160 + } 1.161 + } else if (WSDLConstants.QNAME_IMPORT.equals(name)) { 1.162 + String importedDoc = reader.getAttributeValue(null, "location"); 1.163 + if (importedDoc != null) { 1.164 + importedDocs.add(new URL(src.getSystemId(), importedDoc).toString()); 1.165 + } 1.166 + } else if (SCHEMA_INCLUDE_QNAME.equals(name) || SCHEMA_IMPORT_QNAME.equals(name) || 1.167 + SCHEMA_REDEFINE_QNAME.equals(name)) { 1.168 + String importedDoc = reader.getAttributeValue(null, "schemaLocation"); 1.169 + if (importedDoc != null) { 1.170 + importedDocs.add(new URL(src.getSystemId(), importedDoc).toString()); 1.171 + } 1.172 + } 1.173 + } 1.174 + return new WSDLImpl( 1.175 + rootName,systemId,src,tns,hasPortType,hasService,importedDocs,allServices); 1.176 + } else { 1.177 + return new SDDocumentImpl(rootName,systemId,src); 1.178 + } 1.179 + } finally { 1.180 + reader.close(); 1.181 + } 1.182 + } catch (WebServiceException e) { 1.183 + throw new ServerRtException("runtime.parser.wsdl", systemId,e); 1.184 + } catch (IOException e) { 1.185 + throw new ServerRtException("runtime.parser.wsdl", systemId,e); 1.186 + } catch (XMLStreamException e) { 1.187 + throw new ServerRtException("runtime.parser.wsdl", systemId,e); 1.188 + } 1.189 + } 1.190 + 1.191 + protected SDDocumentImpl(QName rootName, URL url, SDDocumentSource source) { 1.192 + this(rootName, url, source, new HashSet<String>()); 1.193 + } 1.194 + 1.195 + protected SDDocumentImpl(QName rootName, URL url, SDDocumentSource source, Set<String> imports) { 1.196 + if (url == null) { 1.197 + throw new IllegalArgumentException("Cannot construct SDDocument with null URL."); 1.198 + } 1.199 + this.rootName = rootName; 1.200 + this.source = source; 1.201 + this.url = url; 1.202 + this.imports = imports; 1.203 + } 1.204 + 1.205 + void setFilters(List<SDDocumentFilter> filters) { 1.206 + this.filters = filters; 1.207 + } 1.208 + 1.209 + void setResolver(SDDocumentResolver sddocResolver) { 1.210 + this.sddocResolver = sddocResolver; 1.211 + } 1.212 + 1.213 + public QName getRootName() { 1.214 + return rootName; 1.215 + } 1.216 + 1.217 + public boolean isWSDL() { 1.218 + return false; 1.219 + } 1.220 + 1.221 + public boolean isSchema() { 1.222 + return false; 1.223 + } 1.224 + 1.225 + public URL getURL() { 1.226 + return url; 1.227 + } 1.228 + 1.229 + public XMLStreamReader read(XMLInputFactory xif) throws IOException, XMLStreamException { 1.230 + return source.read(xif); 1.231 + } 1.232 + 1.233 + public XMLStreamReader read() throws IOException, XMLStreamException { 1.234 + return source.read(); 1.235 + } 1.236 + 1.237 + public URL getSystemId() { 1.238 + return url; 1.239 + } 1.240 + 1.241 + public Set<String> getImports() { 1.242 + return imports; 1.243 + } 1.244 + 1.245 + public void writeTo(OutputStream os) throws IOException { 1.246 + XMLStreamWriter w = null; 1.247 + try { 1.248 + //generate the WSDL with utf-8 encoding and XML version 1.0 1.249 + w = XMLStreamWriterFactory.create(os, "UTF-8"); 1.250 + w.writeStartDocument("UTF-8", "1.0"); 1.251 + new XMLStreamReaderToXMLStreamWriter().bridge(source.read(), w); 1.252 + w.writeEndDocument(); 1.253 + } catch (XMLStreamException e) { 1.254 + IOException ioe = new IOException(e.getMessage()); 1.255 + ioe.initCause(e); 1.256 + throw ioe; 1.257 + } finally { 1.258 + try { 1.259 + if (w != null) 1.260 + w.close(); 1.261 + } catch (XMLStreamException e) { 1.262 + IOException ioe = new IOException(e.getMessage()); 1.263 + ioe.initCause(e); 1.264 + throw ioe; 1.265 + } 1.266 + } 1.267 + } 1.268 + 1.269 + 1.270 + public void writeTo(PortAddressResolver portAddressResolver, DocumentAddressResolver resolver, OutputStream os) throws IOException { 1.271 + XMLStreamWriter w = null; 1.272 + try { 1.273 + //generate the WSDL with utf-8 encoding and XML version 1.0 1.274 + w = XMLStreamWriterFactory.create(os, "UTF-8"); 1.275 + w.writeStartDocument("UTF-8", "1.0"); 1.276 + writeTo(portAddressResolver,resolver,w); 1.277 + w.writeEndDocument(); 1.278 + } catch (XMLStreamException e) { 1.279 + IOException ioe = new IOException(e.getMessage()); 1.280 + ioe.initCause(e); 1.281 + throw ioe; 1.282 + } finally { 1.283 + try { 1.284 + if (w != null) 1.285 + w.close(); 1.286 + } catch (XMLStreamException e) { 1.287 + IOException ioe = new IOException(e.getMessage()); 1.288 + ioe.initCause(e); 1.289 + throw ioe; 1.290 + } 1.291 + } 1.292 + } 1.293 + 1.294 + public void writeTo(PortAddressResolver portAddressResolver, DocumentAddressResolver resolver, XMLStreamWriter out) throws XMLStreamException, IOException { 1.295 + if (filters != null) { 1.296 + for (SDDocumentFilter f : filters) { 1.297 + out = f.filter(this,out); 1.298 + } 1.299 + } 1.300 + 1.301 + XMLStreamReader xsr = source.read(); 1.302 + try { 1.303 + out.writeComment(VERSION_COMMENT); 1.304 + new WSDLPatcher(portAddressResolver, new DocumentLocationResolverImpl(resolver)).bridge(xsr,out); 1.305 + } finally { 1.306 + xsr.close(); 1.307 + } 1.308 + } 1.309 + 1.310 + 1.311 + /** 1.312 + * {@link SDDocument.Schema} implementation. 1.313 + * 1.314 + * @author Kohsuke Kawaguchi 1.315 + */ 1.316 + private static final class SchemaImpl extends SDDocumentImpl implements SDDocument.Schema { 1.317 + private final String targetNamespace; 1.318 + 1.319 + public SchemaImpl(QName rootName, URL url, SDDocumentSource source, String targetNamespace, 1.320 + Set<String> imports) { 1.321 + super(rootName, url, source, imports); 1.322 + this.targetNamespace = targetNamespace; 1.323 + } 1.324 + 1.325 + public String getTargetNamespace() { 1.326 + return targetNamespace; 1.327 + } 1.328 + 1.329 + public boolean isSchema() { 1.330 + return true; 1.331 + } 1.332 + } 1.333 + 1.334 + 1.335 + private static final class WSDLImpl extends SDDocumentImpl implements SDDocument.WSDL { 1.336 + private final String targetNamespace; 1.337 + private final boolean hasPortType; 1.338 + private final boolean hasService; 1.339 + private final Set<QName> allServices; 1.340 + 1.341 + public WSDLImpl(QName rootName, URL url, SDDocumentSource source, String targetNamespace, boolean hasPortType, 1.342 + boolean hasService, Set<String> imports,Set<QName> allServices) { 1.343 + super(rootName, url, source, imports); 1.344 + this.targetNamespace = targetNamespace; 1.345 + this.hasPortType = hasPortType; 1.346 + this.hasService = hasService; 1.347 + this.allServices = allServices; 1.348 + } 1.349 + 1.350 + public String getTargetNamespace() { 1.351 + return targetNamespace; 1.352 + } 1.353 + 1.354 + public boolean hasPortType() { 1.355 + return hasPortType; 1.356 + } 1.357 + 1.358 + public boolean hasService() { 1.359 + return hasService; 1.360 + } 1.361 + 1.362 + public Set<QName> getAllServices() { 1.363 + return allServices; 1.364 + } 1.365 + 1.366 + public boolean isWSDL() { 1.367 + return true; 1.368 + } 1.369 + } 1.370 + 1.371 + private class DocumentLocationResolverImpl implements DocumentLocationResolver { 1.372 + private DocumentAddressResolver delegate; 1.373 + 1.374 + DocumentLocationResolverImpl(DocumentAddressResolver delegate) { 1.375 + this.delegate = delegate; 1.376 + } 1.377 + 1.378 + public String getLocationFor(String namespaceURI, String systemId) { 1.379 + if (sddocResolver == null) { 1.380 + return systemId; 1.381 + } 1.382 + try { 1.383 + URL ref = new URL(getURL(), systemId); 1.384 + SDDocument refDoc = sddocResolver.resolve(ref.toExternalForm()); 1.385 + if (refDoc == null) 1.386 + return systemId; // not something we know. just leave it as is. 1.387 + 1.388 + return delegate.getRelativeAddressFor(SDDocumentImpl.this, refDoc); 1.389 + } catch (MalformedURLException mue) { 1.390 + return null; 1.391 + } 1.392 + } 1.393 + } 1.394 + 1.395 +}