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

Tue, 06 Mar 2012 16:09:35 -0800

author
ohair
date
Tue, 06 Mar 2012 16:09:35 -0800
changeset 286
f50545b5e2f1
child 368
0989ad8c0860
permissions
-rw-r--r--

7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom

ohair@286 1 /*
ohair@286 2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
ohair@286 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286 4 *
ohair@286 5 * This code is free software; you can redistribute it and/or modify it
ohair@286 6 * under the terms of the GNU General Public License version 2 only, as
ohair@286 7 * published by the Free Software Foundation. Oracle designates this
ohair@286 8 * particular file as subject to the "Classpath" exception as provided
ohair@286 9 * by Oracle in the LICENSE file that accompanied this code.
ohair@286 10 *
ohair@286 11 * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286 14 * version 2 for more details (a copy is included in the LICENSE file that
ohair@286 15 * accompanied this code).
ohair@286 16 *
ohair@286 17 * You should have received a copy of the GNU General Public License version
ohair@286 18 * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286 20 *
ohair@286 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286 22 * or visit www.oracle.com if you need additional information or have any
ohair@286 23 * questions.
ohair@286 24 */
ohair@286 25
ohair@286 26 package com.sun.xml.internal.ws.wsdl.writer;
ohair@286 27
ohair@286 28 import com.sun.istack.internal.NotNull;
ohair@286 29 import com.sun.xml.internal.ws.api.server.PortAddressResolver;
ohair@286 30 import com.sun.xml.internal.ws.util.xml.XMLStreamReaderToXMLStreamWriter;
ohair@286 31 import com.sun.xml.internal.ws.wsdl.parser.WSDLConstants;
ohair@286 32 import com.sun.xml.internal.ws.addressing.W3CAddressingConstants;
ohair@286 33 import com.sun.istack.internal.Nullable;
ohair@286 34
ohair@286 35 import javax.xml.namespace.QName;
ohair@286 36 import javax.xml.stream.XMLStreamException;
ohair@286 37 import java.util.logging.Logger;
ohair@286 38
ohair@286 39 /**
ohair@286 40 * Patches WSDL with the correct endpoint address and the relative paths
ohair@286 41 * to other documents.
ohair@286 42 *
ohair@286 43 * @author Jitendra Kotamraju
ohair@286 44 * @author Kohsuke Kawaguchi
ohair@286 45 */
ohair@286 46 public final class WSDLPatcher extends XMLStreamReaderToXMLStreamWriter {
ohair@286 47
ohair@286 48 private static final String NS_XSD = "http://www.w3.org/2001/XMLSchema";
ohair@286 49 private static final QName SCHEMA_INCLUDE_QNAME = new QName(NS_XSD, "include");
ohair@286 50 private static final QName SCHEMA_IMPORT_QNAME = new QName(NS_XSD, "import");
ohair@286 51 private static final QName SCHEMA_REDEFINE_QNAME = new QName(NS_XSD, "redefine");
ohair@286 52
ohair@286 53 private static final Logger logger = Logger.getLogger(
ohair@286 54 com.sun.xml.internal.ws.util.Constants.LoggingDomain + ".wsdl.patcher");
ohair@286 55
ohair@286 56 private final DocumentLocationResolver docResolver;
ohair@286 57 private final PortAddressResolver portAddressResolver;
ohair@286 58
ohair@286 59 //
ohair@286 60 // fields accumulated as we parse through documents
ohair@286 61 //
ohair@286 62 private String targetNamespace;
ohair@286 63 private QName serviceName;
ohair@286 64 private QName portName;
ohair@286 65 private String portAddress;
ohair@286 66
ohair@286 67 // true inside <wsdl:service>/<wsdl:part>/<wsa:EndpointReference>
ohair@286 68 private boolean inEpr;
ohair@286 69 // true inside <wsdl:service>/<wsdl:part>/<wsa:EndpointReference>/<wsa:Address>
ohair@286 70 private boolean inEprAddress;
ohair@286 71
ohair@286 72 /**
ohair@286 73 * Creates a {@link WSDLPatcher} for patching WSDL.
ohair@286 74 *
ohair@286 75 * @param portAddressResolver
ohair@286 76 * address of the endpoint is resolved using this docResolver.
ohair@286 77 * @param docResolver
ohair@286 78 * Consulted to get the import/include document locations.
ohair@286 79 * Must not be null.
ohair@286 80 */
ohair@286 81 public WSDLPatcher(@NotNull PortAddressResolver portAddressResolver,
ohair@286 82 @NotNull DocumentLocationResolver docResolver) {
ohair@286 83 this.portAddressResolver = portAddressResolver;
ohair@286 84 this.docResolver = docResolver;
ohair@286 85 }
ohair@286 86
ohair@286 87 @Override
ohair@286 88 protected void handleAttribute(int i) throws XMLStreamException {
ohair@286 89 QName name = in.getName();
ohair@286 90 String attLocalName = in.getAttributeLocalName(i);
ohair@286 91
ohair@286 92 if((name.equals(SCHEMA_INCLUDE_QNAME) && attLocalName.equals("schemaLocation"))
ohair@286 93 || (name.equals(SCHEMA_IMPORT_QNAME) && attLocalName.equals("schemaLocation"))
ohair@286 94 || (name.equals(SCHEMA_REDEFINE_QNAME) && attLocalName.equals("schemaLocation"))
ohair@286 95 || (name.equals(WSDLConstants.QNAME_IMPORT) && attLocalName.equals("location"))) {
ohair@286 96 // patch this attribute value.
ohair@286 97
ohair@286 98 String relPath = in.getAttributeValue(i);
ohair@286 99 String actualPath = getPatchedImportLocation(relPath);
ohair@286 100 if (actualPath == null) {
ohair@286 101 return; // skip this attribute to leave it up to "implicit reference".
ohair@286 102 }
ohair@286 103
ohair@286 104 logger.fine("Fixing the relative location:"+relPath
ohair@286 105 +" with absolute location:"+actualPath);
ohair@286 106 writeAttribute(i, actualPath);
ohair@286 107 return;
ohair@286 108 }
ohair@286 109
ohair@286 110 if (name.equals(WSDLConstants.NS_SOAP_BINDING_ADDRESS) ||
ohair@286 111 name.equals(WSDLConstants.NS_SOAP12_BINDING_ADDRESS)) {
ohair@286 112
ohair@286 113 if(attLocalName.equals("location")) {
ohair@286 114 portAddress = in.getAttributeValue(i);
ohair@286 115 String value = getAddressLocation();
ohair@286 116 if (value != null) {
ohair@286 117 logger.fine("Service:"+serviceName+ " port:"+portName
ohair@286 118 + " current address "+portAddress+" Patching it with "+value);
ohair@286 119 writeAttribute(i, value);
ohair@286 120 return;
ohair@286 121 }
ohair@286 122 }
ohair@286 123 }
ohair@286 124
ohair@286 125 super.handleAttribute(i);
ohair@286 126 }
ohair@286 127
ohair@286 128 /**
ohair@286 129 * Writes out an {@code i}-th attribute but with a different value.
ohair@286 130 * @param i attribute index
ohair@286 131 * @param value attribute value
ohair@286 132 * @throws XMLStreamException when an error encountered while writing attribute
ohair@286 133 */
ohair@286 134 private void writeAttribute(int i, String value) throws XMLStreamException {
ohair@286 135 String nsUri = in.getAttributeNamespace(i);
ohair@286 136 if(nsUri!=null)
ohair@286 137 out.writeAttribute( in.getAttributePrefix(i), nsUri, in.getAttributeLocalName(i), value );
ohair@286 138 else
ohair@286 139 out.writeAttribute( in.getAttributeLocalName(i), value );
ohair@286 140 }
ohair@286 141
ohair@286 142 @Override
ohair@286 143 protected void handleStartElement() throws XMLStreamException {
ohair@286 144 QName name = in.getName();
ohair@286 145
ohair@286 146 if (name.equals(WSDLConstants.QNAME_DEFINITIONS)) {
ohair@286 147 String value = in.getAttributeValue(null,"targetNamespace");
ohair@286 148 if (value != null) {
ohair@286 149 targetNamespace = value;
ohair@286 150 }
ohair@286 151 } else if (name.equals(WSDLConstants.QNAME_SERVICE)) {
ohair@286 152 String value = in.getAttributeValue(null,"name");
ohair@286 153 if (value != null) {
ohair@286 154 serviceName = new QName(targetNamespace, value);
ohair@286 155 }
ohair@286 156 } else if (name.equals(WSDLConstants.QNAME_PORT)) {
ohair@286 157 String value = in.getAttributeValue(null,"name");
ohair@286 158 if (value != null) {
ohair@286 159 portName = new QName(targetNamespace,value);
ohair@286 160 }
ohair@286 161 } else if (name.equals(W3CAddressingConstants.WSA_EPR_QNAME)) {
ohair@286 162 if (serviceName != null && portName != null) {
ohair@286 163 inEpr = true;
ohair@286 164 }
ohair@286 165 } else if (name.equals(W3CAddressingConstants.WSA_ADDRESS_QNAME)) {
ohair@286 166 if (inEpr) {
ohair@286 167 inEprAddress = true;
ohair@286 168 }
ohair@286 169 }
ohair@286 170 super.handleStartElement();
ohair@286 171 }
ohair@286 172
ohair@286 173 @Override
ohair@286 174 protected void handleEndElement() throws XMLStreamException {
ohair@286 175 QName name = in.getName();
ohair@286 176 if (name.equals(WSDLConstants.QNAME_SERVICE)) {
ohair@286 177 serviceName = null;
ohair@286 178 } else if (name.equals(WSDLConstants.QNAME_PORT)) {
ohair@286 179 portName = null;
ohair@286 180 } else if (name.equals(W3CAddressingConstants.WSA_EPR_QNAME)) {
ohair@286 181 if (inEpr) {
ohair@286 182 inEpr = false;
ohair@286 183 }
ohair@286 184 } else if (name.equals(W3CAddressingConstants.WSA_ADDRESS_QNAME)) {
ohair@286 185 if (inEprAddress) {
ohair@286 186 String value = getAddressLocation();
ohair@286 187 if (value != null) {
ohair@286 188 logger.fine("Fixing EPR Address for service:"+serviceName+ " port:"+portName
ohair@286 189 + " address with "+value);
ohair@286 190 out.writeCharacters(value);
ohair@286 191 }
ohair@286 192 inEprAddress = false;
ohair@286 193 }
ohair@286 194 }
ohair@286 195 super.handleEndElement();
ohair@286 196 }
ohair@286 197
ohair@286 198 @Override
ohair@286 199 protected void handleCharacters() throws XMLStreamException {
ohair@286 200 // handleCharacters() may be called multiple times.
ohair@286 201 if (inEprAddress) {
ohair@286 202 String value = getAddressLocation();
ohair@286 203 if (value != null) {
ohair@286 204 // will write the address with <wsa:Address> end element
ohair@286 205 return;
ohair@286 206 }
ohair@286 207 }
ohair@286 208 super.handleCharacters();
ohair@286 209 }
ohair@286 210
ohair@286 211 /**
ohair@286 212 * Returns the location to be placed into the generated document.
ohair@286 213 *
ohair@286 214 * @param relPath relative URI to be resolved
ohair@286 215 * @return
ohair@286 216 * null to leave it to the "implicit reference".
ohair@286 217 */
ohair@286 218 private @Nullable String getPatchedImportLocation(String relPath) {
ohair@286 219 return docResolver.getLocationFor(null, relPath);
ohair@286 220 }
ohair@286 221
ohair@286 222 /**
ohair@286 223 * For the given service, port names it matches the correct endpoint and
ohair@286 224 * reutrns its endpoint address
ohair@286 225 *
ohair@286 226 * @return returns the resolved endpoint address
ohair@286 227 */
ohair@286 228 private String getAddressLocation() {
ohair@286 229 return (portAddressResolver == null || portName == null)
ohair@286 230 ? null : portAddressResolver.getAddressFor(serviceName, portName.getLocalPart(), portAddress);
ohair@286 231 }
ohair@286 232 }

mercurial