Skip to content

Commit

Permalink
SWITCHYARD-2044 Camel OutboundHandler doesn't apply MessageComposer t…
Browse files Browse the repository at this point in the history
…o out message
  • Loading branch information
igarashitm authored and rcernich committed Apr 9, 2014
1 parent d170d40 commit 00314f9
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -172,25 +172,31 @@ private void handleInOnly(final Exchange exchange) throws HandlerException {

private void handleInOut(final Exchange switchyardExchange) throws HandlerException {
final org.apache.camel.Exchange camelExchange = _producerTemplate.request(_uri, createProcessor(switchyardExchange));
CamelBindingData bindingData = new CamelBindingData(camelExchange.getOut());
Exception camelException = camelExchange.getException();

if (!camelExchange.isFailed()) {
sendResponseToSwitchyard(switchyardExchange, camelExchange.getOut().getBody());
sendResponseToSwitchyard(switchyardExchange, bindingData);

} else {
QName faultName = switchyardExchange.getContract().getProviderOperation().getFaultType();
Class<?> declaredFault = faultName != null && QNameUtil.isJavaMessageType(faultName) ? QNameUtil.toJavaMessageType(faultName) : null;

Object camelFault = camelException;
if (camelFault == null) {
if (camelExchange.hasOut() && camelExchange.getOut().isFault()) {
if (camelExchange.hasOut() && bindingData.getMessage().isFault()) {
// Use Out body as a fault content if camelExchange.getException() returns null
camelFault = camelExchange.getOut().getBody();
camelFault = bindingData.getMessage().getBody();
}
}

if (camelFault != null && declaredFault != null && declaredFault.isAssignableFrom(camelFault.getClass())) {
Message msg = switchyardExchange.createMessage().setContent(camelFault);
Message msg = null;
try {
msg = _messageComposer.compose(bindingData, switchyardExchange);
} catch (Exception e) {
throw new HandlerException(e);
}
switchyardExchange.sendFault(msg);
} else if (camelFault instanceof Throwable) {
throw new HandlerException(Throwable.class.cast(camelFault));
Expand All @@ -204,8 +210,14 @@ private void handleInOut(final Exchange switchyardExchange) throws HandlerExcept
}
}

private void sendResponseToSwitchyard(final Exchange switchyardExchange, final Object payload) {
switchyardExchange.send(switchyardExchange.createMessage().setContent(payload));
private void sendResponseToSwitchyard(final Exchange switchyardExchange, CamelBindingData bindingData) throws HandlerException {
Message msg = null;
try {
msg = _messageComposer.compose(bindingData, switchyardExchange);
} catch (Exception e) {
throw new HandlerException(e);
}
switchyardExchange.send(msg);
}

private Processor createProcessor(final Exchange switchyardExchange) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import javax.transaction.TransactionManager;
import javax.transaction.TransactionSynchronizationRegistry;
import javax.transaction.UserTransaction;
import javax.xml.namespace.QName;

import org.apache.camel.CamelContext;
import org.apache.camel.EndpointInject;
Expand All @@ -46,13 +47,16 @@
import org.switchyard.common.camel.SwitchYardCamelContextImpl;
import org.switchyard.component.camel.common.composer.CamelBindingData;
import org.switchyard.component.camel.common.composer.CamelComposition;
import org.switchyard.component.camel.common.composer.CamelContextMapper;
import org.switchyard.component.camel.common.composer.CamelMessageComposer;
import org.switchyard.component.camel.common.model.CamelBindingModel;
import org.switchyard.component.camel.common.transaction.TransactionManagerFactory;
import org.switchyard.component.common.composer.MessageComposer;
import org.switchyard.component.test.mixins.cdi.CDIMixIn;
import org.switchyard.component.test.mixins.naming.NamingMixIn;
import org.switchyard.config.model.composite.CompositeReferenceModel;
import org.switchyard.metadata.InOnlyService;
import org.switchyard.metadata.InOutService;
import org.switchyard.metadata.ServiceInterface;
import org.switchyard.test.Invoker;
import org.switchyard.test.ServiceOperation;
Expand Down Expand Up @@ -224,6 +228,41 @@ public void startStop() throws Exception {
verify(producerTemplate).stop();
}

@Test
public void routeInOutToCamelUsingMessageComposer() throws Exception {
bindingModel = mock(CamelBindingModel.class);
when(bindingModel.getComponentURI()).thenReturn(URI.create("direct:MessageComposerService"));
when(bindingModel.getName()).thenReturn("mockOutputHandler");
when(bindingModel.getReference()).thenReturn(referenceModel);
MessageComposer<CamelBindingData> myMessageComposer = new CamelMessageComposer() {
@Override
public CamelBindingData decompose(Exchange exchange, CamelBindingData target) throws Exception {
exchange.getContext().setProperty("decomposeInvoked", true, Scope.EXCHANGE);
return super.decompose(exchange, target);
}
@Override
public Message compose(CamelBindingData source, Exchange exchange) throws Exception {
exchange.getContext().setProperty("composeInvoked", true, Scope.EXCHANGE);
return super.compose(source, exchange);
}
};
myMessageComposer.setContextMapper(new CamelContextMapper());
QName serviceName = new QName(_serviceDomain.getName().getNamespaceURI(), "MessageComposerService");
_serviceDomain.registerService(serviceName,
new InOutService(),
new OutboundHandler(bindingModel, (SwitchYardCamelContext) context, myMessageComposer, _serviceDomain) {
{
setState(State.STARTED);
}
}
);
_service = _serviceDomain.registerServiceReference(serviceName, new InOutService());
Exchange exchange = _service.createExchange();
exchange.send(exchange.createMessage().setContent("foo"));
assertThat((Boolean)exchange.getContext().getProperty("decomposeInvoked").getValue(), is(true));
assertThat((Boolean)exchange.getContext().getProperty("composeInvoked").getValue(), is(true));
}

@Override
protected CamelContext createCamelContext() throws Exception {
return new SwitchYardCamelContextImpl();
Expand All @@ -238,6 +277,7 @@ public void configure() {
.log("Before Routing to mock:result body: ${body}")
.to("mock:result");
from("transaction:foo").to("mock:result");
from("direct:MessageComposerService").to("mock:result");
}
};
}
Expand Down

0 comments on commit 00314f9

Please sign in to comment.