ohair@286: /* alanb@368: * 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.server; ohair@286: ohair@286: import com.sun.istack.internal.NotNull; ohair@286: import com.sun.istack.internal.Nullable; alanb@368: import com.sun.xml.internal.stream.buffer.MutableXMLStreamBuffer; ohair@286: import com.sun.xml.internal.ws.api.BindingID; ohair@286: import com.sun.xml.internal.ws.api.WSBinding; alanb@368: import com.sun.xml.internal.ws.api.WSFeatureList; alanb@368: import com.sun.xml.internal.ws.api.databinding.DatabindingConfig; alanb@368: import com.sun.xml.internal.ws.api.databinding.DatabindingFactory; alanb@368: import com.sun.xml.internal.ws.api.databinding.MetadataReader; ohair@286: import com.sun.xml.internal.ws.api.databinding.WSDLGenInfo; ohair@286: import com.sun.xml.internal.ws.api.model.SEIModel; mkos@408: import com.sun.xml.internal.ws.api.model.wsdl.WSDLModel; ohair@286: import com.sun.xml.internal.ws.api.model.wsdl.WSDLPort; mkos@408: import com.sun.xml.internal.ws.api.model.wsdl.WSDLService; alanb@368: import com.sun.xml.internal.ws.api.policy.PolicyResolver; alanb@368: import com.sun.xml.internal.ws.api.policy.PolicyResolverFactory; alanb@368: import com.sun.xml.internal.ws.api.server.AsyncProvider; alanb@368: import com.sun.xml.internal.ws.api.server.Container; alanb@368: import com.sun.xml.internal.ws.api.server.ContainerResolver; alanb@368: import com.sun.xml.internal.ws.api.server.InstanceResolver; alanb@368: import com.sun.xml.internal.ws.api.server.Invoker; alanb@368: import com.sun.xml.internal.ws.api.server.SDDocument; alanb@368: import com.sun.xml.internal.ws.api.server.SDDocumentSource; alanb@368: import com.sun.xml.internal.ws.api.server.WSEndpoint; alanb@368: import com.sun.xml.internal.ws.api.streaming.XMLStreamReaderFactory; ohair@286: import com.sun.xml.internal.ws.api.wsdl.parser.WSDLParserExtension; ohair@286: import com.sun.xml.internal.ws.api.wsdl.parser.XMLEntityResolver; ohair@286: import com.sun.xml.internal.ws.api.wsdl.parser.XMLEntityResolver.Parser; ohair@286: import com.sun.xml.internal.ws.api.wsdl.writer.WSDLGeneratorExtension; ohair@286: import com.sun.xml.internal.ws.binding.BindingImpl; ohair@286: import com.sun.xml.internal.ws.binding.SOAPBindingImpl; ohair@286: import com.sun.xml.internal.ws.binding.WebServiceFeatureList; ohair@286: import com.sun.xml.internal.ws.model.AbstractSEIModelImpl; alanb@368: import com.sun.xml.internal.ws.model.ReflectAnnotationReader; ohair@286: import com.sun.xml.internal.ws.model.RuntimeModeler; ohair@286: import com.sun.xml.internal.ws.model.SOAPSEIModel; alanb@368: import com.sun.xml.internal.ws.policy.PolicyMap; alanb@368: import com.sun.xml.internal.ws.policy.jaxws.PolicyUtil; ohair@286: import com.sun.xml.internal.ws.resources.ServerMessages; ohair@286: import com.sun.xml.internal.ws.server.provider.ProviderInvokerTube; ohair@286: import com.sun.xml.internal.ws.server.sei.SEIInvokerTube; ohair@286: import com.sun.xml.internal.ws.util.HandlerAnnotationInfo; ohair@286: import com.sun.xml.internal.ws.util.HandlerAnnotationProcessor; ohair@286: import com.sun.xml.internal.ws.util.ServiceConfigurationError; ohair@286: import com.sun.xml.internal.ws.util.ServiceFinder; alanb@368: 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.xml.sax.EntityResolver; alanb@368: import org.xml.sax.InputSource; ohair@286: import org.xml.sax.SAXException; ohair@286: ohair@286: import javax.jws.WebService; ohair@286: import javax.xml.namespace.QName; ohair@286: import javax.xml.stream.XMLStreamException; alanb@368: import javax.xml.stream.XMLStreamReader; ohair@286: import javax.xml.ws.Provider; ohair@286: import javax.xml.ws.WebServiceException; alanb@368: import javax.xml.ws.WebServiceFeature; ohair@286: import javax.xml.ws.WebServiceProvider; ohair@286: import javax.xml.ws.soap.SOAPBinding; mkos@408: ohair@286: import java.io.IOException; ohair@286: import java.net.URL; ohair@286: import java.util.ArrayList; ohair@286: import java.util.Collection; ohair@286: import java.util.HashMap; ohair@286: import java.util.List; ohair@286: import java.util.Map; ohair@286: import java.util.logging.Logger; ohair@286: ohair@286: /** ohair@286: * Entry point to the JAX-WS RI server-side runtime. ohair@286: * ohair@286: * @author Kohsuke Kawaguchi ohair@286: * @author Jitendra Kotamraju ohair@286: */ ohair@286: public class EndpointFactory { ohair@286: private static final EndpointFactory instance = new EndpointFactory(); ohair@286: ohair@286: public static EndpointFactory getInstance() { ohair@286: return instance; ohair@286: } ohair@286: ohair@286: /** ohair@286: * Implements {@link WSEndpoint#create}. ohair@286: * ohair@286: * No need to take WebServiceContext implementation. When InvokerPipe is ohair@286: * instantiated, it calls InstanceResolver to set up a WebServiceContext. ohair@286: * We shall only take delegate to getUserPrincipal and isUserInRole from adapter. ohair@286: * ohair@286: *

ohair@286: * Nobody else should be calling this method. ohair@286: */ ohair@286: public static WSEndpoint createEndpoint( ohair@286: Class implType, boolean processHandlerAnnotation, @Nullable Invoker invoker, ohair@286: @Nullable QName serviceName, @Nullable QName portName, ohair@286: @Nullable Container container, @Nullable WSBinding binding, ohair@286: @Nullable SDDocumentSource primaryWsdl, ohair@286: @Nullable Collection metadata, ohair@286: EntityResolver resolver, boolean isTransportSynchronous) { ohair@286: return createEndpoint(implType, processHandlerAnnotation, invoker, serviceName, ohair@286: portName, container, binding, primaryWsdl, metadata, resolver, isTransportSynchronous, true); ohair@286: } ohair@286: ohair@286: public static WSEndpoint createEndpoint( ohair@286: Class implType, boolean processHandlerAnnotation, @Nullable Invoker invoker, ohair@286: @Nullable QName serviceName, @Nullable QName portName, ohair@286: @Nullable Container container, @Nullable WSBinding binding, ohair@286: @Nullable SDDocumentSource primaryWsdl, ohair@286: @Nullable Collection metadata, ohair@286: EntityResolver resolver, boolean isTransportSynchronous, boolean isStandard) { ohair@286: EndpointFactory factory = container != null ? container.getSPI(EndpointFactory.class) : null; ohair@286: if (factory == null) ohair@286: factory = EndpointFactory.getInstance(); ohair@286: ohair@286: return factory.create( ohair@286: implType,processHandlerAnnotation, invoker,serviceName,portName,container,binding,primaryWsdl,metadata,resolver,isTransportSynchronous,isStandard); ohair@286: } ohair@286: ohair@286: /** ohair@286: * Implements {@link WSEndpoint#create}. ohair@286: * ohair@286: * No need to take WebServiceContext implementation. When InvokerPipe is ohair@286: * instantiated, it calls InstanceResolver to set up a WebServiceContext. ohair@286: * We shall only take delegate to getUserPrincipal and isUserInRole from adapter. ohair@286: * ohair@286: *

ohair@286: * Nobody else should be calling this method. ohair@286: */ ohair@286: public WSEndpoint create( ohair@286: Class implType, boolean processHandlerAnnotation, @Nullable Invoker invoker, ohair@286: @Nullable QName serviceName, @Nullable QName portName, ohair@286: @Nullable Container container, @Nullable WSBinding binding, ohair@286: @Nullable SDDocumentSource primaryWsdl, ohair@286: @Nullable Collection metadata, ohair@286: EntityResolver resolver, boolean isTransportSynchronous) { ohair@286: return create(implType, processHandlerAnnotation, invoker, serviceName, ohair@286: portName, container, binding, primaryWsdl, metadata, resolver, isTransportSynchronous, ohair@286: true); ohair@286: ohair@286: } ohair@286: ohair@286: public WSEndpoint create( ohair@286: Class implType, boolean processHandlerAnnotation, @Nullable Invoker invoker, ohair@286: @Nullable QName serviceName, @Nullable QName portName, ohair@286: @Nullable Container container, @Nullable WSBinding binding, ohair@286: @Nullable SDDocumentSource primaryWsdl, ohair@286: @Nullable Collection metadata, ohair@286: EntityResolver resolver, boolean isTransportSynchronous, boolean isStandard) { ohair@286: ohair@286: if(implType ==null) ohair@286: throw new IllegalArgumentException(); ohair@286: alanb@368: MetadataReader metadataReader = getExternalMetadatReader(implType, binding); alanb@368: ohair@286: if (isStandard) { alanb@368: verifyImplementorClass(implType, metadataReader); ohair@286: } ohair@286: ohair@286: if (invoker == null) { ohair@286: invoker = InstanceResolver.createDefault(implType).createInvoker(); ohair@286: } ohair@286: ohair@286: List md = new ArrayList(); ohair@286: if(metadata!=null) ohair@286: md.addAll(metadata); ohair@286: ohair@286: if(primaryWsdl!=null && !md.contains(primaryWsdl)) ohair@286: md.add(primaryWsdl); ohair@286: ohair@286: if(container==null) ohair@286: container = ContainerResolver.getInstance().getContainer(); ohair@286: ohair@286: if(serviceName==null) alanb@368: serviceName = getDefaultServiceName(implType, metadataReader); ohair@286: ohair@286: if(portName==null) alanb@368: portName = getDefaultPortName(serviceName,implType, metadataReader); ohair@286: ohair@286: {// error check ohair@286: String serviceNS = serviceName.getNamespaceURI(); ohair@286: String portNS = portName.getNamespaceURI(); ohair@286: if (!serviceNS.equals(portNS)) { ohair@286: throw new ServerRtException("wrong.tns.for.port",portNS, serviceNS); ohair@286: } ohair@286: } ohair@286: ohair@286: // setting a default binding ohair@286: if (binding == null) ohair@286: binding = BindingImpl.create(BindingID.parse(implType)); ohair@286: ohair@286: if ( isStandard && primaryWsdl != null) { ohair@286: verifyPrimaryWSDL(primaryWsdl, serviceName); ohair@286: } ohair@286: ohair@286: QName portTypeName = null; ohair@286: if (isStandard && implType.getAnnotation(WebServiceProvider.class)==null) { alanb@368: portTypeName = RuntimeModeler.getPortTypeName(implType, metadataReader); ohair@286: } ohair@286: ohair@286: // Categorises the documents as WSDL, Schema etc ohair@286: List docList = categoriseMetadata(md, serviceName, portTypeName); ohair@286: // Finds the primary WSDL and makes sure that metadata doesn't have ohair@286: // two concrete or abstract WSDLs alanb@368: SDDocumentImpl primaryDoc = primaryWsdl != null ? SDDocumentImpl.create(primaryWsdl,serviceName,portTypeName) : findPrimary(docList); ohair@286: ohair@286: EndpointAwareTube terminal; mkos@408: WSDLPort wsdlPort = null; ohair@286: AbstractSEIModelImpl seiModel = null; ohair@286: // create WSDL model ohair@286: if (primaryDoc != null) { alanb@368: wsdlPort = getWSDLPort(primaryDoc, docList, serviceName, portName, container, resolver); ohair@286: } ohair@286: ohair@286: WebServiceFeatureList features=((BindingImpl)binding).getFeatures(); ohair@286: if (isStandard) { ohair@286: features.parseAnnotations(implType); ohair@286: } ohair@286: PolicyMap policyMap = null; ohair@286: // create terminal pipe that invokes the application ohair@286: if (isUseProviderTube(implType, isStandard)) { ohair@286: //TODO incase of Provider, provide a way to User for complete control of the message processing by giving ohair@286: // ability to turn off the WSDL/Policy based features and its associated tubes. ohair@286: ohair@286: //Even in case of Provider, merge all features configured via WSDL/Policy or deployment configuration ohair@286: Iterable configFtrs; ohair@286: if(wsdlPort != null) { ohair@286: policyMap = wsdlPort.getOwner().getParent().getPolicyMap(); ohair@286: //Merge features from WSDL and other policy configuration ohair@286: configFtrs = wsdlPort.getFeatures(); ohair@286: } else { ohair@286: //No WSDL, so try to merge features from Policy configuration ohair@286: policyMap = PolicyResolverFactory.create().resolve( ohair@286: new PolicyResolver.ServerContext(null, container, implType, false)); ohair@286: configFtrs = PolicyUtil.getPortScopedFeatures(policyMap,serviceName,portName); ohair@286: } ohair@286: features.mergeFeatures(configFtrs, true); alanb@368: terminal = createProviderInvokerTube(implType, binding, invoker, container); ohair@286: } else { ohair@286: // Create runtime model for non Provider endpoints mkos@384: seiModel = createSEIModel(wsdlPort, implType, serviceName, portName, binding, primaryDoc); ohair@286: if(binding instanceof SOAPBindingImpl){ ohair@286: //set portKnownHeaders on Binding, so that they can be used for MU processing ohair@286: ((SOAPBindingImpl)binding).setPortKnownHeaders( ohair@286: ((SOAPSEIModel)seiModel).getKnownHeaders()); ohair@286: } ohair@286: // Generate WSDL for SEI endpoints(not for Provider endpoints) ohair@286: if (primaryDoc == null) { ohair@286: primaryDoc = generateWSDL(binding, seiModel, docList, container, implType); ohair@286: // create WSDL model alanb@368: wsdlPort = getWSDLPort(primaryDoc, docList, serviceName, portName, container, resolver); ohair@286: seiModel.freeze(wsdlPort); ohair@286: } ohair@286: policyMap = wsdlPort.getOwner().getParent().getPolicyMap(); ohair@286: // New Features might have been added in WSDL through Policy. ohair@286: //Merge features from WSDL and other policy configuration ohair@286: // This sets only the wsdl features that are not already set(enabled/disabled) ohair@286: features.mergeFeatures(wsdlPort.getFeatures(), true); ohair@286: terminal = createSEIInvokerTube(seiModel,invoker,binding); ohair@286: } ohair@286: ohair@286: // Process @HandlerChain, if handler-chain is not set via Deployment Descriptor ohair@286: if (processHandlerAnnotation) { ohair@286: processHandlerAnnotation(binding, implType, serviceName, portName); ohair@286: } ohair@286: // Selects only required metadata for this endpoint from the passed-in metadata ohair@286: if (primaryDoc != null) { alanb@368: docList = findMetadataClosure(primaryDoc, docList, resolver); ohair@286: } alanb@368: ohair@286: ServiceDefinitionImpl serviceDefiniton = (primaryDoc != null) ? new ServiceDefinitionImpl(docList, primaryDoc) : null; ohair@286: ohair@286: return create(serviceName, portName, binding, container, seiModel, wsdlPort, implType, serviceDefiniton, ohair@286: terminal, isTransportSynchronous, policyMap); ohair@286: } ohair@286: ohair@286: protected WSEndpoint create(QName serviceName, QName portName, WSBinding binding, Container container, SEIModel seiModel, WSDLPort wsdlPort, Class implType, ServiceDefinitionImpl serviceDefinition, EndpointAwareTube terminal, boolean isTransportSynchronous, PolicyMap policyMap) { ohair@286: return new WSEndpointImpl(serviceName, portName, binding, container, seiModel, ohair@286: wsdlPort, implType, serviceDefinition, terminal, isTransportSynchronous, policyMap); ohair@286: } ohair@286: ohair@286: protected boolean isUseProviderTube(Class implType, boolean isStandard) { ohair@286: return !isStandard || implType.getAnnotation(WebServiceProvider.class)!=null; ohair@286: } ohair@286: ohair@286: protected EndpointAwareTube createSEIInvokerTube(AbstractSEIModelImpl seiModel, Invoker invoker, WSBinding binding) { ohair@286: return new SEIInvokerTube(seiModel,invoker,binding); ohair@286: } ohair@286: alanb@368: protected EndpointAwareTube createProviderInvokerTube(final Class implType, final WSBinding binding, alanb@368: final Invoker invoker, final Container container) { alanb@368: return ProviderInvokerTube.create(implType, binding, invoker, container); ohair@286: } ohair@286: /** ohair@286: * Goes through the original metadata documents and collects the required ones. ohair@286: * This done traversing from primary WSDL and its imports until it builds a ohair@286: * complete set of documents(transitive closure) for the endpoint. ohair@286: * ohair@286: * @param primaryDoc primary WSDL doc ohair@286: * @param docList complete metadata ohair@286: * @return new metadata that doesn't contain extraneous documnets. ohair@286: */ alanb@368: private static List findMetadataClosure(SDDocumentImpl primaryDoc, List docList, EntityResolver resolver) { ohair@286: // create a map for old metadata ohair@286: Map oldMap = new HashMap(); ohair@286: for(SDDocumentImpl doc : docList) { ohair@286: oldMap.put(doc.getSystemId().toString(), doc); ohair@286: } ohair@286: // create a map for new metadata ohair@286: Map newMap = new HashMap(); ohair@286: newMap.put(primaryDoc.getSystemId().toString(), primaryDoc); ohair@286: ohair@286: List remaining = new ArrayList(); ohair@286: remaining.addAll(primaryDoc.getImports()); ohair@286: while(!remaining.isEmpty()) { ohair@286: String url = remaining.remove(0); ohair@286: SDDocumentImpl doc = oldMap.get(url); ohair@286: if (doc == null) { ohair@286: // old metadata doesn't have this imported doc, may be external alanb@368: if (resolver != null) { alanb@368: try { alanb@368: InputSource source = resolver.resolveEntity(null, url); alanb@368: if (source != null) { alanb@368: MutableXMLStreamBuffer xsb = new MutableXMLStreamBuffer(); alanb@368: XMLStreamReader reader = XmlUtil.newXMLInputFactory(true).createXMLStreamReader(source.getByteStream()); alanb@368: xsb.createFromXMLStreamReader(reader); alanb@368: alanb@368: SDDocumentSource sdocSource = SDDocumentImpl.create(new URL(url), xsb); alanb@368: doc = SDDocumentImpl.create(sdocSource, null, null); alanb@368: } alanb@368: } catch (Exception ex) { alanb@368: ex.printStackTrace(); alanb@368: } alanb@368: } ohair@286: } ohair@286: // Check if new metadata already contains this doc alanb@368: if (doc != null && !newMap.containsKey(url)) { ohair@286: newMap.put(url, doc); ohair@286: remaining.addAll(doc.getImports()); ohair@286: } ohair@286: } ohair@286: List newMetadata = new ArrayList(); ohair@286: newMetadata.addAll(newMap.values()); ohair@286: return newMetadata; ohair@286: } ohair@286: ohair@286: private static void processHandlerAnnotation(WSBinding binding, Class implType, QName serviceName, QName portName) { ohair@286: HandlerAnnotationInfo chainInfo = ohair@286: HandlerAnnotationProcessor.buildHandlerInfo( ohair@286: implType, serviceName, portName, binding); ohair@286: if (chainInfo != null) { ohair@286: binding.setHandlerChain(chainInfo.getHandlers()); ohair@286: if (binding instanceof SOAPBinding) { ohair@286: ((SOAPBinding) binding).setRoles(chainInfo.getRoles()); ohair@286: } ohair@286: } ohair@286: ohair@286: } ohair@286: ohair@286: /** ohair@286: * Verifies if the endpoint implementor class has @WebService or @WebServiceProvider ohair@286: * annotation ohair@286: * ohair@286: * @return ohair@286: * true if it is a Provider or AsyncProvider endpoint ohair@286: * false otherwise ohair@286: * @throws java.lang.IllegalArgumentException ohair@286: * If it doesn't have any one of @WebService or @WebServiceProvider ohair@286: * If it has both @WebService and @WebServiceProvider annotations ohair@286: */ ohair@286: public static boolean verifyImplementorClass(Class clz) { alanb@368: return verifyImplementorClass(clz, null); alanb@368: } alanb@368: alanb@368: /** alanb@368: * Verifies if the endpoint implementor class has @WebService or @WebServiceProvider alanb@368: * annotation; passing MetadataReader instance allows to read annotations from alanb@368: * xml descriptor instead of class's annotations alanb@368: * alanb@368: * @return alanb@368: * true if it is a Provider or AsyncProvider endpoint alanb@368: * false otherwise alanb@368: * @throws java.lang.IllegalArgumentException alanb@368: * If it doesn't have any one of @WebService or @WebServiceProvider alanb@368: * If it has both @WebService and @WebServiceProvider annotations alanb@368: */ alanb@368: public static boolean verifyImplementorClass(Class clz, MetadataReader metadataReader) { alanb@368: alanb@368: if (metadataReader == null) { alanb@368: metadataReader = new ReflectAnnotationReader(); alanb@368: } alanb@368: alanb@368: WebServiceProvider wsProvider = metadataReader.getAnnotation(WebServiceProvider.class, clz); alanb@368: WebService ws = metadataReader.getAnnotation(WebService.class, clz); ohair@286: if (wsProvider == null && ws == null) { ohair@286: throw new IllegalArgumentException(clz +" has neither @WebService nor @WebServiceProvider annotation"); ohair@286: } ohair@286: if (wsProvider != null && ws != null) { ohair@286: throw new IllegalArgumentException(clz +" has both @WebService and @WebServiceProvider annotations"); ohair@286: } ohair@286: if (wsProvider != null) { ohair@286: if (Provider.class.isAssignableFrom(clz) || AsyncProvider.class.isAssignableFrom(clz)) { ohair@286: return true; ohair@286: } ohair@286: throw new IllegalArgumentException(clz +" doesn't implement Provider or AsyncProvider interface"); ohair@286: } ohair@286: return false; ohair@286: } ohair@286: ohair@286: ohair@286: private static AbstractSEIModelImpl createSEIModel(WSDLPort wsdlPort, mkos@384: Class implType, @NotNull QName serviceName, @NotNull QName portName, WSBinding binding, mkos@384: SDDocumentSource primaryWsdl) { ohair@286: DatabindingFactory fac = DatabindingFactory.newInstance(); ohair@286: DatabindingConfig config = new DatabindingConfig(); ohair@286: config.setEndpointClass(implType); ohair@286: config.getMappingInfo().setServiceName(serviceName); ohair@286: config.setWsdlPort(wsdlPort); ohair@286: config.setWSBinding(binding); ohair@286: config.setClassLoader(implType.getClassLoader()); ohair@286: config.getMappingInfo().setPortName(portName); mkos@384: if (primaryWsdl != null) config.setWsdlURL(primaryWsdl.getSystemId()); alanb@368: config.setMetadataReader(getExternalMetadatReader(implType, binding)); alanb@368: ohair@286: com.sun.xml.internal.ws.db.DatabindingImpl rt = (com.sun.xml.internal.ws.db.DatabindingImpl)fac.createRuntime(config); ohair@286: return (AbstractSEIModelImpl) rt.getModel(); ohair@286: } ohair@286: alanb@368: public static MetadataReader getExternalMetadatReader(Class implType, WSBinding binding) { alanb@368: com.oracle.webservices.internal.api.databinding.ExternalMetadataFeature ef = binding.getFeature( alanb@368: com.oracle.webservices.internal.api.databinding.ExternalMetadataFeature.class); alanb@368: // TODO-Miran: would it be necessary to disable secure xml processing? alanb@368: if (ef != null) alanb@368: return ef.getMetadataReader(implType.getClassLoader(), false); alanb@368: return null; alanb@368: } alanb@368: ohair@286: /** ohair@286: *Set the mtom enable setting from wsdl model (mtom policy assertion) on to @link WSBinding} if DD has ohair@286: * not already set it on BindingID. Also check conflicts. ohair@286: */ ohair@286: /* ohair@286: private static void applyEffectiveMtomSetting(WSDLBoundPortType wsdlBinding, WSBinding binding){ ohair@286: if(wsdlBinding.isMTOMEnabled()){ ohair@286: BindingID bindingId = binding.getBindingId(); ohair@286: if(bindingId.isMTOMEnabled() == null){ ohair@286: binding.setMTOMEnabled(true); ohair@286: }else if (bindingId.isMTOMEnabled() != null && bindingId.isMTOMEnabled() == Boolean.FALSE){ ohair@286: //TODO: i18N ohair@286: throw new ServerRtException("Deployment failed! Mtom policy assertion in WSDL is enabled whereas the deplyment descriptor setting wants to disable it!"); ohair@286: } ohair@286: } ohair@286: } ohair@286: */ ohair@286: /** ohair@286: * If service name is not already set via DD or programmatically, it uses ohair@286: * annotations {@link WebServiceProvider}, {@link WebService} on implementorClass to get PortName. ohair@286: * ohair@286: * @return non-null service name ohair@286: */ ohair@286: public static @NotNull QName getDefaultServiceName(Class implType) { alanb@368: return getDefaultServiceName(implType, null); alanb@368: } alanb@368: alanb@368: public static @NotNull QName getDefaultServiceName(Class implType, MetadataReader metadataReader) { alanb@368: return getDefaultServiceName(implType, true, metadataReader); ohair@286: } ohair@286: ohair@286: public static @NotNull QName getDefaultServiceName(Class implType, boolean isStandard) { alanb@368: return getDefaultServiceName(implType, isStandard, null); alanb@368: } alanb@368: alanb@368: public static @NotNull QName getDefaultServiceName(Class implType, boolean isStandard, MetadataReader metadataReader) { alanb@368: if (metadataReader == null) { alanb@368: metadataReader = new ReflectAnnotationReader(); alanb@368: } ohair@286: QName serviceName; alanb@368: WebServiceProvider wsProvider = metadataReader.getAnnotation(WebServiceProvider.class, implType); ohair@286: if (wsProvider!=null) { ohair@286: String tns = wsProvider.targetNamespace(); ohair@286: String local = wsProvider.serviceName(); ohair@286: serviceName = new QName(tns, local); ohair@286: } else { alanb@368: serviceName = RuntimeModeler.getServiceName(implType, metadataReader, isStandard); ohair@286: } ohair@286: assert serviceName != null; ohair@286: return serviceName; ohair@286: } ohair@286: ohair@286: /** ohair@286: * If portName is not already set via DD or programmatically, it uses ohair@286: * annotations on implementorClass to get PortName. ohair@286: * ohair@286: * @return non-null port name ohair@286: */ ohair@286: public static @NotNull QName getDefaultPortName(QName serviceName, Class implType) { alanb@368: return getDefaultPortName(serviceName, implType, null); alanb@368: } alanb@368: alanb@368: public static @NotNull QName getDefaultPortName(QName serviceName, Class implType, MetadataReader metadataReader) { alanb@368: return getDefaultPortName(serviceName, implType, true, metadataReader); ohair@286: } ohair@286: ohair@286: public static @NotNull QName getDefaultPortName(QName serviceName, Class implType, boolean isStandard) { alanb@368: return getDefaultPortName(serviceName, implType, isStandard, null); alanb@368: } alanb@368: alanb@368: public static @NotNull QName getDefaultPortName(QName serviceName, Class implType, boolean isStandard, MetadataReader metadataReader) { alanb@368: if (metadataReader == null) { alanb@368: metadataReader = new ReflectAnnotationReader(); alanb@368: } ohair@286: QName portName; alanb@368: WebServiceProvider wsProvider = metadataReader.getAnnotation(WebServiceProvider.class, implType); ohair@286: if (wsProvider!=null) { ohair@286: String tns = wsProvider.targetNamespace(); ohair@286: String local = wsProvider.portName(); ohair@286: portName = new QName(tns, local); ohair@286: } else { alanb@368: portName = RuntimeModeler.getPortName(implType, metadataReader, serviceName.getNamespaceURI(), isStandard); ohair@286: } ohair@286: assert portName != null; ohair@286: return portName; ohair@286: } ohair@286: ohair@286: /** ohair@286: * Returns the wsdl from @WebService, or @WebServiceProvider annotation using ohair@286: * wsdlLocation element. ohair@286: * ohair@286: * @param implType ohair@286: * endpoint implementation class ohair@286: * make sure that you called {@link #verifyImplementorClass} on it. ohair@286: * @return wsdl if there is wsdlLocation, else null ohair@286: */ ohair@286: public static @Nullable String getWsdlLocation(Class implType) { alanb@368: return getWsdlLocation(implType, new ReflectAnnotationReader()); alanb@368: } alanb@368: alanb@368: /** alanb@368: * Returns the wsdl from @WebService, or @WebServiceProvider annotation using alanb@368: * wsdlLocation element. alanb@368: * alanb@368: * @param implType alanb@368: * endpoint implementation class alanb@368: * make sure that you called {@link #verifyImplementorClass} on it. alanb@368: * @return wsdl if there is wsdlLocation, else null alanb@368: */ alanb@368: public static @Nullable String getWsdlLocation(Class implType, MetadataReader metadataReader) { alanb@368: alanb@368: if (metadataReader == null) { alanb@368: metadataReader = new ReflectAnnotationReader(); alanb@368: } alanb@368: alanb@368: WebService ws = metadataReader.getAnnotation(WebService.class, implType); ohair@286: if (ws != null) { alanb@368: return nullIfEmpty(ws.wsdlLocation()); ohair@286: } else { ohair@286: WebServiceProvider wsProvider = implType.getAnnotation(WebServiceProvider.class); ohair@286: assert wsProvider != null; alanb@368: return nullIfEmpty(wsProvider.wsdlLocation()); ohair@286: } alanb@368: } alanb@368: alanb@368: private static String nullIfEmpty(String string) { alanb@368: if (string.length() < 1) { alanb@368: string = null; ohair@286: } alanb@368: return string; ohair@286: } ohair@286: ohair@286: /** ohair@286: * Generates the WSDL and XML Schema for the endpoint if necessary ohair@286: * It generates WSDL only for SOAP1.1, and for XSOAP1.2 bindings ohair@286: */ ohair@286: private static SDDocumentImpl generateWSDL(WSBinding binding, AbstractSEIModelImpl seiModel, List docs, ohair@286: Container container, Class implType) { ohair@286: BindingID bindingId = binding.getBindingId(); ohair@286: if (!bindingId.canGenerateWSDL()) { ohair@286: throw new ServerRtException("can.not.generate.wsdl", bindingId); ohair@286: } ohair@286: ohair@286: if (bindingId.toString().equals(SOAPBindingImpl.X_SOAP12HTTP_BINDING)) { ohair@286: String msg = ServerMessages.GENERATE_NON_STANDARD_WSDL(); ohair@286: logger.warning(msg); ohair@286: } ohair@286: ohair@286: // Generate WSDL and schema documents using runtime model ohair@286: WSDLGenResolver wsdlResolver = new WSDLGenResolver(docs,seiModel.getServiceQName(),seiModel.getPortTypeName()); ohair@286: WSDLGenInfo wsdlGenInfo = new WSDLGenInfo(); ohair@286: wsdlGenInfo.setWsdlResolver(wsdlResolver); ohair@286: wsdlGenInfo.setContainer(container); ohair@286: wsdlGenInfo.setExtensions(ServiceFinder.find(WSDLGeneratorExtension.class).toArray()); ohair@286: wsdlGenInfo.setInlineSchemas(false); alanb@368: wsdlGenInfo.setSecureXmlProcessingDisabled(isSecureXmlProcessingDisabled(binding.getFeatures())); ohair@286: seiModel.getDatabinding().generateWSDL(wsdlGenInfo); ohair@286: // WSDLGenerator wsdlGen = new WSDLGenerator(seiModel, wsdlResolver, binding, container, implType, false, ohair@286: // ServiceFinder.find(WSDLGeneratorExtension.class).toArray()); ohair@286: // wsdlGen.doGeneration(); ohair@286: return wsdlResolver.updateDocs(); ohair@286: } ohair@286: alanb@368: private static boolean isSecureXmlProcessingDisabled(WSFeatureList featureList) { alanb@368: // TODO-Miran: would it be necessary to disable secure xml processing? alanb@368: return false; alanb@368: } alanb@368: ohair@286: /** ohair@286: * Builds {@link SDDocumentImpl} from {@link SDDocumentSource}. ohair@286: */ ohair@286: private static List categoriseMetadata( ohair@286: List src, QName serviceName, QName portTypeName) { ohair@286: ohair@286: List r = new ArrayList(src.size()); ohair@286: for (SDDocumentSource doc : src) { ohair@286: r.add(SDDocumentImpl.create(doc,serviceName,portTypeName)); ohair@286: } ohair@286: return r; ohair@286: } ohair@286: ohair@286: /** ohair@286: * Verifies whether the given primaryWsdl contains the given serviceName. ohair@286: * If the WSDL doesn't have the service, it throws an WebServiceException. ohair@286: */ ohair@286: private static void verifyPrimaryWSDL(@NotNull SDDocumentSource primaryWsdl, @NotNull QName serviceName) { ohair@286: SDDocumentImpl primaryDoc = SDDocumentImpl.create(primaryWsdl,serviceName,null); ohair@286: if (!(primaryDoc instanceof SDDocument.WSDL)) { ohair@286: throw new WebServiceException(primaryWsdl.getSystemId()+ ohair@286: " is not a WSDL. But it is passed as a primary WSDL"); ohair@286: } ohair@286: SDDocument.WSDL wsdlDoc = (SDDocument.WSDL)primaryDoc; ohair@286: if (!wsdlDoc.hasService()) { ohair@286: if(wsdlDoc.getAllServices().isEmpty()) ohair@286: throw new WebServiceException("Not a primary WSDL="+primaryWsdl.getSystemId()+ ohair@286: " since it doesn't have Service "+serviceName); ohair@286: else ohair@286: throw new WebServiceException("WSDL "+primaryDoc.getSystemId() ohair@286: +" has the following services "+wsdlDoc.getAllServices() ohair@286: +" but not "+serviceName+". Maybe you forgot to specify a serviceName and/or targetNamespace in @WebService/@WebServiceProvider?"); ohair@286: } ohair@286: } ohair@286: ohair@286: /** ohair@286: * Finds the primary WSDL document from the list of metadata documents. If ohair@286: * there are two metadata documents that qualify for primary, it throws an ohair@286: * exception. If there are two metadata documents that qualify for porttype, ohair@286: * it throws an exception. ohair@286: * ohair@286: * @return primay wsdl document, null if is not there in the docList ohair@286: * ohair@286: */ ohair@286: private static @Nullable SDDocumentImpl findPrimary(@NotNull List docList) { ohair@286: SDDocumentImpl primaryDoc = null; ohair@286: boolean foundConcrete = false; ohair@286: boolean foundAbstract = false; ohair@286: for(SDDocumentImpl doc : docList) { ohair@286: if (doc instanceof SDDocument.WSDL) { ohair@286: SDDocument.WSDL wsdlDoc = (SDDocument.WSDL)doc; ohair@286: if (wsdlDoc.hasService()) { ohair@286: primaryDoc = doc; ohair@286: if (foundConcrete) { ohair@286: throw new ServerRtException("duplicate.primary.wsdl", doc.getSystemId() ); ohair@286: } ohair@286: foundConcrete = true; ohair@286: } ohair@286: if (wsdlDoc.hasPortType()) { ohair@286: if (foundAbstract) { ohair@286: throw new ServerRtException("duplicate.abstract.wsdl", doc.getSystemId()); ohair@286: } ohair@286: foundAbstract = true; ohair@286: } ohair@286: } ohair@286: } ohair@286: return primaryDoc; ohair@286: } ohair@286: ohair@286: /** ohair@286: * Parses the primary WSDL and returns the {@link WSDLPort} for the given service and port names ohair@286: * ohair@286: * @param primaryWsdl Primary WSDL ohair@286: * @param metadata it may contain imported WSDL and schema documents ohair@286: * @param serviceName service name in wsdl ohair@286: * @param portName port name in WSDL ohair@286: * @param container container in which this service is running ohair@286: * @return non-null wsdl port object ohair@286: */ mkos@408: private static @NotNull WSDLPort getWSDLPort(SDDocumentSource primaryWsdl, List metadata, alanb@368: @NotNull QName serviceName, @NotNull QName portName, Container container, alanb@368: EntityResolver resolver) { ohair@286: URL wsdlUrl = primaryWsdl.getSystemId(); ohair@286: try { ohair@286: // TODO: delegate to another entity resolver mkos@408: WSDLModel wsdlDoc = RuntimeWSDLParser.parse( alanb@368: new Parser(primaryWsdl), new EntityResolverImpl(metadata, resolver), ohair@286: false, container, ServiceFinder.find(WSDLParserExtension.class).toArray()); ohair@286: if(wsdlDoc.getServices().size() == 0) { ohair@286: throw new ServerRtException(ServerMessages.localizableRUNTIME_PARSER_WSDL_NOSERVICE_IN_WSDLMODEL(wsdlUrl)); ohair@286: } mkos@408: WSDLService wsdlService = wsdlDoc.getService(serviceName); ohair@286: if (wsdlService == null) { ohair@286: throw new ServerRtException(ServerMessages.localizableRUNTIME_PARSER_WSDL_INCORRECTSERVICE(serviceName,wsdlUrl)); ohair@286: } mkos@408: WSDLPort wsdlPort = wsdlService.get(portName); ohair@286: if (wsdlPort == null) { ohair@286: throw new ServerRtException(ServerMessages.localizableRUNTIME_PARSER_WSDL_INCORRECTSERVICEPORT(serviceName, portName, wsdlUrl)); ohair@286: } ohair@286: return wsdlPort; ohair@286: } catch (IOException e) { ohair@286: throw new ServerRtException("runtime.parser.wsdl", wsdlUrl,e); ohair@286: } catch (XMLStreamException e) { ohair@286: throw new ServerRtException("runtime.saxparser.exception", e.getMessage(), e.getLocation(), e); ohair@286: } catch (SAXException e) { ohair@286: throw new ServerRtException("runtime.parser.wsdl", wsdlUrl,e); ohair@286: } catch (ServiceConfigurationError e) { ohair@286: throw new ServerRtException("runtime.parser.wsdl", wsdlUrl,e); ohair@286: } ohair@286: } ohair@286: ohair@286: /** ohair@286: * {@link XMLEntityResolver} that can resolve to {@link SDDocumentSource}s. ohair@286: */ ohair@286: private static final class EntityResolverImpl implements XMLEntityResolver { ohair@286: private Map metadata = new HashMap(); alanb@368: private EntityResolver resolver; ohair@286: alanb@368: public EntityResolverImpl(List metadata, EntityResolver resolver) { ohair@286: for (SDDocumentSource doc : metadata) { ohair@286: this.metadata.put(doc.getSystemId().toExternalForm(),doc); ohair@286: } alanb@368: this.resolver = resolver; ohair@286: } ohair@286: ohair@286: public Parser resolveEntity (String publicId, String systemId) throws IOException, XMLStreamException { ohair@286: if (systemId != null) { ohair@286: SDDocumentSource doc = metadata.get(systemId); ohair@286: if (doc != null) ohair@286: return new Parser(doc); ohair@286: } alanb@368: if (resolver != null) { alanb@368: try { alanb@368: InputSource source = resolver.resolveEntity(publicId, systemId); alanb@368: if (source != null) { alanb@368: Parser p = new Parser(null, XMLStreamReaderFactory.create(source, true)); alanb@368: return p; alanb@368: } alanb@368: } catch (SAXException e) { alanb@368: throw new XMLStreamException(e); alanb@368: } alanb@368: } ohair@286: return null; ohair@286: } ohair@286: ohair@286: } ohair@286: ohair@286: private static final Logger logger = Logger.getLogger( ohair@286: com.sun.xml.internal.ws.util.Constants.LoggingDomain + ".server.endpoint"); ohair@286: }