org.apache.camel.dataformat.bindy.csv.BindyCsvDataFormat Java Examples

The following examples show how to use org.apache.camel.dataformat.bindy.csv.BindyCsvDataFormat. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BindyIntegrationTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
@Test
public void testMarshal() throws Exception {

    final DataFormat bindy = new BindyCsvDataFormat(Customer.class);

    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:start")
            .marshal(bindy);
        }
    });

    camelctx.start();
    try {
        ProducerTemplate producer = camelctx.createProducerTemplate();
        String result = producer.requestBody("direct:start", new Customer("John", "Doe"), String.class);
        Assert.assertEquals("John,Doe", result.trim());
    } finally {
        camelctx.close();
    }
}
 
Example #2
Source File: BindyIntegrationTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
@Test
public void testUnmarshal() throws Exception {

    final DataFormat bindy = new BindyCsvDataFormat(Customer.class);

    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:start")
            .unmarshal(bindy);
        }
    });

    camelctx.start();
    try {
        ProducerTemplate producer = camelctx.createProducerTemplate();
        Customer result = producer.requestBody("direct:start", "John,Doe", Customer.class);
        Assert.assertEquals("John", result.getFirstName());
        Assert.assertEquals("Doe", result.getLastName());
    } finally {
        camelctx.close();
    }
}
 
Example #3
Source File: NormalizerRoute.java    From camel-cookbook-examples with Apache License 2.0 5 votes vote down vote up
@Override
public void configure() throws Exception {
    final DataFormat bindy = new BindyCsvDataFormat(org.camelcookbook.transformation.csv.model.BookModel.class);
    final DataFormat jaxb = new JaxbDataFormat("org.camelcookbook.transformation.myschema");

    final XmlJsonDataFormat xmlJsonFormat = new XmlJsonDataFormat();
    xmlJsonFormat.setRootName("bookstore");
    xmlJsonFormat.setElementName("book");
    xmlJsonFormat.setExpandableProperties(Arrays.asList("author", "author"));

    from("direct:start")
        .choice()
        .when(header(Exchange.FILE_NAME).endsWith(".csv"))
            .unmarshal(bindy)
            .bean(MyNormalizer.class, "bookModelToJaxb")
            .to("mock:csv")
        .when(header(Exchange.FILE_NAME).endsWith(".json"))
            .unmarshal(xmlJsonFormat)
            .to("mock:json")
        .when(header(Exchange.FILE_NAME).endsWith(".xml"))
            .unmarshal(jaxb)
            .to("mock:xml")
        .otherwise()
            .to("mock:unknown")
            .stop()
        .end()
        .to("mock:normalized");
}
 
Example #4
Source File: CsvRoute.java    From camel-cookbook-examples with Apache License 2.0 5 votes vote down vote up
@Override
public void configure() throws Exception {
    final DataFormat bindy = new BindyCsvDataFormat(org.camelcookbook.transformation.csv.model.BookModel.class);

    from("direct:unmarshal").unmarshal(bindy);
    from("direct:marshal").marshal(bindy);
}
 
Example #5
Source File: BindyCsvDataFormatAutoConfiguration.java    From camel-spring-boot with Apache License 2.0 4 votes vote down vote up
@Bean(name = "bindy-csv-dataformat-factory")
@ConditionalOnMissingBean(BindyCsvDataFormat.class)
public DataFormatFactory configureBindyCsvDataFormatFactory()
        throws Exception {
    return new DataFormatFactory() {
        @Override
        public DataFormat newInstance() {
            BindyCsvDataFormat dataformat = new BindyCsvDataFormat();
            if (CamelContextAware.class
                    .isAssignableFrom(BindyCsvDataFormat.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);
                CamelPropertiesHelper.setCamelProperties(camelContext,
                        dataformat, parameters, false);
            } catch (Exception e) {
                throw new RuntimeCamelException(e);
            }
            if (ObjectHelper.isNotEmpty(customizers)) {
                for (DataFormatCustomizer<BindyCsvDataFormat> customizer : customizers) {
                    boolean useCustomizer = (customizer instanceof HasId)
                            ? HierarchicalPropertiesEvaluator.evaluate(
                                    applicationContext.getEnvironment(),
                                    "camel.dataformat.customizer",
                                    "camel.dataformat.bindy-csv.customizer",
                                    ((HasId) customizer).getId())
                            : HierarchicalPropertiesEvaluator.evaluate(
                                    applicationContext.getEnvironment(),
                                    "camel.dataformat.customizer",
                                    "camel.dataformat.bindy-csv.customizer");
                    if (useCustomizer) {
                        LOGGER.debug(
                                "Configure dataformat {}, with customizer {}",
                                dataformat, customizer);
                        customizer.customize(dataformat);
                    }
                }
            }
            return dataformat;
        }
    };
}