1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/ws/server/EndpointFactory.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,783 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2013, 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.server; 1.30 + 1.31 +import com.sun.istack.internal.NotNull; 1.32 +import com.sun.istack.internal.Nullable; 1.33 +import com.sun.xml.internal.stream.buffer.MutableXMLStreamBuffer; 1.34 +import com.sun.xml.internal.ws.api.BindingID; 1.35 +import com.sun.xml.internal.ws.api.WSBinding; 1.36 +import com.sun.xml.internal.ws.api.WSFeatureList; 1.37 +import com.sun.xml.internal.ws.api.databinding.DatabindingConfig; 1.38 +import com.sun.xml.internal.ws.api.databinding.DatabindingFactory; 1.39 +import com.sun.xml.internal.ws.api.databinding.MetadataReader; 1.40 +import com.sun.xml.internal.ws.api.databinding.WSDLGenInfo; 1.41 +import com.sun.xml.internal.ws.api.model.SEIModel; 1.42 +import com.sun.xml.internal.ws.api.model.wsdl.WSDLModel; 1.43 +import com.sun.xml.internal.ws.api.model.wsdl.WSDLPort; 1.44 +import com.sun.xml.internal.ws.api.model.wsdl.WSDLService; 1.45 +import com.sun.xml.internal.ws.api.policy.PolicyResolver; 1.46 +import com.sun.xml.internal.ws.api.policy.PolicyResolverFactory; 1.47 +import com.sun.xml.internal.ws.api.server.AsyncProvider; 1.48 +import com.sun.xml.internal.ws.api.server.Container; 1.49 +import com.sun.xml.internal.ws.api.server.ContainerResolver; 1.50 +import com.sun.xml.internal.ws.api.server.InstanceResolver; 1.51 +import com.sun.xml.internal.ws.api.server.Invoker; 1.52 +import com.sun.xml.internal.ws.api.server.SDDocument; 1.53 +import com.sun.xml.internal.ws.api.server.SDDocumentSource; 1.54 +import com.sun.xml.internal.ws.api.server.WSEndpoint; 1.55 +import com.sun.xml.internal.ws.api.streaming.XMLStreamReaderFactory; 1.56 +import com.sun.xml.internal.ws.api.wsdl.parser.WSDLParserExtension; 1.57 +import com.sun.xml.internal.ws.api.wsdl.parser.XMLEntityResolver; 1.58 +import com.sun.xml.internal.ws.api.wsdl.parser.XMLEntityResolver.Parser; 1.59 +import com.sun.xml.internal.ws.api.wsdl.writer.WSDLGeneratorExtension; 1.60 +import com.sun.xml.internal.ws.binding.BindingImpl; 1.61 +import com.sun.xml.internal.ws.binding.SOAPBindingImpl; 1.62 +import com.sun.xml.internal.ws.binding.WebServiceFeatureList; 1.63 +import com.sun.xml.internal.ws.model.AbstractSEIModelImpl; 1.64 +import com.sun.xml.internal.ws.model.ReflectAnnotationReader; 1.65 +import com.sun.xml.internal.ws.model.RuntimeModeler; 1.66 +import com.sun.xml.internal.ws.model.SOAPSEIModel; 1.67 +import com.sun.xml.internal.ws.policy.PolicyMap; 1.68 +import com.sun.xml.internal.ws.policy.jaxws.PolicyUtil; 1.69 +import com.sun.xml.internal.ws.resources.ServerMessages; 1.70 +import com.sun.xml.internal.ws.server.provider.ProviderInvokerTube; 1.71 +import com.sun.xml.internal.ws.server.sei.SEIInvokerTube; 1.72 +import com.sun.xml.internal.ws.util.HandlerAnnotationInfo; 1.73 +import com.sun.xml.internal.ws.util.HandlerAnnotationProcessor; 1.74 +import com.sun.xml.internal.ws.util.ServiceConfigurationError; 1.75 +import com.sun.xml.internal.ws.util.ServiceFinder; 1.76 +import com.sun.xml.internal.ws.util.xml.XmlUtil; 1.77 +import com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser; 1.78 + 1.79 +import org.xml.sax.EntityResolver; 1.80 +import org.xml.sax.InputSource; 1.81 +import org.xml.sax.SAXException; 1.82 + 1.83 +import javax.jws.WebService; 1.84 +import javax.xml.namespace.QName; 1.85 +import javax.xml.stream.XMLStreamException; 1.86 +import javax.xml.stream.XMLStreamReader; 1.87 +import javax.xml.ws.Provider; 1.88 +import javax.xml.ws.WebServiceException; 1.89 +import javax.xml.ws.WebServiceFeature; 1.90 +import javax.xml.ws.WebServiceProvider; 1.91 +import javax.xml.ws.soap.SOAPBinding; 1.92 + 1.93 +import java.io.IOException; 1.94 +import java.net.URL; 1.95 +import java.util.ArrayList; 1.96 +import java.util.Collection; 1.97 +import java.util.HashMap; 1.98 +import java.util.List; 1.99 +import java.util.Map; 1.100 +import java.util.logging.Logger; 1.101 + 1.102 +/** 1.103 + * Entry point to the JAX-WS RI server-side runtime. 1.104 + * 1.105 + * @author Kohsuke Kawaguchi 1.106 + * @author Jitendra Kotamraju 1.107 + */ 1.108 +public class EndpointFactory { 1.109 + private static final EndpointFactory instance = new EndpointFactory(); 1.110 + 1.111 + public static EndpointFactory getInstance() { 1.112 + return instance; 1.113 + } 1.114 + 1.115 + /** 1.116 + * Implements {@link WSEndpoint#create}. 1.117 + * 1.118 + * No need to take WebServiceContext implementation. When InvokerPipe is 1.119 + * instantiated, it calls InstanceResolver to set up a WebServiceContext. 1.120 + * We shall only take delegate to getUserPrincipal and isUserInRole from adapter. 1.121 + * 1.122 + * <p> 1.123 + * Nobody else should be calling this method. 1.124 + */ 1.125 + public static <T> WSEndpoint<T> createEndpoint( 1.126 + Class<T> implType, boolean processHandlerAnnotation, @Nullable Invoker invoker, 1.127 + @Nullable QName serviceName, @Nullable QName portName, 1.128 + @Nullable Container container, @Nullable WSBinding binding, 1.129 + @Nullable SDDocumentSource primaryWsdl, 1.130 + @Nullable Collection<? extends SDDocumentSource> metadata, 1.131 + EntityResolver resolver, boolean isTransportSynchronous) { 1.132 + return createEndpoint(implType, processHandlerAnnotation, invoker, serviceName, 1.133 + portName, container, binding, primaryWsdl, metadata, resolver, isTransportSynchronous, true); 1.134 + } 1.135 + 1.136 + public static <T> WSEndpoint<T> createEndpoint( 1.137 + Class<T> implType, boolean processHandlerAnnotation, @Nullable Invoker invoker, 1.138 + @Nullable QName serviceName, @Nullable QName portName, 1.139 + @Nullable Container container, @Nullable WSBinding binding, 1.140 + @Nullable SDDocumentSource primaryWsdl, 1.141 + @Nullable Collection<? extends SDDocumentSource> metadata, 1.142 + EntityResolver resolver, boolean isTransportSynchronous, boolean isStandard) { 1.143 + EndpointFactory factory = container != null ? container.getSPI(EndpointFactory.class) : null; 1.144 + if (factory == null) 1.145 + factory = EndpointFactory.getInstance(); 1.146 + 1.147 + return factory.create( 1.148 + implType,processHandlerAnnotation, invoker,serviceName,portName,container,binding,primaryWsdl,metadata,resolver,isTransportSynchronous,isStandard); 1.149 + } 1.150 + 1.151 + /** 1.152 + * Implements {@link WSEndpoint#create}. 1.153 + * 1.154 + * No need to take WebServiceContext implementation. When InvokerPipe is 1.155 + * instantiated, it calls InstanceResolver to set up a WebServiceContext. 1.156 + * We shall only take delegate to getUserPrincipal and isUserInRole from adapter. 1.157 + * 1.158 + * <p> 1.159 + * Nobody else should be calling this method. 1.160 + */ 1.161 + public <T> WSEndpoint<T> create( 1.162 + Class<T> implType, boolean processHandlerAnnotation, @Nullable Invoker invoker, 1.163 + @Nullable QName serviceName, @Nullable QName portName, 1.164 + @Nullable Container container, @Nullable WSBinding binding, 1.165 + @Nullable SDDocumentSource primaryWsdl, 1.166 + @Nullable Collection<? extends SDDocumentSource> metadata, 1.167 + EntityResolver resolver, boolean isTransportSynchronous) { 1.168 + return create(implType, processHandlerAnnotation, invoker, serviceName, 1.169 + portName, container, binding, primaryWsdl, metadata, resolver, isTransportSynchronous, 1.170 + true); 1.171 + 1.172 + } 1.173 + 1.174 + public <T> WSEndpoint<T> create( 1.175 + Class<T> implType, boolean processHandlerAnnotation, @Nullable Invoker invoker, 1.176 + @Nullable QName serviceName, @Nullable QName portName, 1.177 + @Nullable Container container, @Nullable WSBinding binding, 1.178 + @Nullable SDDocumentSource primaryWsdl, 1.179 + @Nullable Collection<? extends SDDocumentSource> metadata, 1.180 + EntityResolver resolver, boolean isTransportSynchronous, boolean isStandard) { 1.181 + 1.182 + if(implType ==null) 1.183 + throw new IllegalArgumentException(); 1.184 + 1.185 + MetadataReader metadataReader = getExternalMetadatReader(implType, binding); 1.186 + 1.187 + if (isStandard) { 1.188 + verifyImplementorClass(implType, metadataReader); 1.189 + } 1.190 + 1.191 + if (invoker == null) { 1.192 + invoker = InstanceResolver.createDefault(implType).createInvoker(); 1.193 + } 1.194 + 1.195 + List<SDDocumentSource> md = new ArrayList<SDDocumentSource>(); 1.196 + if(metadata!=null) 1.197 + md.addAll(metadata); 1.198 + 1.199 + if(primaryWsdl!=null && !md.contains(primaryWsdl)) 1.200 + md.add(primaryWsdl); 1.201 + 1.202 + if(container==null) 1.203 + container = ContainerResolver.getInstance().getContainer(); 1.204 + 1.205 + if(serviceName==null) 1.206 + serviceName = getDefaultServiceName(implType, metadataReader); 1.207 + 1.208 + if(portName==null) 1.209 + portName = getDefaultPortName(serviceName,implType, metadataReader); 1.210 + 1.211 + {// error check 1.212 + String serviceNS = serviceName.getNamespaceURI(); 1.213 + String portNS = portName.getNamespaceURI(); 1.214 + if (!serviceNS.equals(portNS)) { 1.215 + throw new ServerRtException("wrong.tns.for.port",portNS, serviceNS); 1.216 + } 1.217 + } 1.218 + 1.219 + // setting a default binding 1.220 + if (binding == null) 1.221 + binding = BindingImpl.create(BindingID.parse(implType)); 1.222 + 1.223 + if ( isStandard && primaryWsdl != null) { 1.224 + verifyPrimaryWSDL(primaryWsdl, serviceName); 1.225 + } 1.226 + 1.227 + QName portTypeName = null; 1.228 + if (isStandard && implType.getAnnotation(WebServiceProvider.class)==null) { 1.229 + portTypeName = RuntimeModeler.getPortTypeName(implType, metadataReader); 1.230 + } 1.231 + 1.232 + // Categorises the documents as WSDL, Schema etc 1.233 + List<SDDocumentImpl> docList = categoriseMetadata(md, serviceName, portTypeName); 1.234 + // Finds the primary WSDL and makes sure that metadata doesn't have 1.235 + // two concrete or abstract WSDLs 1.236 + SDDocumentImpl primaryDoc = primaryWsdl != null ? SDDocumentImpl.create(primaryWsdl,serviceName,portTypeName) : findPrimary(docList); 1.237 + 1.238 + EndpointAwareTube terminal; 1.239 + WSDLPort wsdlPort = null; 1.240 + AbstractSEIModelImpl seiModel = null; 1.241 + // create WSDL model 1.242 + if (primaryDoc != null) { 1.243 + wsdlPort = getWSDLPort(primaryDoc, docList, serviceName, portName, container, resolver); 1.244 + } 1.245 + 1.246 + WebServiceFeatureList features=((BindingImpl)binding).getFeatures(); 1.247 + if (isStandard) { 1.248 + features.parseAnnotations(implType); 1.249 + } 1.250 + PolicyMap policyMap = null; 1.251 + // create terminal pipe that invokes the application 1.252 + if (isUseProviderTube(implType, isStandard)) { 1.253 + //TODO incase of Provider, provide a way to User for complete control of the message processing by giving 1.254 + // ability to turn off the WSDL/Policy based features and its associated tubes. 1.255 + 1.256 + //Even in case of Provider, merge all features configured via WSDL/Policy or deployment configuration 1.257 + Iterable<WebServiceFeature> configFtrs; 1.258 + if(wsdlPort != null) { 1.259 + policyMap = wsdlPort.getOwner().getParent().getPolicyMap(); 1.260 + //Merge features from WSDL and other policy configuration 1.261 + configFtrs = wsdlPort.getFeatures(); 1.262 + } else { 1.263 + //No WSDL, so try to merge features from Policy configuration 1.264 + policyMap = PolicyResolverFactory.create().resolve( 1.265 + new PolicyResolver.ServerContext(null, container, implType, false)); 1.266 + configFtrs = PolicyUtil.getPortScopedFeatures(policyMap,serviceName,portName); 1.267 + } 1.268 + features.mergeFeatures(configFtrs, true); 1.269 + terminal = createProviderInvokerTube(implType, binding, invoker, container); 1.270 + } else { 1.271 + // Create runtime model for non Provider endpoints 1.272 + seiModel = createSEIModel(wsdlPort, implType, serviceName, portName, binding, primaryDoc); 1.273 + if(binding instanceof SOAPBindingImpl){ 1.274 + //set portKnownHeaders on Binding, so that they can be used for MU processing 1.275 + ((SOAPBindingImpl)binding).setPortKnownHeaders( 1.276 + ((SOAPSEIModel)seiModel).getKnownHeaders()); 1.277 + } 1.278 + // Generate WSDL for SEI endpoints(not for Provider endpoints) 1.279 + if (primaryDoc == null) { 1.280 + primaryDoc = generateWSDL(binding, seiModel, docList, container, implType); 1.281 + // create WSDL model 1.282 + wsdlPort = getWSDLPort(primaryDoc, docList, serviceName, portName, container, resolver); 1.283 + seiModel.freeze(wsdlPort); 1.284 + } 1.285 + policyMap = wsdlPort.getOwner().getParent().getPolicyMap(); 1.286 + // New Features might have been added in WSDL through Policy. 1.287 + //Merge features from WSDL and other policy configuration 1.288 + // This sets only the wsdl features that are not already set(enabled/disabled) 1.289 + features.mergeFeatures(wsdlPort.getFeatures(), true); 1.290 + terminal = createSEIInvokerTube(seiModel,invoker,binding); 1.291 + } 1.292 + 1.293 + // Process @HandlerChain, if handler-chain is not set via Deployment Descriptor 1.294 + if (processHandlerAnnotation) { 1.295 + processHandlerAnnotation(binding, implType, serviceName, portName); 1.296 + } 1.297 + // Selects only required metadata for this endpoint from the passed-in metadata 1.298 + if (primaryDoc != null) { 1.299 + docList = findMetadataClosure(primaryDoc, docList, resolver); 1.300 + } 1.301 + 1.302 + ServiceDefinitionImpl serviceDefiniton = (primaryDoc != null) ? new ServiceDefinitionImpl(docList, primaryDoc) : null; 1.303 + 1.304 + return create(serviceName, portName, binding, container, seiModel, wsdlPort, implType, serviceDefiniton, 1.305 + terminal, isTransportSynchronous, policyMap); 1.306 + } 1.307 + 1.308 + protected <T> WSEndpoint<T> create(QName serviceName, QName portName, WSBinding binding, Container container, SEIModel seiModel, WSDLPort wsdlPort, Class<T> implType, ServiceDefinitionImpl serviceDefinition, EndpointAwareTube terminal, boolean isTransportSynchronous, PolicyMap policyMap) { 1.309 + return new WSEndpointImpl<T>(serviceName, portName, binding, container, seiModel, 1.310 + wsdlPort, implType, serviceDefinition, terminal, isTransportSynchronous, policyMap); 1.311 + } 1.312 + 1.313 + protected boolean isUseProviderTube(Class<?> implType, boolean isStandard) { 1.314 + return !isStandard || implType.getAnnotation(WebServiceProvider.class)!=null; 1.315 + } 1.316 + 1.317 + protected EndpointAwareTube createSEIInvokerTube(AbstractSEIModelImpl seiModel, Invoker invoker, WSBinding binding) { 1.318 + return new SEIInvokerTube(seiModel,invoker,binding); 1.319 + } 1.320 + 1.321 + protected <T> EndpointAwareTube createProviderInvokerTube(final Class<T> implType, final WSBinding binding, 1.322 + final Invoker invoker, final Container container) { 1.323 + return ProviderInvokerTube.create(implType, binding, invoker, container); 1.324 + } 1.325 + /** 1.326 + * Goes through the original metadata documents and collects the required ones. 1.327 + * This done traversing from primary WSDL and its imports until it builds a 1.328 + * complete set of documents(transitive closure) for the endpoint. 1.329 + * 1.330 + * @param primaryDoc primary WSDL doc 1.331 + * @param docList complete metadata 1.332 + * @return new metadata that doesn't contain extraneous documnets. 1.333 + */ 1.334 + private static List<SDDocumentImpl> findMetadataClosure(SDDocumentImpl primaryDoc, List<SDDocumentImpl> docList, EntityResolver resolver) { 1.335 + // create a map for old metadata 1.336 + Map<String, SDDocumentImpl> oldMap = new HashMap<String, SDDocumentImpl>(); 1.337 + for(SDDocumentImpl doc : docList) { 1.338 + oldMap.put(doc.getSystemId().toString(), doc); 1.339 + } 1.340 + // create a map for new metadata 1.341 + Map<String, SDDocumentImpl> newMap = new HashMap<String, SDDocumentImpl>(); 1.342 + newMap.put(primaryDoc.getSystemId().toString(), primaryDoc); 1.343 + 1.344 + List<String> remaining = new ArrayList<String>(); 1.345 + remaining.addAll(primaryDoc.getImports()); 1.346 + while(!remaining.isEmpty()) { 1.347 + String url = remaining.remove(0); 1.348 + SDDocumentImpl doc = oldMap.get(url); 1.349 + if (doc == null) { 1.350 + // old metadata doesn't have this imported doc, may be external 1.351 + if (resolver != null) { 1.352 + try { 1.353 + InputSource source = resolver.resolveEntity(null, url); 1.354 + if (source != null) { 1.355 + MutableXMLStreamBuffer xsb = new MutableXMLStreamBuffer(); 1.356 + XMLStreamReader reader = XmlUtil.newXMLInputFactory(true).createXMLStreamReader(source.getByteStream()); 1.357 + xsb.createFromXMLStreamReader(reader); 1.358 + 1.359 + SDDocumentSource sdocSource = SDDocumentImpl.create(new URL(url), xsb); 1.360 + doc = SDDocumentImpl.create(sdocSource, null, null); 1.361 + } 1.362 + } catch (Exception ex) { 1.363 + ex.printStackTrace(); 1.364 + } 1.365 + } 1.366 + } 1.367 + // Check if new metadata already contains this doc 1.368 + if (doc != null && !newMap.containsKey(url)) { 1.369 + newMap.put(url, doc); 1.370 + remaining.addAll(doc.getImports()); 1.371 + } 1.372 + } 1.373 + List<SDDocumentImpl> newMetadata = new ArrayList<SDDocumentImpl>(); 1.374 + newMetadata.addAll(newMap.values()); 1.375 + return newMetadata; 1.376 + } 1.377 + 1.378 + private static <T> void processHandlerAnnotation(WSBinding binding, Class<T> implType, QName serviceName, QName portName) { 1.379 + HandlerAnnotationInfo chainInfo = 1.380 + HandlerAnnotationProcessor.buildHandlerInfo( 1.381 + implType, serviceName, portName, binding); 1.382 + if (chainInfo != null) { 1.383 + binding.setHandlerChain(chainInfo.getHandlers()); 1.384 + if (binding instanceof SOAPBinding) { 1.385 + ((SOAPBinding) binding).setRoles(chainInfo.getRoles()); 1.386 + } 1.387 + } 1.388 + 1.389 + } 1.390 + 1.391 + /** 1.392 + * Verifies if the endpoint implementor class has @WebService or @WebServiceProvider 1.393 + * annotation 1.394 + * 1.395 + * @return 1.396 + * true if it is a Provider or AsyncProvider endpoint 1.397 + * false otherwise 1.398 + * @throws java.lang.IllegalArgumentException 1.399 + * If it doesn't have any one of @WebService or @WebServiceProvider 1.400 + * If it has both @WebService and @WebServiceProvider annotations 1.401 + */ 1.402 + public static boolean verifyImplementorClass(Class<?> clz) { 1.403 + return verifyImplementorClass(clz, null); 1.404 + } 1.405 + 1.406 + /** 1.407 + * Verifies if the endpoint implementor class has @WebService or @WebServiceProvider 1.408 + * annotation; passing MetadataReader instance allows to read annotations from 1.409 + * xml descriptor instead of class's annotations 1.410 + * 1.411 + * @return 1.412 + * true if it is a Provider or AsyncProvider endpoint 1.413 + * false otherwise 1.414 + * @throws java.lang.IllegalArgumentException 1.415 + * If it doesn't have any one of @WebService or @WebServiceProvider 1.416 + * If it has both @WebService and @WebServiceProvider annotations 1.417 + */ 1.418 + public static boolean verifyImplementorClass(Class<?> clz, MetadataReader metadataReader) { 1.419 + 1.420 + if (metadataReader == null) { 1.421 + metadataReader = new ReflectAnnotationReader(); 1.422 + } 1.423 + 1.424 + WebServiceProvider wsProvider = metadataReader.getAnnotation(WebServiceProvider.class, clz); 1.425 + WebService ws = metadataReader.getAnnotation(WebService.class, clz); 1.426 + if (wsProvider == null && ws == null) { 1.427 + throw new IllegalArgumentException(clz +" has neither @WebService nor @WebServiceProvider annotation"); 1.428 + } 1.429 + if (wsProvider != null && ws != null) { 1.430 + throw new IllegalArgumentException(clz +" has both @WebService and @WebServiceProvider annotations"); 1.431 + } 1.432 + if (wsProvider != null) { 1.433 + if (Provider.class.isAssignableFrom(clz) || AsyncProvider.class.isAssignableFrom(clz)) { 1.434 + return true; 1.435 + } 1.436 + throw new IllegalArgumentException(clz +" doesn't implement Provider or AsyncProvider interface"); 1.437 + } 1.438 + return false; 1.439 + } 1.440 + 1.441 + 1.442 + private static AbstractSEIModelImpl createSEIModel(WSDLPort wsdlPort, 1.443 + Class<?> implType, @NotNull QName serviceName, @NotNull QName portName, WSBinding binding, 1.444 + SDDocumentSource primaryWsdl) { 1.445 + DatabindingFactory fac = DatabindingFactory.newInstance(); 1.446 + DatabindingConfig config = new DatabindingConfig(); 1.447 + config.setEndpointClass(implType); 1.448 + config.getMappingInfo().setServiceName(serviceName); 1.449 + config.setWsdlPort(wsdlPort); 1.450 + config.setWSBinding(binding); 1.451 + config.setClassLoader(implType.getClassLoader()); 1.452 + config.getMappingInfo().setPortName(portName); 1.453 + if (primaryWsdl != null) config.setWsdlURL(primaryWsdl.getSystemId()); 1.454 + config.setMetadataReader(getExternalMetadatReader(implType, binding)); 1.455 + 1.456 + com.sun.xml.internal.ws.db.DatabindingImpl rt = (com.sun.xml.internal.ws.db.DatabindingImpl)fac.createRuntime(config); 1.457 + return (AbstractSEIModelImpl) rt.getModel(); 1.458 + } 1.459 + 1.460 + public static MetadataReader getExternalMetadatReader(Class<?> implType, WSBinding binding) { 1.461 + com.oracle.webservices.internal.api.databinding.ExternalMetadataFeature ef = binding.getFeature( 1.462 + com.oracle.webservices.internal.api.databinding.ExternalMetadataFeature.class); 1.463 + // TODO-Miran: would it be necessary to disable secure xml processing? 1.464 + if (ef != null) 1.465 + return ef.getMetadataReader(implType.getClassLoader(), false); 1.466 + return null; 1.467 + } 1.468 + 1.469 + /** 1.470 + *Set the mtom enable setting from wsdl model (mtom policy assertion) on to @link WSBinding} if DD has 1.471 + * not already set it on BindingID. Also check conflicts. 1.472 + */ 1.473 + /* 1.474 + private static void applyEffectiveMtomSetting(WSDLBoundPortType wsdlBinding, WSBinding binding){ 1.475 + if(wsdlBinding.isMTOMEnabled()){ 1.476 + BindingID bindingId = binding.getBindingId(); 1.477 + if(bindingId.isMTOMEnabled() == null){ 1.478 + binding.setMTOMEnabled(true); 1.479 + }else if (bindingId.isMTOMEnabled() != null && bindingId.isMTOMEnabled() == Boolean.FALSE){ 1.480 + //TODO: i18N 1.481 + throw new ServerRtException("Deployment failed! Mtom policy assertion in WSDL is enabled whereas the deplyment descriptor setting wants to disable it!"); 1.482 + } 1.483 + } 1.484 + } 1.485 + */ 1.486 + /** 1.487 + * If service name is not already set via DD or programmatically, it uses 1.488 + * annotations {@link WebServiceProvider}, {@link WebService} on implementorClass to get PortName. 1.489 + * 1.490 + * @return non-null service name 1.491 + */ 1.492 + public static @NotNull QName getDefaultServiceName(Class<?> implType) { 1.493 + return getDefaultServiceName(implType, null); 1.494 + } 1.495 + 1.496 + public static @NotNull QName getDefaultServiceName(Class<?> implType, MetadataReader metadataReader) { 1.497 + return getDefaultServiceName(implType, true, metadataReader); 1.498 + } 1.499 + 1.500 + public static @NotNull QName getDefaultServiceName(Class<?> implType, boolean isStandard) { 1.501 + return getDefaultServiceName(implType, isStandard, null); 1.502 + } 1.503 + 1.504 + public static @NotNull QName getDefaultServiceName(Class<?> implType, boolean isStandard, MetadataReader metadataReader) { 1.505 + if (metadataReader == null) { 1.506 + metadataReader = new ReflectAnnotationReader(); 1.507 + } 1.508 + QName serviceName; 1.509 + WebServiceProvider wsProvider = metadataReader.getAnnotation(WebServiceProvider.class, implType); 1.510 + if (wsProvider!=null) { 1.511 + String tns = wsProvider.targetNamespace(); 1.512 + String local = wsProvider.serviceName(); 1.513 + serviceName = new QName(tns, local); 1.514 + } else { 1.515 + serviceName = RuntimeModeler.getServiceName(implType, metadataReader, isStandard); 1.516 + } 1.517 + assert serviceName != null; 1.518 + return serviceName; 1.519 + } 1.520 + 1.521 + /** 1.522 + * If portName is not already set via DD or programmatically, it uses 1.523 + * annotations on implementorClass to get PortName. 1.524 + * 1.525 + * @return non-null port name 1.526 + */ 1.527 + public static @NotNull QName getDefaultPortName(QName serviceName, Class<?> implType) { 1.528 + return getDefaultPortName(serviceName, implType, null); 1.529 + } 1.530 + 1.531 + public static @NotNull QName getDefaultPortName(QName serviceName, Class<?> implType, MetadataReader metadataReader) { 1.532 + return getDefaultPortName(serviceName, implType, true, metadataReader); 1.533 + } 1.534 + 1.535 + public static @NotNull QName getDefaultPortName(QName serviceName, Class<?> implType, boolean isStandard) { 1.536 + return getDefaultPortName(serviceName, implType, isStandard, null); 1.537 + } 1.538 + 1.539 + public static @NotNull QName getDefaultPortName(QName serviceName, Class<?> implType, boolean isStandard, MetadataReader metadataReader) { 1.540 + if (metadataReader == null) { 1.541 + metadataReader = new ReflectAnnotationReader(); 1.542 + } 1.543 + QName portName; 1.544 + WebServiceProvider wsProvider = metadataReader.getAnnotation(WebServiceProvider.class, implType); 1.545 + if (wsProvider!=null) { 1.546 + String tns = wsProvider.targetNamespace(); 1.547 + String local = wsProvider.portName(); 1.548 + portName = new QName(tns, local); 1.549 + } else { 1.550 + portName = RuntimeModeler.getPortName(implType, metadataReader, serviceName.getNamespaceURI(), isStandard); 1.551 + } 1.552 + assert portName != null; 1.553 + return portName; 1.554 + } 1.555 + 1.556 + /** 1.557 + * Returns the wsdl from @WebService, or @WebServiceProvider annotation using 1.558 + * wsdlLocation element. 1.559 + * 1.560 + * @param implType 1.561 + * endpoint implementation class 1.562 + * make sure that you called {@link #verifyImplementorClass} on it. 1.563 + * @return wsdl if there is wsdlLocation, else null 1.564 + */ 1.565 + public static @Nullable String getWsdlLocation(Class<?> implType) { 1.566 + return getWsdlLocation(implType, new ReflectAnnotationReader()); 1.567 + } 1.568 + 1.569 + /** 1.570 + * Returns the wsdl from @WebService, or @WebServiceProvider annotation using 1.571 + * wsdlLocation element. 1.572 + * 1.573 + * @param implType 1.574 + * endpoint implementation class 1.575 + * make sure that you called {@link #verifyImplementorClass} on it. 1.576 + * @return wsdl if there is wsdlLocation, else null 1.577 + */ 1.578 + public static @Nullable String getWsdlLocation(Class<?> implType, MetadataReader metadataReader) { 1.579 + 1.580 + if (metadataReader == null) { 1.581 + metadataReader = new ReflectAnnotationReader(); 1.582 + } 1.583 + 1.584 + WebService ws = metadataReader.getAnnotation(WebService.class, implType); 1.585 + if (ws != null) { 1.586 + return nullIfEmpty(ws.wsdlLocation()); 1.587 + } else { 1.588 + WebServiceProvider wsProvider = implType.getAnnotation(WebServiceProvider.class); 1.589 + assert wsProvider != null; 1.590 + return nullIfEmpty(wsProvider.wsdlLocation()); 1.591 + } 1.592 + } 1.593 + 1.594 + private static String nullIfEmpty(String string) { 1.595 + if (string.length() < 1) { 1.596 + string = null; 1.597 + } 1.598 + return string; 1.599 + } 1.600 + 1.601 + /** 1.602 + * Generates the WSDL and XML Schema for the endpoint if necessary 1.603 + * It generates WSDL only for SOAP1.1, and for XSOAP1.2 bindings 1.604 + */ 1.605 + private static SDDocumentImpl generateWSDL(WSBinding binding, AbstractSEIModelImpl seiModel, List<SDDocumentImpl> docs, 1.606 + Container container, Class implType) { 1.607 + BindingID bindingId = binding.getBindingId(); 1.608 + if (!bindingId.canGenerateWSDL()) { 1.609 + throw new ServerRtException("can.not.generate.wsdl", bindingId); 1.610 + } 1.611 + 1.612 + if (bindingId.toString().equals(SOAPBindingImpl.X_SOAP12HTTP_BINDING)) { 1.613 + String msg = ServerMessages.GENERATE_NON_STANDARD_WSDL(); 1.614 + logger.warning(msg); 1.615 + } 1.616 + 1.617 + // Generate WSDL and schema documents using runtime model 1.618 + WSDLGenResolver wsdlResolver = new WSDLGenResolver(docs,seiModel.getServiceQName(),seiModel.getPortTypeName()); 1.619 + WSDLGenInfo wsdlGenInfo = new WSDLGenInfo(); 1.620 + wsdlGenInfo.setWsdlResolver(wsdlResolver); 1.621 + wsdlGenInfo.setContainer(container); 1.622 + wsdlGenInfo.setExtensions(ServiceFinder.find(WSDLGeneratorExtension.class).toArray()); 1.623 + wsdlGenInfo.setInlineSchemas(false); 1.624 + wsdlGenInfo.setSecureXmlProcessingDisabled(isSecureXmlProcessingDisabled(binding.getFeatures())); 1.625 + seiModel.getDatabinding().generateWSDL(wsdlGenInfo); 1.626 +// WSDLGenerator wsdlGen = new WSDLGenerator(seiModel, wsdlResolver, binding, container, implType, false, 1.627 +// ServiceFinder.find(WSDLGeneratorExtension.class).toArray()); 1.628 +// wsdlGen.doGeneration(); 1.629 + return wsdlResolver.updateDocs(); 1.630 + } 1.631 + 1.632 + private static boolean isSecureXmlProcessingDisabled(WSFeatureList featureList) { 1.633 + // TODO-Miran: would it be necessary to disable secure xml processing? 1.634 + return false; 1.635 + } 1.636 + 1.637 + /** 1.638 + * Builds {@link SDDocumentImpl} from {@link SDDocumentSource}. 1.639 + */ 1.640 + private static List<SDDocumentImpl> categoriseMetadata( 1.641 + List<SDDocumentSource> src, QName serviceName, QName portTypeName) { 1.642 + 1.643 + List<SDDocumentImpl> r = new ArrayList<SDDocumentImpl>(src.size()); 1.644 + for (SDDocumentSource doc : src) { 1.645 + r.add(SDDocumentImpl.create(doc,serviceName,portTypeName)); 1.646 + } 1.647 + return r; 1.648 + } 1.649 + 1.650 + /** 1.651 + * Verifies whether the given primaryWsdl contains the given serviceName. 1.652 + * If the WSDL doesn't have the service, it throws an WebServiceException. 1.653 + */ 1.654 + private static void verifyPrimaryWSDL(@NotNull SDDocumentSource primaryWsdl, @NotNull QName serviceName) { 1.655 + SDDocumentImpl primaryDoc = SDDocumentImpl.create(primaryWsdl,serviceName,null); 1.656 + if (!(primaryDoc instanceof SDDocument.WSDL)) { 1.657 + throw new WebServiceException(primaryWsdl.getSystemId()+ 1.658 + " is not a WSDL. But it is passed as a primary WSDL"); 1.659 + } 1.660 + SDDocument.WSDL wsdlDoc = (SDDocument.WSDL)primaryDoc; 1.661 + if (!wsdlDoc.hasService()) { 1.662 + if(wsdlDoc.getAllServices().isEmpty()) 1.663 + throw new WebServiceException("Not a primary WSDL="+primaryWsdl.getSystemId()+ 1.664 + " since it doesn't have Service "+serviceName); 1.665 + else 1.666 + throw new WebServiceException("WSDL "+primaryDoc.getSystemId() 1.667 + +" has the following services "+wsdlDoc.getAllServices() 1.668 + +" but not "+serviceName+". Maybe you forgot to specify a serviceName and/or targetNamespace in @WebService/@WebServiceProvider?"); 1.669 + } 1.670 + } 1.671 + 1.672 + /** 1.673 + * Finds the primary WSDL document from the list of metadata documents. If 1.674 + * there are two metadata documents that qualify for primary, it throws an 1.675 + * exception. If there are two metadata documents that qualify for porttype, 1.676 + * it throws an exception. 1.677 + * 1.678 + * @return primay wsdl document, null if is not there in the docList 1.679 + * 1.680 + */ 1.681 + private static @Nullable SDDocumentImpl findPrimary(@NotNull List<SDDocumentImpl> docList) { 1.682 + SDDocumentImpl primaryDoc = null; 1.683 + boolean foundConcrete = false; 1.684 + boolean foundAbstract = false; 1.685 + for(SDDocumentImpl doc : docList) { 1.686 + if (doc instanceof SDDocument.WSDL) { 1.687 + SDDocument.WSDL wsdlDoc = (SDDocument.WSDL)doc; 1.688 + if (wsdlDoc.hasService()) { 1.689 + primaryDoc = doc; 1.690 + if (foundConcrete) { 1.691 + throw new ServerRtException("duplicate.primary.wsdl", doc.getSystemId() ); 1.692 + } 1.693 + foundConcrete = true; 1.694 + } 1.695 + if (wsdlDoc.hasPortType()) { 1.696 + if (foundAbstract) { 1.697 + throw new ServerRtException("duplicate.abstract.wsdl", doc.getSystemId()); 1.698 + } 1.699 + foundAbstract = true; 1.700 + } 1.701 + } 1.702 + } 1.703 + return primaryDoc; 1.704 + } 1.705 + 1.706 + /** 1.707 + * Parses the primary WSDL and returns the {@link WSDLPort} for the given service and port names 1.708 + * 1.709 + * @param primaryWsdl Primary WSDL 1.710 + * @param metadata it may contain imported WSDL and schema documents 1.711 + * @param serviceName service name in wsdl 1.712 + * @param portName port name in WSDL 1.713 + * @param container container in which this service is running 1.714 + * @return non-null wsdl port object 1.715 + */ 1.716 + private static @NotNull WSDLPort getWSDLPort(SDDocumentSource primaryWsdl, List<? extends SDDocumentSource> metadata, 1.717 + @NotNull QName serviceName, @NotNull QName portName, Container container, 1.718 + EntityResolver resolver) { 1.719 + URL wsdlUrl = primaryWsdl.getSystemId(); 1.720 + try { 1.721 + // TODO: delegate to another entity resolver 1.722 + WSDLModel wsdlDoc = RuntimeWSDLParser.parse( 1.723 + new Parser(primaryWsdl), new EntityResolverImpl(metadata, resolver), 1.724 + false, container, ServiceFinder.find(WSDLParserExtension.class).toArray()); 1.725 + if(wsdlDoc.getServices().size() == 0) { 1.726 + throw new ServerRtException(ServerMessages.localizableRUNTIME_PARSER_WSDL_NOSERVICE_IN_WSDLMODEL(wsdlUrl)); 1.727 + } 1.728 + WSDLService wsdlService = wsdlDoc.getService(serviceName); 1.729 + if (wsdlService == null) { 1.730 + throw new ServerRtException(ServerMessages.localizableRUNTIME_PARSER_WSDL_INCORRECTSERVICE(serviceName,wsdlUrl)); 1.731 + } 1.732 + WSDLPort wsdlPort = wsdlService.get(portName); 1.733 + if (wsdlPort == null) { 1.734 + throw new ServerRtException(ServerMessages.localizableRUNTIME_PARSER_WSDL_INCORRECTSERVICEPORT(serviceName, portName, wsdlUrl)); 1.735 + } 1.736 + return wsdlPort; 1.737 + } catch (IOException e) { 1.738 + throw new ServerRtException("runtime.parser.wsdl", wsdlUrl,e); 1.739 + } catch (XMLStreamException e) { 1.740 + throw new ServerRtException("runtime.saxparser.exception", e.getMessage(), e.getLocation(), e); 1.741 + } catch (SAXException e) { 1.742 + throw new ServerRtException("runtime.parser.wsdl", wsdlUrl,e); 1.743 + } catch (ServiceConfigurationError e) { 1.744 + throw new ServerRtException("runtime.parser.wsdl", wsdlUrl,e); 1.745 + } 1.746 + } 1.747 + 1.748 + /** 1.749 + * {@link XMLEntityResolver} that can resolve to {@link SDDocumentSource}s. 1.750 + */ 1.751 + private static final class EntityResolverImpl implements XMLEntityResolver { 1.752 + private Map<String,SDDocumentSource> metadata = new HashMap<String,SDDocumentSource>(); 1.753 + private EntityResolver resolver; 1.754 + 1.755 + public EntityResolverImpl(List<? extends SDDocumentSource> metadata, EntityResolver resolver) { 1.756 + for (SDDocumentSource doc : metadata) { 1.757 + this.metadata.put(doc.getSystemId().toExternalForm(),doc); 1.758 + } 1.759 + this.resolver = resolver; 1.760 + } 1.761 + 1.762 + public Parser resolveEntity (String publicId, String systemId) throws IOException, XMLStreamException { 1.763 + if (systemId != null) { 1.764 + SDDocumentSource doc = metadata.get(systemId); 1.765 + if (doc != null) 1.766 + return new Parser(doc); 1.767 + } 1.768 + if (resolver != null) { 1.769 + try { 1.770 + InputSource source = resolver.resolveEntity(publicId, systemId); 1.771 + if (source != null) { 1.772 + Parser p = new Parser(null, XMLStreamReaderFactory.create(source, true)); 1.773 + return p; 1.774 + } 1.775 + } catch (SAXException e) { 1.776 + throw new XMLStreamException(e); 1.777 + } 1.778 + } 1.779 + return null; 1.780 + } 1.781 + 1.782 + } 1.783 + 1.784 + private static final Logger logger = Logger.getLogger( 1.785 + com.sun.xml.internal.ws.util.Constants.LoggingDomain + ".server.endpoint"); 1.786 +}