com.sun.xml.ws.client.WSServiceDelegate Maven / Gradle / Ivy
Show all versions of jaxws-rt Show documentation
/*
* Copyright (c) 1997, 2022 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package com.sun.xml.ws.client;
import com.sun.istack.NotNull;
import com.sun.istack.Nullable;
import com.sun.xml.ws.Closeable;
import com.sun.xml.ws.api.BindingID;
import com.sun.xml.ws.api.ComponentFeature;
import com.sun.xml.ws.api.ComponentsFeature;
import com.sun.xml.ws.api.ComponentFeature.Target;
import com.sun.xml.ws.api.EndpointAddress;
import com.sun.xml.ws.api.WSService;
import com.sun.xml.ws.api.addressing.WSEndpointReference;
import com.sun.xml.ws.api.client.ServiceInterceptor;
import com.sun.xml.ws.api.client.ServiceInterceptorFactory;
import com.sun.xml.ws.api.databinding.DatabindingConfig;
import com.sun.xml.ws.api.databinding.DatabindingFactory;
import com.sun.xml.ws.api.databinding.MetadataReader;
import com.sun.xml.ws.api.model.SEIModel;
import com.sun.xml.ws.api.model.wsdl.WSDLModel;
import com.sun.xml.ws.api.model.wsdl.WSDLPort;
import com.sun.xml.ws.api.model.wsdl.WSDLService;
import com.sun.xml.ws.api.pipe.Stubs;
import com.sun.xml.ws.api.server.Container;
import com.sun.xml.ws.api.server.ContainerResolver;
import com.sun.xml.ws.api.wsdl.parser.WSDLParserExtension;
import com.sun.xml.ws.binding.BindingImpl;
import com.sun.xml.ws.binding.WebServiceFeatureList;
import com.sun.xml.ws.client.HandlerConfigurator.AnnotationConfigurator;
import com.sun.xml.ws.client.HandlerConfigurator.HandlerResolverImpl;
import com.sun.xml.ws.client.sei.SEIStub;
import com.sun.xml.ws.developer.MemberSubmissionAddressingFeature;
import com.sun.xml.ws.developer.UsesJAXBContextFeature;
import com.sun.xml.ws.developer.WSBindingProvider;
import com.sun.xml.ws.model.RuntimeModeler;
import com.sun.xml.ws.model.SOAPSEIModel;
import com.sun.xml.ws.model.wsdl.WSDLPortImpl;
import com.sun.xml.ws.resources.ClientMessages;
import com.sun.xml.ws.resources.DispatchMessages;
import com.sun.xml.ws.resources.ProviderApiMessages;
import com.sun.xml.ws.util.JAXWSUtils;
import com.sun.xml.ws.util.ServiceConfigurationError;
import com.sun.xml.ws.util.ServiceFinder;
import com.sun.xml.ws.util.xml.XmlUtil;
import com.sun.xml.ws.wsdl.parser.RuntimeWSDLParser;
import org.xml.sax.EntityResolver;
import org.xml.sax.SAXException;
import jakarta.jws.HandlerChain;
import jakarta.jws.WebService;
import jakarta.xml.bind.JAXBContext;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamException;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import jakarta.xml.ws.BindingProvider;
import jakarta.xml.ws.Dispatch;
import jakarta.xml.ws.EndpointReference;
import jakarta.xml.ws.Service;
import jakarta.xml.ws.WebServiceClient;
import jakarta.xml.ws.WebServiceException;
import jakarta.xml.ws.WebServiceFeature;
import jakarta.xml.ws.handler.HandlerResolver;
import jakarta.xml.ws.soap.AddressingFeature;
import java.io.IOException;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Executor;
/**
* Service
objects provide the client view of a Web service.
*
* Service
acts as a factory of the following:
*
* - Proxies for a target service endpoint.
*
- Instances of
jakarta.xml.ws.Dispatch
for
* dynamic message-oriented invocation of a remote
* operation.
*
*
*
* The ports available on a service can be enumerated using the
* getPorts
method. Alternatively, you can pass a
* service endpoint interface to the unary getPort
method
* and let the runtime select a compatible port.
*
*
Handler chains for all the objects created by a Service
* can be set by means of the provided HandlerRegistry
.
*
*
An Executor
may be set on the service in order
* to gain better control over the threads used to dispatch asynchronous
* callbacks. For instance, thread pooling with certain parameters
* can be enabled by creating a ThreadPoolExecutor
and
* registering it with the service.
*
* @author WS Development Team
* @see Executor
* @since JAX-WS 2.0
*/
public class WSServiceDelegate extends WSService {
/**
* All ports.
*
* This includes ports statically known to WSDL, as well as
* ones that are dynamically added
* through {@link #addPort(QName, String, String)}.
*
* For statically known ports we'll have {@link SEIPortInfo}.
* For dynamically added ones we'll have {@link PortInfo}.
*/
private final Map ports = new HashMap<>();
// For monitoring
protected Map getQNameToPortInfoMap() { return ports; }
/**
* Whenever we create {@link BindingProvider}, we use this to configure handlers.
*/
private @NotNull HandlerConfigurator handlerConfigurator = new HandlerResolverImpl(null);
private final Class extends Service> serviceClass;
private final WebServiceFeatureList features;
/**
* Name of the service for which this {@link WSServiceDelegate} is created for.
*/
private final @NotNull QName serviceName;
/**
* Information about SEI, keyed by their interface type.
*/
// private final Map seiContext = new HashMap();
private final Map seiContext = new HashMap<>();
// This executor is used for all the async invocations for all proxies
// created from this service. But once the proxy is created, then changing
// this executor doesn't affect the already created proxies.
private volatile Executor executor;
/**
* The WSDL service that this {@link Service} object represents.
*
* This field is null iff no WSDL is given to {@link Service}.
* This fiels can be be null if the service is created without wsdl but later
* the epr supplies a wsdl that can be parsed.
*/
private @Nullable WSDLService wsdlService;
private final Container container;
/**
* Multiple {@link ServiceInterceptor}s are aggregated into one.
*/
/*package*/ final @NotNull ServiceInterceptor serviceInterceptor;
private URL wsdlURL;
public WSServiceDelegate(URL wsdlDocumentLocation, QName serviceName, Class extends Service> serviceClass, WebServiceFeature... features) {
this(wsdlDocumentLocation, serviceName, serviceClass, new WebServiceFeatureList(features));
}
protected WSServiceDelegate(URL wsdlDocumentLocation, QName serviceName, Class extends Service> serviceClass, WebServiceFeatureList features) {
this(
wsdlDocumentLocation==null ? null : new StreamSource(wsdlDocumentLocation.toExternalForm()),
serviceName,serviceClass, features);
wsdlURL = wsdlDocumentLocation;
}
/**
* @param serviceClass
* Either {@link Service}.class or other generated service-derived classes.
*/
public WSServiceDelegate(@Nullable Source wsdl, @NotNull QName serviceName, @NotNull final Class extends Service> serviceClass, WebServiceFeature... features) {
this(wsdl, serviceName, serviceClass, new WebServiceFeatureList(features));
}
/**
* @param serviceClass
* Either {@link Service}.class or other generated service-derived classes.
*/
protected WSServiceDelegate(@Nullable Source wsdl, @NotNull QName serviceName, @NotNull final Class extends Service> serviceClass, WebServiceFeatureList features) {
this(wsdl, null, serviceName, serviceClass, features);
}
/**
* @param serviceClass
* Either {@link Service}.class or other generated service-derived classes.
*/
public WSServiceDelegate(@Nullable Source wsdl, @Nullable WSDLService service, @NotNull QName serviceName, @NotNull final Class extends Service> serviceClass, WebServiceFeature... features) {
this(wsdl, service, serviceName, serviceClass, new WebServiceFeatureList(features));
}
/**
* @param serviceClass
* Either {@link Service}.class or other generated service-derived classes.
*/
public WSServiceDelegate(@Nullable Source wsdl, @Nullable WSDLService service, @NotNull QName serviceName, @NotNull final Class extends Service> serviceClass, WebServiceFeatureList features) {
//we cant create a Service without serviceName
if (serviceName == null) {
throw new WebServiceException(ClientMessages.INVALID_SERVICE_NAME_NULL(null));
}
this.features = features;
InitParams initParams = INIT_PARAMS.get();
INIT_PARAMS.set(null); // mark it as consumed
if(initParams==null) {
initParams = EMPTY_PARAMS;
}
this.serviceName = serviceName;
this.serviceClass = serviceClass;
Container tContainer = initParams.getContainer()!=null ? initParams.getContainer() : ContainerResolver.getInstance().getContainer();
if (tContainer == Container.NONE) {
tContainer = new ClientContainer();
}
this.container = tContainer;
ComponentFeature cf = this.features.get(ComponentFeature.class);
if (cf != null) {
switch(cf.getTarget()) {
case SERVICE:
getComponents().add(cf.getComponent());
break;
case CONTAINER:
this.container.getComponents().add(cf.getComponent());
break;
default:
throw new IllegalArgumentException();
}
}
ComponentsFeature csf = this.features.get(ComponentsFeature.class);
if (csf != null) {
for (ComponentFeature cfi : csf.getComponentFeatures()) {
switch(cfi.getTarget()) {
case SERVICE:
getComponents().add(cfi.getComponent());
break;
case CONTAINER:
this.container.getComponents().add(cfi.getComponent());
break;
default:
throw new IllegalArgumentException();
}
}
}
// load interceptor
ServiceInterceptor interceptor = ServiceInterceptorFactory.load(this, Thread.currentThread().getContextClassLoader());
ServiceInterceptor si = container.getSPI(ServiceInterceptor.class);
if (si != null) {
interceptor = ServiceInterceptor.aggregate(interceptor, si);
}
this.serviceInterceptor = interceptor;
if (service == null) {
//if wsdl is null, try and get it from the WebServiceClient.wsdlLocation
if(wsdl == null){
if(serviceClass != Service.class){
WebServiceClient wsClient = AccessController.doPrivileged(new PrivilegedAction<>() {
@Override
public WebServiceClient run() {
return serviceClass.getAnnotation(WebServiceClient.class);
}
});
String wsdlLocation = wsClient.wsdlLocation();
wsdlLocation = JAXWSUtils.absolutize(JAXWSUtils.getFileOrURLName(wsdlLocation));
wsdl = new StreamSource(wsdlLocation);
}
}
if (wsdl != null) {
try {
URL url = wsdl.getSystemId()==null ? null : JAXWSUtils.getEncodedURL(wsdl.getSystemId());
WSDLModel model = parseWSDL(url, wsdl, serviceClass);
service = model.getService(this.serviceName);
if (service == null)
throw new WebServiceException(
ClientMessages.INVALID_SERVICE_NAME(this.serviceName,
buildNameList(model.getServices().keySet())));
// fill in statically known ports
for (WSDLPort port : service.getPorts())
ports.put(port.getName(), new PortInfo(this, port));
} catch (MalformedURLException e) {
throw new WebServiceException(ClientMessages.INVALID_WSDL_URL(wsdl.getSystemId()));
}
}
} else {
// fill in statically known ports
for (WSDLPort port : service.getPorts())
ports.put(port.getName(), new PortInfo(this, port));
}
this.wsdlService = service;
if (serviceClass != Service.class) {
//if @HandlerChain present, set HandlerResolver on service context
HandlerChain handlerChain =
AccessController.doPrivileged(new PrivilegedAction<>() {
@Override
public HandlerChain run() {
return serviceClass.getAnnotation(HandlerChain.class);
}
});
if (handlerChain != null)
handlerConfigurator = new AnnotationConfigurator(this);
}
}
/**
* Parses the WSDL and builds {@link com.sun.xml.ws.api.model.wsdl.WSDLModel}.
* @param wsdlDocumentLocation
* Either this or {@code wsdl} parameter must be given.
* Null location means the system won't be able to resolve relative references in the WSDL.
*/
private WSDLModel parseWSDL(URL wsdlDocumentLocation, Source wsdlSource, Class serviceClass) {
try {
return RuntimeWSDLParser.parse(wsdlDocumentLocation, wsdlSource, createCatalogResolver(),
true, getContainer(), serviceClass, ServiceFinder.find(WSDLParserExtension.class).toArray());
} catch (IOException | ServiceConfigurationError | SAXException | XMLStreamException e) {
throw new WebServiceException(e);
}
}
protected EntityResolver createCatalogResolver() {
return XmlUtil.createDefaultCatalogResolver();
}
@Override
public Executor getExecutor() {
return executor;
}
@Override
public void setExecutor(Executor executor) {
this.executor = executor;
}
@Override
public HandlerResolver getHandlerResolver() {
return handlerConfigurator.getResolver();
}
/*package*/ final HandlerConfigurator getHandlerConfigurator() {
return handlerConfigurator;
}
@Override
public void setHandlerResolver(HandlerResolver resolver) {
handlerConfigurator = new HandlerResolverImpl(resolver);
}
@Override
public T getPort(QName portName, Class portInterface) throws WebServiceException {
return getPort(portName, portInterface, EMPTY_FEATURES);
}
@Override
public T getPort(QName portName, Class portInterface, WebServiceFeature... features) {
if (portName == null || portInterface == null)
throw new IllegalArgumentException();
WSDLService tWsdlService = this.wsdlService;
if (tWsdlService == null) {
// assigning it to local variable and not setting it back to this.wsdlService intentionally
// as we don't want to include the service instance with information gathered from sei
tWsdlService = getWSDLModelfromSEI(portInterface);
//still null? throw error need wsdl metadata to create a proxy
if (tWsdlService == null) {
throw new WebServiceException(ProviderApiMessages.NO_WSDL_NO_PORT(portInterface.getName()));
}
}
WSDLPort portModel = getPortModel(tWsdlService, portName);
return getPort(portModel.getEPR(), portName, portInterface, new WebServiceFeatureList(features));
}
@Override
public T getPort(EndpointReference epr, Class portInterface, WebServiceFeature... features) {
return getPort(WSEndpointReference.create(epr),portInterface,features);
}
@Override
public T getPort(WSEndpointReference wsepr, Class portInterface, WebServiceFeature... features) {
//get the portType from SEI, so that it can be used if EPR does n't have endpointName
WebServiceFeatureList featureList = new WebServiceFeatureList(features);
QName portTypeName = RuntimeModeler.getPortTypeName(portInterface, getMetadadaReader(featureList, portInterface.getClassLoader()));
//if port name is not specified in EPR, it will use portTypeName to get it from the WSDL model.
QName portName = getPortNameFromEPR(wsepr, portTypeName);
return getPort(wsepr,portName,portInterface, featureList);
}
protected T getPort(WSEndpointReference wsepr, QName portName, Class portInterface,
WebServiceFeatureList features) {
ComponentFeature cf = features.get(ComponentFeature.class);
if (cf != null && !Target.STUB.equals(cf.getTarget())) {
throw new IllegalArgumentException();
}
ComponentsFeature csf = features.get(ComponentsFeature.class);
if (csf != null) {
for (ComponentFeature cfi : csf.getComponentFeatures()) {
if (!Target.STUB.equals(cfi.getTarget()))
throw new IllegalArgumentException();
}
}
features.addAll(this.features);
SEIPortInfo spi = addSEI(portName, portInterface, features);
return createEndpointIFBaseProxy(wsepr,portName,portInterface,features, spi);
}
@Override
public T getPort(Class portInterface, WebServiceFeature... features) {
//get the portType from SEI
QName portTypeName = RuntimeModeler.getPortTypeName(portInterface, getMetadadaReader(new WebServiceFeatureList(features), portInterface.getClassLoader()));
WSDLService tmpWsdlService = this.wsdlService;
if (tmpWsdlService == null) {
// assigning it to local variable and not setting it back to this.wsdlService intentionally
// as we don't want to include the service instance with information gathered from sei
tmpWsdlService = getWSDLModelfromSEI(portInterface);
//still null? throw error need wsdl metadata to create a proxy
if(tmpWsdlService == null) {
throw new WebServiceException(ProviderApiMessages.NO_WSDL_NO_PORT(portInterface.getName()));
}
}
//get the first port corresponding to the SEI
WSDLPort port = tmpWsdlService.getMatchingPort(portTypeName);
if (port == null) {
throw new WebServiceException(ClientMessages.UNDEFINED_PORT_TYPE(portTypeName));
}
QName portName = port.getName();
return getPort(portName, portInterface,features);
}
@Override
public T getPort(Class portInterface) throws WebServiceException {
return getPort(portInterface, EMPTY_FEATURES);
}
@Override
public void addPort(QName portName, String bindingId, String endpointAddress) throws WebServiceException {
if (!ports.containsKey(portName)) {
BindingID bid = (bindingId == null) ? BindingID.SOAP11_HTTP : BindingID.parse(bindingId);
ports.put(portName,
new PortInfo(this, (endpointAddress == null) ? null :
EndpointAddress.create(endpointAddress), portName, bid));
} else
throw new WebServiceException(DispatchMessages.DUPLICATE_PORT(portName.toString()));
}
@Override
public Dispatch createDispatch(QName portName, Class aClass, Service.Mode mode) throws WebServiceException {
return createDispatch(portName, aClass, mode, EMPTY_FEATURES);
}
@Override
public Dispatch createDispatch(QName portName, WSEndpointReference wsepr, Class aClass, Service.Mode mode, WebServiceFeature... features) {
return createDispatch(portName, wsepr, aClass, mode, new WebServiceFeatureList(features));
}
public Dispatch createDispatch(QName portName, WSEndpointReference wsepr, Class aClass, Service.Mode mode, WebServiceFeatureList features) {
PortInfo port = safeGetPort(portName);
ComponentFeature cf = features.get(ComponentFeature.class);
if (cf != null && !Target.STUB.equals(cf.getTarget())) {
throw new IllegalArgumentException();
}
ComponentsFeature csf = features.get(ComponentsFeature.class);
if (csf != null) {
for (ComponentFeature cfi : csf.getComponentFeatures()) {
if (!Target.STUB.equals(cfi.getTarget()))
throw new IllegalArgumentException();
}
}
features.addAll(this.features);
BindingImpl binding = port.createBinding(features, null, null);
binding.setMode(mode);
Dispatch dispatch = Stubs.createDispatch(port, this, binding, aClass, mode, wsepr);
serviceInterceptor.postCreateDispatch((WSBindingProvider) dispatch);
return dispatch;
}
@Override
public Dispatch createDispatch(QName portName, Class aClass, Service.Mode mode, WebServiceFeature... features) {
return createDispatch(portName, aClass, mode, new WebServiceFeatureList(features));
}
public Dispatch createDispatch(QName portName, Class aClass, Service.Mode mode, WebServiceFeatureList features) {
WSEndpointReference wsepr = null;
boolean isAddressingEnabled = false;
AddressingFeature af = features.get(AddressingFeature.class);
if (af == null) {
af = this.features.get(AddressingFeature.class);
}
if (af != null && af.isEnabled())
isAddressingEnabled = true;
MemberSubmissionAddressingFeature msa = features.get(MemberSubmissionAddressingFeature.class);
if (msa == null) {
msa = this.features.get(MemberSubmissionAddressingFeature.class);
}
if (msa != null && msa.isEnabled())
isAddressingEnabled = true;
if(isAddressingEnabled && wsdlService != null && wsdlService.get(portName) != null) {
wsepr = wsdlService.get(portName).getEPR();
}
return createDispatch(portName, wsepr, aClass, mode, features);
}
@Override
public Dispatch createDispatch(EndpointReference endpointReference, Class type, Service.Mode mode, WebServiceFeature... features) {
WSEndpointReference wsepr = new WSEndpointReference(endpointReference);
QName portName = addPortEpr(wsepr);
return createDispatch(portName, wsepr, type, mode, features);
}
/**
* Obtains {@link PortInfo} for the given name, with error check.
*/
public
@NotNull
PortInfo safeGetPort(QName portName) {
PortInfo port = ports.get(portName);
if (port == null) {
throw new WebServiceException(ClientMessages.INVALID_PORT_NAME(portName, buildNameList(ports.keySet())));
}
return port;
}
private StringBuilder buildNameList(Collection names) {
StringBuilder sb = new StringBuilder();
for (QName qn : names) {
if (sb.length() > 0) sb.append(',');
sb.append(qn);
}
return sb;
}
public EndpointAddress getEndpointAddress(QName qName) {
PortInfo p = ports.get(qName);
return p != null ? p.targetEndpoint : null;
}
@Override
public Dispatch