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

Fri, 04 Oct 2013 16:21:34 +0100

author
mkos
date
Fri, 04 Oct 2013 16:21:34 +0100
changeset 408
b0610cd08440
parent 368
0989ad8c0860
child 515
6cd506508147
permissions
-rw-r--r--

8025054: Update JAX-WS RI integration to 2.2.9-b130926.1035
Reviewed-by: chegar

     1 /*
     2  * Copyright (c) 1997, 2013, 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 JAXBContext eprjc = getEPRJaxbContext();
    85     /**
    86      * Convenient singleton instance.
    87      */
    88     public static final ProviderImpl INSTANCE = new ProviderImpl();
    90     @Override
    91     public Endpoint createEndpoint(String bindingId, Object implementor) {
    92         return new EndpointImpl(
    93             (bindingId != null) ? BindingID.parse(bindingId) : BindingID.parse(implementor.getClass()),
    94             implementor);
    95     }
    97     @Override
    98     public ServiceDelegate createServiceDelegate( URL wsdlDocumentLocation, QName serviceName, Class serviceClass) {
    99          return new WSServiceDelegate(wsdlDocumentLocation, serviceName, serviceClass);
   100     }
   102     public ServiceDelegate createServiceDelegate( URL wsdlDocumentLocation, QName serviceName, Class serviceClass,
   103                                                   WebServiceFeature ... features) {
   104         for (WebServiceFeature feature : features) {
   105             if (!(feature instanceof ServiceSharedFeatureMarker))
   106             throw new WebServiceException("Doesn't support any Service specific features");
   107         }
   108         return new WSServiceDelegate(wsdlDocumentLocation, serviceName, serviceClass, features);
   109     }
   111     public ServiceDelegate createServiceDelegate( Source wsdlSource, QName serviceName, Class serviceClass) {
   112         return new WSServiceDelegate(wsdlSource, serviceName, serviceClass);
   113    }
   115     @Override
   116     public Endpoint createAndPublishEndpoint(String address,
   117                                              Object implementor) {
   118         Endpoint endpoint = new EndpointImpl(
   119             BindingID.parse(implementor.getClass()),
   120             implementor);
   121         endpoint.publish(address);
   122         return endpoint;
   123     }
   125     public Endpoint createEndpoint(String bindingId, Object implementor, WebServiceFeature... features) {
   126         return new EndpointImpl(
   127             (bindingId != null) ? BindingID.parse(bindingId) : BindingID.parse(implementor.getClass()),
   128             implementor, features);
   129     }
   131     public Endpoint createAndPublishEndpoint(String address, Object implementor, WebServiceFeature... features) {
   132         Endpoint endpoint = new EndpointImpl(
   133             BindingID.parse(implementor.getClass()), implementor, features);
   134         endpoint.publish(address);
   135         return endpoint;
   136     }
   138     public Endpoint createEndpoint(String bindingId, Class implementorClass, Invoker invoker, WebServiceFeature... features) {
   139         return new EndpointImpl(
   140             (bindingId != null) ? BindingID.parse(bindingId) : BindingID.parse(implementorClass),
   141             implementorClass, invoker, features);
   142     }
   144     public EndpointReference readEndpointReference(final Source eprInfoset) {
   145         // EPR constructors are private, so we need privilege escalation.
   146         // this unmarshalling can only access instances of a fixed, known set of classes,
   147         // so doing that shouldn't introduce security vulnerability.
   148         return AccessController.doPrivileged(new PrivilegedAction<EndpointReference>() {
   149             public EndpointReference run() {
   150                 try {
   151                     Unmarshaller unmarshaller = eprjc.createUnmarshaller();
   152                     return (EndpointReference) unmarshaller.unmarshal(eprInfoset);
   153                 } catch (JAXBException e) {
   154                     throw new WebServiceException("Error creating Marshaller or marshalling.", e);
   155                 }
   156             }
   157         });
   158     }
   160     public <T> T getPort(EndpointReference endpointReference, Class<T> clazz, WebServiceFeature... webServiceFeatures) {
   161         /*
   162         final @NotNull MemberSubmissionEndpointReference msepr =
   163                 EndpointReferenceUtil.transform(MemberSubmissionEndpointReference.class, endpointReference);
   164                 WSService service = new WSServiceDelegate(msepr.toWSDLSource(), msepr.serviceName.name, Service.class);
   165                 */
   166         if(endpointReference == null)
   167             throw new WebServiceException(ProviderApiMessages.NULL_EPR());
   168         WSEndpointReference wsepr =  new WSEndpointReference(endpointReference);
   169         WSEndpointReference.Metadata metadata = wsepr.getMetaData();
   170         WSService service;
   171         if(metadata.getWsdlSource() != null)
   172             service = (WSService) createServiceDelegate(metadata.getWsdlSource(), metadata.getServiceName(), Service.class);
   173         else
   174             throw new WebServiceException("WSDL metadata is missing in EPR");
   175         return service.getPort(wsepr, clazz, webServiceFeatures);
   176     }
   178     public W3CEndpointReference createW3CEndpointReference(String address, QName serviceName, QName portName, List<Element> metadata, String wsdlDocumentLocation, List<Element> referenceParameters) {
   179         return createW3CEndpointReference(address, null, serviceName, portName, metadata, wsdlDocumentLocation, referenceParameters, null, null);
   180     }
   182     public W3CEndpointReference createW3CEndpointReference(String address, QName interfaceName, QName serviceName, QName portName,
   183             List<Element> metadata, String wsdlDocumentLocation, List<Element> referenceParameters,
   184             List<Element> elements, Map<QName, String> attributes) {
   185         Container container = ContainerResolver.getInstance().getContainer();
   186         if (address == null) {
   187             if (serviceName == null || portName == null) {
   188                 throw new IllegalStateException(ProviderApiMessages.NULL_ADDRESS_SERVICE_ENDPOINT());
   189             } else {
   190                 //check if it is run in a Java EE Container and if so, get address using serviceName and portName
   191                 Module module = container.getSPI(Module.class);
   192                 if (module != null) {
   193                     List<BoundEndpoint> beList = module.getBoundEndpoints();
   194                     for (BoundEndpoint be : beList) {
   195                         WSEndpoint wse = be.getEndpoint();
   196                         if (wse.getServiceName().equals(serviceName) && wse.getPortName().equals(portName)) {
   197                             try {
   198                                 address = be.getAddress().toString();
   199                             } catch (WebServiceException e) {
   200                                 // May be the container does n't support this
   201                                 //just ignore the exception
   202                             }
   203                             break;
   204                         }
   205                     }
   206                 }
   207                 //address is still null? may be its not run in a JavaEE Container
   208                 if (address == null)
   209                     throw new IllegalStateException(ProviderApiMessages.NULL_ADDRESS());
   210             }
   211         }
   212         if((serviceName==null) && (portName != null)) {
   213             throw new IllegalStateException(ProviderApiMessages.NULL_SERVICE());
   214         }
   215         //Validate Service and Port in WSDL
   216         String wsdlTargetNamespace = null;
   217         if (wsdlDocumentLocation != null) {
   218             try {
   219                 EntityResolver er = XmlUtil.createDefaultCatalogResolver();
   221                 URL wsdlLoc = new URL(wsdlDocumentLocation);
   222                 WSDLModel wsdlDoc = RuntimeWSDLParser.parse(wsdlLoc, new StreamSource(wsdlLoc.toExternalForm()), er,
   223                         true, container, ServiceFinder.find(WSDLParserExtension.class).toArray());
   224                 if (serviceName != null) {
   225                     WSDLService wsdlService = wsdlDoc.getService(serviceName);
   226                     if (wsdlService == null)
   227                         throw new IllegalStateException(ProviderApiMessages.NOTFOUND_SERVICE_IN_WSDL(
   228                                 serviceName,wsdlDocumentLocation));
   229                     if (portName != null) {
   230                         WSDLPort wsdlPort = wsdlService.get(portName);
   231                         if (wsdlPort == null)
   232                             throw new IllegalStateException(ProviderApiMessages.NOTFOUND_PORT_IN_WSDL(
   233                                     portName,serviceName,wsdlDocumentLocation));
   234                     }
   235                     wsdlTargetNamespace = serviceName.getNamespaceURI();
   236                 } else {
   237                     QName firstService = wsdlDoc.getFirstServiceName();
   238                     wsdlTargetNamespace = firstService.getNamespaceURI();
   239                 }
   240             } catch (Exception e) {
   241                 throw new IllegalStateException(ProviderApiMessages.ERROR_WSDL(wsdlDocumentLocation),e);
   242             }
   243         }
   244         //wcf3.0/3.5 rejected empty metadata element.
   245         if (metadata != null && metadata.size() == 0) {
   246            metadata = null;
   247         }
   248         return new WSEndpointReference(
   249             AddressingVersion.fromSpecClass(W3CEndpointReference.class),
   250             address, serviceName, portName, interfaceName, metadata, wsdlDocumentLocation, wsdlTargetNamespace,referenceParameters, elements, attributes).toSpec(W3CEndpointReference.class);
   252     }
   254     private static JAXBContext getEPRJaxbContext() {
   255         // EPRs have package and private fields, so we need privilege escalation.
   256         // this access only fixed, known set of classes, so doing that
   257         // shouldn't introduce security vulnerability.
   258         return AccessController.doPrivileged(new PrivilegedAction<JAXBContext>() {
   259             public JAXBContext run() {
   260                 try {
   261                     return JAXBContext.newInstance(MemberSubmissionEndpointReference.class, W3CEndpointReference.class);
   262                 } catch (JAXBException e) {
   263                     throw new WebServiceException("Error creating JAXBContext for W3CEndpointReference. ", e);
   264                 }
   265             }
   266         });
   267     }
   268 }

mercurial