Search in sources :

Example 11 with JaxbDataFormat

use of org.apache.camel.converter.jaxb.JaxbDataFormat in project camel by apache.

the class JaxbDataFormatAutoConfiguration method configureJaxbDataFormatFactory.

@Bean(name = "jaxb-dataformat-factory")
@ConditionalOnClass(CamelContext.class)
@ConditionalOnMissingBean(JaxbDataFormat.class)
public DataFormatFactory configureJaxbDataFormatFactory(final CamelContext camelContext, final JaxbDataFormatConfiguration configuration) {
    return new DataFormatFactory() {

        public DataFormat newInstance() {
            JaxbDataFormat dataformat = new JaxbDataFormat();
            if (CamelContextAware.class.isAssignableFrom(JaxbDataFormat.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) JaxbDataFormat(org.apache.camel.converter.jaxb.JaxbDataFormat) 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 12 with JaxbDataFormat

use of org.apache.camel.converter.jaxb.JaxbDataFormat in project camel by apache.

the class JmsJaxbTest method createRouteBuilder.

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

        @Override
        public void configure() throws Exception {
            errorHandler(deadLetterChannel("jms:queue:error").redeliveryDelay(0));
            onException(InvalidOrderException.class).maximumRedeliveries(0).handled(true).to("jms:queue:invalid");
            DataFormat jaxb = new JaxbDataFormat("org.apache.camel.itest.jms");
            from("jms:queue:in").unmarshal(jaxb).choice().when().method(JmsJaxbTest.class, "isWine").to("jms:queue:wine").otherwise().throwException(new InvalidOrderException("We only like wine")).end();
            from("jms:queue:wine").to("mock:wine");
            from("jms:queue:error").to("mock:error");
            from("jms:queue:invalid").to("mock:invalid");
        }
    };
}
Also used : RouteBuilder(org.apache.camel.builder.RouteBuilder) DataFormat(org.apache.camel.spi.DataFormat) JaxbDataFormat(org.apache.camel.converter.jaxb.JaxbDataFormat) JaxbDataFormat(org.apache.camel.converter.jaxb.JaxbDataFormat)

Example 13 with JaxbDataFormat

use of org.apache.camel.converter.jaxb.JaxbDataFormat in project camel by apache.

the class RestSwaggerComponentTest method createRouteBuilder.

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

        @Override
        public void configure() throws Exception {
            final JAXBContext jaxbContext = JAXBContext.newInstance(Pet.class);
            final JaxbDataFormat jaxb = new JaxbDataFormat(jaxbContext);
            jaxb.setJaxbProviderProperties(Collections.singletonMap(Marshaller.JAXB_FORMATTED_OUTPUT, false));
            from("direct:getPetById").to("petStore:getPetById").unmarshal(jaxb);
            from("direct:addPet").marshal(jaxb).to("petStore:addPet").unmarshal(jaxb);
        }
    };
}
Also used : RouteBuilder(org.apache.camel.builder.RouteBuilder) JAXBContext(javax.xml.bind.JAXBContext) JaxbDataFormat(org.apache.camel.converter.jaxb.JaxbDataFormat)

Example 14 with JaxbDataFormat

use of org.apache.camel.converter.jaxb.JaxbDataFormat in project camel by apache.

the class JaxbDataFormatMustBeJAXBElementTest method createRouteBuilder.

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

        @Override
        public void configure() throws Exception {
            JaxbDataFormat jaxb = new JaxbDataFormat(JAXBContext.newInstance(Foo.class));
            jaxb.setPrettyPrint(false);
            jaxb.setMustBeJAXBElement(false);
            from("direct:start").marshal(jaxb).to("log:xml", "mock:result");
            JaxbDataFormat jaxb2 = new JaxbDataFormat(JAXBContext.newInstance(Foo.class));
            jaxb2.setPrettyPrint(false);
            jaxb2.setMustBeJAXBElement(true);
            from("direct:start2").marshal(jaxb2).to("log:xml", "mock:result2");
        }
    };
}
Also used : RouteBuilder(org.apache.camel.builder.RouteBuilder) JaxbDataFormat(org.apache.camel.converter.jaxb.JaxbDataFormat)

Example 15 with JaxbDataFormat

use of org.apache.camel.converter.jaxb.JaxbDataFormat in project camel by apache.

the class CamelJaxbTest method createRouteBuilder.

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

        public void configure() throws Exception {
            JaxbDataFormat dataFormat = new JaxbDataFormat("org.apache.camel.foo.bar");
            dataFormat.setSchemaLocation("person.xsd");
            dataFormat.setIgnoreJAXBElement(false);
            JaxbDataFormat filterEnabledFormat = new JaxbDataFormat("org.apache.camel.foo.bar");
            filterEnabledFormat.setFilterNonXmlChars(true);
            JaxbDataFormat customWriterFormat = new JaxbDataFormat("org.apache.camel.foo.bar");
            customWriterFormat.setXmlStreamWriterWrapper(new TestXmlStreamWriter());
            JaxbDataFormat customWriterAndFilterFormat = new JaxbDataFormat("org.apache.camel.foo.bar");
            customWriterAndFilterFormat.setFilterNonXmlChars(true);
            customWriterAndFilterFormat.setXmlStreamWriterWrapper(new TestXmlStreamWriter());
            from("direct:getJAXBElementValue").unmarshal(new JaxbDataFormat("org.apache.camel.foo.bar")).to("mock:result");
            from("direct:getJAXBElement").unmarshal(dataFormat).to("mock:result");
            from("direct:unmarshalFilteringEnabled").unmarshal(filterEnabledFormat).to("mock:result");
            from("direct:marshal").marshal(dataFormat).to("mock:result");
            from("direct:marshalFilteringEnabled").marshal(filterEnabledFormat).to("mock:result");
            from("direct:marshalCustomWriter").marshal(customWriterFormat).to("mock:result");
            from("direct:marshalCustomWriterAndFiltering").marshal(customWriterAndFilterFormat).to("mock:result");
            from("direct:unmarshall").unmarshal().jaxb(PersonType.class.getPackage().getName()).to("mock:result");
        }
    };
}
Also used : RouteBuilder(org.apache.camel.builder.RouteBuilder) PersonType(org.apache.camel.foo.bar.PersonType) JaxbDataFormat(org.apache.camel.converter.jaxb.JaxbDataFormat)

Aggregations

JaxbDataFormat (org.apache.camel.converter.jaxb.JaxbDataFormat)17 RouteBuilder (org.apache.camel.builder.RouteBuilder)14 DataFormat (org.apache.camel.spi.DataFormat)6 Exchange (org.apache.camel.Exchange)3 Processor (org.apache.camel.Processor)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 Test (org.junit.Test)2 HashMap (java.util.HashMap)1 JAXBContext (javax.xml.bind.JAXBContext)1 CamelContextAware (org.apache.camel.CamelContextAware)1 RuntimeCamelException (org.apache.camel.RuntimeCamelException)1 PersonType (org.apache.camel.foo.bar.PersonType)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 SwitchYardMessage (org.switchyard.common.camel.SwitchYardMessage)1