Search in sources :

Example 1 with BeanIODataFormat

use of org.apache.camel.dataformat.beanio.BeanIODataFormat in project camel by apache.

the class CsvTest method createRouteBuilder.

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

        @Override
        public void configure() throws Exception {
            // START SNIPPET: e1
            // setup beanio data format using the mapping file, loaded from the classpath
            DataFormat format = new BeanIODataFormat("org/apache/camel/dataformat/beanio/csv/mappings.xml", "stream1");
            // a route which uses the bean io data format to format a CSV data
            // to java objects
            from("direct:unmarshal").unmarshal(format).split(body()).to("mock:beanio-unmarshal");
            // convert list of java objects back to flat format
            from("direct:marshal").marshal(format).to("mock:beanio-marshal");
        // END SNIPPET: e1
        }
    };
}
Also used : BeanIODataFormat(org.apache.camel.dataformat.beanio.BeanIODataFormat) RouteBuilder(org.apache.camel.builder.RouteBuilder) DataFormat(org.apache.camel.spi.DataFormat) BeanIODataFormat(org.apache.camel.dataformat.beanio.BeanIODataFormat)

Example 2 with BeanIODataFormat

use of org.apache.camel.dataformat.beanio.BeanIODataFormat in project camel by apache.

the class CsvTestWithProperties method createRouteBuilder.

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

        @Override
        public void configure() throws Exception {
            // START SNIPPET: e1
            // setup beanio data format using the mapping file, loaded from the classpath
            BeanIODataFormat format = new BeanIODataFormat("org/apache/camel/dataformat/beanio/csv/mappingsWithProperties.xml", "stream1");
            Properties properties = new Properties();
            properties.setProperty("field1", "firstName");
            properties.setProperty("field2", "lastName");
            format.setProperties(properties);
            // a route which uses the bean io data format to format a CSV data
            // to java objects
            from("direct:unmarshal").unmarshal(format).split(body()).to("mock:beanio-unmarshal");
            // convert list of java objects back to flat format
            from("direct:marshal").marshal(format).to("mock:beanio-marshal");
        // END SNIPPET: e1
        }
    };
}
Also used : BeanIODataFormat(org.apache.camel.dataformat.beanio.BeanIODataFormat) RouteBuilder(org.apache.camel.builder.RouteBuilder) Properties(java.util.Properties)

Example 3 with BeanIODataFormat

use of org.apache.camel.dataformat.beanio.BeanIODataFormat in project wildfly-camel by wildfly-extras.

the class BeanIOIntegrationTest method testMarshal.

@Test
public void testMarshal() throws Exception {
    DataFormat beanio = new BeanIODataFormat(MAPPINGS_XML, "customerStream");
    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("direct:start").marshal(beanio);
        }
    });
    camelctx.start();
    try {
        ProducerTemplate producer = camelctx.createProducerTemplate();
        Customer customer = new Customer("Peter", "Post", "Street", "12345");
        String result = producer.requestBody("direct:start", customer, String.class);
        Assert.assertEquals("Peter,Post,Street,12345", result.trim());
    } finally {
        camelctx.stop();
    }
}
Also used : BeanIODataFormat(org.apache.camel.dataformat.beanio.BeanIODataFormat) CamelContext(org.apache.camel.CamelContext) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) ProducerTemplate(org.apache.camel.ProducerTemplate) RouteBuilder(org.apache.camel.builder.RouteBuilder) Customer(org.wildfly.camel.test.beanio.subA.Customer) BeanIODataFormat(org.apache.camel.dataformat.beanio.BeanIODataFormat) DataFormat(org.apache.camel.spi.DataFormat) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) Test(org.junit.Test)

Example 4 with BeanIODataFormat

use of org.apache.camel.dataformat.beanio.BeanIODataFormat in project camel by apache.

the class BeanIODataFormatAutoConfiguration method configureBeanIODataFormatFactory.

@Bean(name = "beanio-dataformat-factory")
@ConditionalOnClass(CamelContext.class)
@ConditionalOnMissingBean(BeanIODataFormat.class)
public DataFormatFactory configureBeanIODataFormatFactory(final CamelContext camelContext, final BeanIODataFormatConfiguration configuration) {
    return new DataFormatFactory() {

        public DataFormat newInstance() {
            BeanIODataFormat dataformat = new BeanIODataFormat();
            if (CamelContextAware.class.isAssignableFrom(BeanIODataFormat.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 : BeanIODataFormat(org.apache.camel.dataformat.beanio.BeanIODataFormat) DataFormatFactory(org.apache.camel.spi.DataFormatFactory) CamelContextAware(org.apache.camel.CamelContextAware) HashMap(java.util.HashMap) RuntimeCamelException(org.apache.camel.RuntimeCamelException) 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 5 with BeanIODataFormat

use of org.apache.camel.dataformat.beanio.BeanIODataFormat in project wildfly-camel by wildfly-extras.

the class BeanIOIntegrationTest method testUnmarshal.

@Test
public void testUnmarshal() throws Exception {
    DataFormat beanio = new BeanIODataFormat(MAPPINGS_XML, "customerStream");
    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("direct:start").unmarshal(beanio);
        }
    });
    camelctx.start();
    try {
        ProducerTemplate producer = camelctx.createProducerTemplate();
        List<?> result = producer.requestBody("direct:start", "Peter,Post,Street,12345", List.class);
        Assert.assertEquals(1, result.size());
        Customer customer = (Customer) result.get(0);
        Assert.assertEquals("Peter", customer.getFirstName());
        Assert.assertEquals("Post", customer.getLastName());
        Assert.assertEquals("Street", customer.getStreet());
        Assert.assertEquals("12345", customer.getZip());
    } finally {
        camelctx.stop();
    }
}
Also used : BeanIODataFormat(org.apache.camel.dataformat.beanio.BeanIODataFormat) CamelContext(org.apache.camel.CamelContext) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) ProducerTemplate(org.apache.camel.ProducerTemplate) RouteBuilder(org.apache.camel.builder.RouteBuilder) Customer(org.wildfly.camel.test.beanio.subA.Customer) BeanIODataFormat(org.apache.camel.dataformat.beanio.BeanIODataFormat) DataFormat(org.apache.camel.spi.DataFormat) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) Test(org.junit.Test)

Aggregations

BeanIODataFormat (org.apache.camel.dataformat.beanio.BeanIODataFormat)5 RouteBuilder (org.apache.camel.builder.RouteBuilder)4 DataFormat (org.apache.camel.spi.DataFormat)3 CamelContext (org.apache.camel.CamelContext)2 ProducerTemplate (org.apache.camel.ProducerTemplate)2 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)2 Test (org.junit.Test)2 Customer (org.wildfly.camel.test.beanio.subA.Customer)2 HashMap (java.util.HashMap)1 Properties (java.util.Properties)1 CamelContextAware (org.apache.camel.CamelContextAware)1 RuntimeCamelException (org.apache.camel.RuntimeCamelException)1 DataFormatFactory (org.apache.camel.spi.DataFormatFactory)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