src/share/jaxws_classes/com/sun/xml/internal/ws/spi/ProviderImpl.java

changeset 0
373ffda63c9a
child 637
9c07ef4934dd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/ws/spi/ProviderImpl.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,273 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2014, 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.spi;
    1.30 +
    1.31 +
    1.32 +import com.sun.xml.internal.ws.api.BindingID;
    1.33 +import com.sun.xml.internal.ws.api.WSService;
    1.34 +import com.sun.xml.internal.ws.api.ServiceSharedFeatureMarker;
    1.35 +import com.sun.xml.internal.ws.api.addressing.AddressingVersion;
    1.36 +import com.sun.xml.internal.ws.api.addressing.WSEndpointReference;
    1.37 +import com.sun.xml.internal.ws.api.model.wsdl.WSDLModel;
    1.38 +import com.sun.xml.internal.ws.api.model.wsdl.WSDLPort;
    1.39 +import com.sun.xml.internal.ws.api.model.wsdl.WSDLService;
    1.40 +import com.sun.xml.internal.ws.api.server.BoundEndpoint;
    1.41 +import com.sun.xml.internal.ws.api.server.Container;
    1.42 +import com.sun.xml.internal.ws.api.server.ContainerResolver;
    1.43 +import com.sun.xml.internal.ws.api.server.Module;
    1.44 +import com.sun.xml.internal.ws.api.server.WSEndpoint;
    1.45 +import com.sun.xml.internal.ws.api.wsdl.parser.WSDLParserExtension;
    1.46 +import com.sun.xml.internal.ws.client.WSServiceDelegate;
    1.47 +import com.sun.xml.internal.ws.developer.MemberSubmissionEndpointReference;
    1.48 +import com.sun.xml.internal.ws.resources.ProviderApiMessages;
    1.49 +import com.sun.xml.internal.ws.transport.http.server.EndpointImpl;
    1.50 +import com.sun.xml.internal.ws.util.ServiceFinder;
    1.51 +import com.sun.xml.internal.ws.util.xml.XmlUtil;
    1.52 +import com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser;
    1.53 +
    1.54 +import org.w3c.dom.Element;
    1.55 +import org.xml.sax.EntityResolver;
    1.56 +
    1.57 +import javax.xml.bind.JAXBContext;
    1.58 +import javax.xml.bind.JAXBException;
    1.59 +import javax.xml.bind.Unmarshaller;
    1.60 +import javax.xml.namespace.QName;
    1.61 +import javax.xml.transform.Source;
    1.62 +import javax.xml.transform.stream.StreamSource;
    1.63 +import javax.xml.ws.Endpoint;
    1.64 +import javax.xml.ws.EndpointReference;
    1.65 +import javax.xml.ws.Service;
    1.66 +import javax.xml.ws.WebServiceException;
    1.67 +import javax.xml.ws.WebServiceFeature;
    1.68 +import javax.xml.ws.spi.Provider;
    1.69 +import javax.xml.ws.spi.ServiceDelegate;
    1.70 +import javax.xml.ws.spi.Invoker;
    1.71 +import javax.xml.ws.wsaddressing.W3CEndpointReference;
    1.72 +
    1.73 +import java.net.URL;
    1.74 +import java.security.AccessController;
    1.75 +import java.security.PrivilegedAction;
    1.76 +import java.util.List;
    1.77 +import java.util.Map;
    1.78 +
    1.79 +/**
    1.80 + * The entry point to the JAX-WS RI from the JAX-WS API.
    1.81 + *
    1.82 + * @author WS Development Team
    1.83 + */
    1.84 +public class ProviderImpl extends Provider {
    1.85 +
    1.86 +    private final static ContextClassloaderLocal<JAXBContext> eprjc = new ContextClassloaderLocal<JAXBContext>() {
    1.87 +        @Override
    1.88 +        protected JAXBContext initialValue() throws Exception {
    1.89 +            return getEPRJaxbContext();
    1.90 +        }
    1.91 +    };
    1.92 +
    1.93 +    /**
    1.94 +     * Convenient singleton instance.
    1.95 +     */
    1.96 +    public static final ProviderImpl INSTANCE = new ProviderImpl();
    1.97 +
    1.98 +    @Override
    1.99 +    public Endpoint createEndpoint(String bindingId, Object implementor) {
   1.100 +        return new EndpointImpl(
   1.101 +            (bindingId != null) ? BindingID.parse(bindingId) : BindingID.parse(implementor.getClass()),
   1.102 +            implementor);
   1.103 +    }
   1.104 +
   1.105 +    @Override
   1.106 +    public ServiceDelegate createServiceDelegate( URL wsdlDocumentLocation, QName serviceName, Class serviceClass) {
   1.107 +         return new WSServiceDelegate(wsdlDocumentLocation, serviceName, serviceClass);
   1.108 +    }
   1.109 +
   1.110 +    public ServiceDelegate createServiceDelegate( URL wsdlDocumentLocation, QName serviceName, Class serviceClass,
   1.111 +                                                  WebServiceFeature ... features) {
   1.112 +        for (WebServiceFeature feature : features) {
   1.113 +            if (!(feature instanceof ServiceSharedFeatureMarker))
   1.114 +            throw new WebServiceException("Doesn't support any Service specific features");
   1.115 +        }
   1.116 +        return new WSServiceDelegate(wsdlDocumentLocation, serviceName, serviceClass, features);
   1.117 +    }
   1.118 +
   1.119 +    public ServiceDelegate createServiceDelegate( Source wsdlSource, QName serviceName, Class serviceClass) {
   1.120 +        return new WSServiceDelegate(wsdlSource, serviceName, serviceClass);
   1.121 +   }
   1.122 +
   1.123 +    @Override
   1.124 +    public Endpoint createAndPublishEndpoint(String address,
   1.125 +                                             Object implementor) {
   1.126 +        Endpoint endpoint = new EndpointImpl(
   1.127 +            BindingID.parse(implementor.getClass()),
   1.128 +            implementor);
   1.129 +        endpoint.publish(address);
   1.130 +        return endpoint;
   1.131 +    }
   1.132 +
   1.133 +    public Endpoint createEndpoint(String bindingId, Object implementor, WebServiceFeature... features) {
   1.134 +        return new EndpointImpl(
   1.135 +            (bindingId != null) ? BindingID.parse(bindingId) : BindingID.parse(implementor.getClass()),
   1.136 +            implementor, features);
   1.137 +    }
   1.138 +
   1.139 +    public Endpoint createAndPublishEndpoint(String address, Object implementor, WebServiceFeature... features) {
   1.140 +        Endpoint endpoint = new EndpointImpl(
   1.141 +            BindingID.parse(implementor.getClass()), implementor, features);
   1.142 +        endpoint.publish(address);
   1.143 +        return endpoint;
   1.144 +    }
   1.145 +
   1.146 +    public Endpoint createEndpoint(String bindingId, Class implementorClass, Invoker invoker, WebServiceFeature... features) {
   1.147 +        return new EndpointImpl(
   1.148 +            (bindingId != null) ? BindingID.parse(bindingId) : BindingID.parse(implementorClass),
   1.149 +            implementorClass, invoker, features);
   1.150 +    }
   1.151 +
   1.152 +    public EndpointReference readEndpointReference(final Source eprInfoset) {
   1.153 +        // EPR constructors are private, so we need privilege escalation.
   1.154 +        // this unmarshalling can only access instances of a fixed, known set of classes,
   1.155 +        // so doing that shouldn't introduce security vulnerability.
   1.156 +        return AccessController.doPrivileged(new PrivilegedAction<EndpointReference>() {
   1.157 +            public EndpointReference run() {
   1.158 +                try {
   1.159 +                    Unmarshaller unmarshaller = eprjc.get().createUnmarshaller();
   1.160 +                    return (EndpointReference) unmarshaller.unmarshal(eprInfoset);
   1.161 +                } catch (JAXBException e) {
   1.162 +                    throw new WebServiceException("Error creating Marshaller or marshalling.", e);
   1.163 +                }
   1.164 +            }
   1.165 +        });
   1.166 +    }
   1.167 +
   1.168 +    public <T> T getPort(EndpointReference endpointReference, Class<T> clazz, WebServiceFeature... webServiceFeatures) {
   1.169 +        /*
   1.170 +        final @NotNull MemberSubmissionEndpointReference msepr =
   1.171 +                EndpointReferenceUtil.transform(MemberSubmissionEndpointReference.class, endpointReference);
   1.172 +                WSService service = new WSServiceDelegate(msepr.toWSDLSource(), msepr.serviceName.name, Service.class);
   1.173 +                */
   1.174 +        if(endpointReference == null)
   1.175 +            throw new WebServiceException(ProviderApiMessages.NULL_EPR());
   1.176 +        WSEndpointReference wsepr =  new WSEndpointReference(endpointReference);
   1.177 +        WSEndpointReference.Metadata metadata = wsepr.getMetaData();
   1.178 +        WSService service;
   1.179 +        if(metadata.getWsdlSource() != null)
   1.180 +            service = (WSService) createServiceDelegate(metadata.getWsdlSource(), metadata.getServiceName(), Service.class);
   1.181 +        else
   1.182 +            throw new WebServiceException("WSDL metadata is missing in EPR");
   1.183 +        return service.getPort(wsepr, clazz, webServiceFeatures);
   1.184 +    }
   1.185 +
   1.186 +    public W3CEndpointReference createW3CEndpointReference(String address, QName serviceName, QName portName, List<Element> metadata, String wsdlDocumentLocation, List<Element> referenceParameters) {
   1.187 +        return createW3CEndpointReference(address, null, serviceName, portName, metadata, wsdlDocumentLocation, referenceParameters, null, null);
   1.188 +    }
   1.189 +
   1.190 +    public W3CEndpointReference createW3CEndpointReference(String address, QName interfaceName, QName serviceName, QName portName,
   1.191 +            List<Element> metadata, String wsdlDocumentLocation, List<Element> referenceParameters,
   1.192 +            List<Element> elements, Map<QName, String> attributes) {
   1.193 +        Container container = ContainerResolver.getInstance().getContainer();
   1.194 +        if (address == null) {
   1.195 +            if (serviceName == null || portName == null) {
   1.196 +                throw new IllegalStateException(ProviderApiMessages.NULL_ADDRESS_SERVICE_ENDPOINT());
   1.197 +            } else {
   1.198 +                //check if it is run in a Java EE Container and if so, get address using serviceName and portName
   1.199 +                Module module = container.getSPI(Module.class);
   1.200 +                if (module != null) {
   1.201 +                    List<BoundEndpoint> beList = module.getBoundEndpoints();
   1.202 +                    for (BoundEndpoint be : beList) {
   1.203 +                        WSEndpoint wse = be.getEndpoint();
   1.204 +                        if (wse.getServiceName().equals(serviceName) && wse.getPortName().equals(portName)) {
   1.205 +                            try {
   1.206 +                                address = be.getAddress().toString();
   1.207 +                            } catch (WebServiceException e) {
   1.208 +                                // May be the container does n't support this
   1.209 +                                //just ignore the exception
   1.210 +                            }
   1.211 +                            break;
   1.212 +                        }
   1.213 +                    }
   1.214 +                }
   1.215 +                //address is still null? may be its not run in a JavaEE Container
   1.216 +                if (address == null)
   1.217 +                    throw new IllegalStateException(ProviderApiMessages.NULL_ADDRESS());
   1.218 +            }
   1.219 +        }
   1.220 +        if((serviceName==null) && (portName != null)) {
   1.221 +            throw new IllegalStateException(ProviderApiMessages.NULL_SERVICE());
   1.222 +        }
   1.223 +        //Validate Service and Port in WSDL
   1.224 +        String wsdlTargetNamespace = null;
   1.225 +        if (wsdlDocumentLocation != null) {
   1.226 +            try {
   1.227 +                EntityResolver er = XmlUtil.createDefaultCatalogResolver();
   1.228 +
   1.229 +                URL wsdlLoc = new URL(wsdlDocumentLocation);
   1.230 +                WSDLModel wsdlDoc = RuntimeWSDLParser.parse(wsdlLoc, new StreamSource(wsdlLoc.toExternalForm()), er,
   1.231 +                        true, container, ServiceFinder.find(WSDLParserExtension.class).toArray());
   1.232 +                if (serviceName != null) {
   1.233 +                    WSDLService wsdlService = wsdlDoc.getService(serviceName);
   1.234 +                    if (wsdlService == null)
   1.235 +                        throw new IllegalStateException(ProviderApiMessages.NOTFOUND_SERVICE_IN_WSDL(
   1.236 +                                serviceName,wsdlDocumentLocation));
   1.237 +                    if (portName != null) {
   1.238 +                        WSDLPort wsdlPort = wsdlService.get(portName);
   1.239 +                        if (wsdlPort == null)
   1.240 +                            throw new IllegalStateException(ProviderApiMessages.NOTFOUND_PORT_IN_WSDL(
   1.241 +                                    portName,serviceName,wsdlDocumentLocation));
   1.242 +                    }
   1.243 +                    wsdlTargetNamespace = serviceName.getNamespaceURI();
   1.244 +                } else {
   1.245 +                    QName firstService = wsdlDoc.getFirstServiceName();
   1.246 +                    wsdlTargetNamespace = firstService.getNamespaceURI();
   1.247 +                }
   1.248 +            } catch (Exception e) {
   1.249 +                throw new IllegalStateException(ProviderApiMessages.ERROR_WSDL(wsdlDocumentLocation),e);
   1.250 +            }
   1.251 +        }
   1.252 +        //wcf3.0/3.5 rejected empty metadata element.
   1.253 +        if (metadata != null && metadata.size() == 0) {
   1.254 +           metadata = null;
   1.255 +        }
   1.256 +        return new WSEndpointReference(
   1.257 +            AddressingVersion.fromSpecClass(W3CEndpointReference.class),
   1.258 +            address, serviceName, portName, interfaceName, metadata, wsdlDocumentLocation, wsdlTargetNamespace,referenceParameters, elements, attributes).toSpec(W3CEndpointReference.class);
   1.259 +
   1.260 +    }
   1.261 +
   1.262 +    private static JAXBContext getEPRJaxbContext() {
   1.263 +        // EPRs have package and private fields, so we need privilege escalation.
   1.264 +        // this access only fixed, known set of classes, so doing that
   1.265 +        // shouldn't introduce security vulnerability.
   1.266 +        return AccessController.doPrivileged(new PrivilegedAction<JAXBContext>() {
   1.267 +            public JAXBContext run() {
   1.268 +                try {
   1.269 +                    return JAXBContext.newInstance(MemberSubmissionEndpointReference.class, W3CEndpointReference.class);
   1.270 +                } catch (JAXBException e) {
   1.271 +                    throw new WebServiceException("Error creating JAXBContext for W3CEndpointReference. ", e);
   1.272 +                }
   1.273 +            }
   1.274 +        });
   1.275 +    }
   1.276 +}

mercurial