Search in sources :

Example 1 with CxfOperationException

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;
}
Also used : CxfOperationException(org.apache.camel.component.cxf.CxfOperationException)

Example 2 with CxfOperationException

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());
}
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 3 with CxfOperationException

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));
}
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 4 with CxfOperationException

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());
}
Also used : Exchange(org.apache.camel.Exchange) 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 5 with CxfOperationException

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));
}
Also used : Exchange(org.apache.camel.Exchange) 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)

Aggregations

CxfOperationException (org.apache.camel.component.cxf.CxfOperationException)5 Exchange (org.apache.camel.Exchange)4 Message (org.apache.camel.Message)4 Customer (org.apache.camel.component.cxf.jaxrs.testbean.Customer)4 Test (org.junit.Test)4 Processor (org.apache.camel.Processor)2