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

Thu, 12 Oct 2017 19:44:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 19:44:07 +0800
changeset 760
e530533619ec
parent 721
06807f9a6835
parent 637
9c07ef4934dd
permissions
-rw-r--r--

merge

     1 /*
     2  * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    24  */
    26 package com.sun.xml.internal.ws.spi;
    29 import com.sun.xml.internal.ws.api.BindingID;
    30 import com.sun.xml.internal.ws.api.WSService;
    31 import com.sun.xml.internal.ws.api.ServiceSharedFeatureMarker;
    32 import com.sun.xml.internal.ws.api.addressing.AddressingVersion;
    33 import com.sun.xml.internal.ws.api.addressing.WSEndpointReference;
    34 import com.sun.xml.internal.ws.api.model.wsdl.WSDLModel;
    35 import com.sun.xml.internal.ws.api.model.wsdl.WSDLPort;
    36 import com.sun.xml.internal.ws.api.model.wsdl.WSDLService;
    37 import com.sun.xml.internal.ws.api.server.BoundEndpoint;
    38 import com.sun.xml.internal.ws.api.server.Container;
    39 import com.sun.xml.internal.ws.api.server.ContainerResolver;
    40 import com.sun.xml.internal.ws.api.server.Module;
    41 import com.sun.xml.internal.ws.api.server.WSEndpoint;
    42 import com.sun.xml.internal.ws.api.wsdl.parser.WSDLParserExtension;
    43 import com.sun.xml.internal.ws.client.WSServiceDelegate;
    44 import com.sun.xml.internal.ws.developer.MemberSubmissionEndpointReference;
    45 import com.sun.xml.internal.ws.resources.ProviderApiMessages;
    46 import com.sun.xml.internal.ws.transport.http.server.EndpointImpl;
    47 import com.sun.xml.internal.ws.util.ServiceFinder;
    48 import com.sun.xml.internal.ws.util.xml.XmlUtil;
    49 import com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser;
    51 import org.w3c.dom.Element;
    52 import org.xml.sax.EntityResolver;
    54 import javax.xml.bind.JAXBContext;
    55 import javax.xml.bind.JAXBException;
    56 import javax.xml.bind.Unmarshaller;
    57 import javax.xml.namespace.QName;
    58 import javax.xml.transform.Source;
    59 import javax.xml.transform.stream.StreamSource;
    60 import javax.xml.ws.Endpoint;
    61 import javax.xml.ws.EndpointReference;
    62 import javax.xml.ws.Service;
    63 import javax.xml.ws.WebServiceException;
    64 import javax.xml.ws.WebServiceFeature;
    65 import javax.xml.ws.spi.Provider;
    66 import javax.xml.ws.spi.ServiceDelegate;
    67 import javax.xml.ws.spi.Invoker;
    68 import javax.xml.ws.wsaddressing.W3CEndpointReference;
    70 import java.net.URL;
    71 import java.security.AccessController;
    72 import java.security.PrivilegedAction;
    73 import java.util.List;
    74 import java.util.Map;
    76 /**
    77  * The entry point to the JAX-WS RI from the JAX-WS API.
    78  *
    79  * @author WS Development Team
    80  */
    81 public class ProviderImpl extends Provider {
    83     private final static ContextClassloaderLocal<JAXBContext> eprjc = new ContextClassloaderLocal<JAXBContext>() {
    84         @Override
    85         protected JAXBContext initialValue() throws Exception {
    86             return getEPRJaxbContext();
    87         }
    88     };
    90     /**
    91      * Convenient singleton instance.
    92      */
    93     public static final ProviderImpl INSTANCE = new ProviderImpl();
    95     @Override
    96     public Endpoint createEndpoint(String bindingId, Object implementor) {
    97         return new EndpointImpl(
    98             (bindingId != null) ? BindingID.parse(bindingId) : BindingID.parse(implementor.getClass()),
    99             implementor);
   100     }
   102     @Override
   103     public ServiceDelegate createServiceDelegate( URL wsdlDocumentLocation, QName serviceName, Class serviceClass) {
   104          return new WSServiceDelegate(wsdlDocumentLocation, serviceName, serviceClass);
   105     }
   107     public ServiceDelegate createServiceDelegate( URL wsdlDocumentLocation, QName serviceName, Class serviceClass,
   108                                                   WebServiceFeature ... features) {
   109         for (WebServiceFeature feature : features) {
   110             if (!(feature instanceof ServiceSharedFeatureMarker))
   111             throw new WebServiceException("Doesn't support any Service specific features");
   112         }
   113         return new WSServiceDelegate(wsdlDocumentLocation, serviceName, serviceClass, features);
   114     }
   116     public ServiceDelegate createServiceDelegate( Source wsdlSource, QName serviceName, Class serviceClass) {
   117         return new WSServiceDelegate(wsdlSource, serviceName, serviceClass);
   118    }
   120     @Override
   121     public Endpoint createAndPublishEndpoint(String address,
   122                                              Object implementor) {
   123         Endpoint endpoint = new EndpointImpl(
   124             BindingID.parse(implementor.getClass()),
   125             implementor);
   126         endpoint.publish(address);
   127         return endpoint;
   128     }
   130     public Endpoint createEndpoint(String bindingId, Object implementor, WebServiceFeature... features) {
   131         return new EndpointImpl(
   132             (bindingId != null) ? BindingID.parse(bindingId) : BindingID.parse(implementor.getClass()),
   133             implementor, features);
   134     }
   136     public Endpoint createAndPublishEndpoint(String address, Object implementor, WebServiceFeature... features) {
   137         Endpoint endpoint = new EndpointImpl(
   138             BindingID.parse(implementor.getClass()), implementor, features);
   139         endpoint.publish(address);
   140         return endpoint;
   141     }
   143     public Endpoint createEndpoint(String bindingId, Class implementorClass, Invoker invoker, WebServiceFeature... features) {
   144         return new EndpointImpl(
   145             (bindingId != null) ? BindingID.parse(bindingId) : BindingID.parse(implementorClass),
   146             implementorClass, invoker, features);
   147     }
   149     public EndpointReference readEndpointReference(final Source eprInfoset) {
   150         try {
   151             Unmarshaller unmarshaller = eprjc.get().createUnmarshaller();
   152             return (EndpointReference) unmarshaller.unmarshal(eprInfoset);
   153         } catch (JAXBException e) {
   154             throw new WebServiceException("Error creating Marshaller or marshalling.", e);
   155         }
   156     }
   158     public <T> T getPort(EndpointReference endpointReference, Class<T> clazz, WebServiceFeature... webServiceFeatures) {
   159         /*
   160         final @NotNull MemberSubmissionEndpointReference msepr =
   161                 EndpointReferenceUtil.transform(MemberSubmissionEndpointReference.class, endpointReference);
   162                 WSService service = new WSServiceDelegate(msepr.toWSDLSource(), msepr.serviceName.name, Service.class);
   163                 */
   164         if(endpointReference == null)
   165             throw new WebServiceException(ProviderApiMessages.NULL_EPR());
   166         WSEndpointReference wsepr =  new WSEndpointReference(endpointReference);
   167         WSEndpointReference.Metadata metadata = wsepr.getMetaData();
   168         WSService service;
   169         if(metadata.getWsdlSource() != null)
   170             service = (WSService) createServiceDelegate(metadata.getWsdlSource(), metadata.getServiceName(), Service.class);
   171         else
   172             throw new WebServiceException("WSDL metadata is missing in EPR");
   173         return service.getPort(wsepr, clazz, webServiceFeatures);
   174     }
   176     public W3CEndpointReference createW3CEndpointReference(String address, QName serviceName, QName portName, List<Element> metadata, String wsdlDocumentLocation, List<Element> referenceParameters) {
   177         return createW3CEndpointReference(address, null, serviceName, portName, metadata, wsdlDocumentLocation, referenceParameters, null, null);
   178     }
   180     public W3CEndpointReference createW3CEndpointReference(String address, QName interfaceName, QName serviceName, QName portName,
   181             List<Element> metadata, String wsdlDocumentLocation, List<Element> referenceParameters,
   182             List<Element> elements, Map<QName, String> attributes) {
   183         Container container = ContainerResolver.getInstance().getContainer();
   184         if (address == null) {
   185             if (serviceName == null || portName == null) {
   186                 throw new IllegalStateException(ProviderApiMessages.NULL_ADDRESS_SERVICE_ENDPOINT());
   187             } else {
   188                 //check if it is run in a Java EE Container and if so, get address using serviceName and portName
   189                 Module module = container.getSPI(Module.class);
   190                 if (module != null) {
   191                     List<BoundEndpoint> beList = module.getBoundEndpoints();
   192                     for (BoundEndpoint be : beList) {
   193                         WSEndpoint wse = be.getEndpoint();
   194                         if (wse.getServiceName().equals(serviceName) && wse.getPortName().equals(portName)) {
   195                             try {
   196                                 address = be.getAddress().toString();
   197                             } catch (WebServiceException e) {
   198                                 // May be the container does n't support this
   199                                 //just ignore the exception
   200                             }
   201                             break;
   202                         }
   203                     }
   204                 }
   205                 //address is still null? may be its not run in a JavaEE Container
   206                 if (address == null)
   207                     throw new IllegalStateException(ProviderApiMessages.NULL_ADDRESS());
   208             }
   209         }
   210         if((serviceName==null) && (portName != null)) {
   211             throw new IllegalStateException(ProviderApiMessages.NULL_SERVICE());
   212         }
   213         //Validate Service and Port in WSDL
   214         String wsdlTargetNamespace = null;
   215         if (wsdlDocumentLocation != null) {
   216             try {
   217                 EntityResolver er = XmlUtil.createDefaultCatalogResolver();
   219                 URL wsdlLoc = new URL(wsdlDocumentLocation);
   220                 WSDLModel wsdlDoc = RuntimeWSDLParser.parse(wsdlLoc, new StreamSource(wsdlLoc.toExternalForm()), er,
   221                         true, container, ServiceFinder.find(WSDLParserExtension.class).toArray());
   222                 if (serviceName != null) {
   223                     WSDLService wsdlService = wsdlDoc.getService(serviceName);
   224                     if (wsdlService == null)
   225                         throw new IllegalStateException(ProviderApiMessages.NOTFOUND_SERVICE_IN_WSDL(
   226                                 serviceName,wsdlDocumentLocation));
   227                     if (portName != null) {
   228                         WSDLPort wsdlPort = wsdlService.get(portName);
   229                         if (wsdlPort == null)
   230                             throw new IllegalStateException(ProviderApiMessages.NOTFOUND_PORT_IN_WSDL(
   231                                     portName,serviceName,wsdlDocumentLocation));
   232                     }
   233                     wsdlTargetNamespace = serviceName.getNamespaceURI();
   234                 } else {
   235                     QName firstService = wsdlDoc.getFirstServiceName();
   236                     wsdlTargetNamespace = firstService.getNamespaceURI();
   237                 }
   238             } catch (Exception e) {
   239                 throw new IllegalStateException(ProviderApiMessages.ERROR_WSDL(wsdlDocumentLocation),e);
   240             }
   241         }
   242         //wcf3.0/3.5 rejected empty metadata element.
   243         if (metadata != null && metadata.size() == 0) {
   244            metadata = null;
   245         }
   246         return new WSEndpointReference(
   247             AddressingVersion.fromSpecClass(W3CEndpointReference.class),
   248             address, serviceName, portName, interfaceName, metadata, wsdlDocumentLocation, wsdlTargetNamespace,referenceParameters, elements, attributes).toSpec(W3CEndpointReference.class);
   250     }
   252     private static JAXBContext getEPRJaxbContext() {
   253         // EPRs have package and private fields, so we need privilege escalation.
   254         // this access only fixed, known set of classes, so doing that
   255         // shouldn't introduce security vulnerability.
   256         return AccessController.doPrivileged(new PrivilegedAction<JAXBContext>() {
   257             public JAXBContext run() {
   258                 try {
   259                     return JAXBContext.newInstance(MemberSubmissionEndpointReference.class, W3CEndpointReference.class);
   260                 } catch (JAXBException e) {
   261                     throw new WebServiceException("Error creating JAXBContext for W3CEndpointReference. ", e);
   262                 }
   263             }
   264         });
   265     }
   266 }

mercurial