use of org.apache.camel.component.cxf.CxfOperationException in project camel by apache.
the class CxfRsProducer method populateCxfRsProducerException.
protected CxfOperationException populateCxfRsProducerException(Exchange exchange, Response response, int responseCode) {
CxfOperationException exception;
String uri = exchange.getFromEndpoint().getEndpointUri();
String statusText = statusTextFromResponseCode(responseCode);
Map<String, String> headers = parseResponseHeaders(response, exchange);
//Get the response detail string
String copy = exchange.getContext().getTypeConverter().convertTo(String.class, response.getEntity());
if (responseCode >= 300 && responseCode < 400) {
String redirectLocation;
if (response.getMetadata().getFirst("Location") != null) {
redirectLocation = response.getMetadata().getFirst("location").toString();
exception = new CxfOperationException(uri, responseCode, statusText, redirectLocation, headers, copy);
} else {
//no redirect location
exception = new CxfOperationException(uri, responseCode, statusText, null, headers, copy);
}
} else {
//internal server error(error code 500)
exception = new CxfOperationException(uri, responseCode, statusText, null, headers, copy);
}
return exception;
}
use of org.apache.camel.component.cxf.CxfOperationException 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.component.cxf.CxfOperationException in project camel by apache.
the class CxfRsProducerTest method testProducer422Response.
@Test
public void testProducer422Response() {
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();
// Try to create a new Customer with an invalid name
message.setHeader(Exchange.HTTP_METHOD, "POST");
message.setHeader(Exchange.HTTP_PATH, "/customerservice/customers");
Customer customer = new Customer();
customer.setId(8888);
// will trigger a 422 response (a common REST server validation response code)
customer.setName("");
message.setBody(customer);
}
});
assertNotNull("Expect the exception here", exchange.getException());
assertThat("Exception should be a CxfOperationException", exchange.getException(), instanceOf(CxfOperationException.class));
CxfOperationException cxfOperationException = CxfOperationException.class.cast(exchange.getException());
assertThat("CXF operation exception has correct response code", cxfOperationException.getStatusCode(), is(422));
}
use of org.apache.camel.component.cxf.CxfOperationException in project camel by apache.
the class CxfRsAsyncProducerTest method testGetCustomerExceptionWithCxfRsEndpoint.
@Test
public void testGetCustomerExceptionWithCxfRsEndpoint() {
Exchange exchange = template.send("cxfrs://http://localhost:" + getPort1() + "/" + getClass().getSimpleName() + "/?httpClientAPI=true", newExchange -> {
newExchange.setPattern(ExchangePattern.InOut);
Message message = newExchange.getIn();
message.setHeader(Exchange.HTTP_METHOD, "PUT");
message.setHeader(Exchange.HTTP_PATH, "/customerservice/customers");
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.component.cxf.CxfOperationException in project camel by apache.
the class CxfRsAsyncProducerTest method testProducer422Response.
@Test
public void testProducer422Response() {
Exchange exchange = template.send("cxfrs://http://localhost:" + getPort1() + "/" + getClass().getSimpleName() + "/?httpClientAPI=true", newExchange -> {
newExchange.setPattern(ExchangePattern.InOut);
Message message = newExchange.getIn();
message.setHeader(Exchange.HTTP_METHOD, "POST");
message.setHeader(Exchange.HTTP_PATH, "/customerservice/customers");
Customer customer = new Customer();
customer.setId(8888);
customer.setName("");
message.setBody(customer);
});
assertNotNull("Expect the exception here", exchange.getException());
assertThat("Exception should be a CxfOperationException", exchange.getException(), instanceOf(CxfOperationException.class));
CxfOperationException cxfOperationException = CxfOperationException.class.cast(exchange.getException());
assertThat("CXF operation exception has correct response code", cxfOperationException.getStatusCode(), is(422));
}