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

Thu, 31 Aug 2017 15:18:52 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:18:52 +0800
changeset 637
9c07ef4934dd
parent 515
6cd506508147
parent 0
373ffda63c9a
child 760
e530533619ec
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         // EPR constructors are private, so we need privilege escalation.
   151         // this unmarshalling can only access instances of a fixed, known set of classes,
   152         // so doing that shouldn't introduce security vulnerability.
   153         return AccessController.doPrivileged(new PrivilegedAction<EndpointReference>() {
   154             public EndpointReference run() {
   155                 try {
   156                     Unmarshaller unmarshaller = eprjc.get().createUnmarshaller();
   157                     return (EndpointReference) unmarshaller.unmarshal(eprInfoset);
   158                 } catch (JAXBException e) {
   159                     throw new WebServiceException("Error creating Marshaller or marshalling.", e);
   160                 }
   161             }
   162         });
   163     }
   165     public <T> T getPort(EndpointReference endpointReference, Class<T> clazz, WebServiceFeature... webServiceFeatures) {
   166         /*
   167         final @NotNull MemberSubmissionEndpointReference msepr =
   168                 EndpointReferenceUtil.transform(MemberSubmissionEndpointReference.class, endpointReference);
   169                 WSService service = new WSServiceDelegate(msepr.toWSDLSource(), msepr.serviceName.name, Service.class);
   170                 */
   171         if(endpointReference == null)
   172             throw new WebServiceException(ProviderApiMessages.NULL_EPR());
   173         WSEndpointReference wsepr =  new WSEndpointReference(endpointReference);
   174         WSEndpointReference.Metadata metadata = wsepr.getMetaData();
   175         WSService service;
   176         if(metadata.getWsdlSource() != null)
   177             service = (WSService) createServiceDelegate(metadata.getWsdlSource(), metadata.getServiceName(), Service.class);
   178         else
   179             throw new WebServiceException("WSDL metadata is missing in EPR");
   180         return service.getPort(wsepr, clazz, webServiceFeatures);
   181     }
   183     public W3CEndpointReference createW3CEndpointReference(String address, QName serviceName, QName portName, List<Element> metadata, String wsdlDocumentLocation, List<Element> referenceParameters) {
   184         return createW3CEndpointReference(address, null, serviceName, portName, metadata, wsdlDocumentLocation, referenceParameters, null, null);
   185     }
   187     public W3CEndpointReference createW3CEndpointReference(String address, QName interfaceName, QName serviceName, QName portName,
   188             List<Element> metadata, String wsdlDocumentLocation, List<Element> referenceParameters,
   189             List<Element> elements, Map<QName, String> attributes) {
   190         Container container = ContainerResolver.getInstance().getContainer();
   191         if (address == null) {
   192             if (serviceName == null || portName == null) {
   193                 throw new IllegalStateException(ProviderApiMessages.NULL_ADDRESS_SERVICE_ENDPOINT());
   194             } else {
   195                 //check if it is run in a Java EE Container and if so, get address using serviceName and portName
   196                 Module module = container.getSPI(Module.class);
   197                 if (module != null) {
   198                     List<BoundEndpoint> beList = module.getBoundEndpoints();
   199                     for (BoundEndpoint be : beList) {
   200                         WSEndpoint wse = be.getEndpoint();
   201                         if (wse.getServiceName().equals(serviceName) && wse.getPortName().equals(portName)) {
   202                             try {
   203                                 address = be.getAddress().toString();
   204                             } catch (WebServiceException e) {
   205                                 // May be the container does n't support this
   206                                 //just ignore the exception
   207                             }
   208                             break;
   209                         }
   210                     }
   211                 }
   212                 //address is still null? may be its not run in a JavaEE Container
   213                 if (address == null)
   214                     throw new IllegalStateException(ProviderApiMessages.NULL_ADDRESS());
   215             }
   216         }
   217         if((serviceName==null) && (portName != null)) {
   218             throw new IllegalStateException(ProviderApiMessages.NULL_SERVICE());
   219         }
   220         //Validate Service and Port in WSDL
   221         String wsdlTargetNamespace = null;
   222         if (wsdlDocumentLocation != null) {
   223             try {
   224                 EntityResolver er = XmlUtil.createDefaultCatalogResolver();
   226                 URL wsdlLoc = new URL(wsdlDocumentLocation);
   227                 WSDLModel wsdlDoc = RuntimeWSDLParser.parse(wsdlLoc, new StreamSource(wsdlLoc.toExternalForm()), er,
   228                         true, container, ServiceFinder.find(WSDLParserExtension.class).toArray());
   229                 if (serviceName != null) {
   230                     WSDLService wsdlService = wsdlDoc.getService(serviceName);
   231                     if (wsdlService == null)
   232                         throw new IllegalStateException(ProviderApiMessages.NOTFOUND_SERVICE_IN_WSDL(
   233                                 serviceName,wsdlDocumentLocation));
   234                     if (portName != null) {
   235                         WSDLPort wsdlPort = wsdlService.get(portName);
   236                         if (wsdlPort == null)
   237                             throw new IllegalStateException(ProviderApiMessages.NOTFOUND_PORT_IN_WSDL(
   238                                     portName,serviceName,wsdlDocumentLocation));
   239                     }
   240                     wsdlTargetNamespace = serviceName.getNamespaceURI();
   241                 } else {
   242                     QName firstService = wsdlDoc.getFirstServiceName();
   243                     wsdlTargetNamespace = firstService.getNamespaceURI();
   244                 }
   245             } catch (Exception e) {
   246                 throw new IllegalStateException(ProviderApiMessages.ERROR_WSDL(wsdlDocumentLocation),e);
   247             }
   248         }
   249         //wcf3.0/3.5 rejected empty metadata element.
   250         if (metadata != null && metadata.size() == 0) {
   251            metadata = null;
   252         }
   253         return new WSEndpointReference(
   254             AddressingVersion.fromSpecClass(W3CEndpointReference.class),
   255             address, serviceName, portName, interfaceName, metadata, wsdlDocumentLocation, wsdlTargetNamespace,referenceParameters, elements, attributes).toSpec(W3CEndpointReference.class);
   257     }
   259     private static JAXBContext getEPRJaxbContext() {
   260         // EPRs have package and private fields, so we need privilege escalation.
   261         // this access only fixed, known set of classes, so doing that
   262         // shouldn't introduce security vulnerability.
   263         return AccessController.doPrivileged(new PrivilegedAction<JAXBContext>() {
   264             public JAXBContext run() {
   265                 try {
   266                     return JAXBContext.newInstance(MemberSubmissionEndpointReference.class, W3CEndpointReference.class);
   267                 } catch (JAXBException e) {
   268                     throw new WebServiceException("Error creating JAXBContext for W3CEndpointReference. ", e);
   269                 }
   270             }
   271         });
   272     }
   273 }

mercurial