Search in sources :

Example 6 with DataFormat

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 + "'");
    }
}
Also used : Exchange(org.apache.camel.Exchange) CamelContext(org.apache.camel.CamelContext) InputStream(java.io.InputStream) DataFormat(org.apache.camel.spi.DataFormat) OutputStreamBuilder(org.apache.camel.converter.stream.OutputStreamBuilder)

Example 7 with DataFormat

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;
}
Also used : DataFormat(org.apache.camel.spi.DataFormat) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) Key(java.security.Key)

Example 8 with DataFormat

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;
}
Also used : DataFormat(org.apache.camel.spi.DataFormat)

Example 9 with DataFormat

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());
}
Also used : DataFormat(org.apache.camel.spi.DataFormat) Test(org.junit.Test)

Example 10 with DataFormat

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());
}
Also used : DataFormat(org.apache.camel.spi.DataFormat)

Aggregations

DataFormat (org.apache.camel.spi.DataFormat)45 RouteBuilder (org.apache.camel.builder.RouteBuilder)20 Exchange (org.apache.camel.Exchange)8 Processor (org.apache.camel.Processor)8 JaxbDataFormat (org.apache.camel.converter.jaxb.JaxbDataFormat)6 CamelContext (org.apache.camel.CamelContext)5 Test (org.junit.Test)5 JAXBContext (javax.xml.bind.JAXBContext)2 Rfc5425FrameDecoder (org.apache.camel.component.syslog.netty.Rfc5425FrameDecoder)2 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)2 JndiRegistry (org.apache.camel.impl.JndiRegistry)2 SimpleRegistry (org.apache.camel.impl.SimpleRegistry)2 QRY_A19 (ca.uhn.hl7v2.model.v24.message.QRY_A19)1 QRD (ca.uhn.hl7v2.model.v24.segment.QRD)1 PipeParser (ca.uhn.hl7v2.parser.PipeParser)1 ByteBuf (io.netty.buffer.ByteBuf)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStream (java.io.InputStream)1 Key (java.security.Key)1 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)1