1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/ws/spi/ProviderImpl.java Tue Mar 06 16:09:35 2012 -0800 1.3 @@ -0,0 +1,266 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.xml.internal.ws.spi; 1.30 + 1.31 + 1.32 +import com.sun.xml.internal.ws.api.BindingID; 1.33 +import com.sun.xml.internal.ws.api.WSService; 1.34 +import com.sun.xml.internal.ws.api.ServiceSharedFeatureMarker; 1.35 +import com.sun.xml.internal.ws.api.addressing.AddressingVersion; 1.36 +import com.sun.xml.internal.ws.api.addressing.WSEndpointReference; 1.37 +import com.sun.xml.internal.ws.api.model.wsdl.WSDLPort; 1.38 +import com.sun.xml.internal.ws.api.model.wsdl.WSDLService; 1.39 +import com.sun.xml.internal.ws.api.server.BoundEndpoint; 1.40 +import com.sun.xml.internal.ws.api.server.Container; 1.41 +import com.sun.xml.internal.ws.api.server.ContainerResolver; 1.42 +import com.sun.xml.internal.ws.api.server.Module; 1.43 +import com.sun.xml.internal.ws.api.server.WSEndpoint; 1.44 +import com.sun.xml.internal.ws.api.wsdl.parser.WSDLParserExtension; 1.45 +import com.sun.xml.internal.ws.client.WSServiceDelegate; 1.46 +import com.sun.xml.internal.ws.developer.MemberSubmissionEndpointReference; 1.47 +import com.sun.xml.internal.ws.model.wsdl.WSDLModelImpl; 1.48 +import com.sun.xml.internal.ws.resources.ProviderApiMessages; 1.49 +import com.sun.xml.internal.ws.transport.http.server.EndpointImpl; 1.50 +import com.sun.xml.internal.ws.util.ServiceFinder; 1.51 +import com.sun.xml.internal.ws.util.xml.XmlUtil; 1.52 +import com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser; 1.53 +import org.w3c.dom.Element; 1.54 +import org.xml.sax.EntityResolver; 1.55 + 1.56 +import javax.xml.bind.JAXBContext; 1.57 +import javax.xml.bind.JAXBException; 1.58 +import javax.xml.bind.Unmarshaller; 1.59 +import javax.xml.namespace.QName; 1.60 +import javax.xml.transform.Source; 1.61 +import javax.xml.transform.stream.StreamSource; 1.62 +import javax.xml.ws.Endpoint; 1.63 +import javax.xml.ws.EndpointReference; 1.64 +import javax.xml.ws.Service; 1.65 +import javax.xml.ws.WebServiceException; 1.66 +import javax.xml.ws.WebServiceFeature; 1.67 +import javax.xml.ws.spi.Provider; 1.68 +import javax.xml.ws.spi.ServiceDelegate; 1.69 +import javax.xml.ws.spi.Invoker; 1.70 +import javax.xml.ws.wsaddressing.W3CEndpointReference; 1.71 +import java.net.URL; 1.72 +import java.security.AccessController; 1.73 +import java.security.PrivilegedAction; 1.74 +import java.util.List; 1.75 +import java.util.Map; 1.76 + 1.77 +/** 1.78 + * The entry point to the JAX-WS RI from the JAX-WS API. 1.79 + * 1.80 + * @author WS Development Team 1.81 + */ 1.82 +public class ProviderImpl extends Provider { 1.83 + 1.84 + private final static JAXBContext eprjc = getEPRJaxbContext(); 1.85 + 1.86 + /** 1.87 + * Convenient singleton instance. 1.88 + */ 1.89 + public static final ProviderImpl INSTANCE = new ProviderImpl(); 1.90 + 1.91 + @Override 1.92 + public Endpoint createEndpoint(String bindingId, Object implementor) { 1.93 + return new EndpointImpl( 1.94 + (bindingId != null) ? BindingID.parse(bindingId) : BindingID.parse(implementor.getClass()), 1.95 + implementor); 1.96 + } 1.97 + 1.98 + @Override 1.99 + public ServiceDelegate createServiceDelegate( URL wsdlDocumentLocation, QName serviceName, Class serviceClass) { 1.100 + return new WSServiceDelegate(wsdlDocumentLocation, serviceName, serviceClass); 1.101 + } 1.102 + 1.103 + public ServiceDelegate createServiceDelegate( URL wsdlDocumentLocation, QName serviceName, Class serviceClass, 1.104 + WebServiceFeature ... features) { 1.105 + for (WebServiceFeature feature : features) { 1.106 + if (!(feature instanceof ServiceSharedFeatureMarker)) 1.107 + throw new WebServiceException("Doesn't support any Service specific features"); 1.108 + } 1.109 + return new WSServiceDelegate(wsdlDocumentLocation, serviceName, serviceClass, features); 1.110 + } 1.111 + 1.112 + public ServiceDelegate createServiceDelegate( Source wsdlSource, QName serviceName, Class serviceClass) { 1.113 + return new WSServiceDelegate(wsdlSource, serviceName, serviceClass); 1.114 + } 1.115 + 1.116 + @Override 1.117 + public Endpoint createAndPublishEndpoint(String address, 1.118 + Object implementor) { 1.119 + Endpoint endpoint = new EndpointImpl( 1.120 + BindingID.parse(implementor.getClass()), 1.121 + implementor); 1.122 + endpoint.publish(address); 1.123 + return endpoint; 1.124 + } 1.125 + 1.126 + public Endpoint createEndpoint(String bindingId, Object implementor, WebServiceFeature... features) { 1.127 + return new EndpointImpl( 1.128 + (bindingId != null) ? BindingID.parse(bindingId) : BindingID.parse(implementor.getClass()), 1.129 + implementor, features); 1.130 + } 1.131 + 1.132 + public Endpoint createAndPublishEndpoint(String address, Object implementor, WebServiceFeature... features) { 1.133 + Endpoint endpoint = new EndpointImpl( 1.134 + BindingID.parse(implementor.getClass()), implementor, features); 1.135 + endpoint.publish(address); 1.136 + return endpoint; 1.137 + } 1.138 + 1.139 + public Endpoint createEndpoint(String bindingId, Class implementorClass, Invoker invoker, WebServiceFeature... features) { 1.140 + return new EndpointImpl( 1.141 + (bindingId != null) ? BindingID.parse(bindingId) : BindingID.parse(implementorClass), 1.142 + implementorClass, invoker, features); 1.143 + } 1.144 + 1.145 + public EndpointReference readEndpointReference(final Source eprInfoset) { 1.146 + // EPR constructors are private, so we need privilege escalation. 1.147 + // this unmarshalling can only access instances of a fixed, known set of classes, 1.148 + // so doing that shouldn't introduce security vulnerability. 1.149 + return AccessController.doPrivileged(new PrivilegedAction<EndpointReference>() { 1.150 + public EndpointReference run() { 1.151 + try { 1.152 + Unmarshaller unmarshaller = eprjc.createUnmarshaller(); 1.153 + return (EndpointReference) unmarshaller.unmarshal(eprInfoset); 1.154 + } catch (JAXBException e) { 1.155 + throw new WebServiceException("Error creating Marshaller or marshalling.", e); 1.156 + } 1.157 + } 1.158 + }); 1.159 + } 1.160 + 1.161 + public <T> T getPort(EndpointReference endpointReference, Class<T> clazz, WebServiceFeature... webServiceFeatures) { 1.162 + /* 1.163 + final @NotNull MemberSubmissionEndpointReference msepr = 1.164 + EndpointReferenceUtil.transform(MemberSubmissionEndpointReference.class, endpointReference); 1.165 + WSService service = new WSServiceDelegate(msepr.toWSDLSource(), msepr.serviceName.name, Service.class); 1.166 + */ 1.167 + if(endpointReference == null) 1.168 + throw new WebServiceException(ProviderApiMessages.NULL_EPR()); 1.169 + WSEndpointReference wsepr = new WSEndpointReference(endpointReference); 1.170 + WSEndpointReference.Metadata metadata = wsepr.getMetaData(); 1.171 + WSService service; 1.172 + if(metadata.getWsdlSource() != null) 1.173 + service = (WSService) createServiceDelegate(metadata.getWsdlSource(), metadata.getServiceName(), Service.class); 1.174 + else 1.175 + throw new WebServiceException("WSDL metadata is missing in EPR"); 1.176 + return service.getPort(wsepr, clazz, webServiceFeatures); 1.177 + } 1.178 + 1.179 + public W3CEndpointReference createW3CEndpointReference(String address, QName serviceName, QName portName, List<Element> metadata, String wsdlDocumentLocation, List<Element> referenceParameters) { 1.180 + return createW3CEndpointReference(address, null, serviceName, portName, metadata, wsdlDocumentLocation, referenceParameters, null, null); 1.181 + } 1.182 + 1.183 + public W3CEndpointReference createW3CEndpointReference(String address, QName interfaceName, QName serviceName, QName portName, 1.184 + List<Element> metadata, String wsdlDocumentLocation, List<Element> referenceParameters, 1.185 + List<Element> elements, Map<QName, String> attributes) { 1.186 + Container container = ContainerResolver.getInstance().getContainer(); 1.187 + if (address == null) { 1.188 + if (serviceName == null || portName == null) { 1.189 + throw new IllegalStateException(ProviderApiMessages.NULL_ADDRESS_SERVICE_ENDPOINT()); 1.190 + } else { 1.191 + //check if it is run in a Java EE Container and if so, get address using serviceName and portName 1.192 + Module module = container.getSPI(Module.class); 1.193 + if (module != null) { 1.194 + List<BoundEndpoint> beList = module.getBoundEndpoints(); 1.195 + for (BoundEndpoint be : beList) { 1.196 + WSEndpoint wse = be.getEndpoint(); 1.197 + if (wse.getServiceName().equals(serviceName) && wse.getPortName().equals(portName)) { 1.198 + try { 1.199 + address = be.getAddress().toString(); 1.200 + } catch (WebServiceException e) { 1.201 + // May be the container does n't support this 1.202 + //just ignore the exception 1.203 + } 1.204 + break; 1.205 + } 1.206 + } 1.207 + } 1.208 + //address is still null? may be its not run in a JavaEE Container 1.209 + if (address == null) 1.210 + throw new IllegalStateException(ProviderApiMessages.NULL_ADDRESS()); 1.211 + } 1.212 + } 1.213 + if((serviceName==null) && (portName != null)) { 1.214 + throw new IllegalStateException(ProviderApiMessages.NULL_SERVICE()); 1.215 + } 1.216 + //Validate Service and Port in WSDL 1.217 + String wsdlTargetNamespace = null; 1.218 + if (wsdlDocumentLocation != null) { 1.219 + try { 1.220 + EntityResolver er = XmlUtil.createDefaultCatalogResolver(); 1.221 + 1.222 + URL wsdlLoc = new URL(wsdlDocumentLocation); 1.223 + WSDLModelImpl wsdlDoc = RuntimeWSDLParser.parse(wsdlLoc, new StreamSource(wsdlLoc.toExternalForm()), er, 1.224 + true, container, ServiceFinder.find(WSDLParserExtension.class).toArray()); 1.225 + if (serviceName != null) { 1.226 + WSDLService wsdlService = wsdlDoc.getService(serviceName); 1.227 + if (wsdlService == null) 1.228 + throw new IllegalStateException(ProviderApiMessages.NOTFOUND_SERVICE_IN_WSDL( 1.229 + serviceName,wsdlDocumentLocation)); 1.230 + if (portName != null) { 1.231 + WSDLPort wsdlPort = wsdlService.get(portName); 1.232 + if (wsdlPort == null) 1.233 + throw new IllegalStateException(ProviderApiMessages.NOTFOUND_PORT_IN_WSDL( 1.234 + portName,serviceName,wsdlDocumentLocation)); 1.235 + } 1.236 + wsdlTargetNamespace = serviceName.getNamespaceURI(); 1.237 + } else { 1.238 + QName firstService = wsdlDoc.getFirstServiceName(); 1.239 + wsdlTargetNamespace = firstService.getNamespaceURI(); 1.240 + } 1.241 + } catch (Exception e) { 1.242 + throw new IllegalStateException(ProviderApiMessages.ERROR_WSDL(wsdlDocumentLocation),e); 1.243 + } 1.244 + } 1.245 + //wcf3.0/3.5 rejected empty metadata element. 1.246 + if (metadata != null && metadata.size() == 0) { 1.247 + metadata = null; 1.248 + } 1.249 + return new WSEndpointReference( 1.250 + AddressingVersion.fromSpecClass(W3CEndpointReference.class), 1.251 + address, serviceName, portName, interfaceName, metadata, wsdlDocumentLocation, wsdlTargetNamespace,referenceParameters, elements, attributes).toSpec(W3CEndpointReference.class); 1.252 + 1.253 + } 1.254 + 1.255 + private static JAXBContext getEPRJaxbContext() { 1.256 + // EPRs have package and private fields, so we need privilege escalation. 1.257 + // this access only fixed, known set of classes, so doing that 1.258 + // shouldn't introduce security vulnerability. 1.259 + return AccessController.doPrivileged(new PrivilegedAction<JAXBContext>() { 1.260 + public JAXBContext run() { 1.261 + try { 1.262 + return JAXBContext.newInstance(MemberSubmissionEndpointReference.class, W3CEndpointReference.class); 1.263 + } catch (JAXBException e) { 1.264 + throw new WebServiceException("Error creating JAXBContext for W3CEndpointReference. ", e); 1.265 + } 1.266 + } 1.267 + }); 1.268 + } 1.269 +}