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