Search in sources :

Example 26 with RouteBuilder

use of org.apache.camel.builder.RouteBuilder 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 27 with RouteBuilder

use of org.apache.camel.builder.RouteBuilder 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)

Example 28 with RouteBuilder

use of org.apache.camel.builder.RouteBuilder 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 29 with RouteBuilder

use of org.apache.camel.builder.RouteBuilder 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 30 with RouteBuilder

use of org.apache.camel.builder.RouteBuilder in project camel by apache.

the class LoadDistributorFeatureTest method startRoute.

private void startRoute(DefaultCamelContext ctx, final String proxy, final String real) throws Exception {
    ctx.addRoutes(new RouteBuilder() {

        public void configure() {
            List<String> serviceList = new ArrayList<String>();
            serviceList.add(SERVICE_ADDRESS_1);
            serviceList.add(SERVICE_ADDRESS_2);
            SequentialStrategy strategy = new SequentialStrategy();
            strategy.setAlternateAddresses(serviceList);
            LoadDistributorFeature ldf = new LoadDistributorFeature();
            ldf.setStrategy(strategy);
            CxfEndpoint endpoint = (CxfEndpoint) (endpoint(real));
            endpoint.getFeatures().add(ldf);
            from(proxy).to(endpoint);
        }
    });
    ctx.start();
}
Also used : SequentialStrategy(org.apache.cxf.clustering.SequentialStrategy) RouteBuilder(org.apache.camel.builder.RouteBuilder) LoadDistributorFeature(org.apache.cxf.clustering.LoadDistributorFeature) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

RouteBuilder (org.apache.camel.builder.RouteBuilder)1744 Exchange (org.apache.camel.Exchange)624 Processor (org.apache.camel.Processor)542 Test (org.junit.Test)470 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)338 CamelExecutionException (org.apache.camel.CamelExecutionException)135 FailedToCreateRouteException (org.apache.camel.FailedToCreateRouteException)119 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)100 File (java.io.File)65 IOException (java.io.IOException)61 CamelContext (org.apache.camel.CamelContext)61 ResolveEndpointFailedException (org.apache.camel.ResolveEndpointFailedException)42 HashMap (java.util.HashMap)34 Path (org.apache.hadoop.fs.Path)34 CountDownLatch (java.util.concurrent.CountDownLatch)32 Configuration (org.apache.hadoop.conf.Configuration)32 ArrayFile (org.apache.hadoop.io.ArrayFile)30 SequenceFile (org.apache.hadoop.io.SequenceFile)30 Endpoint (org.apache.camel.Endpoint)27 RuntimeCamelException (org.apache.camel.RuntimeCamelException)26