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));
}
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));
}
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());
}
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
}
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);
}
};
}
Aggregations