1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/ws/wsdl/writer/WSDLPatcher.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,237 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.xml.internal.ws.wsdl.writer; 1.30 + 1.31 +import com.sun.istack.internal.NotNull; 1.32 +import com.sun.xml.internal.ws.api.server.PortAddressResolver; 1.33 +import com.sun.xml.internal.ws.util.xml.XMLStreamReaderToXMLStreamWriter; 1.34 +import com.sun.xml.internal.ws.wsdl.parser.WSDLConstants; 1.35 +import com.sun.xml.internal.ws.addressing.W3CAddressingConstants; 1.36 +import com.sun.xml.internal.ws.addressing.v200408.MemberSubmissionAddressingConstants; 1.37 +import com.sun.istack.internal.Nullable; 1.38 + 1.39 +import javax.xml.namespace.QName; 1.40 +import javax.xml.stream.XMLStreamException; 1.41 +import java.util.logging.Logger; 1.42 + 1.43 +/** 1.44 + * Patches WSDL with the correct endpoint address and the relative paths 1.45 + * to other documents. 1.46 + * 1.47 + * @author Jitendra Kotamraju 1.48 + * @author Kohsuke Kawaguchi 1.49 + */ 1.50 +public final class WSDLPatcher extends XMLStreamReaderToXMLStreamWriter { 1.51 + 1.52 + private static final String NS_XSD = "http://www.w3.org/2001/XMLSchema"; 1.53 + private static final QName SCHEMA_INCLUDE_QNAME = new QName(NS_XSD, "include"); 1.54 + private static final QName SCHEMA_IMPORT_QNAME = new QName(NS_XSD, "import"); 1.55 + private static final QName SCHEMA_REDEFINE_QNAME = new QName(NS_XSD, "redefine"); 1.56 + 1.57 + private static final Logger logger = Logger.getLogger( 1.58 + com.sun.xml.internal.ws.util.Constants.LoggingDomain + ".wsdl.patcher"); 1.59 + 1.60 + private final DocumentLocationResolver docResolver; 1.61 + private final PortAddressResolver portAddressResolver; 1.62 + 1.63 + // 1.64 + // fields accumulated as we parse through documents 1.65 + // 1.66 + private String targetNamespace; 1.67 + private QName serviceName; 1.68 + private QName portName; 1.69 + private String portAddress; 1.70 + 1.71 + // true inside <wsdl:service>/<wsdl:part>/<wsa:EndpointReference> 1.72 + private boolean inEpr; 1.73 + // true inside <wsdl:service>/<wsdl:part>/<wsa:EndpointReference>/<wsa:Address> 1.74 + private boolean inEprAddress; 1.75 + 1.76 + /** 1.77 + * Creates a {@link WSDLPatcher} for patching WSDL. 1.78 + * 1.79 + * @param portAddressResolver 1.80 + * address of the endpoint is resolved using this docResolver. 1.81 + * @param docResolver 1.82 + * Consulted to get the import/include document locations. 1.83 + * Must not be null. 1.84 + */ 1.85 + public WSDLPatcher(@NotNull PortAddressResolver portAddressResolver, 1.86 + @NotNull DocumentLocationResolver docResolver) { 1.87 + this.portAddressResolver = portAddressResolver; 1.88 + this.docResolver = docResolver; 1.89 + } 1.90 + 1.91 + @Override 1.92 + protected void handleAttribute(int i) throws XMLStreamException { 1.93 + QName name = in.getName(); 1.94 + String attLocalName = in.getAttributeLocalName(i); 1.95 + 1.96 + if((name.equals(SCHEMA_INCLUDE_QNAME) && attLocalName.equals("schemaLocation")) 1.97 + || (name.equals(SCHEMA_IMPORT_QNAME) && attLocalName.equals("schemaLocation")) 1.98 + || (name.equals(SCHEMA_REDEFINE_QNAME) && attLocalName.equals("schemaLocation")) 1.99 + || (name.equals(WSDLConstants.QNAME_IMPORT) && attLocalName.equals("location"))) { 1.100 + // patch this attribute value. 1.101 + 1.102 + String relPath = in.getAttributeValue(i); 1.103 + String actualPath = getPatchedImportLocation(relPath); 1.104 + if (actualPath == null) { 1.105 + return; // skip this attribute to leave it up to "implicit reference". 1.106 + } 1.107 + 1.108 + logger.fine("Fixing the relative location:"+relPath 1.109 + +" with absolute location:"+actualPath); 1.110 + writeAttribute(i, actualPath); 1.111 + return; 1.112 + } 1.113 + 1.114 + if (name.equals(WSDLConstants.NS_SOAP_BINDING_ADDRESS) || 1.115 + name.equals(WSDLConstants.NS_SOAP12_BINDING_ADDRESS)) { 1.116 + 1.117 + if(attLocalName.equals("location")) { 1.118 + portAddress = in.getAttributeValue(i); 1.119 + String value = getAddressLocation(); 1.120 + if (value != null) { 1.121 + logger.fine("Service:"+serviceName+ " port:"+portName 1.122 + + " current address "+portAddress+" Patching it with "+value); 1.123 + writeAttribute(i, value); 1.124 + return; 1.125 + } 1.126 + } 1.127 + } 1.128 + 1.129 + super.handleAttribute(i); 1.130 + } 1.131 + 1.132 + /** 1.133 + * Writes out an {@code i}-th attribute but with a different value. 1.134 + * @param i attribute index 1.135 + * @param value attribute value 1.136 + * @throws XMLStreamException when an error encountered while writing attribute 1.137 + */ 1.138 + private void writeAttribute(int i, String value) throws XMLStreamException { 1.139 + String nsUri = in.getAttributeNamespace(i); 1.140 + if(nsUri!=null) 1.141 + out.writeAttribute( in.getAttributePrefix(i), nsUri, in.getAttributeLocalName(i), value ); 1.142 + else 1.143 + out.writeAttribute( in.getAttributeLocalName(i), value ); 1.144 + } 1.145 + 1.146 + @Override 1.147 + protected void handleStartElement() throws XMLStreamException { 1.148 + QName name = in.getName(); 1.149 + 1.150 + if (name.equals(WSDLConstants.QNAME_DEFINITIONS)) { 1.151 + String value = in.getAttributeValue(null,"targetNamespace"); 1.152 + if (value != null) { 1.153 + targetNamespace = value; 1.154 + } 1.155 + } else if (name.equals(WSDLConstants.QNAME_SERVICE)) { 1.156 + String value = in.getAttributeValue(null,"name"); 1.157 + if (value != null) { 1.158 + serviceName = new QName(targetNamespace, value); 1.159 + } 1.160 + } else if (name.equals(WSDLConstants.QNAME_PORT)) { 1.161 + String value = in.getAttributeValue(null,"name"); 1.162 + if (value != null) { 1.163 + portName = new QName(targetNamespace,value); 1.164 + } 1.165 + } else if (name.equals(W3CAddressingConstants.WSA_EPR_QNAME) 1.166 + || name.equals(MemberSubmissionAddressingConstants.WSA_EPR_QNAME)) { 1.167 + if (serviceName != null && portName != null) { 1.168 + inEpr = true; 1.169 + } 1.170 + } else if (name.equals(W3CAddressingConstants.WSA_ADDRESS_QNAME) 1.171 + || name.equals(MemberSubmissionAddressingConstants.WSA_ADDRESS_QNAME)) { 1.172 + if (inEpr) { 1.173 + inEprAddress = true; 1.174 + } 1.175 + } 1.176 + super.handleStartElement(); 1.177 + } 1.178 + 1.179 + @Override 1.180 + protected void handleEndElement() throws XMLStreamException { 1.181 + QName name = in.getName(); 1.182 + if (name.equals(WSDLConstants.QNAME_SERVICE)) { 1.183 + serviceName = null; 1.184 + } else if (name.equals(WSDLConstants.QNAME_PORT)) { 1.185 + portName = null; 1.186 + } else if (name.equals(W3CAddressingConstants.WSA_EPR_QNAME) 1.187 + || name.equals(MemberSubmissionAddressingConstants.WSA_EPR_QNAME)) { 1.188 + if (inEpr) { 1.189 + inEpr = false; 1.190 + } 1.191 + } else if (name.equals(W3CAddressingConstants.WSA_ADDRESS_QNAME) 1.192 + || name.equals(MemberSubmissionAddressingConstants.WSA_ADDRESS_QNAME)) { 1.193 + if (inEprAddress) { 1.194 + String value = getAddressLocation(); 1.195 + if (value != null) { 1.196 + logger.fine("Fixing EPR Address for service:"+serviceName+ " port:"+portName 1.197 + + " address with "+value); 1.198 + out.writeCharacters(value); 1.199 + } 1.200 + inEprAddress = false; 1.201 + } 1.202 + } 1.203 + super.handleEndElement(); 1.204 + } 1.205 + 1.206 + @Override 1.207 + protected void handleCharacters() throws XMLStreamException { 1.208 + // handleCharacters() may be called multiple times. 1.209 + if (inEprAddress) { 1.210 + String value = getAddressLocation(); 1.211 + if (value != null) { 1.212 + // will write the address with <wsa:Address> end element 1.213 + return; 1.214 + } 1.215 + } 1.216 + super.handleCharacters(); 1.217 + } 1.218 + 1.219 + /** 1.220 + * Returns the location to be placed into the generated document. 1.221 + * 1.222 + * @param relPath relative URI to be resolved 1.223 + * @return 1.224 + * null to leave it to the "implicit reference". 1.225 + */ 1.226 + private @Nullable String getPatchedImportLocation(String relPath) { 1.227 + return docResolver.getLocationFor(null, relPath); 1.228 + } 1.229 + 1.230 + /** 1.231 + * For the given service, port names it matches the correct endpoint and 1.232 + * reutrns its endpoint address 1.233 + * 1.234 + * @return returns the resolved endpoint address 1.235 + */ 1.236 + private String getAddressLocation() { 1.237 + return (portAddressResolver == null || portName == null) 1.238 + ? null : portAddressResolver.getAddressFor(serviceName, portName.getLocalPart(), portAddress); 1.239 + } 1.240 +}