Search in sources :

Example 26 with HttpOperationFailedException

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

the class HttpThrowExceptionOnFailureTest method httpGetWhichReturnsHttp501ShouldThrowAnExceptionWithIgnoreResponseBody.

@Test
public void httpGetWhichReturnsHttp501ShouldThrowAnExceptionWithIgnoreResponseBody() throws Exception {
    Exchange reply = template.request("http4://" + localServer.getInetAddress().getHostName() + ":" + localServer.getLocalPort() + "/XXX?throwExceptionOnFailure=true&ignoreResponseBody=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 27 with HttpOperationFailedException

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

the class JettyHandle404Test method createRouteBuilder.

@Override
protected RouteBuilder createRouteBuilder() throws Exception {
    return new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            // setup the jetty component with the customx error handler
            JettyHttpComponent jettyComponent = (JettyHttpComponent) context.getComponent("jetty");
            jettyComponent.setErrorHandler(new MyErrorHandler());
            // disable error handling
            errorHandler(noErrorHandler());
            from("direct:start").enrich("direct:tohttp", new AggregationStrategy() {

                public Exchange aggregate(Exchange original, Exchange resource) {
                    // get the response code
                    Integer code = resource.getIn().getHeader(Exchange.HTTP_RESPONSE_CODE, Integer.class);
                    assertEquals(404, code.intValue());
                    return resource;
                }
            }).to("mock:result");
            // use this sub route as indirection to handle the HttpOperationFailedException
            // and set the data back as data on the exchange to not cause the exception to be thrown
            from("direct:tohttp").doTry().to(getProducerUrl()).doCatch(HttpOperationFailedException.class).process(new Processor() {

                public void process(Exchange exchange) {
                    // copy the caused exception values to the exchange as we want the response in the regular exchange
                    // instead as an exception that will get thrown and thus the route breaks
                    HttpOperationFailedException cause = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, HttpOperationFailedException.class);
                    exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, cause.getStatusCode());
                    exchange.getOut().setBody(cause.getResponseBody());
                }
            }).end();
            // this is our jetty server where we simulate the 404
            from("jetty://http://localhost:{{port}}/myserver").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    exchange.getOut().setBody("Page not found");
                    exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, 404);
                }
            });
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) HttpOperationFailedException(org.apache.camel.http.common.HttpOperationFailedException) HttpOperationFailedException(org.apache.camel.http.common.HttpOperationFailedException) AggregationStrategy(org.apache.camel.processor.aggregate.AggregationStrategy)

Example 28 with HttpOperationFailedException

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

the class HttpStreamCacheFileTest method testStreamCacheToFileShouldBeDeletedInCaseOfException.

@Test
public void testStreamCacheToFileShouldBeDeletedInCaseOfException() throws Exception {
    try {
        template.requestBody("direct:start", null, String.class);
        fail("Should have thrown an exception");
    } catch (CamelExecutionException e) {
        HttpOperationFailedException hofe = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause());
        String s = context.getTypeConverter().convertTo(String.class, hofe.getResponseBody());
        assertEquals("Response body", responseBody, s);
    }
    // the temporary files should have been deleted
    File file = new File("target/cachedir");
    String[] files = file.list();
    assertEquals("There should be no files", 0, files.length);
}
Also used : CamelExecutionException(org.apache.camel.CamelExecutionException) HttpOperationFailedException(org.apache.camel.http.common.HttpOperationFailedException) File(java.io.File) Test(org.junit.Test)

Aggregations

HttpOperationFailedException (org.apache.camel.http.common.HttpOperationFailedException)28 Test (org.junit.Test)22 Exchange (org.apache.camel.Exchange)17 Processor (org.apache.camel.Processor)10 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 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 RouteBuilder (org.apache.camel.builder.RouteBuilder)2 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)2 StopWatch (org.apache.camel.util.StopWatch)2 File (java.io.File)1