Search in sources :

Example 16 with Processor

use of org.apache.camel.Processor in project camel by apache.

the class CxfPayLoadMessageXmlBindingRouterTest method createRouteBuilder.

protected RouteBuilder createRouteBuilder() {
    return new RouteBuilder() {

        public void configure() {
            from("cxf:bean:routerEndpoint?dataFormat=PAYLOAD").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    CxfPayload<?> payload = exchange.getIn().getBody(CxfPayload.class);
                    List<Source> elements = payload.getBodySources();
                    assertNotNull("We should get the elements here", elements);
                    assertEquals("Get the wrong elements size", elements.size(), 1);
                    Element el = new XmlConverter().toDOMElement(elements.get(0));
                    assertEquals("Get the wrong namespace URI", el.getNamespaceURI(), "http://cxf.component.camel.apache.org/");
                }
            }).to("cxf:bean:serviceEndpoint?dataFormat=PAYLOAD");
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) Element(org.w3c.dom.Element) Source(javax.xml.transform.Source) XmlConverter(org.apache.camel.converter.jaxp.XmlConverter)

Example 17 with Processor

use of org.apache.camel.Processor in project camel by apache.

the class CxfPayLoadSoapHeaderTest method createRouteBuilder.

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

        public void configure() {
            // START SNIPPET: payload
            from(getRouterEndpointURI()).process(new Processor() {

                @SuppressWarnings("unchecked")
                public void process(Exchange exchange) throws Exception {
                    CxfPayload<SoapHeader> payload = exchange.getIn().getBody(CxfPayload.class);
                    List<Source> elements = payload.getBodySources();
                    assertNotNull("We should get the elements here", elements);
                    assertEquals("Get the wrong elements size", 1, elements.size());
                    Element el = new XmlConverter().toDOMElement(elements.get(0));
                    elements.set(0, new DOMSource(el));
                    assertEquals("Get the wrong namespace URI", "http://camel.apache.org/pizza/types", el.getNamespaceURI());
                    List<SoapHeader> headers = payload.getHeaders();
                    assertNotNull("We should get the headers here", headers);
                    assertEquals("Get the wrong headers size", headers.size(), 1);
                    assertEquals("Get the wrong namespace URI", ((Element) (headers.get(0).getObject())).getNamespaceURI(), "http://camel.apache.org/pizza/types");
                    // alternatively you can also get the SOAP header via the camel header:
                    headers = exchange.getIn().getHeader(Header.HEADER_LIST, List.class);
                    assertNotNull("We should get the headers here", headers);
                    assertEquals("Get the wrong headers size", headers.size(), 1);
                    assertEquals("Get the wrong namespace URI", ((Element) (headers.get(0).getObject())).getNamespaceURI(), "http://camel.apache.org/pizza/types");
                }
            }).to(getServiceEndpointURI());
        // END SNIPPET: payload
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) DOMSource(javax.xml.transform.dom.DOMSource) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) Element(org.w3c.dom.Element) SoapHeader(org.apache.cxf.binding.soap.SoapHeader) List(java.util.List) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) XmlConverter(org.apache.camel.converter.jaxp.XmlConverter)

Example 18 with Processor

use of org.apache.camel.Processor in project camel by apache.

the class CxfMessageHeadersRelayTest method testInoutHeaderCXFClientNoServiceClassNoRelay.

@Test
public void testInoutHeaderCXFClientNoServiceClassNoRelay() throws Exception {
    // TODO: Fix this test later
    QName qname = QName.valueOf("{http://apache.org/camel/component/cxf/soap/headers}SOAPHeaderInfo");
    String uri = "cxf:bean:routerNoRelayNoServiceClassEndpoint?headerFilterStrategy=#dropAllMessageHeadersStrategy";
    String requestHeader = "<ns2:SOAPHeaderInfo xmlns:ns2=\"http://apache.org/camel/" + "component/cxf/soap/headers\"><originator>CxfSoapHeaderRoutePropagationTest.testInOutHeader Requestor" + "</originator><message>Invoking CxfSoapHeaderRoutePropagationTest.testInOutHeader() Request" + "</message></ns2:SOAPHeaderInfo>";
    String requestBody = "<ns2:inoutHeader xmlns:ns2=\"http://apache.org/camel/component/cxf/soap/headers\">" + "<requestType>CXF user</requestType></ns2:inoutHeader>";
    List<Source> elements = new ArrayList<Source>();
    elements.add(new DOMSource(StaxUtils.read(new StringReader(requestBody)).getDocumentElement()));
    final List<SoapHeader> headers = new ArrayList<SoapHeader>();
    headers.add(new SoapHeader(qname, StaxUtils.read(new StringReader(requestHeader)).getDocumentElement()));
    final CxfPayload<SoapHeader> cxfPayload = new CxfPayload<SoapHeader>(headers, elements, null);
    Exchange exchange = template.request(uri, new Processor() {

        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setBody(cxfPayload);
            exchange.getIn().setHeader(CxfConstants.OPERATION_NAME, "inoutHeader");
            exchange.getIn().setHeader(Header.HEADER_LIST, headers);
        }
    });
    CxfPayload<?> out = exchange.getOut().getBody(CxfPayload.class);
    assertEquals(1, out.getBodySources().size());
    assertTrue(out.getBodySources().get(0) instanceof DOMSource);
    assertEquals(0, out.getHeaders().size());
    String responseExp = "<ns2:inoutHeaderResponse xmlns:ns2=\"http://apache.org/camel/" + "component/cxf/soap/headers\"><responseType>pass</responseType>" + "</ns2:inoutHeaderResponse>";
    String response = StaxUtils.toString(out.getBody().get(0));
    //REVISIT use a more reliable comparison to tolerate some namespaces being added to the root element
    assertTrue(response, response.startsWith(responseExp.substring(0, 87)) && response.endsWith(responseExp.substring(88, responseExp.length())));
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Processor(org.apache.camel.Processor) CxfPayload(org.apache.camel.component.cxf.CxfPayload) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) JAXBException(javax.xml.bind.JAXBException) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) StringReader(java.io.StringReader) SoapHeader(org.apache.cxf.binding.soap.SoapHeader) Test(org.junit.Test)

Example 19 with Processor

use of org.apache.camel.Processor in project camel by apache.

the class CxfRsConsumerSimpleBindingTest method createRouteBuilder.

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

        public void configure() {
            from(CXF_RS_ENDPOINT_URI).recipientList(simple("direct:${header.operationName}"));
            from("direct:getCustomer").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    assertNotNull(exchange.getIn().getHeader("id"));
                    long id = exchange.getIn().getHeader("id", Long.class);
                    if (id == 123) {
                        assertEquals("123", exchange.getIn().getHeader("id"));
                        assertEquals(MessageContentsList.class, exchange.getIn().getBody().getClass());
                        exchange.getOut().setBody(new Customer(123, "Raul"));
                        exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, 200);
                    } else if (id == 456) {
                        exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, 404);
                    } else {
                        fail();
                    }
                }
            });
            from("direct:updateCustomer").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    assertEquals("123", exchange.getIn().getHeader("id"));
                    Customer c = exchange.getIn().getBody(Customer.class);
                    assertEquals(123, c.getId());
                    assertNotNull(c);
                }
            });
            from("direct:newCustomer").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    Customer c = exchange.getIn().getBody(Customer.class);
                    assertNotNull(c);
                    assertEquals(123, c.getId());
                }
            });
            from("direct:listVipCustomers").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    assertEquals("gold", exchange.getIn().getHeader("status", String.class));
                    assertEquals(MessageContentsList.class, exchange.getIn().getBody().getClass());
                    assertEquals(0, exchange.getIn().getBody(MessageContentsList.class).size());
                    CustomerList response = new CustomerList();
                    List<Customer> list = new ArrayList<Customer>(2);
                    list.add(new Customer(123, "Raul"));
                    list.add(new Customer(456, "Raul2"));
                    response.setCustomers(list);
                    exchange.getOut().setBody(response);
                }
            });
            from("direct:updateVipCustomer").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    assertEquals("gold", exchange.getIn().getHeader("status", String.class));
                    assertEquals("123", exchange.getIn().getHeader("id"));
                    Customer c = exchange.getIn().getBody(Customer.class);
                    assertEquals(123, c.getId());
                    assertNotNull(c);
                }
            });
            from("direct:deleteVipCustomer").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    assertEquals("gold", exchange.getIn().getHeader("status", String.class));
                    assertEquals("123", exchange.getIn().getHeader("id"));
                }
            });
            from("direct:uploadImageInputStream").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    assertEquals("123", exchange.getIn().getHeader("id"));
                    assertEquals("image/jpeg", exchange.getIn().getHeader("Content-Type"));
                    assertTrue(InputStream.class.isAssignableFrom(exchange.getIn().getBody().getClass()));
                    InputStream is = exchange.getIn().getBody(InputStream.class);
                    is.close();
                    exchange.getOut().setBody(null);
                }
            });
            from("direct:uploadImageDataHandler").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    assertEquals("123", exchange.getIn().getHeader("id"));
                    assertEquals("image/jpeg", exchange.getIn().getHeader("Content-Type"));
                    assertTrue(DataHandler.class.isAssignableFrom(exchange.getIn().getBody().getClass()));
                    DataHandler dh = exchange.getIn().getBody(DataHandler.class);
                    assertEquals("image/jpeg", dh.getContentType());
                    dh.getInputStream().close();
                    exchange.getOut().setBody(null);
                }
            });
            from("direct:multipartPostWithParametersAndPayload").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    assertEquals("abcd", exchange.getIn().getHeader("query"));
                    assertEquals("123", exchange.getIn().getHeader("id"));
                    assertNotNull(exchange.getIn().getAttachment("part1"));
                    assertNotNull(exchange.getIn().getAttachment("part2"));
                    assertNull(exchange.getIn().getHeader("part1"));
                    assertNull(exchange.getIn().getHeader("part2"));
                    assertEquals(Customer.class, exchange.getIn().getHeader("body").getClass());
                    exchange.getOut().setBody(null);
                }
            });
            from("direct:multipartPostWithoutParameters").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    assertNotNull(exchange.getIn().getAttachment("part1"));
                    assertNotNull(exchange.getIn().getAttachment("part2"));
                    assertNull(exchange.getIn().getHeader("part1"));
                    assertNull(exchange.getIn().getHeader("part2"));
                    assertEquals(Customer.class, exchange.getIn().getHeader("body").getClass());
                    exchange.getOut().setBody(null);
                }
            });
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) MessageContentsList(org.apache.cxf.message.MessageContentsList) Customer(org.apache.camel.component.cxf.jaxrs.simplebinding.testbean.Customer) InputStream(java.io.InputStream) MessageContentsList(org.apache.cxf.message.MessageContentsList) ArrayList(java.util.ArrayList) List(java.util.List) CustomerList(org.apache.camel.component.cxf.jaxrs.simplebinding.testbean.CustomerList) DataHandler(javax.activation.DataHandler) CustomerList(org.apache.camel.component.cxf.jaxrs.simplebinding.testbean.CustomerList)

Example 20 with Processor

use of org.apache.camel.Processor in project camel by apache.

the class CxfEndpointBeansRouterTest method testCxfBusConfiguration.

@Test
public void testCxfBusConfiguration() throws Exception {
    // get the camelContext from application context
    CamelContext camelContext = ctx.getBean("camel", CamelContext.class);
    ProducerTemplate template = camelContext.createProducerTemplate();
    Exchange reply = template.request("cxf:bean:serviceEndpoint", new Processor() {

        public void process(final Exchange exchange) {
            final List<String> params = new ArrayList<String>();
            params.add("hello");
            exchange.getIn().setBody(params);
            exchange.getIn().setHeader(CxfConstants.OPERATION_NAME, "echo");
        }
    });
    Exception ex = reply.getException();
    assertTrue("Should get the fault here", ex instanceof org.apache.cxf.interceptor.Fault || ex instanceof HTTPException);
}
Also used : CamelContext(org.apache.camel.CamelContext) Exchange(org.apache.camel.Exchange) ProducerTemplate(org.apache.camel.ProducerTemplate) Processor(org.apache.camel.Processor) HTTPException(org.apache.cxf.transport.http.HTTPException) ArrayList(java.util.ArrayList) List(java.util.List) HTTPException(org.apache.cxf.transport.http.HTTPException) Test(org.junit.Test)

Aggregations

Processor (org.apache.camel.Processor)1467 Exchange (org.apache.camel.Exchange)1368 Test (org.junit.Test)634 RouteBuilder (org.apache.camel.builder.RouteBuilder)543 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)164 Message (org.apache.camel.Message)119 ArrayList (java.util.ArrayList)65 HashMap (java.util.HashMap)64 IOException (java.io.IOException)55 CamelExecutionException (org.apache.camel.CamelExecutionException)52 Endpoint (org.apache.camel.Endpoint)46 Map (java.util.Map)45 File (java.io.File)38 List (java.util.List)34 Producer (org.apache.camel.Producer)33 DefaultExchange (org.apache.camel.impl.DefaultExchange)29 SendProcessor (org.apache.camel.processor.SendProcessor)26 AggregationStrategy (org.apache.camel.processor.aggregate.AggregationStrategy)26 CountDownLatch (java.util.concurrent.CountDownLatch)24 Expression (org.apache.camel.Expression)24