Search in sources :

Example 1 with ProtobufDataFormat

use of org.apache.camel.dataformat.protobuf.ProtobufDataFormat in project camel by apache.

the class ProtobufDataFormatAutoConfiguration method configureProtobufDataFormatFactory.

@Bean(name = "protobuf-dataformat-factory")
@ConditionalOnClass(CamelContext.class)
@ConditionalOnMissingBean(ProtobufDataFormat.class)
public DataFormatFactory configureProtobufDataFormatFactory(final CamelContext camelContext, final ProtobufDataFormatConfiguration configuration) {
    return new DataFormatFactory() {

        public DataFormat newInstance() {
            ProtobufDataFormat dataformat = new ProtobufDataFormat();
            if (CamelContextAware.class.isAssignableFrom(ProtobufDataFormat.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) RuntimeCamelException(org.apache.camel.RuntimeCamelException) ProtobufDataFormat(org.apache.camel.dataformat.protobuf.ProtobufDataFormat) 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 ProtobufDataFormat

use of org.apache.camel.dataformat.protobuf.ProtobufDataFormat in project wildfly-camel by wildfly-extras.

the class ProtobufIntegrationTest method testMarshall.

@Test
public void testMarshall() throws Exception {
    final ProtobufDataFormat format = new ProtobufDataFormat(Person.getDefaultInstance());
    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("direct:start").marshal(format);
        }
    });
    Person person = Person.newBuilder().setId(1).setName("John Doe").build();
    camelctx.start();
    try {
        ProducerTemplate producer = camelctx.createProducerTemplate();
        String result = producer.requestBody("direct:start", person, String.class);
        Assert.assertEquals("John Doe", result.trim());
    } 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) Person(org.wildfly.camel.test.protobuf.model.AddressBookProtos.Person) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) ProtobufDataFormat(org.apache.camel.dataformat.protobuf.ProtobufDataFormat) Test(org.junit.Test)

Example 3 with ProtobufDataFormat

use of org.apache.camel.dataformat.protobuf.ProtobufDataFormat in project wildfly-camel by wildfly-extras.

the class ProtobufIntegrationTest method testUnmarshall.

@Test
public void testUnmarshall() throws Exception {
    final ProtobufDataFormat format = new ProtobufDataFormat(Person.getDefaultInstance());
    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("direct:start").unmarshal(format);
        }
    });
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Person person = Person.newBuilder().setId(1).setName("John Doe").build();
    person.writeTo(baos);
    camelctx.start();
    try {
        ProducerTemplate producer = camelctx.createProducerTemplate();
        Person result = producer.requestBody("direct:start", baos.toByteArray(), Person.class);
        Assert.assertEquals("John Doe", result.getName().trim());
    } 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) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Person(org.wildfly.camel.test.protobuf.model.AddressBookProtos.Person) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) ProtobufDataFormat(org.apache.camel.dataformat.protobuf.ProtobufDataFormat) Test(org.junit.Test)

Aggregations

ProtobufDataFormat (org.apache.camel.dataformat.protobuf.ProtobufDataFormat)3 CamelContext (org.apache.camel.CamelContext)2 ProducerTemplate (org.apache.camel.ProducerTemplate)2 RouteBuilder (org.apache.camel.builder.RouteBuilder)2 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)2 Test (org.junit.Test)2 Person (org.wildfly.camel.test.protobuf.model.AddressBookProtos.Person)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 HashMap (java.util.HashMap)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