org.mule.module.cxf.MuleInvoker Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mule-module-cxf Show documentation
Show all versions of mule-module-cxf Show documentation
A Mule module for web service connectivity using CXF.
The newest version!
/*
* Copyright (c) MuleSoft, Inc. All rights reserved. http://www.mulesoft.com
* The software in this package is published under the terms of the CPAL v1.0
* license, a copy of which has been included with this distribution in the
* LICENSE.txt file.
*/
package org.mule.module.cxf;
import org.mule.NonBlockingVoidMuleEvent;
import org.mule.VoidMuleEvent;
import org.mule.api.MuleEvent;
import org.mule.api.MuleException;
import org.mule.api.MuleMessage;
import org.mule.api.config.MuleProperties;
import org.mule.api.execution.ExecutionCallback;
import org.mule.api.transport.PropertyScope;
import org.mule.component.ComponentException;
import org.mule.config.ExceptionHelper;
import org.mule.execution.ErrorHandlingExecutionTemplate;
import org.mule.transformer.types.DataTypeFactory;
import org.mule.transport.NullPayload;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.List;
import javax.script.ScriptException;
import javax.xml.stream.XMLStreamReader;
import org.apache.cxf.common.util.PropertyUtils;
import org.apache.cxf.frontend.MethodDispatcher;
import org.apache.cxf.helpers.CastUtils;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.message.Exchange;
import org.apache.cxf.message.FaultMode;
import org.apache.cxf.message.Message;
import org.apache.cxf.message.MessageContentsList;
import org.apache.cxf.service.Service;
import org.apache.cxf.service.invoker.Invoker;
import org.apache.cxf.service.model.BindingOperationInfo;
/**
* Invokes a Mule Service via a CXF binding.
*/
public class MuleInvoker implements Invoker
{
private final CxfInboundMessageProcessor cxfMmessageProcessor;
private Class> targetClass;
public MuleInvoker(CxfInboundMessageProcessor cxfMmessageProcessor, Class> targetClass)
{
this.cxfMmessageProcessor = cxfMmessageProcessor;
this.targetClass = targetClass;
}
public Object invoke(Exchange exchange, Object o)
{
// this is the original request. Keep it to copy all the message properties from it
final MuleEvent event = (MuleEvent) exchange.get(CxfConstants.MULE_EVENT);
MuleEvent responseEvent = null;
if (PropertyUtils.isTrue(exchange.remove(CxfConstants.NON_BLOCKING_RESPONSE)))
{
responseEvent = event;
}
else
{
try
{
MuleMessage reqMsg = event.getMessage();
Object payload = extractPayload(exchange.getInMessage());
Class payloadClass = payload != null ? payload.getClass() : Object.class;
reqMsg.setPayload(payload, DataTypeFactory.create(payloadClass, cxfMmessageProcessor.getMimeType()));
BindingOperationInfo bop = exchange.get(BindingOperationInfo.class);
Service svc = exchange.get(Service.class);
if (!cxfMmessageProcessor.isProxy())
{
MethodDispatcher md = (MethodDispatcher) svc.get(MethodDispatcher.class.getName());
Method m = md.getMethod(bop);
if (targetClass != null)
{
m = matchMethod(m, targetClass);
}
reqMsg.setProperty(MuleProperties.MULE_METHOD_PROPERTY, m, PropertyScope.INVOCATION);
}
if (bop != null)
{
reqMsg.setProperty(CxfConstants.INBOUND_OPERATION, bop.getOperationInfo().getName(), PropertyScope.INVOCATION);
reqMsg.setProperty(CxfConstants.INBOUND_SERVICE, svc.getName(), PropertyScope.INVOCATION);
}
ErrorHandlingExecutionTemplate errorHandlingExecutionTemplate = ErrorHandlingExecutionTemplate.createErrorHandlingExecutionTemplate(event.getMuleContext(), event.getFlowConstruct().getExceptionListener());
responseEvent = errorHandlingExecutionTemplate.execute(new ExecutionCallback()
{
@Override
public MuleEvent process() throws Exception
{
return cxfMmessageProcessor.processNext(event);
}
});
}
catch (MuleException e)
{
exchange.put(CxfConstants.MULE_EVENT, event);
Throwable cause = e;
// See MULE-6329
if(Boolean.valueOf((String) event.getMessage().getInvocationProperty(CxfConstants.UNWRAP_MULE_EXCEPTIONS)))
{
cause = ExceptionHelper.getNonMuleException(e);
// Exceptions thrown from a ScriptComponent or a ScriptTransformer are going to be wrapped on a
// ScriptException
if(cause instanceof ScriptException && cause.getCause() != null)
{
cause = cause.getCause();
}
}
else if(e instanceof ComponentException) {
cause = e.getCause();
}
throw new Fault(cause);
}
catch (Exception e)
{
exchange.put(CxfConstants.MULE_EVENT, event);
throw new Fault(e);
}
if (!event.getExchangePattern().hasResponse())
{
// weird response from AbstractInterceptingMessageProcessor
responseEvent = null;
}
if (responseEvent instanceof NonBlockingVoidMuleEvent)
{
exchange.put(Message.SUSPENDED_INVOCATION, true);
exchange.put(CxfConstants.MULE_EVENT, responseEvent);
return null;
}
}
if (responseEvent != null && !VoidMuleEvent.getInstance().equals(responseEvent))
{
exchange.put(CxfConstants.MULE_EVENT, responseEvent);
MuleMessage resMessage = responseEvent.getMessage();
if (resMessage.getExceptionPayload() != null)
{
Throwable cause = resMessage.getExceptionPayload().getException();
if (cause instanceof ComponentException)
{
cause = cause.getCause();
}
exchange.getInMessage().put(FaultMode.class, FaultMode.UNCHECKED_APPLICATION_FAULT);
if (cause instanceof Fault)
{
throw (Fault) cause;
}
throw new Fault(cause);
}
else if (resMessage.getPayload() instanceof NullPayload)
{
return new MessageContentsList((Object)null);
}
else if (cxfMmessageProcessor.isProxy())
{
resMessage.getPayload();
return new Object[] { resMessage };
}
else
{
return new Object[]{resMessage.getPayload()};
}
}
else
{
exchange.getInMessage().getInterceptorChain().abort();
if (exchange.getOutMessage() != null)
{
exchange.getOutMessage().getInterceptorChain().abort();
}
exchange.put(CxfConstants.MULE_EVENT, null);
return null;
}
}
protected Object extractPayload(Message cxfMessage)
{
if (cxfMmessageProcessor.isProxy())
{
// First we have to verify if we have the resulting stream reader (in case the message
// has been intercepted and changed, we have no list but the actual XMLStreamReader)
XMLStreamReader streamReader = cxfMessage.getContent(XMLStreamReader.class);
if (streamReader != null)
{
return streamReader;
}
}
List © 2015 - 2025 Weber Informatics LLC | Privacy Policy