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
/**
* 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.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.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";
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 service
* @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();
if (bfo.getBindingId().equals("http://schemas.xmlsoap.org/wsdl/soap/")) {
for (Object o : bfo.getExtensors().get()) {
if (o instanceof SOAPBinding) {
SOAPBinding soapB = (SOAPBinding)o;
if (soapB.getTransportURI().equals("http://schemas.xmlsoap.org/soap/http")) {
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 {
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.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(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);
chain.setFaultObserver(outFaultObserver);
prepareConduitSelector(message);
// add additional interceptors and such
modifyChain(chain, message, false);
try {
chain.doIntercept(message);
} catch (Fault fault) {
enrichFault(fault);
throw fault;
}
if (callback != null) {
return null;
} else {
return processResult(message, exchange, oi, resContext);
}
} finally {
if (origLoader != null) {
origLoader.reset();
}
if (origBus != bus) {
BusFactory.setThreadDefaultBus(origBus);
}
}
}
/**
* TODO This is SOAP specific code and should not be in cxf core
* @param fault
*/
private void enrichFault(Fault fault) {
if (fault.getCause().getCause() instanceof IOException
|| fault.getCause() instanceof IOException) {
String soap11NS = "http://schemas.xmlsoap.org/soap/envelope/";
String soap12NS = "http://www.w3.org/2003/05/soap-envelope";
QName faultCode = fault.getFaultCode();
//for SoapFault, if it's underlying cause is IOException,
//it means something like server is down or can't create
//connection, according to soap spec we should set fault as
//Server Fault
if (faultCode.getNamespaceURI().equals(
soap11NS)
&& faultCode.getLocalPart().equals("Client")) {
faultCode = new QName(soap11NS, "Server");
fault.setFaultCode(faultCode);
}
if (faultCode.getNamespaceURI().equals(
soap12NS)
&& faultCode.getLocalPart().equals("Sender")) {
faultCode = new QName(soap12NS, "Receiver");
fault.setFaultCode(faultCode);
}
}
}
protected Object[] processResult(Message message,
Exchange exchange,
BindingOperationInfo oi,
Map resContext) throws Exception {
Exception ex = null;
// Check to see if there is a Fault from the outgoing chain if it's an out Message
if (!message.get(Message.INBOUND_MESSAGE).equals(Boolean.TRUE)) {
ex = message.getContent(Exception.class);
}
boolean mepCompleteCalled = false;
if (ex != null) {
getConduitSelector().complete(exchange);
mepCompleteCalled = true;
if (message.getContent(Exception.class) != null) {
throw ex;
}
}
ex = message.getExchange().get(Exception.class);
if (ex != null) {
if (!mepCompleteCalled) {
getConduitSelector().complete(exchange);
}
throw ex;
}
//REVISIT
// - use a protocol neutral no-content marker instead of 202?
// - move the decoupled destination property name into api
Integer responseCode = (Integer)exchange.get(Message.RESPONSE_CODE);
if (null != responseCode && 202 == responseCode) {
Endpoint ep = exchange.getEndpoint();
if (null != ep && null != ep.getEndpointInfo() && null == ep.getEndpointInfo().
getProperty("org.apache.cxf.ws.addressing.MAPAggregator.decoupledDestination")) {
return null;
}
}
// Wait for a response if we need to
if (oi != null && !oi.getOperationInfo().isOneWay()) {
synchronized (exchange) {
waitResponse(exchange);
}
}
// leave the input stream open for the caller
Boolean keepConduitAlive = (Boolean)exchange.get(Client.KEEP_CONDUIT_ALIVE);
if (keepConduitAlive == null || !keepConduitAlive) {
getConduitSelector().complete(exchange);
}
// Grab the response objects if there are any
List