use of org.apache.cxf.phase.PhaseInterceptorChain in project tomee by apache.
the class ClientImpl method doInvoke.
private Object[] doInvoke(final ClientCallback callback, BindingOperationInfo oi, Object[] params, Map<String, Object> context, Exchange exchange) throws Exception {
Bus origBus = BusFactory.getAndSetThreadDefaultBus(bus);
ClassLoaderHolder origLoader = null;
Map<String, Object> resContext = 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();
// on message
if (context == null) {
context = new HashMap<>();
}
Map<String, Object> 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 ResponseContext(responseContext);
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) {
completeExchange(message.getExchange());
if (message.getContent(Exception.class) == null) {
// handle the right response
Message inMsg = message.getExchange().getInMessage();
Map<String, Object> ctx = responseContext.get(Thread.currentThread());
List<Object> resList = CastUtils.cast(inMsg.getContent(List.class));
Object[] result = resList == null ? null : resList.toArray();
callback.handleResponse(ctx, result);
return;
}
}
outFaultObserver.onMessage(message);
}
});
}
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;
}
return processResult(message, exchange, oi, resContext);
} finally {
// ensure ResponseContext has HTTP RESPONSE CODE
if (null != exchange) {
Integer responseCode = (Integer) exchange.get(Message.RESPONSE_CODE);
resContext.put(MessageContext.HTTP_RESPONSE_CODE, responseCode);
resContext.put(org.apache.cxf.message.Message.RESPONSE_CODE, responseCode);
setResponseContext(resContext);
}
if (origLoader != null) {
origLoader.reset();
}
if (origBus != bus) {
BusFactory.setThreadDefaultBus(origBus);
}
}
}
use of org.apache.cxf.phase.PhaseInterceptorChain in project tomee by apache.
the class EjbInterceptor method intercept.
@AroundInvoke
public Object intercept(InvocationContext context) throws Exception {
Endpoint endpoint = this.exchange.get(Endpoint.class);
Service service = endpoint.getService();
Binding binding = ((JaxWsEndpointImpl) endpoint).getJaxwsBinding();
this.exchange.put(InvocationContext.class, context);
if (binding.getHandlerChain() == null || binding.getHandlerChain().isEmpty()) {
// no handlers so let's just directly invoke the bean
log.debug("No handlers found.");
EjbMethodInvoker invoker = (EjbMethodInvoker) service.getInvoker();
return invoker.directEjbInvoke(this.exchange, this.method, this.params);
} else {
// have handlers so have to run handlers now and redo data binding
// as handlers can change the soap message
log.debug("Handlers found.");
Message inMessage = exchange.getInMessage();
PhaseInterceptorChain chain = new PhaseInterceptorChain(bus.getExtension(PhaseManager.class).getInPhases());
chain.setFaultObserver(endpoint.getOutFaultObserver());
/*
* Since we have to re-do data binding and the XMLStreamReader
* contents are already consumed by prior data binding step
* we have to reinitialize the XMLStreamReader from the SOAPMessage
* created by SAAJInInterceptor.
*/
if (inMessage instanceof SoapMessage) {
try {
reserialize((SoapMessage) inMessage);
} catch (Exception e) {
throw new ServerRuntimeException("Failed to reserialize soap message", e);
}
} else {
// TODO: how to handle XML/HTTP binding?
}
this.exchange.setOutMessage(null);
// install default interceptors
chain.add(new ServiceInvokerInterceptor());
// chain.add(new OutgoingChainInterceptor()); // it is already in the enclosing chain, if we add it there we are in the tx so we write the message in the tx!
// See http://cwiki.apache.org/CXF20DOC/interceptors.html
// install Holder and Wrapper interceptors
chain.add(new WrapperClassInInterceptor());
chain.add(new HolderInInterceptor());
// install interceptors for handler processing
chain.add(new MustUnderstandInterceptor());
chain.add(new LogicalHandlerInInterceptor(binding));
chain.add(new SOAPHandlerInterceptor(binding));
// install data binding interceptors - todo: check we need it
copyDataBindingInterceptors(chain, inMessage.getInterceptorChain());
InterceptorChain oldChain = inMessage.getInterceptorChain();
inMessage.setInterceptorChain(chain);
try {
chain.doIntercept(inMessage);
} finally {
inMessage.setInterceptorChain(oldChain);
}
// TODO: the result should be deserialized from SOAPMessage
Object result = getResult();
return result;
}
}
Aggregations