/** Clones the passed JaxbDataFormat and then augments it with with Drools related namespaces * * @param jaxbDataFormat * @return */ public static JaxbDataFormat augmentJaxbDataFormatDefinition(JaxbDataFormat jaxbDataFormat) { Set<String> set = new HashSet<String>(); for (String clsName : DroolsJaxbHelperProviderImpl.JAXB_ANNOTATED_CMD) { set.add(clsName.substring(0, clsName.lastIndexOf('.'))); } StringBuilder sb = new StringBuilder(); String contextPath = jaxbDataFormat.getContextPath(); if (contextPath != null) { sb.append(contextPath); } sb.append(":"); for (String pkgName : set) { sb.append(pkgName); sb.append(':'); } jaxbDataFormat.setContextPath(sb.toString()); return jaxbDataFormat; }
@Override protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { @Override public void configure() throws Exception { JaxbDataFormat df = new JaxbDataFormat(); df.setContextPath("org.apache.camel.example"); df.setNamespacePrefixRef("myPrefix"); from("direct:start") .marshal(df) .to("mock:result"); } }; }
@Override public void configure() throws Exception { JaxbDataFormat jaxb = new JaxbDataFormat(); jaxb.setContextPath("com.ameliant.devoxx.model"); from("jms:backend").id("stub.backend") .unmarshal(jaxb) .process(exchange -> { Message in = exchange.getIn(); OrderQuery orderQuery = in.getBody(OrderQuery.class); String orderId = orderQuery.getOrderId(); OrderDetails orderDetails = new OrderDetailsBuilder().buildOrderDetails(orderId); in.setBody(orderDetails); }) .log("Stub replying with: ${body}") .marshal(jaxb); }
/** configures camel-drools integration and defines 3 routes: * 1) testing route (connection to drools with JAXB command format) * 2) unmarshalling route (for unmarshalling command results) * 3) marshalling route (enables creating commands through API and converting to XML) */ private CamelContext configure(StatefulKnowledgeSession session) throws Exception { Context context = new JndiContext(); context.bind("ksession", session); CamelContext camelContext = new DefaultCamelContext(context); camelContext.addRoutes(new RouteBuilder() { @Override public void configure() throws Exception { JaxbDataFormat jdf = new JaxbDataFormat(); jdf.setContextPath("org.kie.camel.testdomain"); jdf.setPrettyPrint(true); from("direct:test-session").policy(new KiePolicy()).unmarshal(jdf).to("kie://ksession").marshal(jdf); from("direct:unmarshall").policy(new KiePolicy()).unmarshal(jdf); from("direct:marshall").policy(new KiePolicy()).marshal(jdf); } }); return camelContext; }
@Test public void testJaxbMarshal() throws Exception { final JaxbDataFormat format = new JaxbDataFormat(); format.setContextPath("org.wildfly.camel.test.jaxb.model"); CamelContext camelctx = new DefaultCamelContext(); camelctx.addRoutes(new RouteBuilder() { @Override public void configure() throws Exception { from("direct:start") .marshal(format); } }); camelctx.start(); try (InputStream input = getClass().getResourceAsStream("/customer.xml")) { String expected = XMLUtils.compactXML(input); ProducerTemplate producer = camelctx.createProducerTemplate(); Customer customer = new Customer("John", "Doe"); String result = producer.requestBody("direct:start", customer, String.class); Assert.assertEquals(expected, XMLUtils.compactXML(result)); } finally { camelctx.stop(); } }
@Test public void testJaxbUnmarshal() throws Exception { final JaxbDataFormat format = new JaxbDataFormat(); format.setContextPath("org.wildfly.camel.test.jaxb.model"); CamelContext camelctx = new DefaultCamelContext(); camelctx.addRoutes(new RouteBuilder() { @Override public void configure() throws Exception { from("direct:start") .unmarshal(format); } }); camelctx.start(); try (InputStream input = getClass().getResourceAsStream("/customer.xml")) { ProducerTemplate producer = camelctx.createProducerTemplate(); Customer customer = producer.requestBody("direct:start", input, Customer.class); Assert.assertEquals("John", customer.getFirstName()); Assert.assertEquals("Doe", customer.getLastName()); } finally { camelctx.stop(); } }
@Override protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { @Override public void configure() throws Exception { JaxbDataFormat jaxb = new JaxbDataFormat(false); jaxb.setContextPath("org.apache.camel.component.spring.ws.jaxb"); // request webservice from("direct:webservice-marshall") .marshal(jaxb) .to("spring-ws:http://localhost/?soapAction=http://www.stockquotes.edu/GetQuote&webServiceTemplate=#webServiceTemplate") .convertBodyTo(String.class); // request webservice from("direct:webservice-marshall-unmarshall") .marshal(jaxb) .to("spring-ws:http://localhost/?soapAction=http://www.stockquotes.edu/GetQuote&webServiceTemplate=#webServiceTemplate") .unmarshal(jaxb); // provide web service from("spring-ws:soapaction:http://www.stockquotes.edu/GetQuote?endpointMapping=#endpointMapping").process( new StockQuoteResponseProcessor()); } }; }
@Override protected RouteBuilder createRouteBuilder() throws Exception { routeBuilder = new RouteBuilder() { public void configure() throws Exception { JaxbDataFormat def = new JaxbDataFormat(); def.setPrettyPrint(true); // TODO does not work: def.setContextPath( "org.drools.camel.testdomain:org.drools.pipeline.camel" ); def.setContextPath("org.kie.pipeline.camel"); from("direct:test-with-session").policy(new KiePolicy()).unmarshal(def).to("kie:ksession1").marshal(def); from("direct:test-no-session").policy(new KiePolicy()).unmarshal(def).to("kie:dynamic").marshal(def); } }; return routeBuilder; }
@Override protected RouteBuilder createRouteBuilder() throws Exception { routeBuilder = new RouteBuilder() { public void configure() throws Exception { JaxbDataFormat def = new JaxbDataFormat(); def.setPrettyPrint(true); def.setContextPath("org.kie.pipeline.camel"); from("direct:test-with-session").policy(new KiePolicy()).unmarshal(def).to("kie:ksession1").marshal(def); } }; return routeBuilder; }
@Override protected RouteBuilder createRouteBuilder() throws Exception { routeBuilder = new RouteBuilder() { public void configure() throws Exception { JaxbDataFormat def = new JaxbDataFormat(); def.setPrettyPrint(true); // TODO does not work: def.setContextPath( "org.drools.camel.testdomain:org.drools.pipeline.camel" ); def.setContextPath("org.kie.pipeline.camel"); from("direct:test-with-session").policy(new KiePolicy()).unmarshal(def).to("kie:ksession1").marshal(def); } }; return routeBuilder; }
protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { public void configure() throws Exception { JaxbDataFormat jaxbDf = new JaxbDataFormat(); jaxbDf.setContextPath("org.kie.camel.testdomain"); from("direct:exec").policy(new KiePolicy()).unmarshal(dataformat).to("kie://ksession1").marshal(dataformat); from("direct:execWithLookup").policy(new KiePolicy()).unmarshal(dataformat).to("kie://dynamic").marshal(dataformat); from("direct:unmarshal").policy(new KiePolicy()).unmarshal(dataformat); from("direct:marshal").policy(new KiePolicy()).marshal(dataformat); from("direct:to-xstream").policy(new KiePolicy()).unmarshal(dataformat).marshal("xstream"); from("direct:to-jaxb").policy(new KiePolicy()).unmarshal(dataformat).marshal(jaxbDf); } }; }
@Override public void configure() throws Exception { /** * Configure JAXB so that it can discover model classes. */ JaxbDataFormat jaxbDataFormat = new JaxbDataFormat(); jaxbDataFormat.setContextPath(Order.class.getPackage().getName()); /** * Configure a simple dead letter strategy. Whenever an IllegalStateException * is encountered this takes care of rolling back the JMS and JPA transactions. The * problem message is sent to the WildFly dead letter JMS queue (DLQ). */ onException(IllegalStateException.class) .maximumRedeliveries(1) .handled(true) .to("jms:queue:DLQ") .markRollbackOnly(); /** * This route generates a random order every 15 seconds */ from("timer:order?period=15s&delay=0") .bean("orderGenerator", "generateOrder") .setHeader(Exchange.FILE_NAME).method("orderGenerator", "generateFileName") .to("file://{{jboss.server.data.dir}}/orders"); /** * This route consumes XML files from JBOSS_HOME/standalone/data/orders and sends * the file content to JMS destination OrdersQueue. */ from("file:{{jboss.server.data.dir}}/orders") .transacted() .convertBodyTo(String.class) .to("jms:queue:OrdersQueue"); /** * This route consumes messages from JMS destination OrdersQueue, unmarshalls the XML * message body using JAXB to an Order entity object. The order is then sent to the JPA * endpoint for persisting within an in-memory database. * * Whenever an order quantity greater than 10 is encountered, the route throws an IllegalStateException * which forces the JMS / JPA transaction to be rolled back and the message to be delivered to the dead letter * queue. */ from("jms:queue:OrdersQueue") .unmarshal(jaxbDataFormat) .to("jpa:Order") .choice() .when(simple("${body.quantity} > 10")) .log("Order quantity is greater than 10 - rolling back transaction!") .throwException(new IllegalStateException("Invalid quantity")) .otherwise() .log("Order processed successfully"); }
/** * Uses the JAXB data format */ public T jaxb() { return dataFormat(new JaxbDataFormat()); }
/** * Uses the JAXB data format with context path */ public T jaxb(String contextPath) { JaxbDataFormat dataFormat = new JaxbDataFormat(); dataFormat.setContextPath(contextPath); return dataFormat(dataFormat); }
/** * Uses the JAXB data format turning pretty printing on or off */ public T jaxb(boolean prettyPrint) { return dataFormat(new JaxbDataFormat(prettyPrint)); }
protected JaxbDataFormat getDataFormat(Class<?> xmlClass) { JaxbDataFormat dataFormat = new JaxbDataFormat(false); dataFormat.setContextPath(xmlClass.getPackage().getName()); dataFormat.setSchema("classpath:org/cleverbus/core/camel/jaxb/mock.xsd"); return dataFormat; }
/** * Creates a JAXB data format for marshalling and unmarshalling the specified class * to/from its native XML representation. * <p/> * If rootQName is provided, this data format will work with classes that are not root elements * (not annotated with {@link XmlRootElement}). * * @param xmlClass the class that this data format will be able to marshal and unmarshal * @param rootQName the QName (optional) of the root element in XML, * which is necessary for marshalling classes without XmlRootElement to XML * @return the Camel {@link DataFormatDefinition} instance for use in routes */ public static JaxbDataFormat jaxb(Class<?> xmlClass, @Nullable QName rootQName) { JaxbDataFormat dataFormat = jaxb(xmlClass); // partial marshaling - class without @XmlRootElement if (rootQName != null) { dataFormat.setFragment(true); dataFormat.setPartClass(xmlClass.getName()); dataFormat.setPartNamespace(rootQName.toString()); } return dataFormat; }
/** * Creates a JAXB data format for <strong>unmarshalling</strong> (only) * the specified class to/from its native XML representation. * Unlike data format returned by {@link #jaxb(Class)}, * this data format works with classes that are not root elements * (e.g., not annotated with {@link XmlRootElement}). * * @param xmlClass the class that this data format will be able to marshal and unmarshal * @return the Camel {@link DataFormatDefinition} instance for use in routes * @see #jaxbFragment(Class, String) for marshalling fragments (as this one can only unmarshal) */ public static JaxbDataFormat jaxbFragment(Class<?> xmlClass) { JaxbDataFormat jaxbIn = jaxb(xmlClass); // partial unmarshaling - class without @XmlRootElement jaxbIn.setFragment(true); jaxbIn.setPartClass(xmlClass.getName()); return jaxbIn; }
/** * Creates a JAXB data format for marshalling and unmarshalling * the specified class to/from its native XML representation. * Unlike data format returned by {@link #jaxb(Class)}, * this data format works with classes that are not root elements * (e.g., not annotated with {@link XmlRootElement}). * * @param xmlClass the class that this data format will be able to marshal and unmarshal * @param partNamespace the namespace (optional) with the name of the root element * (name in XML, not its Java class name), * which is necessary for marshalling the class to XML * @return the Camel {@link DataFormatDefinition} instance for use in routes */ public static JaxbDataFormat jaxbFragment(Class<?> xmlClass, String partNamespace) { JaxbDataFormat dataFormat = jaxbFragment(xmlClass); // partial marshaling - class without @XmlRootElement dataFormat.setFragment(true); dataFormat.setPartClass(xmlClass.getName()); dataFormat.setPartNamespace(partNamespace); return dataFormat; }
/** * Creates a JAXB data format for marshalling and unmarshalling * the specified class to/from its native XML representation. * * @param xmlClass the class that this data format will be able to marshal and unmarshal * @return the Camel {@link DataFormatDefinition} instance for use in routes */ public static JaxbDataFormat jaxb(Class<?> xmlClass) { JaxbDataFormat dataFormat = new JaxbDataFormat(false); dataFormat.setContextPath(xmlClass.getPackage().getName()); return dataFormat; }