Fri, 24 Oct 2014 15:02:28 +0200
8054367: More references for endpoints
Summary: fix also reviewed by Iaroslav.Savytskyi@oracle.com, Alexander.Fomin@oracle.com
Reviewed-by: mullan, skoivu
ohair@286 | 1 | /* |
mkos@515 | 2 | * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. |
ohair@286 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
ohair@286 | 4 | * |
ohair@286 | 5 | * This code is free software; you can redistribute it and/or modify it |
ohair@286 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ohair@286 | 7 | * published by the Free Software Foundation. Oracle designates this |
ohair@286 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@286 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
ohair@286 | 10 | * |
ohair@286 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
ohair@286 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
ohair@286 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
ohair@286 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
ohair@286 | 15 | * accompanied this code). |
ohair@286 | 16 | * |
ohair@286 | 17 | * You should have received a copy of the GNU General Public License version |
ohair@286 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
ohair@286 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
ohair@286 | 20 | * |
ohair@286 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@286 | 22 | * or visit www.oracle.com if you need additional information or have any |
ohair@286 | 23 | * questions. |
ohair@286 | 24 | */ |
ohair@286 | 25 | |
ohair@286 | 26 | package com.sun.xml.internal.ws.spi; |
ohair@286 | 27 | |
ohair@286 | 28 | |
ohair@286 | 29 | import com.sun.xml.internal.ws.api.BindingID; |
ohair@286 | 30 | import com.sun.xml.internal.ws.api.WSService; |
ohair@286 | 31 | import com.sun.xml.internal.ws.api.ServiceSharedFeatureMarker; |
ohair@286 | 32 | import com.sun.xml.internal.ws.api.addressing.AddressingVersion; |
ohair@286 | 33 | import com.sun.xml.internal.ws.api.addressing.WSEndpointReference; |
mkos@408 | 34 | import com.sun.xml.internal.ws.api.model.wsdl.WSDLModel; |
ohair@286 | 35 | import com.sun.xml.internal.ws.api.model.wsdl.WSDLPort; |
ohair@286 | 36 | import com.sun.xml.internal.ws.api.model.wsdl.WSDLService; |
ohair@286 | 37 | import com.sun.xml.internal.ws.api.server.BoundEndpoint; |
ohair@286 | 38 | import com.sun.xml.internal.ws.api.server.Container; |
ohair@286 | 39 | import com.sun.xml.internal.ws.api.server.ContainerResolver; |
ohair@286 | 40 | import com.sun.xml.internal.ws.api.server.Module; |
ohair@286 | 41 | import com.sun.xml.internal.ws.api.server.WSEndpoint; |
ohair@286 | 42 | import com.sun.xml.internal.ws.api.wsdl.parser.WSDLParserExtension; |
ohair@286 | 43 | import com.sun.xml.internal.ws.client.WSServiceDelegate; |
ohair@286 | 44 | import com.sun.xml.internal.ws.developer.MemberSubmissionEndpointReference; |
ohair@286 | 45 | import com.sun.xml.internal.ws.resources.ProviderApiMessages; |
ohair@286 | 46 | import com.sun.xml.internal.ws.transport.http.server.EndpointImpl; |
ohair@286 | 47 | import com.sun.xml.internal.ws.util.ServiceFinder; |
ohair@286 | 48 | import com.sun.xml.internal.ws.util.xml.XmlUtil; |
ohair@286 | 49 | import com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser; |
mkos@408 | 50 | |
ohair@286 | 51 | import org.w3c.dom.Element; |
ohair@286 | 52 | import org.xml.sax.EntityResolver; |
ohair@286 | 53 | |
ohair@286 | 54 | import javax.xml.bind.JAXBContext; |
ohair@286 | 55 | import javax.xml.bind.JAXBException; |
ohair@286 | 56 | import javax.xml.bind.Unmarshaller; |
ohair@286 | 57 | import javax.xml.namespace.QName; |
ohair@286 | 58 | import javax.xml.transform.Source; |
ohair@286 | 59 | import javax.xml.transform.stream.StreamSource; |
ohair@286 | 60 | import javax.xml.ws.Endpoint; |
ohair@286 | 61 | import javax.xml.ws.EndpointReference; |
ohair@286 | 62 | import javax.xml.ws.Service; |
ohair@286 | 63 | import javax.xml.ws.WebServiceException; |
ohair@286 | 64 | import javax.xml.ws.WebServiceFeature; |
ohair@286 | 65 | import javax.xml.ws.spi.Provider; |
ohair@286 | 66 | import javax.xml.ws.spi.ServiceDelegate; |
ohair@286 | 67 | import javax.xml.ws.spi.Invoker; |
ohair@286 | 68 | import javax.xml.ws.wsaddressing.W3CEndpointReference; |
mkos@408 | 69 | |
ohair@286 | 70 | import java.net.URL; |
ohair@286 | 71 | import java.security.AccessController; |
ohair@286 | 72 | import java.security.PrivilegedAction; |
ohair@286 | 73 | import java.util.List; |
ohair@286 | 74 | import java.util.Map; |
ohair@286 | 75 | |
ohair@286 | 76 | /** |
ohair@286 | 77 | * The entry point to the JAX-WS RI from the JAX-WS API. |
ohair@286 | 78 | * |
ohair@286 | 79 | * @author WS Development Team |
ohair@286 | 80 | */ |
ohair@286 | 81 | public class ProviderImpl extends Provider { |
ohair@286 | 82 | |
mkos@515 | 83 | private final static ContextClassloaderLocal<JAXBContext> eprjc = new ContextClassloaderLocal<JAXBContext>() { |
mkos@515 | 84 | @Override |
mkos@515 | 85 | protected JAXBContext initialValue() throws Exception { |
mkos@515 | 86 | return getEPRJaxbContext(); |
mkos@515 | 87 | } |
mkos@515 | 88 | }; |
ohair@286 | 89 | |
ohair@286 | 90 | /** |
ohair@286 | 91 | * Convenient singleton instance. |
ohair@286 | 92 | */ |
ohair@286 | 93 | public static final ProviderImpl INSTANCE = new ProviderImpl(); |
ohair@286 | 94 | |
ohair@286 | 95 | @Override |
ohair@286 | 96 | public Endpoint createEndpoint(String bindingId, Object implementor) { |
ohair@286 | 97 | return new EndpointImpl( |
ohair@286 | 98 | (bindingId != null) ? BindingID.parse(bindingId) : BindingID.parse(implementor.getClass()), |
ohair@286 | 99 | implementor); |
ohair@286 | 100 | } |
ohair@286 | 101 | |
ohair@286 | 102 | @Override |
ohair@286 | 103 | public ServiceDelegate createServiceDelegate( URL wsdlDocumentLocation, QName serviceName, Class serviceClass) { |
ohair@286 | 104 | return new WSServiceDelegate(wsdlDocumentLocation, serviceName, serviceClass); |
ohair@286 | 105 | } |
ohair@286 | 106 | |
ohair@286 | 107 | public ServiceDelegate createServiceDelegate( URL wsdlDocumentLocation, QName serviceName, Class serviceClass, |
ohair@286 | 108 | WebServiceFeature ... features) { |
ohair@286 | 109 | for (WebServiceFeature feature : features) { |
ohair@286 | 110 | if (!(feature instanceof ServiceSharedFeatureMarker)) |
ohair@286 | 111 | throw new WebServiceException("Doesn't support any Service specific features"); |
ohair@286 | 112 | } |
ohair@286 | 113 | return new WSServiceDelegate(wsdlDocumentLocation, serviceName, serviceClass, features); |
ohair@286 | 114 | } |
ohair@286 | 115 | |
ohair@286 | 116 | public ServiceDelegate createServiceDelegate( Source wsdlSource, QName serviceName, Class serviceClass) { |
ohair@286 | 117 | return new WSServiceDelegate(wsdlSource, serviceName, serviceClass); |
ohair@286 | 118 | } |
ohair@286 | 119 | |
ohair@286 | 120 | @Override |
ohair@286 | 121 | public Endpoint createAndPublishEndpoint(String address, |
ohair@286 | 122 | Object implementor) { |
ohair@286 | 123 | Endpoint endpoint = new EndpointImpl( |
ohair@286 | 124 | BindingID.parse(implementor.getClass()), |
ohair@286 | 125 | implementor); |
ohair@286 | 126 | endpoint.publish(address); |
ohair@286 | 127 | return endpoint; |
ohair@286 | 128 | } |
ohair@286 | 129 | |
ohair@286 | 130 | public Endpoint createEndpoint(String bindingId, Object implementor, WebServiceFeature... features) { |
ohair@286 | 131 | return new EndpointImpl( |
ohair@286 | 132 | (bindingId != null) ? BindingID.parse(bindingId) : BindingID.parse(implementor.getClass()), |
ohair@286 | 133 | implementor, features); |
ohair@286 | 134 | } |
ohair@286 | 135 | |
ohair@286 | 136 | public Endpoint createAndPublishEndpoint(String address, Object implementor, WebServiceFeature... features) { |
ohair@286 | 137 | Endpoint endpoint = new EndpointImpl( |
ohair@286 | 138 | BindingID.parse(implementor.getClass()), implementor, features); |
ohair@286 | 139 | endpoint.publish(address); |
ohair@286 | 140 | return endpoint; |
ohair@286 | 141 | } |
ohair@286 | 142 | |
ohair@286 | 143 | public Endpoint createEndpoint(String bindingId, Class implementorClass, Invoker invoker, WebServiceFeature... features) { |
ohair@286 | 144 | return new EndpointImpl( |
ohair@286 | 145 | (bindingId != null) ? BindingID.parse(bindingId) : BindingID.parse(implementorClass), |
ohair@286 | 146 | implementorClass, invoker, features); |
ohair@286 | 147 | } |
ohair@286 | 148 | |
ohair@286 | 149 | public EndpointReference readEndpointReference(final Source eprInfoset) { |
mkos@721 | 150 | try { |
mkos@721 | 151 | Unmarshaller unmarshaller = eprjc.get().createUnmarshaller(); |
mkos@721 | 152 | return (EndpointReference) unmarshaller.unmarshal(eprInfoset); |
mkos@721 | 153 | } catch (JAXBException e) { |
mkos@721 | 154 | throw new WebServiceException("Error creating Marshaller or marshalling.", e); |
mkos@721 | 155 | } |
ohair@286 | 156 | } |
ohair@286 | 157 | |
ohair@286 | 158 | public <T> T getPort(EndpointReference endpointReference, Class<T> clazz, WebServiceFeature... webServiceFeatures) { |
ohair@286 | 159 | /* |
ohair@286 | 160 | final @NotNull MemberSubmissionEndpointReference msepr = |
ohair@286 | 161 | EndpointReferenceUtil.transform(MemberSubmissionEndpointReference.class, endpointReference); |
ohair@286 | 162 | WSService service = new WSServiceDelegate(msepr.toWSDLSource(), msepr.serviceName.name, Service.class); |
ohair@286 | 163 | */ |
ohair@286 | 164 | if(endpointReference == null) |
ohair@286 | 165 | throw new WebServiceException(ProviderApiMessages.NULL_EPR()); |
ohair@286 | 166 | WSEndpointReference wsepr = new WSEndpointReference(endpointReference); |
ohair@286 | 167 | WSEndpointReference.Metadata metadata = wsepr.getMetaData(); |
ohair@286 | 168 | WSService service; |
ohair@286 | 169 | if(metadata.getWsdlSource() != null) |
ohair@286 | 170 | service = (WSService) createServiceDelegate(metadata.getWsdlSource(), metadata.getServiceName(), Service.class); |
ohair@286 | 171 | else |
ohair@286 | 172 | throw new WebServiceException("WSDL metadata is missing in EPR"); |
ohair@286 | 173 | return service.getPort(wsepr, clazz, webServiceFeatures); |
ohair@286 | 174 | } |
ohair@286 | 175 | |
ohair@286 | 176 | public W3CEndpointReference createW3CEndpointReference(String address, QName serviceName, QName portName, List<Element> metadata, String wsdlDocumentLocation, List<Element> referenceParameters) { |
ohair@286 | 177 | return createW3CEndpointReference(address, null, serviceName, portName, metadata, wsdlDocumentLocation, referenceParameters, null, null); |
ohair@286 | 178 | } |
ohair@286 | 179 | |
ohair@286 | 180 | public W3CEndpointReference createW3CEndpointReference(String address, QName interfaceName, QName serviceName, QName portName, |
ohair@286 | 181 | List<Element> metadata, String wsdlDocumentLocation, List<Element> referenceParameters, |
ohair@286 | 182 | List<Element> elements, Map<QName, String> attributes) { |
ohair@286 | 183 | Container container = ContainerResolver.getInstance().getContainer(); |
ohair@286 | 184 | if (address == null) { |
ohair@286 | 185 | if (serviceName == null || portName == null) { |
ohair@286 | 186 | throw new IllegalStateException(ProviderApiMessages.NULL_ADDRESS_SERVICE_ENDPOINT()); |
ohair@286 | 187 | } else { |
ohair@286 | 188 | //check if it is run in a Java EE Container and if so, get address using serviceName and portName |
ohair@286 | 189 | Module module = container.getSPI(Module.class); |
ohair@286 | 190 | if (module != null) { |
ohair@286 | 191 | List<BoundEndpoint> beList = module.getBoundEndpoints(); |
ohair@286 | 192 | for (BoundEndpoint be : beList) { |
ohair@286 | 193 | WSEndpoint wse = be.getEndpoint(); |
ohair@286 | 194 | if (wse.getServiceName().equals(serviceName) && wse.getPortName().equals(portName)) { |
ohair@286 | 195 | try { |
ohair@286 | 196 | address = be.getAddress().toString(); |
ohair@286 | 197 | } catch (WebServiceException e) { |
ohair@286 | 198 | // May be the container does n't support this |
ohair@286 | 199 | //just ignore the exception |
ohair@286 | 200 | } |
ohair@286 | 201 | break; |
ohair@286 | 202 | } |
ohair@286 | 203 | } |
ohair@286 | 204 | } |
ohair@286 | 205 | //address is still null? may be its not run in a JavaEE Container |
ohair@286 | 206 | if (address == null) |
ohair@286 | 207 | throw new IllegalStateException(ProviderApiMessages.NULL_ADDRESS()); |
ohair@286 | 208 | } |
ohair@286 | 209 | } |
ohair@286 | 210 | if((serviceName==null) && (portName != null)) { |
ohair@286 | 211 | throw new IllegalStateException(ProviderApiMessages.NULL_SERVICE()); |
ohair@286 | 212 | } |
ohair@286 | 213 | //Validate Service and Port in WSDL |
ohair@286 | 214 | String wsdlTargetNamespace = null; |
ohair@286 | 215 | if (wsdlDocumentLocation != null) { |
ohair@286 | 216 | try { |
ohair@286 | 217 | EntityResolver er = XmlUtil.createDefaultCatalogResolver(); |
ohair@286 | 218 | |
ohair@286 | 219 | URL wsdlLoc = new URL(wsdlDocumentLocation); |
mkos@408 | 220 | WSDLModel wsdlDoc = RuntimeWSDLParser.parse(wsdlLoc, new StreamSource(wsdlLoc.toExternalForm()), er, |
ohair@286 | 221 | true, container, ServiceFinder.find(WSDLParserExtension.class).toArray()); |
ohair@286 | 222 | if (serviceName != null) { |
ohair@286 | 223 | WSDLService wsdlService = wsdlDoc.getService(serviceName); |
ohair@286 | 224 | if (wsdlService == null) |
ohair@286 | 225 | throw new IllegalStateException(ProviderApiMessages.NOTFOUND_SERVICE_IN_WSDL( |
ohair@286 | 226 | serviceName,wsdlDocumentLocation)); |
ohair@286 | 227 | if (portName != null) { |
ohair@286 | 228 | WSDLPort wsdlPort = wsdlService.get(portName); |
ohair@286 | 229 | if (wsdlPort == null) |
ohair@286 | 230 | throw new IllegalStateException(ProviderApiMessages.NOTFOUND_PORT_IN_WSDL( |
ohair@286 | 231 | portName,serviceName,wsdlDocumentLocation)); |
ohair@286 | 232 | } |
ohair@286 | 233 | wsdlTargetNamespace = serviceName.getNamespaceURI(); |
ohair@286 | 234 | } else { |
ohair@286 | 235 | QName firstService = wsdlDoc.getFirstServiceName(); |
ohair@286 | 236 | wsdlTargetNamespace = firstService.getNamespaceURI(); |
ohair@286 | 237 | } |
ohair@286 | 238 | } catch (Exception e) { |
ohair@286 | 239 | throw new IllegalStateException(ProviderApiMessages.ERROR_WSDL(wsdlDocumentLocation),e); |
ohair@286 | 240 | } |
ohair@286 | 241 | } |
ohair@286 | 242 | //wcf3.0/3.5 rejected empty metadata element. |
ohair@286 | 243 | if (metadata != null && metadata.size() == 0) { |
ohair@286 | 244 | metadata = null; |
ohair@286 | 245 | } |
ohair@286 | 246 | return new WSEndpointReference( |
ohair@286 | 247 | AddressingVersion.fromSpecClass(W3CEndpointReference.class), |
ohair@286 | 248 | address, serviceName, portName, interfaceName, metadata, wsdlDocumentLocation, wsdlTargetNamespace,referenceParameters, elements, attributes).toSpec(W3CEndpointReference.class); |
ohair@286 | 249 | |
ohair@286 | 250 | } |
ohair@286 | 251 | |
ohair@286 | 252 | private static JAXBContext getEPRJaxbContext() { |
ohair@286 | 253 | // EPRs have package and private fields, so we need privilege escalation. |
ohair@286 | 254 | // this access only fixed, known set of classes, so doing that |
ohair@286 | 255 | // shouldn't introduce security vulnerability. |
ohair@286 | 256 | return AccessController.doPrivileged(new PrivilegedAction<JAXBContext>() { |
ohair@286 | 257 | public JAXBContext run() { |
ohair@286 | 258 | try { |
ohair@286 | 259 | return JAXBContext.newInstance(MemberSubmissionEndpointReference.class, W3CEndpointReference.class); |
ohair@286 | 260 | } catch (JAXBException e) { |
ohair@286 | 261 | throw new WebServiceException("Error creating JAXBContext for W3CEndpointReference. ", e); |
ohair@286 | 262 | } |
ohair@286 | 263 | } |
ohair@286 | 264 | }); |
ohair@286 | 265 | } |
ohair@286 | 266 | } |