use of org.apache.camel.spi.Transformer in project camel by apache.
the class ContractAdvice method applyTransformerChain.
private boolean applyTransformerChain(Message message, DataType from, DataType to) throws Exception {
CamelContext context = message.getExchange().getContext();
Transformer fromTransformer = context.resolveTransformer(from.getModel());
Transformer toTransformer = context.resolveTransformer(to.getModel());
if (fromTransformer != null && toTransformer != null) {
LOG.debug("Applying transformer 1/2: from='{}', to='{}', transformer='{}'", from, to, fromTransformer);
fromTransformer.transform(message, from, new DataType(Object.class));
LOG.debug("Applying transformer 2/2: from='{}', to='{}', transformer='{}'", from, to, toTransformer);
toTransformer.transform(message, new DataType(Object.class), to);
return true;
}
return false;
}
use of org.apache.camel.spi.Transformer in project camel by apache.
the class TransformerBuilderTest method testEndpointTransformer.
public void testEndpointTransformer() throws Exception {
CamelContext ctx = new DefaultCamelContext();
RouteBuilder builder = new RouteBuilder() {
@Override
public void configure() throws Exception {
transformer().fromType("json:foo").toType("xml:bar").withUri("direct:transformer");
from("direct:transformer").log("test");
}
};
ctx.addRoutes(builder);
ctx.start();
Transformer transformer = ctx.resolveTransformer(new DataType("json:foo"), new DataType("xml:bar"));
assertNotNull(transformer);
assertEquals(ProcessorTransformer.class, transformer.getClass());
ProcessorTransformer pt = (ProcessorTransformer) transformer;
Field f = ProcessorTransformer.class.getDeclaredField("processor");
f.setAccessible(true);
Object processor = f.get(pt);
assertEquals(SendProcessor.class, processor.getClass());
SendProcessor sp = (SendProcessor) processor;
assertEquals("direct://transformer", sp.getEndpoint().getEndpointUri());
}
use of org.apache.camel.spi.Transformer in project camel by apache.
the class DefaultTransformerRegistry method put.
@Override
public Transformer put(TransformerKey key, Transformer transformer) {
// at first we must see if the key already exists and then replace it back, so it stays the same spot
Transformer answer = staticMap.remove(key);
if (answer != null) {
// replace existing
staticMap.put(key, transformer);
return answer;
}
answer = super.remove(key);
if (answer != null) {
// replace existing
super.put(key, transformer);
return answer;
}
// we want transformers to be static if they are part of setting up or starting routes
if (context.isSetupRoutes() || context.isStartingRoutes()) {
answer = staticMap.put(key, transformer);
} else {
answer = super.put(key, transformer);
}
return answer;
}
use of org.apache.camel.spi.Transformer in project camel by apache.
the class DefaultTransformerRegistry method resolveTransformer.
@Override
public Transformer resolveTransformer(TransformerKey key) {
if (ObjectHelper.isEmpty(key.getScheme()) && key.getTo() == null) {
return null;
}
// try exact match
Transformer answer = get(aliasMap.containsKey(key) ? aliasMap.get(key) : key);
if (answer != null || ObjectHelper.isNotEmpty(key.getScheme())) {
return answer;
}
// try wildcard match for next - add an alias if matched
TransformerKey alias = null;
if (key.getFrom() != null && ObjectHelper.isNotEmpty(key.getFrom().getName())) {
alias = new TransformerKey(new DataType(key.getFrom().getModel()), key.getTo());
answer = get(alias);
}
if (answer == null && ObjectHelper.isNotEmpty(key.getTo().getName())) {
alias = new TransformerKey(key.getFrom(), new DataType(key.getTo().getModel()));
answer = get(alias);
}
if (answer == null && key.getFrom() != null && ObjectHelper.isNotEmpty(key.getFrom().getName()) && ObjectHelper.isNotEmpty(key.getTo().getName())) {
alias = new TransformerKey(new DataType(key.getFrom().getModel()), new DataType(key.getTo().getModel()));
answer = get(alias);
}
if (answer == null && key.getFrom() != null) {
alias = new TransformerKey(key.getFrom().getModel());
answer = get(alias);
}
if (answer == null) {
alias = new TransformerKey(key.getTo().getModel());
answer = get(alias);
}
if (answer != null) {
aliasMap.put(key, alias);
}
return answer;
}
Aggregations