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

changeset 286
f50545b5e2f1
child 368
0989ad8c0860
     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	Tue Mar 06 16:09:35 2012 -0800
     1.3 @@ -0,0 +1,232 @@
     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.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.istack.internal.Nullable;
    1.37 +
    1.38 +import javax.xml.namespace.QName;
    1.39 +import javax.xml.stream.XMLStreamException;
    1.40 +import java.util.logging.Logger;
    1.41 +
    1.42 +/**
    1.43 + * Patches WSDL with the correct endpoint address and the relative paths
    1.44 + * to other documents.
    1.45 + *
    1.46 + * @author Jitendra Kotamraju
    1.47 + * @author Kohsuke Kawaguchi
    1.48 + */
    1.49 +public final class WSDLPatcher extends XMLStreamReaderToXMLStreamWriter {
    1.50 +
    1.51 +    private static final String NS_XSD = "http://www.w3.org/2001/XMLSchema";
    1.52 +    private static final QName SCHEMA_INCLUDE_QNAME = new QName(NS_XSD, "include");
    1.53 +    private static final QName SCHEMA_IMPORT_QNAME = new QName(NS_XSD, "import");
    1.54 +    private static final QName SCHEMA_REDEFINE_QNAME = new QName(NS_XSD, "redefine");
    1.55 +
    1.56 +    private static final Logger logger = Logger.getLogger(
    1.57 +            com.sun.xml.internal.ws.util.Constants.LoggingDomain + ".wsdl.patcher");
    1.58 +
    1.59 +    private final DocumentLocationResolver docResolver;
    1.60 +    private final PortAddressResolver portAddressResolver;
    1.61 +
    1.62 +    //
    1.63 +    // fields accumulated as we parse through documents
    1.64 +    //
    1.65 +    private String targetNamespace;
    1.66 +    private QName serviceName;
    1.67 +    private QName portName;
    1.68 +    private String portAddress;
    1.69 +
    1.70 +    // true inside <wsdl:service>/<wsdl:part>/<wsa:EndpointReference>
    1.71 +    private boolean inEpr;
    1.72 +    // true inside <wsdl:service>/<wsdl:part>/<wsa:EndpointReference>/<wsa:Address>
    1.73 +    private boolean inEprAddress;
    1.74 +
    1.75 +    /**
    1.76 +     * Creates a {@link WSDLPatcher} for patching WSDL.
    1.77 +     *
    1.78 +     * @param portAddressResolver
    1.79 +     *      address of the endpoint is resolved using this docResolver.
    1.80 +     * @param docResolver
    1.81 +     *      Consulted to get the import/include document locations.
    1.82 +     *      Must not be null.
    1.83 +     */
    1.84 +    public WSDLPatcher(@NotNull PortAddressResolver portAddressResolver,
    1.85 +            @NotNull DocumentLocationResolver docResolver) {
    1.86 +        this.portAddressResolver = portAddressResolver;
    1.87 +        this.docResolver = docResolver;
    1.88 +    }
    1.89 +
    1.90 +    @Override
    1.91 +    protected void handleAttribute(int i) throws XMLStreamException {
    1.92 +        QName name = in.getName();
    1.93 +        String attLocalName = in.getAttributeLocalName(i);
    1.94 +
    1.95 +        if((name.equals(SCHEMA_INCLUDE_QNAME) && attLocalName.equals("schemaLocation"))
    1.96 +        || (name.equals(SCHEMA_IMPORT_QNAME)  && attLocalName.equals("schemaLocation"))
    1.97 +        || (name.equals(SCHEMA_REDEFINE_QNAME)  && attLocalName.equals("schemaLocation"))
    1.98 +        || (name.equals(WSDLConstants.QNAME_IMPORT)  && attLocalName.equals("location"))) {
    1.99 +            // patch this attribute value.
   1.100 +
   1.101 +            String relPath = in.getAttributeValue(i);
   1.102 +            String actualPath = getPatchedImportLocation(relPath);
   1.103 +            if (actualPath == null) {
   1.104 +                return; // skip this attribute to leave it up to "implicit reference".
   1.105 +            }
   1.106 +
   1.107 +            logger.fine("Fixing the relative location:"+relPath
   1.108 +                    +" with absolute location:"+actualPath);
   1.109 +            writeAttribute(i, actualPath);
   1.110 +            return;
   1.111 +        }
   1.112 +
   1.113 +        if (name.equals(WSDLConstants.NS_SOAP_BINDING_ADDRESS) ||
   1.114 +            name.equals(WSDLConstants.NS_SOAP12_BINDING_ADDRESS)) {
   1.115 +
   1.116 +            if(attLocalName.equals("location")) {
   1.117 +                portAddress = in.getAttributeValue(i);
   1.118 +                String value = getAddressLocation();
   1.119 +                if (value != null) {
   1.120 +                    logger.fine("Service:"+serviceName+ " port:"+portName
   1.121 +                            + " current address "+portAddress+" Patching it with "+value);
   1.122 +                    writeAttribute(i, value);
   1.123 +                    return;
   1.124 +                }
   1.125 +            }
   1.126 +        }
   1.127 +
   1.128 +        super.handleAttribute(i);
   1.129 +    }
   1.130 +
   1.131 +    /**
   1.132 +     * Writes out an {@code i}-th attribute but with a different value.
   1.133 +     * @param i attribute index
   1.134 +     * @param value attribute value
   1.135 +     * @throws XMLStreamException when an error encountered while writing attribute
   1.136 +     */
   1.137 +    private void writeAttribute(int i, String value) throws XMLStreamException {
   1.138 +        String nsUri = in.getAttributeNamespace(i);
   1.139 +        if(nsUri!=null)
   1.140 +            out.writeAttribute( in.getAttributePrefix(i), nsUri, in.getAttributeLocalName(i), value );
   1.141 +        else
   1.142 +            out.writeAttribute( in.getAttributeLocalName(i), value );
   1.143 +    }
   1.144 +
   1.145 +    @Override
   1.146 +    protected void handleStartElement() throws XMLStreamException {
   1.147 +        QName name = in.getName();
   1.148 +
   1.149 +        if (name.equals(WSDLConstants.QNAME_DEFINITIONS)) {
   1.150 +            String value = in.getAttributeValue(null,"targetNamespace");
   1.151 +            if (value != null) {
   1.152 +                targetNamespace = value;
   1.153 +            }
   1.154 +        } else if (name.equals(WSDLConstants.QNAME_SERVICE)) {
   1.155 +            String value = in.getAttributeValue(null,"name");
   1.156 +            if (value != null) {
   1.157 +                serviceName = new QName(targetNamespace, value);
   1.158 +            }
   1.159 +        } else if (name.equals(WSDLConstants.QNAME_PORT)) {
   1.160 +            String value = in.getAttributeValue(null,"name");
   1.161 +            if (value != null) {
   1.162 +                portName = new QName(targetNamespace,value);
   1.163 +            }
   1.164 +        } else if (name.equals(W3CAddressingConstants.WSA_EPR_QNAME)) {
   1.165 +            if (serviceName != null && portName != null) {
   1.166 +                inEpr = true;
   1.167 +            }
   1.168 +        } else if (name.equals(W3CAddressingConstants.WSA_ADDRESS_QNAME)) {
   1.169 +            if (inEpr) {
   1.170 +                inEprAddress = true;
   1.171 +            }
   1.172 +        }
   1.173 +        super.handleStartElement();
   1.174 +    }
   1.175 +
   1.176 +    @Override
   1.177 +    protected void handleEndElement() throws XMLStreamException {
   1.178 +        QName name = in.getName();
   1.179 +        if (name.equals(WSDLConstants.QNAME_SERVICE)) {
   1.180 +            serviceName = null;
   1.181 +        } else if (name.equals(WSDLConstants.QNAME_PORT)) {
   1.182 +            portName = null;
   1.183 +        } else if (name.equals(W3CAddressingConstants.WSA_EPR_QNAME)) {
   1.184 +            if (inEpr) {
   1.185 +                inEpr = false;
   1.186 +            }
   1.187 +        } else if (name.equals(W3CAddressingConstants.WSA_ADDRESS_QNAME)) {
   1.188 +            if (inEprAddress) {
   1.189 +                String value = getAddressLocation();
   1.190 +                if (value != null) {
   1.191 +                    logger.fine("Fixing EPR Address for service:"+serviceName+ " port:"+portName
   1.192 +                                + " address with "+value);
   1.193 +                    out.writeCharacters(value);
   1.194 +                }
   1.195 +                inEprAddress = false;
   1.196 +            }
   1.197 +        }
   1.198 +        super.handleEndElement();
   1.199 +    }
   1.200 +
   1.201 +    @Override
   1.202 +    protected void handleCharacters() throws XMLStreamException {
   1.203 +        // handleCharacters() may be called multiple times.
   1.204 +        if (inEprAddress) {
   1.205 +            String value = getAddressLocation();
   1.206 +            if (value != null) {
   1.207 +                // will write the address with <wsa:Address> end element
   1.208 +                return;
   1.209 +            }
   1.210 +        }
   1.211 +        super.handleCharacters();
   1.212 +    }
   1.213 +
   1.214 +    /**
   1.215 +     * Returns the location to be placed into the generated document.
   1.216 +     *
   1.217 +     * @param relPath relative URI to be resolved
   1.218 +     * @return
   1.219 +     *      null to leave it to the "implicit reference".
   1.220 +     */
   1.221 +    private @Nullable String getPatchedImportLocation(String relPath) {
   1.222 +        return docResolver.getLocationFor(null, relPath);
   1.223 +    }
   1.224 +
   1.225 +    /**
   1.226 +     * For the given service, port names it matches the correct endpoint and
   1.227 +     * reutrns its endpoint address
   1.228 +     *
   1.229 +     * @return returns the resolved endpoint address
   1.230 +     */
   1.231 +    private String getAddressLocation() {
   1.232 +        return (portAddressResolver == null || portName == null)
   1.233 +                ? null : portAddressResolver.getAddressFor(serviceName, portName.getLocalPart(), portAddress);
   1.234 +    }
   1.235 +}

mercurial