org.apache.cxf.endpoint.ClientImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cxf-bundle-minimal Show documentation
Show all versions of cxf-bundle-minimal Show documentation
Apache CXF Minimal Bundle Jar
The newest version!
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.cxf.endpoint;
import java.io.Closeable;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.wsdl.extensions.soap.SOAPBinding;
import javax.wsdl.extensions.soap12.SOAP12Binding;
import javax.xml.namespace.QName;
import org.apache.cxf.Bus;
import org.apache.cxf.BusFactory;
import org.apache.cxf.binding.Binding;
import org.apache.cxf.common.classloader.ClassLoaderUtils;
import org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder;
import org.apache.cxf.common.i18n.UncheckedException;
import org.apache.cxf.common.logging.LogUtils;
import org.apache.cxf.common.util.PropertyUtils;
import org.apache.cxf.helpers.CastUtils;
import org.apache.cxf.interceptor.AbstractBasicInterceptorProvider;
import org.apache.cxf.interceptor.ClientOutFaultObserver;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.interceptor.Interceptor;
import org.apache.cxf.interceptor.InterceptorChain;
import org.apache.cxf.interceptor.InterceptorProvider;
import org.apache.cxf.message.Exchange;
import org.apache.cxf.message.ExchangeImpl;
import org.apache.cxf.message.Message;
import org.apache.cxf.message.MessageContentsList;
import org.apache.cxf.message.MessageImpl;
import org.apache.cxf.phase.PhaseChainCache;
import org.apache.cxf.phase.PhaseInterceptorChain;
import org.apache.cxf.phase.PhaseManager;
import org.apache.cxf.service.Service;
import org.apache.cxf.service.model.BindingInfo;
import org.apache.cxf.service.model.BindingMessageInfo;
import org.apache.cxf.service.model.BindingOperationInfo;
import org.apache.cxf.service.model.EndpointInfo;
import org.apache.cxf.service.model.InterfaceInfo;
import org.apache.cxf.service.model.MessageInfo;
import org.apache.cxf.service.model.OperationInfo;
import org.apache.cxf.service.model.ServiceInfo;
import org.apache.cxf.transport.Conduit;
import org.apache.cxf.transport.MessageObserver;
import org.apache.cxf.workqueue.SynchronousExecutor;
import org.apache.cxf.wsdl.WSDLServiceFactory;
public class ClientImpl
extends AbstractBasicInterceptorProvider
implements Client, Retryable, MessageObserver {
public static final String THREAD_LOCAL_REQUEST_CONTEXT = "thread.local.request.context";
/**
* When a synchronous request/response invoke is done using an asynchronous transport mechanism,
* this is the timeout used for waiting for the response. Default is 60 seconds.
*/
public static final String SYNC_TIMEOUT = "cxf.synchronous.timeout";
public static final String FINISHED = "exchange.finished";
private static final Logger LOG = LogUtils.getL7dLogger(ClientImpl.class);
protected Bus bus;
protected ConduitSelector conduitSelector;
protected ClientOutFaultObserver outFaultObserver;
protected int synchronousTimeout = 60000; // default 60 second timeout
protected PhaseChainCache outboundChainCache = new PhaseChainCache();
protected PhaseChainCache inboundChainCache = new PhaseChainCache();
protected Map currentRequestContext = new ConcurrentHashMap(8, 0.75f, 4);
protected Map requestContext
= Collections.synchronizedMap(new WeakHashMap());
protected Map> responseContext
= Collections.synchronizedMap(new WeakHashMap>());
protected Executor executor;
public ClientImpl(Bus b, Endpoint e) {
this(b, e, (ConduitSelector)null);
}
public ClientImpl(Bus b, Endpoint e, Conduit c) {
this(b, e, new PreexistingConduitSelector(c));
}
public ClientImpl(Bus b, Endpoint e, ConduitSelector sc) {
bus = b;
outFaultObserver = new ClientOutFaultObserver(bus);
getConduitSelector(sc).setEndpoint(e);
notifyLifecycleManager();
}
public ClientImpl(URL wsdlUrl) {
this(BusFactory.getThreadDefaultBus(), wsdlUrl, (QName)null,
null, SimpleEndpointImplFactory.getSingleton());
}
public ClientImpl(URL wsdlUrl, QName port) {
this(BusFactory.getThreadDefaultBus(), wsdlUrl, (QName)null,
port, SimpleEndpointImplFactory.getSingleton());
}
/**
* Create a Client that uses the default EndpointImpl.
* @param bus
* @param wsdlUrl
* @param service
* @param port
*/
public ClientImpl(Bus bus, URL wsdlUrl, QName service, QName port) {
this(bus, wsdlUrl, service, port, SimpleEndpointImplFactory.getSingleton());
}
/**
* Create a Client that uses a specific EndpointImpl.
* @param bus
* @param wsdlUrl
* @param service
* @param port
* @param endpointImplFactory
*/
public ClientImpl(Bus bus, URL wsdlUrl, QName service,
QName port, EndpointImplFactory endpointImplFactory) {
this.bus = bus;
outFaultObserver = new ClientOutFaultObserver(bus);
Service svc = service == null
? bus.getExtension(WSDLServiceFactory.class).create(wsdlUrl)
: bus.getExtension(WSDLServiceFactory.class).create(wsdlUrl, service);
EndpointInfo epfo = findEndpoint(svc, port);
try {
if (endpointImplFactory != null) {
getConduitSelector().setEndpoint(endpointImplFactory.newEndpointImpl(bus, svc, epfo));
} else {
getConduitSelector().setEndpoint(new EndpointImpl(bus, svc, epfo));
}
} catch (EndpointException epex) {
throw new IllegalStateException("Unable to create endpoint: " + epex.getMessage(), epex);
}
notifyLifecycleManager();
}
/**
* Create a Client that uses a specific EndpointImpl.
* @param bus
* @param svc
* @param port
* @param endpointImplFactory
*/
public ClientImpl(Bus bus, Service svc, QName port,
EndpointImplFactory endpointImplFactory) {
this.bus = bus;
outFaultObserver = new ClientOutFaultObserver(bus);
EndpointInfo epfo = findEndpoint(svc, port);
try {
if (endpointImplFactory != null) {
getConduitSelector().setEndpoint(endpointImplFactory.newEndpointImpl(bus, svc, epfo));
} else {
getConduitSelector().setEndpoint(new EndpointImpl(bus, svc, epfo));
}
} catch (EndpointException epex) {
throw new IllegalStateException("Unable to create endpoint: " + epex.getMessage(), epex);
}
notifyLifecycleManager();
}
public Bus getBus() {
return bus;
}
public void destroy() {
if (bus == null) {
return;
}
ClientLifeCycleManager mgr = bus.getExtension(ClientLifeCycleManager.class);
if (null != mgr) {
mgr.clientDestroyed(this);
}
if (conduitSelector != null) {
if (conduitSelector instanceof Closeable) {
try {
((Closeable)conduitSelector).close();
} catch (IOException e) {
//ignore, we're destroying anyway
}
} else {
getConduit().close();
}
}
bus = null;
conduitSelector = null;
outFaultObserver = null;
outboundChainCache = null;
inboundChainCache = null;
currentRequestContext = null;
requestContext.clear();
requestContext = null;
responseContext.clear();
responseContext = null;
executor = null;
}
private void notifyLifecycleManager() {
ClientLifeCycleManager mgr = bus.getExtension(ClientLifeCycleManager.class);
if (null != mgr) {
mgr.clientCreated(this);
}
}
private EndpointInfo findEndpoint(Service svc, QName port) {
EndpointInfo epfo;
if (port != null) {
epfo = svc.getEndpointInfo(port);
if (epfo == null) {
throw new IllegalArgumentException("The service " + svc.getName()
+ " does not have an endpoint " + port + ".");
}
} else {
epfo = null;
for (ServiceInfo svcfo : svc.getServiceInfos()) {
for (EndpointInfo e : svcfo.getEndpoints()) {
BindingInfo bfo = e.getBinding();
String bid = bfo.getBindingId();
if ("http://schemas.xmlsoap.org/wsdl/soap/".equals(bid)) {
for (Object o : bfo.getExtensors().get()) {
if (o instanceof SOAPBinding) {
SOAPBinding soapB = (SOAPBinding)o;
if ("http://schemas.xmlsoap.org/soap/http".equals(soapB.getTransportURI())) {
epfo = e;
break;
}
}
}
} else if ("http://schemas.xmlsoap.org/wsdl/soap12/".equals(bid)) {
for (Object o : bfo.getExtensors().get()) {
if (o instanceof SOAP12Binding) {
SOAP12Binding soapB = (SOAP12Binding)o;
if ("http://schemas.xmlsoap.org/soap/http".equals(soapB.getTransportURI())) {
epfo = e;
break;
}
}
}
}
}
}
if (epfo == null) {
throw new UnsupportedOperationException(
"Only document-style SOAP 1.1 http are supported "
+ "for auto-selection of endpoint; none were found.");
}
}
return epfo;
}
public Endpoint getEndpoint() {
return getConduitSelector().getEndpoint();
}
public Map getRequestContext() {
if (isThreadLocalRequestContext()) {
if (!requestContext.containsKey(Thread.currentThread())) {
requestContext.put(Thread.currentThread(), new EchoContext(currentRequestContext));
}
return requestContext.get(Thread.currentThread());
}
return currentRequestContext;
}
public Map getResponseContext() {
if (!responseContext.containsKey(Thread.currentThread())) {
responseContext.put(Thread.currentThread(), new HashMap());
}
return responseContext.get(Thread.currentThread());
}
public boolean isThreadLocalRequestContext() {
Object o = currentRequestContext.get(THREAD_LOCAL_REQUEST_CONTEXT);
if (o != null) {
boolean local = false;
if (o instanceof Boolean) {
local = ((Boolean)o).booleanValue();
} else {
local = Boolean.parseBoolean(o.toString());
}
return local;
}
return false;
}
public void setThreadLocalRequestContext(boolean b) {
currentRequestContext.put(THREAD_LOCAL_REQUEST_CONTEXT, b);
}
public Object[] invoke(BindingOperationInfo oi, Object... params) throws Exception {
return invoke(oi, params, null);
}
public Object[] invoke(String operationName, Object... params) throws Exception {
QName q = new QName(getEndpoint().getService().getName().getNamespaceURI(), operationName);
return invoke(q, params);
}
public Object[] invoke(QName operationName, Object... params) throws Exception {
BindingOperationInfo op = getEndpoint().getEndpointInfo().getBinding().getOperation(operationName);
if (op == null) {
throw new UncheckedException(
new org.apache.cxf.common.i18n.Message("NO_OPERATION", LOG, operationName));
}
if (op.isUnwrappedCapable()) {
op = op.getUnwrappedOperation();
}
return invoke(op, params);
}
public Object[] invokeWrapped(String operationName, Object... params) throws Exception {
QName q = new QName(getEndpoint().getService().getName().getNamespaceURI(), operationName);
return invokeWrapped(q, params);
}
public Object[] invokeWrapped(QName operationName, Object... params) throws Exception {
BindingOperationInfo op = getEndpoint().getEndpointInfo().getBinding().getOperation(operationName);
if (op == null) {
throw new UncheckedException(
new org.apache.cxf.common.i18n.Message("NO_OPERATION", LOG, operationName));
}
return invoke(op, params);
}
public Object[] invoke(BindingOperationInfo oi,
Object[] params,
Exchange exchange) throws Exception {
Map context = new HashMap();
Map resp = new HashMap();
Map req = new HashMap(getRequestContext());
context.put(RESPONSE_CONTEXT, resp);
context.put(REQUEST_CONTEXT, req);
try {
return invoke(oi, params, context, exchange);
} finally {
if (responseContext != null) {
responseContext.put(Thread.currentThread(), resp);
}
}
}
public Object[] invoke(BindingOperationInfo oi,
Object[] params,
Map context) throws Exception {
try {
return invoke(oi, params, context, (Exchange)null);
} finally {
if (context != null) {
Map resp = CastUtils.cast((Map, ?>)context.get(RESPONSE_CONTEXT));
if (resp != null && responseContext != null) {
responseContext.put(Thread.currentThread(), resp);
}
}
}
}
public void invoke(ClientCallback callback,
String operationName,
Object... params) throws Exception {
QName q = new QName(getEndpoint().getService().getName().getNamespaceURI(), operationName);
invoke(callback, q, params);
}
public void invoke(ClientCallback callback,
QName operationName,
Object... params) throws Exception {
BindingOperationInfo op = getEndpoint().getEndpointInfo().getBinding().getOperation(operationName);
if (op == null) {
throw new UncheckedException(
new org.apache.cxf.common.i18n.Message("NO_OPERATION", LOG, operationName));
}
if (op.isUnwrappedCapable()) {
op = op.getUnwrappedOperation();
}
invoke(callback, op, params);
}
public void invokeWrapped(ClientCallback callback,
String operationName,
Object... params)
throws Exception {
QName q = new QName(getEndpoint().getService().getName().getNamespaceURI(), operationName);
invokeWrapped(callback, q, params);
}
public void invokeWrapped(ClientCallback callback,
QName operationName,
Object... params)
throws Exception {
BindingOperationInfo op = getEndpoint().getEndpointInfo().getBinding().getOperation(operationName);
if (op == null) {
throw new UncheckedException(
new org.apache.cxf.common.i18n.Message("NO_OPERATION", LOG, operationName));
}
invoke(callback, op, params);
}
public void invoke(ClientCallback callback,
BindingOperationInfo oi,
Object... params) throws Exception {
invoke(callback, oi, params, null, null);
}
public void invoke(ClientCallback callback,
BindingOperationInfo oi,
Object[] params,
Map context) throws Exception {
invoke(callback, oi, params, context, null);
}
public void invoke(ClientCallback callback,
BindingOperationInfo oi,
Object[] params,
Exchange exchange) throws Exception {
invoke(callback, oi, params, null, exchange);
}
public void invoke(ClientCallback callback,
BindingOperationInfo oi,
Object[] params,
Map context,
Exchange exchange) throws Exception {
doInvoke(callback, oi, params, context, exchange);
}
public Object[] invoke(BindingOperationInfo oi,
Object[] params,
Map context,
Exchange exchange) throws Exception {
return doInvoke(null, oi, params, context, exchange);
}
private Object[] doInvoke(final ClientCallback callback,
BindingOperationInfo oi,
Object[] params,
Map context,
Exchange exchange) throws Exception {
Bus origBus = BusFactory.getAndSetThreadDefaultBus(bus);
ClassLoaderHolder origLoader = null;
try {
ClassLoader loader = bus.getExtension(ClassLoader.class);
if (loader != null) {
origLoader = ClassLoaderUtils.setThreadContextClassloader(loader);
}
if (exchange == null) {
exchange = new ExchangeImpl();
}
exchange.setSynchronous(callback == null);
Endpoint endpoint = getEndpoint();
if (LOG.isLoggable(Level.FINE)) {
LOG.fine("Invoke, operation info: " + oi + ", params: " + Arrays.toString(params));
}
Message message = endpoint.getBinding().createMessage();
// Make sure INVOCATION CONTEXT, REQUEST_CONTEXT and RESPONSE_CONTEXT are present
// on message
Map reqContext = null;
Map resContext = null;
if (context == null) {
context = new HashMap();
}
reqContext = CastUtils.cast((Map, ?>)context.get(REQUEST_CONTEXT));
resContext = CastUtils.cast((Map, ?>)context.get(RESPONSE_CONTEXT));
if (reqContext == null) {
reqContext = new HashMap(getRequestContext());
context.put(REQUEST_CONTEXT, reqContext);
}
if (resContext == null) {
resContext = new HashMap();
context.put(RESPONSE_CONTEXT, resContext);
}
message.put(Message.INVOCATION_CONTEXT, context);
setContext(reqContext, message);
exchange.putAll(reqContext);
setParameters(params, message);
if (null != oi) {
exchange.setOneWay(oi.getOutput() == null);
}
exchange.setOutMessage(message);
exchange.put(ClientCallback.class, callback);
setOutMessageProperties(message, oi);
setExchangeProperties(exchange, endpoint, oi);
PhaseInterceptorChain chain = setupInterceptorChain(endpoint);
message.setInterceptorChain(chain);
if (callback == null) {
chain.setFaultObserver(outFaultObserver);
} else {
// We need to wrap the outFaultObserver if the callback is not null
// calling the conduitSelector.complete to make sure the fail over feature works
chain.setFaultObserver(new MessageObserver() {
public void onMessage(Message message) {
Exception ex = message.getContent(Exception.class);
if (ex != null) {
getConduitSelector().complete(message.getExchange());
if (message.getContent(Exception.class) == null) {
// handle the right response
List