use of org.apache.camel.spi.DataFormat in project camel by apache.
the class DataFormatTransformer method transform.
/**
* Perform data transformation with specified from/to type using DataFormat.
* @param message message to apply transformation
* @param from 'from' data type
* @param to 'to' data type
*/
@Override
public void transform(Message message, DataType from, DataType to) throws Exception {
Exchange exchange = message.getExchange();
CamelContext context = exchange.getContext();
// Unmarshaling into Java Object
if ((to == null || to.isJavaType()) && (from.equals(getFrom()) || from.getModel().equals(getModel()))) {
DataFormat dataFormat = getDataFormat(exchange);
LOG.debug("Unmarshaling with '{}'", dataFormat);
Object answer = dataFormat.unmarshal(exchange, message.getBody(InputStream.class));
if (to != null && to.getName() != null) {
Class<?> toClass = context.getClassResolver().resolveClass(to.getName());
if (!toClass.isAssignableFrom(answer.getClass())) {
LOG.debug("Converting to '{}'", toClass.getName());
answer = context.getTypeConverter().mandatoryConvertTo(toClass, answer);
}
}
message.setBody(answer);
// Marshaling from Java Object
} else if ((from == null || from.isJavaType()) && (to.equals(getTo()) || to.getModel().equals(getModel()))) {
Object input = message.getBody();
if (from != null && from.getName() != null) {
Class<?> fromClass = context.getClassResolver().resolveClass(from.getName());
if (!fromClass.isAssignableFrom(input.getClass())) {
LOG.debug("Converting to '{}'", fromClass.getName());
input = context.getTypeConverter().mandatoryConvertTo(fromClass, input);
}
}
OutputStreamBuilder osb = OutputStreamBuilder.withExchange(exchange);
DataFormat dataFormat = getDataFormat(exchange);
LOG.debug("Marshaling with '{}'", dataFormat);
dataFormat.marshal(exchange, message.getBody(), osb);
message.setBody(osb.build());
} else {
throw new IllegalArgumentException("Unsupported transformation: from='" + from + ", to='" + to + "'");
}
}
use of org.apache.camel.spi.DataFormat in project camel by apache.
the class CryptoDataFormat method createDataFormat.
@Override
protected DataFormat createDataFormat(RouteContext routeContext) {
DataFormat cryptoFormat = super.createDataFormat(routeContext);
if (ObjectHelper.isNotEmpty(keyRef)) {
Key key = CamelContextHelper.mandatoryLookup(routeContext.getCamelContext(), keyRef, Key.class);
setProperty(routeContext.getCamelContext(), cryptoFormat, "key", key);
}
if (ObjectHelper.isNotEmpty(algorithmParameterRef)) {
AlgorithmParameterSpec spec = CamelContextHelper.mandatoryLookup(routeContext.getCamelContext(), algorithmParameterRef, AlgorithmParameterSpec.class);
setProperty(routeContext.getCamelContext(), cryptoFormat, "AlgorithmParameterSpec", spec);
}
if (ObjectHelper.isNotEmpty(initVectorRef)) {
byte[] iv = CamelContextHelper.mandatoryLookup(routeContext.getCamelContext(), initVectorRef, byte[].class);
setProperty(routeContext.getCamelContext(), cryptoFormat, "InitializationVector", iv);
}
return cryptoFormat;
}
use of org.apache.camel.spi.DataFormat in project camel by apache.
the class FlatpackDataFormat method createDataFormat.
@Override
protected DataFormat createDataFormat(RouteContext routeContext) {
DataFormat flatpack = super.createDataFormat(routeContext);
if (ObjectHelper.isNotEmpty(parserFactoryRef)) {
Object parserFactory = CamelContextHelper.mandatoryLookup(routeContext.getCamelContext(), parserFactoryRef);
setProperty(routeContext.getCamelContext(), flatpack, "parserFactory", parserFactory);
}
return flatpack;
}
use of org.apache.camel.spi.DataFormat in project camel by apache.
the class DefaultCamelContextResolverTest method testDataFormatWithFallbackNames.
@Test
public void testDataFormatWithFallbackNames() throws Exception {
DataFormat dataFormat = context.resolveDataFormat("green");
assertNotNull("DataFormat not found in registry", dataFormat);
assertTrue("Wrong instance type of the DataFormat", dataFormat instanceof SampleDataFormat);
assertTrue("Here we should have the fallback DataFormat", ((SampleDataFormat) dataFormat).isFallback());
}
use of org.apache.camel.spi.DataFormat in project camel by apache.
the class DataFormatContextAwareTest method testLanguageCamelContextAware.
public void testLanguageCamelContextAware() throws Exception {
DataFormat df = context.resolveDataFormat("my");
assertNotNull(df);
MyDataFormat me = assertIsInstanceOf(MyDataFormat.class, df);
assertNotNull(me.getCamelContext());
}
Aggregations