Search in sources :

Example 26 with Processor

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

the class CxfRsProducerTest method testGetCustomersWithClientProxyAPI.

@Test
public void testGetCustomersWithClientProxyAPI() {
    Exchange exchange = template.send("direct://proxy", new Processor() {

        public void process(Exchange exchange) throws Exception {
            exchange.setPattern(ExchangePattern.InOut);
            Message inMessage = exchange.getIn();
            setupDestinationURL(inMessage);
            // set the operation name 
            inMessage.setHeader(CxfConstants.OPERATION_NAME, "getCustomers");
            // using the proxy client API
            inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_USING_HTTP_API, Boolean.FALSE);
            // set the parameters , if you just have one parameter 
            // camel will put this object into an Object[] itself
            inMessage.setBody(null);
        }
    });
    // get the response message 
    List<Customer> response = CastUtils.cast((List<?>) exchange.getOut().getBody());
    assertNotNull("The response should not be null ", response);
    assertTrue("Dan is missing!", response.contains(new Customer(113, "Dan")));
    assertTrue("John is missing!", response.contains(new Customer(123, "John")));
    assertEquals("Get a wrong response code", 200, exchange.getOut().getHeader(Exchange.HTTP_RESPONSE_CODE));
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) Message(org.apache.camel.Message) Customer(org.apache.camel.component.cxf.jaxrs.testbean.Customer) CxfOperationException(org.apache.camel.component.cxf.CxfOperationException) Test(org.junit.Test)

Example 27 with Processor

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

the class CxfRsProducerTest method testAddCustomerUniqueResponseCodeWithProxyAPI.

@Test
public void testAddCustomerUniqueResponseCodeWithProxyAPI() {
    Exchange exchange = template.send("direct://proxy", new Processor() {

        public void process(Exchange exchange) throws Exception {
            exchange.setPattern(ExchangePattern.InOut);
            Message inMessage = exchange.getIn();
            setupDestinationURL(inMessage);
            // set the operation name 
            inMessage.setHeader(CxfConstants.OPERATION_NAME, "addCustomerUniqueResponseCode");
            // using the proxy client API
            inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_USING_HTTP_API, Boolean.FALSE);
            // set the parameters , if you just have one parameter 
            // camel will put this object into an Object[] itself
            Customer customer = new Customer();
            customer.setId(8888);
            customer.setName("ProxyAPI");
            inMessage.setBody(customer);
        }
    });
    // get the response message 
    Response response = (Response) exchange.getOut().getBody();
    assertNotNull("The response should not be null ", response);
    assertNotNull("The response entity should not be null", response.getEntity());
    // check the response code
    assertEquals("Get a wrong response code", 201, response.getStatus());
    // check the response code from message header
    assertEquals("Get a wrong response code", 201, exchange.getOut().getHeader(Exchange.HTTP_RESPONSE_CODE));
}
Also used : Exchange(org.apache.camel.Exchange) Response(javax.ws.rs.core.Response) Processor(org.apache.camel.Processor) Message(org.apache.camel.Message) Customer(org.apache.camel.component.cxf.jaxrs.testbean.Customer) CxfOperationException(org.apache.camel.component.cxf.CxfOperationException) Test(org.junit.Test)

Example 28 with Processor

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

the class CxfRsProducerTest method testGetCustomerExceptionWithCxfRsEndpoint.

@Test
public void testGetCustomerExceptionWithCxfRsEndpoint() {
    Exchange exchange = template.send("cxfrs://http://localhost:" + getPort1() + "/" + getClass().getSimpleName() + "/?httpClientAPI=true&synchronous=true", new Processor() {

        public void process(Exchange exchange) throws Exception {
            exchange.setPattern(ExchangePattern.InOut);
            Message message = exchange.getIn();
            // set the Http method
            message.setHeader(Exchange.HTTP_METHOD, "PUT");
            // set the relative path
            message.setHeader(Exchange.HTTP_PATH, "/customerservice/customers");
            // we just setup the customer with a wrong id
            Customer customer = new Customer();
            customer.setId(222);
            customer.setName("user");
            message.setBody(customer);
        }
    });
    // we should get the exception here 
    assertNotNull("Expect the exception here", exchange.getException());
    CxfOperationException exception = (CxfOperationException) exchange.getException();
    assertEquals("Get a wrong response body", "Cannot find the customer!", exception.getResponseBody());
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) Message(org.apache.camel.Message) Customer(org.apache.camel.component.cxf.jaxrs.testbean.Customer) CxfOperationException(org.apache.camel.component.cxf.CxfOperationException) CxfOperationException(org.apache.camel.component.cxf.CxfOperationException) Test(org.junit.Test)

Example 29 with Processor

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

the class CxfRsAsyncProducerTest method testGetCustomerWithClientProxyAPI.

@Test
public void testGetCustomerWithClientProxyAPI() {
    // START SNIPPET: ProxyExample
    Exchange exchange = template.send("direct://proxy", new Processor() {

        public void process(Exchange exchange) throws Exception {
            exchange.setPattern(ExchangePattern.InOut);
            Message inMessage = exchange.getIn();
            // set the operation name 
            inMessage.setHeader(CxfConstants.OPERATION_NAME, "getCustomer");
            // using the proxy client API
            inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_USING_HTTP_API, Boolean.FALSE);
            // set a customer header
            inMessage.setHeader("key", "value");
            // setup the accept content type
            inMessage.setHeader(Exchange.ACCEPT_CONTENT_TYPE, "application/json");
            // set the parameters , if you just have one parameter 
            // camel will put this object into an Object[] itself
            inMessage.setBody("123");
        }
    });
    // get the response message 
    Customer response = (Customer) exchange.getOut().getBody();
    assertNotNull("The response should not be null ", response);
    assertEquals("Get a wrong customer id ", String.valueOf(response.getId()), "123");
    assertEquals("Get a wrong customer name", response.getName(), "John");
    assertEquals("Get a wrong response code", 200, exchange.getOut().getHeader(Exchange.HTTP_RESPONSE_CODE));
    assertEquals("Get a wrong header value", "value", exchange.getOut().getHeader("key"));
// END SNIPPET: ProxyExample     
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) Message(org.apache.camel.Message) Customer(org.apache.camel.component.cxf.jaxrs.testbean.Customer) CxfOperationException(org.apache.camel.component.cxf.CxfOperationException) Test(org.junit.Test)

Example 30 with Processor

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

the class CxfRsSpringConsumerTest method createRouteBuilder.

protected RouteBuilder createRouteBuilder() throws Exception {
    final Processor testProcessor = new Processor() {

        public void process(Exchange exchange) throws Exception {
            // just throw the CustomException here
            throw new CustomException("Here is the exception");
        }
    };
    final Processor responseProcessor = new Processor() {

        public void process(Exchange exchange) throws Exception {
            // do something else with the request properties as usual
            // do something else with the response
            exchange.getOut().getBody(Customer.class).setId(246);
        }
    };
    return new RouteBuilder() {

        public void configure() {
            errorHandler(new NoErrorHandlerBuilder());
            from("cxfrs://bean://rsServer").process(testProcessor);
            from("cxfrs://bean://rsServer2").process(testProcessor);
            from("cxfrs://bean://rsServerInvoke?performInvocation=true").process(responseProcessor);
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) Customer(org.apache.camel.component.cxf.jaxrs.testbean.Customer) NoErrorHandlerBuilder(org.apache.camel.builder.NoErrorHandlerBuilder) CustomException(org.apache.camel.component.cxf.jaxrs.testbean.CustomException)

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