Tue, 06 Mar 2012 16:09:35 -0800
7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom
1 /*
2 * Copyright (c) 1997, 2010, 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.WSDLPort;
35 import com.sun.xml.internal.ws.api.model.wsdl.WSDLService;
36 import com.sun.xml.internal.ws.api.server.BoundEndpoint;
37 import com.sun.xml.internal.ws.api.server.Container;
38 import com.sun.xml.internal.ws.api.server.ContainerResolver;
39 import com.sun.xml.internal.ws.api.server.Module;
40 import com.sun.xml.internal.ws.api.server.WSEndpoint;
41 import com.sun.xml.internal.ws.api.wsdl.parser.WSDLParserExtension;
42 import com.sun.xml.internal.ws.client.WSServiceDelegate;
43 import com.sun.xml.internal.ws.developer.MemberSubmissionEndpointReference;
44 import com.sun.xml.internal.ws.model.wsdl.WSDLModelImpl;
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;
50 import org.w3c.dom.Element;
51 import org.xml.sax.EntityResolver;
53 import javax.xml.bind.JAXBContext;
54 import javax.xml.bind.JAXBException;
55 import javax.xml.bind.Unmarshaller;
56 import javax.xml.namespace.QName;
57 import javax.xml.transform.Source;
58 import javax.xml.transform.stream.StreamSource;
59 import javax.xml.ws.Endpoint;
60 import javax.xml.ws.EndpointReference;
61 import javax.xml.ws.Service;
62 import javax.xml.ws.WebServiceException;
63 import javax.xml.ws.WebServiceFeature;
64 import javax.xml.ws.spi.Provider;
65 import javax.xml.ws.spi.ServiceDelegate;
66 import javax.xml.ws.spi.Invoker;
67 import javax.xml.ws.wsaddressing.W3CEndpointReference;
68 import java.net.URL;
69 import java.security.AccessController;
70 import java.security.PrivilegedAction;
71 import java.util.List;
72 import java.util.Map;
74 /**
75 * The entry point to the JAX-WS RI from the JAX-WS API.
76 *
77 * @author WS Development Team
78 */
79 public class ProviderImpl extends Provider {
81 private final static JAXBContext eprjc = getEPRJaxbContext();
83 /**
84 * Convenient singleton instance.
85 */
86 public static final ProviderImpl INSTANCE = new ProviderImpl();
88 @Override
89 public Endpoint createEndpoint(String bindingId, Object implementor) {
90 return new EndpointImpl(
91 (bindingId != null) ? BindingID.parse(bindingId) : BindingID.parse(implementor.getClass()),
92 implementor);
93 }
95 @Override
96 public ServiceDelegate createServiceDelegate( URL wsdlDocumentLocation, QName serviceName, Class serviceClass) {
97 return new WSServiceDelegate(wsdlDocumentLocation, serviceName, serviceClass);
98 }
100 public ServiceDelegate createServiceDelegate( URL wsdlDocumentLocation, QName serviceName, Class serviceClass,
101 WebServiceFeature ... features) {
102 for (WebServiceFeature feature : features) {
103 if (!(feature instanceof ServiceSharedFeatureMarker))
104 throw new WebServiceException("Doesn't support any Service specific features");
105 }
106 return new WSServiceDelegate(wsdlDocumentLocation, serviceName, serviceClass, features);
107 }
109 public ServiceDelegate createServiceDelegate( Source wsdlSource, QName serviceName, Class serviceClass) {
110 return new WSServiceDelegate(wsdlSource, serviceName, serviceClass);
111 }
113 @Override
114 public Endpoint createAndPublishEndpoint(String address,
115 Object implementor) {
116 Endpoint endpoint = new EndpointImpl(
117 BindingID.parse(implementor.getClass()),
118 implementor);
119 endpoint.publish(address);
120 return endpoint;
121 }
123 public Endpoint createEndpoint(String bindingId, Object implementor, WebServiceFeature... features) {
124 return new EndpointImpl(
125 (bindingId != null) ? BindingID.parse(bindingId) : BindingID.parse(implementor.getClass()),
126 implementor, features);
127 }
129 public Endpoint createAndPublishEndpoint(String address, Object implementor, WebServiceFeature... features) {
130 Endpoint endpoint = new EndpointImpl(
131 BindingID.parse(implementor.getClass()), implementor, features);
132 endpoint.publish(address);
133 return endpoint;
134 }
136 public Endpoint createEndpoint(String bindingId, Class implementorClass, Invoker invoker, WebServiceFeature... features) {
137 return new EndpointImpl(
138 (bindingId != null) ? BindingID.parse(bindingId) : BindingID.parse(implementorClass),
139 implementorClass, invoker, features);
140 }
142 public EndpointReference readEndpointReference(final Source eprInfoset) {
143 // EPR constructors are private, so we need privilege escalation.
144 // this unmarshalling can only access instances of a fixed, known set of classes,
145 // so doing that shouldn't introduce security vulnerability.
146 return AccessController.doPrivileged(new PrivilegedAction<EndpointReference>() {
147 public EndpointReference run() {
148 try {
149 Unmarshaller unmarshaller = eprjc.createUnmarshaller();
150 return (EndpointReference) unmarshaller.unmarshal(eprInfoset);
151 } catch (JAXBException e) {
152 throw new WebServiceException("Error creating Marshaller or marshalling.", e);
153 }
154 }
155 });
156 }
158 public <T> T getPort(EndpointReference endpointReference, Class<T> clazz, WebServiceFeature... webServiceFeatures) {
159 /*
160 final @NotNull MemberSubmissionEndpointReference msepr =
161 EndpointReferenceUtil.transform(MemberSubmissionEndpointReference.class, endpointReference);
162 WSService service = new WSServiceDelegate(msepr.toWSDLSource(), msepr.serviceName.name, Service.class);
163 */
164 if(endpointReference == null)
165 throw new WebServiceException(ProviderApiMessages.NULL_EPR());
166 WSEndpointReference wsepr = new WSEndpointReference(endpointReference);
167 WSEndpointReference.Metadata metadata = wsepr.getMetaData();
168 WSService service;
169 if(metadata.getWsdlSource() != null)
170 service = (WSService) createServiceDelegate(metadata.getWsdlSource(), metadata.getServiceName(), Service.class);
171 else
172 throw new WebServiceException("WSDL metadata is missing in EPR");
173 return service.getPort(wsepr, clazz, webServiceFeatures);
174 }
176 public W3CEndpointReference createW3CEndpointReference(String address, QName serviceName, QName portName, List<Element> metadata, String wsdlDocumentLocation, List<Element> referenceParameters) {
177 return createW3CEndpointReference(address, null, serviceName, portName, metadata, wsdlDocumentLocation, referenceParameters, null, null);
178 }
180 public W3CEndpointReference createW3CEndpointReference(String address, QName interfaceName, QName serviceName, QName portName,
181 List<Element> metadata, String wsdlDocumentLocation, List<Element> referenceParameters,
182 List<Element> elements, Map<QName, String> attributes) {
183 Container container = ContainerResolver.getInstance().getContainer();
184 if (address == null) {
185 if (serviceName == null || portName == null) {
186 throw new IllegalStateException(ProviderApiMessages.NULL_ADDRESS_SERVICE_ENDPOINT());
187 } else {
188 //check if it is run in a Java EE Container and if so, get address using serviceName and portName
189 Module module = container.getSPI(Module.class);
190 if (module != null) {
191 List<BoundEndpoint> beList = module.getBoundEndpoints();
192 for (BoundEndpoint be : beList) {
193 WSEndpoint wse = be.getEndpoint();
194 if (wse.getServiceName().equals(serviceName) && wse.getPortName().equals(portName)) {
195 try {
196 address = be.getAddress().toString();
197 } catch (WebServiceException e) {
198 // May be the container does n't support this
199 //just ignore the exception
200 }
201 break;
202 }
203 }
204 }
205 //address is still null? may be its not run in a JavaEE Container
206 if (address == null)
207 throw new IllegalStateException(ProviderApiMessages.NULL_ADDRESS());
208 }
209 }
210 if((serviceName==null) && (portName != null)) {
211 throw new IllegalStateException(ProviderApiMessages.NULL_SERVICE());
212 }
213 //Validate Service and Port in WSDL
214 String wsdlTargetNamespace = null;
215 if (wsdlDocumentLocation != null) {
216 try {
217 EntityResolver er = XmlUtil.createDefaultCatalogResolver();
219 URL wsdlLoc = new URL(wsdlDocumentLocation);
220 WSDLModelImpl wsdlDoc = RuntimeWSDLParser.parse(wsdlLoc, new StreamSource(wsdlLoc.toExternalForm()), er,
221 true, container, ServiceFinder.find(WSDLParserExtension.class).toArray());
222 if (serviceName != null) {
223 WSDLService wsdlService = wsdlDoc.getService(serviceName);
224 if (wsdlService == null)
225 throw new IllegalStateException(ProviderApiMessages.NOTFOUND_SERVICE_IN_WSDL(
226 serviceName,wsdlDocumentLocation));
227 if (portName != null) {
228 WSDLPort wsdlPort = wsdlService.get(portName);
229 if (wsdlPort == null)
230 throw new IllegalStateException(ProviderApiMessages.NOTFOUND_PORT_IN_WSDL(
231 portName,serviceName,wsdlDocumentLocation));
232 }
233 wsdlTargetNamespace = serviceName.getNamespaceURI();
234 } else {
235 QName firstService = wsdlDoc.getFirstServiceName();
236 wsdlTargetNamespace = firstService.getNamespaceURI();
237 }
238 } catch (Exception e) {
239 throw new IllegalStateException(ProviderApiMessages.ERROR_WSDL(wsdlDocumentLocation),e);
240 }
241 }
242 //wcf3.0/3.5 rejected empty metadata element.
243 if (metadata != null && metadata.size() == 0) {
244 metadata = null;
245 }
246 return new WSEndpointReference(
247 AddressingVersion.fromSpecClass(W3CEndpointReference.class),
248 address, serviceName, portName, interfaceName, metadata, wsdlDocumentLocation, wsdlTargetNamespace,referenceParameters, elements, attributes).toSpec(W3CEndpointReference.class);
250 }
252 private static JAXBContext getEPRJaxbContext() {
253 // EPRs have package and private fields, so we need privilege escalation.
254 // this access only fixed, known set of classes, so doing that
255 // shouldn't introduce security vulnerability.
256 return AccessController.doPrivileged(new PrivilegedAction<JAXBContext>() {
257 public JAXBContext run() {
258 try {
259 return JAXBContext.newInstance(MemberSubmissionEndpointReference.class, W3CEndpointReference.class);
260 } catch (JAXBException e) {
261 throw new WebServiceException("Error creating JAXBContext for W3CEndpointReference. ", e);
262 }
263 }
264 });
265 }
266 }