Search in sources :

Example 1 with FlatpackDataFormat

use of org.apache.camel.dataformat.flatpack.FlatpackDataFormat in project camel by apache.

the class FlatpackDataFormatAutoConfiguration method configureFlatpackDataFormatFactory.

@Bean(name = "flatpack-dataformat-factory")
@ConditionalOnClass(CamelContext.class)
@ConditionalOnMissingBean(FlatpackDataFormat.class)
public DataFormatFactory configureFlatpackDataFormatFactory(final CamelContext camelContext, final FlatpackDataFormatConfiguration configuration) {
    return new DataFormatFactory() {

        public DataFormat newInstance() {
            FlatpackDataFormat dataformat = new FlatpackDataFormat();
            if (CamelContextAware.class.isAssignableFrom(FlatpackDataFormat.class)) {
                CamelContextAware contextAware = CamelContextAware.class.cast(dataformat);
                if (contextAware != null) {
                    contextAware.setCamelContext(camelContext);
                }
            }
            try {
                Map<String, Object> parameters = new HashMap<>();
                IntrospectionSupport.getProperties(configuration, parameters, null, false);
                IntrospectionSupport.setProperties(camelContext, camelContext.getTypeConverter(), dataformat, parameters);
            } catch (Exception e) {
                throw new RuntimeCamelException(e);
            }
            return dataformat;
        }
    };
}
Also used : DataFormatFactory(org.apache.camel.spi.DataFormatFactory) CamelContextAware(org.apache.camel.CamelContextAware) HashMap(java.util.HashMap) RuntimeCamelException(org.apache.camel.RuntimeCamelException) FlatpackDataFormat(org.apache.camel.dataformat.flatpack.FlatpackDataFormat) RuntimeCamelException(org.apache.camel.RuntimeCamelException) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnClass(org.springframework.boot.autoconfigure.condition.ConditionalOnClass) ConditionalOnBean(org.springframework.boot.autoconfigure.condition.ConditionalOnBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 2 with FlatpackDataFormat

use of org.apache.camel.dataformat.flatpack.FlatpackDataFormat in project camel by apache.

the class FlatpackFixedLengthWithHeaderAndTrailerDataFormatTest method createRouteBuilder.

@Override
protected RouteBuilder createRouteBuilder() throws Exception {
    return new RouteBuilder() {

        public void configure() throws Exception {
            FlatpackDataFormat df = new FlatpackDataFormat();
            df.setDefinition("PEOPLE-HeaderAndTrailer.pzmap.xml");
            df.setFixed(true);
            from("direct:unmarshal").setHeader("MyFirstHeader", constant("1")).unmarshal(df).setHeader("MySecondHeader", constant("2")).to("mock:unmarshal");
            // with the definition
            from("direct:marshal").marshal(df).convertBodyTo(String.class).to("mock:marshal");
        }
    };
}
Also used : RouteBuilder(org.apache.camel.builder.RouteBuilder) FlatpackDataFormat(org.apache.camel.dataformat.flatpack.FlatpackDataFormat)

Example 3 with FlatpackDataFormat

use of org.apache.camel.dataformat.flatpack.FlatpackDataFormat in project camel by apache.

the class FlatpackDelimitedDataFormatTest method createRouteBuilder.

@Override
protected RouteBuilder createRouteBuilder() throws Exception {
    return new RouteBuilder() {

        public void configure() throws Exception {
            FlatpackDataFormat df = new FlatpackDataFormat();
            df.setDefinition("INVENTORY-Delimited.pzmap.xml");
            from("direct:unmarshal").unmarshal(df).to("mock:unmarshal");
            // with the definition
            from("direct:marshal").marshal(df).convertBodyTo(String.class).to("mock:marshal");
            // without the definition (will auto add column names from the recieved data)
            FlatpackDataFormat df2 = new FlatpackDataFormat();
            from("direct:marshal2").marshal(df2).convertBodyTo(String.class).to("mock:marshal2");
        }
    };
}
Also used : RouteBuilder(org.apache.camel.builder.RouteBuilder) FlatpackDataFormat(org.apache.camel.dataformat.flatpack.FlatpackDataFormat)

Example 4 with FlatpackDataFormat

use of org.apache.camel.dataformat.flatpack.FlatpackDataFormat in project wildfly-camel by wildfly-extras.

the class FlatpackIntegrationTest method testUnmarshal.

@Test
@SuppressWarnings("unchecked")
public void testUnmarshal() throws Exception {
    final FlatpackDataFormat format = new FlatpackDataFormat();
    format.setDefinition(FLATPACK_MAPPING_XML);
    format.setFixed(true);
    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("direct:start").unmarshal(format);
        }
    });
    camelctx.start();
    try (InputStream input = getClass().getClassLoader().getResourceAsStream(FLATPACK_INPUT_TXT)) {
        Assert.assertNotNull("Input not null", input);
        ProducerTemplate producer = camelctx.createProducerTemplate();
        List<Map<String, String>> result = producer.requestBody("direct:start", input, List.class);
        Assert.assertEquals("Expected size 4: " + result, 4, result.size());
        Assert.assertEquals("JOHN", result.get(0).get("FIRSTNAME"));
        Assert.assertEquals("JIMMY", result.get(1).get("FIRSTNAME"));
        Assert.assertEquals("JANE", result.get(2).get("FIRSTNAME"));
        Assert.assertEquals("FRED", result.get(3).get("FIRSTNAME"));
    } finally {
        camelctx.stop();
    }
}
Also used : CamelContext(org.apache.camel.CamelContext) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) ProducerTemplate(org.apache.camel.ProducerTemplate) RouteBuilder(org.apache.camel.builder.RouteBuilder) InputStream(java.io.InputStream) FlatpackDataFormat(org.apache.camel.dataformat.flatpack.FlatpackDataFormat) Map(java.util.Map) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) Test(org.junit.Test)

Example 5 with FlatpackDataFormat

use of org.apache.camel.dataformat.flatpack.FlatpackDataFormat in project camel by apache.

the class FlatpackFixedLengthDataFormatTest method createRouteBuilder.

@Override
protected RouteBuilder createRouteBuilder() throws Exception {
    return new RouteBuilder() {

        public void configure() throws Exception {
            FlatpackDataFormat df = new FlatpackDataFormat();
            df.setDefinition("PEOPLE-FixedLength.pzmap.xml");
            df.setFixed(true);
            df.setIgnoreFirstRecord(false);
            from("direct:unmarshal").unmarshal(df).to("mock:unmarshal");
            // with the definition
            from("direct:marshal").marshal(df).convertBodyTo(String.class).to("mock:marshal");
        }
    };
}
Also used : RouteBuilder(org.apache.camel.builder.RouteBuilder) FlatpackDataFormat(org.apache.camel.dataformat.flatpack.FlatpackDataFormat)

Aggregations

FlatpackDataFormat (org.apache.camel.dataformat.flatpack.FlatpackDataFormat)5 RouteBuilder (org.apache.camel.builder.RouteBuilder)4 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 CamelContext (org.apache.camel.CamelContext)1 CamelContextAware (org.apache.camel.CamelContextAware)1 ProducerTemplate (org.apache.camel.ProducerTemplate)1 RuntimeCamelException (org.apache.camel.RuntimeCamelException)1 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)1 DataFormatFactory (org.apache.camel.spi.DataFormatFactory)1 Test (org.junit.Test)1 ConditionalOnBean (org.springframework.boot.autoconfigure.condition.ConditionalOnBean)1 ConditionalOnClass (org.springframework.boot.autoconfigure.condition.ConditionalOnClass)1 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)1 Bean (org.springframework.context.annotation.Bean)1