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
}
};
}
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
}
};
}
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();
}
}
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;
}
};
}
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();
}
}
Aggregations