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

Sat, 07 Nov 2020 10:30:02 +0800

author
aoqi
date
Sat, 07 Nov 2020 10:30:02 +0800
changeset 1921
7166269ef0f1
parent 637
9c07ef4934dd
permissions
-rw-r--r--

Added tag mips-jdk8u275-b01 for changeset fdbe50121f48

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2012, 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.wsdl.writer;
aoqi@0 27
aoqi@0 28 import com.sun.istack.internal.NotNull;
aoqi@0 29 import com.sun.xml.internal.ws.api.server.PortAddressResolver;
aoqi@0 30 import com.sun.xml.internal.ws.util.xml.XMLStreamReaderToXMLStreamWriter;
aoqi@0 31 import com.sun.xml.internal.ws.wsdl.parser.WSDLConstants;
aoqi@0 32 import com.sun.xml.internal.ws.addressing.W3CAddressingConstants;
aoqi@0 33 import com.sun.xml.internal.ws.addressing.v200408.MemberSubmissionAddressingConstants;
aoqi@0 34 import com.sun.istack.internal.Nullable;
aoqi@0 35
aoqi@0 36 import javax.xml.namespace.QName;
aoqi@0 37 import javax.xml.stream.XMLStreamException;
aoqi@0 38 import java.util.logging.Logger;
aoqi@0 39
aoqi@0 40 /**
aoqi@0 41 * Patches WSDL with the correct endpoint address and the relative paths
aoqi@0 42 * to other documents.
aoqi@0 43 *
aoqi@0 44 * @author Jitendra Kotamraju
aoqi@0 45 * @author Kohsuke Kawaguchi
aoqi@0 46 */
aoqi@0 47 public final class WSDLPatcher extends XMLStreamReaderToXMLStreamWriter {
aoqi@0 48
aoqi@0 49 private static final String NS_XSD = "http://www.w3.org/2001/XMLSchema";
aoqi@0 50 private static final QName SCHEMA_INCLUDE_QNAME = new QName(NS_XSD, "include");
aoqi@0 51 private static final QName SCHEMA_IMPORT_QNAME = new QName(NS_XSD, "import");
aoqi@0 52 private static final QName SCHEMA_REDEFINE_QNAME = new QName(NS_XSD, "redefine");
aoqi@0 53
aoqi@0 54 private static final Logger logger = Logger.getLogger(
aoqi@0 55 com.sun.xml.internal.ws.util.Constants.LoggingDomain + ".wsdl.patcher");
aoqi@0 56
aoqi@0 57 private final DocumentLocationResolver docResolver;
aoqi@0 58 private final PortAddressResolver portAddressResolver;
aoqi@0 59
aoqi@0 60 //
aoqi@0 61 // fields accumulated as we parse through documents
aoqi@0 62 //
aoqi@0 63 private String targetNamespace;
aoqi@0 64 private QName serviceName;
aoqi@0 65 private QName portName;
aoqi@0 66 private String portAddress;
aoqi@0 67
aoqi@0 68 // true inside <wsdl:service>/<wsdl:part>/<wsa:EndpointReference>
aoqi@0 69 private boolean inEpr;
aoqi@0 70 // true inside <wsdl:service>/<wsdl:part>/<wsa:EndpointReference>/<wsa:Address>
aoqi@0 71 private boolean inEprAddress;
aoqi@0 72
aoqi@0 73 /**
aoqi@0 74 * Creates a {@link WSDLPatcher} for patching WSDL.
aoqi@0 75 *
aoqi@0 76 * @param portAddressResolver
aoqi@0 77 * address of the endpoint is resolved using this docResolver.
aoqi@0 78 * @param docResolver
aoqi@0 79 * Consulted to get the import/include document locations.
aoqi@0 80 * Must not be null.
aoqi@0 81 */
aoqi@0 82 public WSDLPatcher(@NotNull PortAddressResolver portAddressResolver,
aoqi@0 83 @NotNull DocumentLocationResolver docResolver) {
aoqi@0 84 this.portAddressResolver = portAddressResolver;
aoqi@0 85 this.docResolver = docResolver;
aoqi@0 86 }
aoqi@0 87
aoqi@0 88 @Override
aoqi@0 89 protected void handleAttribute(int i) throws XMLStreamException {
aoqi@0 90 QName name = in.getName();
aoqi@0 91 String attLocalName = in.getAttributeLocalName(i);
aoqi@0 92
aoqi@0 93 if((name.equals(SCHEMA_INCLUDE_QNAME) && attLocalName.equals("schemaLocation"))
aoqi@0 94 || (name.equals(SCHEMA_IMPORT_QNAME) && attLocalName.equals("schemaLocation"))
aoqi@0 95 || (name.equals(SCHEMA_REDEFINE_QNAME) && attLocalName.equals("schemaLocation"))
aoqi@0 96 || (name.equals(WSDLConstants.QNAME_IMPORT) && attLocalName.equals("location"))) {
aoqi@0 97 // patch this attribute value.
aoqi@0 98
aoqi@0 99 String relPath = in.getAttributeValue(i);
aoqi@0 100 String actualPath = getPatchedImportLocation(relPath);
aoqi@0 101 if (actualPath == null) {
aoqi@0 102 return; // skip this attribute to leave it up to "implicit reference".
aoqi@0 103 }
aoqi@0 104
aoqi@0 105 logger.fine("Fixing the relative location:"+relPath
aoqi@0 106 +" with absolute location:"+actualPath);
aoqi@0 107 writeAttribute(i, actualPath);
aoqi@0 108 return;
aoqi@0 109 }
aoqi@0 110
aoqi@0 111 if (name.equals(WSDLConstants.NS_SOAP_BINDING_ADDRESS) ||
aoqi@0 112 name.equals(WSDLConstants.NS_SOAP12_BINDING_ADDRESS)) {
aoqi@0 113
aoqi@0 114 if(attLocalName.equals("location")) {
aoqi@0 115 portAddress = in.getAttributeValue(i);
aoqi@0 116 String value = getAddressLocation();
aoqi@0 117 if (value != null) {
aoqi@0 118 logger.fine("Service:"+serviceName+ " port:"+portName
aoqi@0 119 + " current address "+portAddress+" Patching it with "+value);
aoqi@0 120 writeAttribute(i, value);
aoqi@0 121 return;
aoqi@0 122 }
aoqi@0 123 }
aoqi@0 124 }
aoqi@0 125
aoqi@0 126 super.handleAttribute(i);
aoqi@0 127 }
aoqi@0 128
aoqi@0 129 /**
aoqi@0 130 * Writes out an {@code i}-th attribute but with a different value.
aoqi@0 131 * @param i attribute index
aoqi@0 132 * @param value attribute value
aoqi@0 133 * @throws XMLStreamException when an error encountered while writing attribute
aoqi@0 134 */
aoqi@0 135 private void writeAttribute(int i, String value) throws XMLStreamException {
aoqi@0 136 String nsUri = in.getAttributeNamespace(i);
aoqi@0 137 if(nsUri!=null)
aoqi@0 138 out.writeAttribute( in.getAttributePrefix(i), nsUri, in.getAttributeLocalName(i), value );
aoqi@0 139 else
aoqi@0 140 out.writeAttribute( in.getAttributeLocalName(i), value );
aoqi@0 141 }
aoqi@0 142
aoqi@0 143 @Override
aoqi@0 144 protected void handleStartElement() throws XMLStreamException {
aoqi@0 145 QName name = in.getName();
aoqi@0 146
aoqi@0 147 if (name.equals(WSDLConstants.QNAME_DEFINITIONS)) {
aoqi@0 148 String value = in.getAttributeValue(null,"targetNamespace");
aoqi@0 149 if (value != null) {
aoqi@0 150 targetNamespace = value;
aoqi@0 151 }
aoqi@0 152 } else if (name.equals(WSDLConstants.QNAME_SERVICE)) {
aoqi@0 153 String value = in.getAttributeValue(null,"name");
aoqi@0 154 if (value != null) {
aoqi@0 155 serviceName = new QName(targetNamespace, value);
aoqi@0 156 }
aoqi@0 157 } else if (name.equals(WSDLConstants.QNAME_PORT)) {
aoqi@0 158 String value = in.getAttributeValue(null,"name");
aoqi@0 159 if (value != null) {
aoqi@0 160 portName = new QName(targetNamespace,value);
aoqi@0 161 }
aoqi@0 162 } else if (name.equals(W3CAddressingConstants.WSA_EPR_QNAME)
aoqi@0 163 || name.equals(MemberSubmissionAddressingConstants.WSA_EPR_QNAME)) {
aoqi@0 164 if (serviceName != null && portName != null) {
aoqi@0 165 inEpr = true;
aoqi@0 166 }
aoqi@0 167 } else if (name.equals(W3CAddressingConstants.WSA_ADDRESS_QNAME)
aoqi@0 168 || name.equals(MemberSubmissionAddressingConstants.WSA_ADDRESS_QNAME)) {
aoqi@0 169 if (inEpr) {
aoqi@0 170 inEprAddress = true;
aoqi@0 171 }
aoqi@0 172 }
aoqi@0 173 super.handleStartElement();
aoqi@0 174 }
aoqi@0 175
aoqi@0 176 @Override
aoqi@0 177 protected void handleEndElement() throws XMLStreamException {
aoqi@0 178 QName name = in.getName();
aoqi@0 179 if (name.equals(WSDLConstants.QNAME_SERVICE)) {
aoqi@0 180 serviceName = null;
aoqi@0 181 } else if (name.equals(WSDLConstants.QNAME_PORT)) {
aoqi@0 182 portName = null;
aoqi@0 183 } else if (name.equals(W3CAddressingConstants.WSA_EPR_QNAME)
aoqi@0 184 || name.equals(MemberSubmissionAddressingConstants.WSA_EPR_QNAME)) {
aoqi@0 185 if (inEpr) {
aoqi@0 186 inEpr = false;
aoqi@0 187 }
aoqi@0 188 } else if (name.equals(W3CAddressingConstants.WSA_ADDRESS_QNAME)
aoqi@0 189 || name.equals(MemberSubmissionAddressingConstants.WSA_ADDRESS_QNAME)) {
aoqi@0 190 if (inEprAddress) {
aoqi@0 191 String value = getAddressLocation();
aoqi@0 192 if (value != null) {
aoqi@0 193 logger.fine("Fixing EPR Address for service:"+serviceName+ " port:"+portName
aoqi@0 194 + " address with "+value);
aoqi@0 195 out.writeCharacters(value);
aoqi@0 196 }
aoqi@0 197 inEprAddress = false;
aoqi@0 198 }
aoqi@0 199 }
aoqi@0 200 super.handleEndElement();
aoqi@0 201 }
aoqi@0 202
aoqi@0 203 @Override
aoqi@0 204 protected void handleCharacters() throws XMLStreamException {
aoqi@0 205 // handleCharacters() may be called multiple times.
aoqi@0 206 if (inEprAddress) {
aoqi@0 207 String value = getAddressLocation();
aoqi@0 208 if (value != null) {
aoqi@0 209 // will write the address with <wsa:Address> end element
aoqi@0 210 return;
aoqi@0 211 }
aoqi@0 212 }
aoqi@0 213 super.handleCharacters();
aoqi@0 214 }
aoqi@0 215
aoqi@0 216 /**
aoqi@0 217 * Returns the location to be placed into the generated document.
aoqi@0 218 *
aoqi@0 219 * @param relPath relative URI to be resolved
aoqi@0 220 * @return
aoqi@0 221 * null to leave it to the "implicit reference".
aoqi@0 222 */
aoqi@0 223 private @Nullable String getPatchedImportLocation(String relPath) {
aoqi@0 224 return docResolver.getLocationFor(null, relPath);
aoqi@0 225 }
aoqi@0 226
aoqi@0 227 /**
aoqi@0 228 * For the given service, port names it matches the correct endpoint and
aoqi@0 229 * reutrns its endpoint address
aoqi@0 230 *
aoqi@0 231 * @return returns the resolved endpoint address
aoqi@0 232 */
aoqi@0 233 private String getAddressLocation() {
aoqi@0 234 return (portAddressResolver == null || portName == null)
aoqi@0 235 ? null : portAddressResolver.getAddressFor(serviceName, portName.getLocalPart(), portAddress);
aoqi@0 236 }
aoqi@0 237 }

mercurial