aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.xml.internal.ws.spi; aoqi@0: aoqi@0: aoqi@0: import com.sun.xml.internal.ws.api.BindingID; aoqi@0: import com.sun.xml.internal.ws.api.WSService; aoqi@0: import com.sun.xml.internal.ws.api.ServiceSharedFeatureMarker; aoqi@0: import com.sun.xml.internal.ws.api.addressing.AddressingVersion; aoqi@0: import com.sun.xml.internal.ws.api.addressing.WSEndpointReference; aoqi@0: import com.sun.xml.internal.ws.api.model.wsdl.WSDLModel; aoqi@0: import com.sun.xml.internal.ws.api.model.wsdl.WSDLPort; aoqi@0: import com.sun.xml.internal.ws.api.model.wsdl.WSDLService; aoqi@0: import com.sun.xml.internal.ws.api.server.BoundEndpoint; aoqi@0: import com.sun.xml.internal.ws.api.server.Container; aoqi@0: import com.sun.xml.internal.ws.api.server.ContainerResolver; aoqi@0: import com.sun.xml.internal.ws.api.server.Module; aoqi@0: import com.sun.xml.internal.ws.api.server.WSEndpoint; aoqi@0: import com.sun.xml.internal.ws.api.wsdl.parser.WSDLParserExtension; aoqi@0: import com.sun.xml.internal.ws.client.WSServiceDelegate; aoqi@0: import com.sun.xml.internal.ws.developer.MemberSubmissionEndpointReference; aoqi@0: import com.sun.xml.internal.ws.resources.ProviderApiMessages; aoqi@0: import com.sun.xml.internal.ws.transport.http.server.EndpointImpl; aoqi@0: import com.sun.xml.internal.ws.util.ServiceFinder; aoqi@0: import com.sun.xml.internal.ws.util.xml.XmlUtil; aoqi@0: import com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser; aoqi@0: aoqi@0: import org.w3c.dom.Element; aoqi@0: import org.xml.sax.EntityResolver; aoqi@0: aoqi@0: import javax.xml.bind.JAXBContext; aoqi@0: import javax.xml.bind.JAXBException; aoqi@0: import javax.xml.bind.Unmarshaller; aoqi@0: import javax.xml.namespace.QName; aoqi@0: import javax.xml.transform.Source; aoqi@0: import javax.xml.transform.stream.StreamSource; aoqi@0: import javax.xml.ws.Endpoint; aoqi@0: import javax.xml.ws.EndpointReference; aoqi@0: import javax.xml.ws.Service; aoqi@0: import javax.xml.ws.WebServiceException; aoqi@0: import javax.xml.ws.WebServiceFeature; aoqi@0: import javax.xml.ws.spi.Provider; aoqi@0: import javax.xml.ws.spi.ServiceDelegate; aoqi@0: import javax.xml.ws.spi.Invoker; aoqi@0: import javax.xml.ws.wsaddressing.W3CEndpointReference; aoqi@0: aoqi@0: import java.net.URL; aoqi@0: import java.security.AccessController; aoqi@0: import java.security.PrivilegedAction; aoqi@0: import java.util.List; aoqi@0: import java.util.Map; aoqi@0: aoqi@0: /** aoqi@0: * The entry point to the JAX-WS RI from the JAX-WS API. aoqi@0: * aoqi@0: * @author WS Development Team aoqi@0: */ aoqi@0: public class ProviderImpl extends Provider { aoqi@0: aoqi@0: private final static ContextClassloaderLocal eprjc = new ContextClassloaderLocal() { aoqi@0: @Override aoqi@0: protected JAXBContext initialValue() throws Exception { aoqi@0: return getEPRJaxbContext(); aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: /** aoqi@0: * Convenient singleton instance. aoqi@0: */ aoqi@0: public static final ProviderImpl INSTANCE = new ProviderImpl(); aoqi@0: aoqi@0: @Override aoqi@0: public Endpoint createEndpoint(String bindingId, Object implementor) { aoqi@0: return new EndpointImpl( aoqi@0: (bindingId != null) ? BindingID.parse(bindingId) : BindingID.parse(implementor.getClass()), aoqi@0: implementor); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public ServiceDelegate createServiceDelegate( URL wsdlDocumentLocation, QName serviceName, Class serviceClass) { aoqi@0: return new WSServiceDelegate(wsdlDocumentLocation, serviceName, serviceClass); aoqi@0: } aoqi@0: aoqi@0: public ServiceDelegate createServiceDelegate( URL wsdlDocumentLocation, QName serviceName, Class serviceClass, aoqi@0: WebServiceFeature ... features) { aoqi@0: for (WebServiceFeature feature : features) { aoqi@0: if (!(feature instanceof ServiceSharedFeatureMarker)) aoqi@0: throw new WebServiceException("Doesn't support any Service specific features"); aoqi@0: } aoqi@0: return new WSServiceDelegate(wsdlDocumentLocation, serviceName, serviceClass, features); aoqi@0: } aoqi@0: aoqi@0: public ServiceDelegate createServiceDelegate( Source wsdlSource, QName serviceName, Class serviceClass) { aoqi@0: return new WSServiceDelegate(wsdlSource, serviceName, serviceClass); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public Endpoint createAndPublishEndpoint(String address, aoqi@0: Object implementor) { aoqi@0: Endpoint endpoint = new EndpointImpl( aoqi@0: BindingID.parse(implementor.getClass()), aoqi@0: implementor); aoqi@0: endpoint.publish(address); aoqi@0: return endpoint; aoqi@0: } aoqi@0: aoqi@0: public Endpoint createEndpoint(String bindingId, Object implementor, WebServiceFeature... features) { aoqi@0: return new EndpointImpl( aoqi@0: (bindingId != null) ? BindingID.parse(bindingId) : BindingID.parse(implementor.getClass()), aoqi@0: implementor, features); aoqi@0: } aoqi@0: aoqi@0: public Endpoint createAndPublishEndpoint(String address, Object implementor, WebServiceFeature... features) { aoqi@0: Endpoint endpoint = new EndpointImpl( aoqi@0: BindingID.parse(implementor.getClass()), implementor, features); aoqi@0: endpoint.publish(address); aoqi@0: return endpoint; aoqi@0: } aoqi@0: aoqi@0: public Endpoint createEndpoint(String bindingId, Class implementorClass, Invoker invoker, WebServiceFeature... features) { aoqi@0: return new EndpointImpl( aoqi@0: (bindingId != null) ? BindingID.parse(bindingId) : BindingID.parse(implementorClass), aoqi@0: implementorClass, invoker, features); aoqi@0: } aoqi@0: aoqi@0: public EndpointReference readEndpointReference(final Source eprInfoset) { mkos@721: try { mkos@721: Unmarshaller unmarshaller = eprjc.get().createUnmarshaller(); mkos@721: return (EndpointReference) unmarshaller.unmarshal(eprInfoset); mkos@721: } catch (JAXBException e) { mkos@721: throw new WebServiceException("Error creating Marshaller or marshalling.", e); mkos@721: } aoqi@0: } aoqi@0: aoqi@0: public T getPort(EndpointReference endpointReference, Class clazz, WebServiceFeature... webServiceFeatures) { aoqi@0: /* aoqi@0: final @NotNull MemberSubmissionEndpointReference msepr = aoqi@0: EndpointReferenceUtil.transform(MemberSubmissionEndpointReference.class, endpointReference); aoqi@0: WSService service = new WSServiceDelegate(msepr.toWSDLSource(), msepr.serviceName.name, Service.class); aoqi@0: */ aoqi@0: if(endpointReference == null) aoqi@0: throw new WebServiceException(ProviderApiMessages.NULL_EPR()); aoqi@0: WSEndpointReference wsepr = new WSEndpointReference(endpointReference); aoqi@0: WSEndpointReference.Metadata metadata = wsepr.getMetaData(); aoqi@0: WSService service; aoqi@0: if(metadata.getWsdlSource() != null) aoqi@0: service = (WSService) createServiceDelegate(metadata.getWsdlSource(), metadata.getServiceName(), Service.class); aoqi@0: else aoqi@0: throw new WebServiceException("WSDL metadata is missing in EPR"); aoqi@0: return service.getPort(wsepr, clazz, webServiceFeatures); aoqi@0: } aoqi@0: aoqi@0: public W3CEndpointReference createW3CEndpointReference(String address, QName serviceName, QName portName, List metadata, String wsdlDocumentLocation, List referenceParameters) { aoqi@0: return createW3CEndpointReference(address, null, serviceName, portName, metadata, wsdlDocumentLocation, referenceParameters, null, null); aoqi@0: } aoqi@0: aoqi@0: public W3CEndpointReference createW3CEndpointReference(String address, QName interfaceName, QName serviceName, QName portName, aoqi@0: List metadata, String wsdlDocumentLocation, List referenceParameters, aoqi@0: List elements, Map attributes) { aoqi@0: Container container = ContainerResolver.getInstance().getContainer(); aoqi@0: if (address == null) { aoqi@0: if (serviceName == null || portName == null) { aoqi@0: throw new IllegalStateException(ProviderApiMessages.NULL_ADDRESS_SERVICE_ENDPOINT()); aoqi@0: } else { aoqi@0: //check if it is run in a Java EE Container and if so, get address using serviceName and portName aoqi@0: Module module = container.getSPI(Module.class); aoqi@0: if (module != null) { aoqi@0: List beList = module.getBoundEndpoints(); aoqi@0: for (BoundEndpoint be : beList) { aoqi@0: WSEndpoint wse = be.getEndpoint(); aoqi@0: if (wse.getServiceName().equals(serviceName) && wse.getPortName().equals(portName)) { aoqi@0: try { aoqi@0: address = be.getAddress().toString(); aoqi@0: } catch (WebServiceException e) { aoqi@0: // May be the container does n't support this aoqi@0: //just ignore the exception aoqi@0: } aoqi@0: break; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: //address is still null? may be its not run in a JavaEE Container aoqi@0: if (address == null) aoqi@0: throw new IllegalStateException(ProviderApiMessages.NULL_ADDRESS()); aoqi@0: } aoqi@0: } aoqi@0: if((serviceName==null) && (portName != null)) { aoqi@0: throw new IllegalStateException(ProviderApiMessages.NULL_SERVICE()); aoqi@0: } aoqi@0: //Validate Service and Port in WSDL aoqi@0: String wsdlTargetNamespace = null; aoqi@0: if (wsdlDocumentLocation != null) { aoqi@0: try { aoqi@0: EntityResolver er = XmlUtil.createDefaultCatalogResolver(); aoqi@0: aoqi@0: URL wsdlLoc = new URL(wsdlDocumentLocation); aoqi@0: WSDLModel wsdlDoc = RuntimeWSDLParser.parse(wsdlLoc, new StreamSource(wsdlLoc.toExternalForm()), er, aoqi@0: true, container, ServiceFinder.find(WSDLParserExtension.class).toArray()); aoqi@0: if (serviceName != null) { aoqi@0: WSDLService wsdlService = wsdlDoc.getService(serviceName); aoqi@0: if (wsdlService == null) aoqi@0: throw new IllegalStateException(ProviderApiMessages.NOTFOUND_SERVICE_IN_WSDL( aoqi@0: serviceName,wsdlDocumentLocation)); aoqi@0: if (portName != null) { aoqi@0: WSDLPort wsdlPort = wsdlService.get(portName); aoqi@0: if (wsdlPort == null) aoqi@0: throw new IllegalStateException(ProviderApiMessages.NOTFOUND_PORT_IN_WSDL( aoqi@0: portName,serviceName,wsdlDocumentLocation)); aoqi@0: } aoqi@0: wsdlTargetNamespace = serviceName.getNamespaceURI(); aoqi@0: } else { aoqi@0: QName firstService = wsdlDoc.getFirstServiceName(); aoqi@0: wsdlTargetNamespace = firstService.getNamespaceURI(); aoqi@0: } aoqi@0: } catch (Exception e) { aoqi@0: throw new IllegalStateException(ProviderApiMessages.ERROR_WSDL(wsdlDocumentLocation),e); aoqi@0: } aoqi@0: } aoqi@0: //wcf3.0/3.5 rejected empty metadata element. aoqi@0: if (metadata != null && metadata.size() == 0) { aoqi@0: metadata = null; aoqi@0: } aoqi@0: return new WSEndpointReference( aoqi@0: AddressingVersion.fromSpecClass(W3CEndpointReference.class), aoqi@0: address, serviceName, portName, interfaceName, metadata, wsdlDocumentLocation, wsdlTargetNamespace,referenceParameters, elements, attributes).toSpec(W3CEndpointReference.class); aoqi@0: aoqi@0: } aoqi@0: aoqi@0: private static JAXBContext getEPRJaxbContext() { aoqi@0: // EPRs have package and private fields, so we need privilege escalation. aoqi@0: // this access only fixed, known set of classes, so doing that aoqi@0: // shouldn't introduce security vulnerability. aoqi@0: return AccessController.doPrivileged(new PrivilegedAction() { aoqi@0: public JAXBContext run() { aoqi@0: try { aoqi@0: return JAXBContext.newInstance(MemberSubmissionEndpointReference.class, W3CEndpointReference.class); aoqi@0: } catch (JAXBException e) { aoqi@0: throw new WebServiceException("Error creating JAXBContext for W3CEndpointReference. ", e); aoqi@0: } aoqi@0: } aoqi@0: }); aoqi@0: } aoqi@0: }