Search in sources :

Example 1 with HttpOperationFailedException

use of org.apache.camel.http.common.HttpOperationFailedException in project camel by apache.

the class JmsToHttpWithOnExceptionRoute method configure.

public void configure() throws Exception {
    port = AvailablePortFinder.getNextAvailable(8000);
    // configure a global transacted error handler
    errorHandler(transactionErrorHandler(required));
    // if its a 404 then regard it as handled
    onException(HttpOperationFailedException.class).onWhen(new Predicate() {

        public boolean matches(Exchange exchange) {
            HttpOperationFailedException e = exchange.getException(HttpOperationFailedException.class);
            return e != null && e.getStatusCode() == 404;
        }
    }).handled(true).to("mock:404").transform(constant(noAccess));
    from("activemq:queue:data").policy(required).to("http://localhost:" + port + "/sender").convertBodyTo(String.class).choice().when().xpath("/reply/status != 'ok'").to("mock:rollback").rollback().otherwise().end();
    // this is our http router
    from("jetty:http://localhost:" + port + "/sender").process(new Processor() {

        public void process(Exchange exchange) throws Exception {
            // first hit is always a error code 500 to force the caller to retry
            if (counter++ < 1) {
                // simulate http error 500
                exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, 500);
                exchange.getOut().setBody("Damn some internal server error");
                return;
            }
            String user = exchange.getIn().getHeader("user", String.class);
            if ("unknown".equals(user)) {
                // no page for a unknown user
                exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, 404);
                exchange.getOut().setBody("Page does not exists");
                return;
            } else if ("guest".equals(user)) {
                // not okay for guest user
                exchange.getOut().setBody(nok);
                return;
            }
            exchange.getOut().setBody(ok);
        }
    });
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) HttpOperationFailedException(org.apache.camel.http.common.HttpOperationFailedException) HttpOperationFailedException(org.apache.camel.http.common.HttpOperationFailedException) Predicate(org.apache.camel.Predicate)

Example 2 with HttpOperationFailedException

use of org.apache.camel.http.common.HttpOperationFailedException in project camel by apache.

the class HttpThrowExceptionOnFailureTest method httpGetWhichReturnsHttp501ShouldThrowAnException.

@Test
public void httpGetWhichReturnsHttp501ShouldThrowAnException() throws Exception {
    Exchange reply = template.request("http4://" + localServer.getInetAddress().getHostName() + ":" + localServer.getLocalPort() + "/XXX?throwExceptionOnFailure=true", new Processor() {

        public void process(Exchange exchange) throws Exception {
        }
    });
    Exception e = reply.getException();
    assertNotNull("Should have thrown an exception", e);
    HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e);
    assertEquals(501, cause.getStatusCode());
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) HttpOperationFailedException(org.apache.camel.http.common.HttpOperationFailedException) HttpOperationFailedException(org.apache.camel.http.common.HttpOperationFailedException) Test(org.junit.Test)

Example 3 with HttpOperationFailedException

use of org.apache.camel.http.common.HttpOperationFailedException in project camel by apache.

the class HttpRedirectTest method httpRedirect.

@Test
public void httpRedirect() throws Exception {
    String uri = "http4://" + localServer.getInetAddress().getHostName() + ":" + localServer.getLocalPort() + "/test?httpClient.redirectsEnabled=false&httpClient.socketTimeout=60000&httpClient.connectTimeout=60000" + "&httpClient.staleConnectionCheckEnabled=false";
    Exchange out = template.request(uri, new Processor() {

        public void process(Exchange exchange) throws Exception {
        // no data
        }
    });
    assertNotNull(out);
    HttpOperationFailedException cause = out.getException(HttpOperationFailedException.class);
    assertNotNull(cause);
    assertEquals(HttpStatus.SC_MOVED_PERMANENTLY, cause.getStatusCode());
    assertEquals("http4://" + localServer.getInetAddress().getHostName() + ":" + localServer.getLocalPort() + "/someplaceelse", cause.getRedirectLocation());
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) HttpOperationFailedException(org.apache.camel.http.common.HttpOperationFailedException) IOException(java.io.IOException) HttpException(org.apache.http.HttpException) HttpOperationFailedException(org.apache.camel.http.common.HttpOperationFailedException) Test(org.junit.Test)

Example 4 with HttpOperationFailedException

use of org.apache.camel.http.common.HttpOperationFailedException in project camel by apache.

the class HttpRedirectTest method testHttpRedirectFromCamelRoute.

@Test
public void testHttpRedirectFromCamelRoute() throws Exception {
    MockEndpoint errorEndpoint = context.getEndpoint("mock:error", MockEndpoint.class);
    errorEndpoint.expectedMessageCount(1);
    MockEndpoint resultEndpoint = context.getEndpoint("mock:result", MockEndpoint.class);
    resultEndpoint.expectedMessageCount(0);
    try {
        template.requestBody("direct:start", "Hello World", String.class);
        fail("Should have thrown an exception");
    } catch (RuntimeCamelException e) {
        HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause());
        assertEquals(302, cause.getStatusCode());
    }
    errorEndpoint.assertIsSatisfied();
    resultEndpoint.assertIsSatisfied();
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) HttpOperationFailedException(org.apache.camel.http.common.HttpOperationFailedException) RuntimeCamelException(org.apache.camel.RuntimeCamelException) Test(org.junit.Test)

Example 5 with HttpOperationFailedException

use of org.apache.camel.http.common.HttpOperationFailedException in project camel by apache.

the class HttpReturnFaultTest method testHttpFault.

@Test
public void testHttpFault() throws Exception {
    Exchange exchange = template.request("http://localhost:{{port}}/test", new Processor() {

        @Override
        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setBody("Hello World!");
        }
    });
    assertTrue(exchange.isFailed());
    HttpOperationFailedException exception = exchange.getException(HttpOperationFailedException.class);
    assertNotNull(exception);
    assertEquals("This is a fault", exception.getResponseBody());
    assertEquals(500, exception.getStatusCode());
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) HttpOperationFailedException(org.apache.camel.http.common.HttpOperationFailedException) HttpOperationFailedException(org.apache.camel.http.common.HttpOperationFailedException) Test(org.junit.Test)

Aggregations

HttpOperationFailedException (org.apache.camel.http.common.HttpOperationFailedException)29 Test (org.junit.Test)23 Exchange (org.apache.camel.Exchange)18 Processor (org.apache.camel.Processor)11 BaseJettyTest (org.apache.camel.component.jetty.BaseJettyTest)6 IOException (java.io.IOException)4 CamelExecutionException (org.apache.camel.CamelExecutionException)4 HttpConsumer (org.apache.camel.http.common.HttpConsumer)4 RouteBuilder (org.apache.camel.builder.RouteBuilder)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 URISyntaxException (java.net.URISyntaxException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 CamelExchangeException (org.apache.camel.CamelExchangeException)2 Predicate (org.apache.camel.Predicate)2 RuntimeCamelException (org.apache.camel.RuntimeCamelException)2 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)2 StopWatch (org.apache.camel.util.StopWatch)2 File (java.io.File)1