{
// ensure properties, hence HTTP headers, are propagated both ways
final TransformerTemplate copyInboundToOutboundPropertiesTransformer = new TransformerTemplate(
new CopyInboundToOutboundPropertiesTransformerCallback());
final DefaultMessageProcessorChainBuilder proxyBuilder = new DefaultMessageProcessorChainBuilder();
proxyBuilder.chain(copyInboundToOutboundPropertiesTransformer);
proxyBuilder.chain(new ResponseMessageProcessorAdapter(copyInboundToOutboundPropertiesTransformer));
if (outboundEndpoint instanceof DynamicURIOutboundEndpoint)
{
// do not mess with endpoints that are already dynamic
proxyBuilder.chain(outboundEndpoint);
}
else
{
// create a templated outbound endpoint to propagate extra path elements (including query parameters)
proxyBuilder.chain(new TransformerTemplate(new TransformerCallback()
{
public Object doTransform(final MuleMessage message) throws Exception
{
final String pathExtension = StringUtils.substringAfter(
(String) message.getInboundProperty("http.request"),
(String) message.getInboundProperty("http.context.path"));
message.setInvocationProperty("http.path.extension",
StringUtils.defaultString(pathExtension));
return message;
}
}));
final OutboundEndpoint dynamicOutboundEndpoint;
if (outboundEndpoint.isDynamic())
{
dynamicOutboundEndpoint = outboundEndpoint;
}
else
{
final String uriTemplate = outboundEndpoint.getEndpointURI().getUri().toString()
+ "#[variable:http.path.extension]";
URIBuilder uriBuilder = new URIBuilder(uriTemplate, muleContext);
DynamicURIBuilder dynamicURIBuilder = new DynamicURIBuilder(uriBuilder);
dynamicOutboundEndpoint = new DynamicOutboundEndpoint(
new EndpointURIEndpointBuilder(outboundEndpoint), dynamicURIBuilder);
}
proxyBuilder.chain(dynamicOutboundEndpoint);
}
MessageProcessor proxyMessageProcessor;
if (cachingStrategy != null)
{
final MessageProcessorChain cachedMessageProcessors = proxyBuilder.build();
proxyMessageProcessor = new MessageProcessor()
{
@Override
public MuleEvent process(MuleEvent event) throws MuleException
{
return cachingStrategy.process(event, cachedMessageProcessors);
}
};
}
else
{
proxyMessageProcessor = proxyBuilder.build();
}
builder.chain(proxyMessageProcessor);
}