Search in sources :

Example 11 with Processor

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

the class CamelEndpointSpringConfigureTest method testCreateDestinationFromSpring.

@Test
public void testCreateDestinationFromSpring() throws Exception {
    CxfEndpoint cxfEndpoint = context.getEndpoint("cxf:bean:serviceEndpoint", CxfEndpoint.class);
    CxfProducer producer = (CxfProducer) cxfEndpoint.createProducer();
    assertNotNull("The producer should not be null", producer);
    producer.start();
    CamelConduit conduit = (CamelConduit) producer.getClient().getConduit();
    assertTrue("we should get SpringCamelContext here", conduit.getCamelContext() instanceof SpringCamelContext);
    assertEquals("The context id should be camel_conduit", "camel_conduit", conduit.getCamelContext().getName());
    cxfEndpoint = context.getEndpoint("cxf:bean:routerEndpoint", CxfEndpoint.class);
    CxfConsumer consumer = (CxfConsumer) cxfEndpoint.createConsumer(new Processor() {

        public void process(Exchange exchange) throws Exception {
        // do nothing here                
        }
    });
    assertNotNull("The consumer should not be null", consumer);
    consumer.start();
    CamelDestination destination = (CamelDestination) consumer.getServer().getDestination();
    assertTrue("we should get SpringCamelContext here", destination.getCamelContext() instanceof SpringCamelContext);
    assertEquals("The context id should be camel_destination", "camel_destination", destination.getCamelContext().getName());
}
Also used : Exchange(org.apache.camel.Exchange) CxfProducer(org.apache.camel.component.cxf.CxfProducer) Processor(org.apache.camel.Processor) SpringCamelContext(org.apache.camel.spring.SpringCamelContext) CxfConsumer(org.apache.camel.component.cxf.CxfConsumer) CamelConduit(org.apache.camel.component.cxf.transport.CamelConduit) CamelDestination(org.apache.camel.component.cxf.transport.CamelDestination) CxfEndpoint(org.apache.camel.component.cxf.CxfEndpoint) Test(org.junit.Test)

Example 12 with Processor

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

the class CxfRawMessageRouterTest method testTheContentTypeOnTheWire.

@Test
public void testTheContentTypeOnTheWire() throws Exception {
    Exchange exchange = template.send(getRouterAddress(), new Processor() {

        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setBody("<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" + "<soap:Body><ns1:echo xmlns:ns1=\"http://cxf.component.camel.apache.org/\">" + "<arg0 xmlns=\"http://cxf.component.camel.apache.org/\">hello world</arg0>" + "</ns1:echo></soap:Body></soap:Envelope>");
        }
    });
    assertNotNull("We should get the Content-Type here", MessageHelper.getContentType(exchange.getOut()));
    assertTrue("Get wrong content type", MessageHelper.getContentType(exchange.getOut()).startsWith("text/xml"));
    assertNotNull("We should get the content-type here", exchange.getOut().getHeader("content-type"));
    String response = exchange.getOut().getBody(String.class);
    assertNotNull("Response should not be null", response);
    assertTrue("We should get right return result", response.indexOf("echo hello world") > 0);
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) Test(org.junit.Test)

Example 13 with Processor

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

the class CxfMessageStreamExceptionTest method createRouteBuilder.

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

        public void configure() {
            // START SNIPPET: onException
            from("direct:start").onException(SoapFault.class).maximumRedeliveries(0).handled(true).process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    SoapFault fault = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, SoapFault.class);
                    exchange.getOut().setFault(true);
                    exchange.getOut().setBody(fault);
                }
            }).end().to(serviceURI);
            // END SNIPPET: onException
            // START SNIPPET: MessageStreamFault
            from(routerEndpointURI).process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    Message out = exchange.getOut();
                    // Set the message body with the 
                    out.setBody(this.getClass().getResourceAsStream("SoapFaultMessage.xml"));
                    // Set the response code here
                    out.setHeader(org.apache.cxf.message.Message.RESPONSE_CODE, new Integer(500));
                }
            });
        // END SNIPPET: MessageStreamFault
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) SoapFault(org.apache.cxf.binding.soap.SoapFault) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) Message(org.apache.camel.Message)

Example 14 with Processor

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

the class CxfMixedModeRouterTest method createRouteBuilder.

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

        public void configure() {
            errorHandler(noErrorHandler());
            from(routerEndpointURI).process(new Processor() {

                // convert request message
                public void process(Exchange exchange) throws Exception {
                    CxfPayload<?> message = exchange.getIn().getBody(CxfPayload.class);
                    List<String> params = new ArrayList<String>();
                    if (message != null) {
                        // convert CxfPayload to list of objects any way you like
                        Element element = new XmlConverter().toDOMElement(message.getBody().get(0));
                        params.add(element.getFirstChild().getTextContent());
                    }
                    // replace the body
                    exchange.getIn().setBody(params);
                // if you need to change the operation name
                //exchange.getIn().setHeader(CxfConstants.OPERATION_NAME, GREET_ME_OPERATION);      
                }
            }).to(serviceEndpointURI).process(new Processor() {

                // convert response to CxfPayload
                public void process(Exchange exchange) throws Exception {
                    List<?> list = exchange.getIn().getBody(List.class);
                    CxfPayload<SoapHeader> message = null;
                    if (list != null) {
                        // convert the list of objects to CxfPayload any way you like
                        String s = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<ns1:echoResponse xmlns:ns1=\"http://cxf.component.camel.apache.org/\">" + "<return xmlns=\"http://cxf.component.camel.apache.org/\">" + list.get(0) + "</return></ns1:echoResponse>";
                        List<Element> body = new ArrayList<Element>();
                        body.add(StaxUtils.read(new StringReader(s)).getDocumentElement());
                        message = new CxfPayload<SoapHeader>(new ArrayList<SoapHeader>(), body);
                    }
                    exchange.getIn().setBody(message);
                    // we probably should be smarter in detecting data format based on message body
                    // but for now we need to explicitly reset the mode (see CAMEL-3420)
                    exchange.setProperty(CxfConstants.DATA_FORMAT_PROPERTY, DataFormat.PAYLOAD);
                }
            });
        }
    };
}
Also used : Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) Element(org.w3c.dom.Element) XmlConverter(org.apache.camel.converter.jaxp.XmlConverter) Exchange(org.apache.camel.Exchange) StringReader(java.io.StringReader) SoapHeader(org.apache.cxf.binding.soap.SoapHeader) ArrayList(java.util.ArrayList) List(java.util.List)

Example 15 with Processor

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

the class CxfPayLoadMessageRouterAddressOverrideTest method createRouteBuilder.

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

        public void configure() {
            from(routerEndpointURI).process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    exchange.getIn().setHeader(Exchange.DESTINATION_OVERRIDE_URL, getServiceAddress());
                    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(serviceEndpointURI);
        }
    };
}
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)

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