Search in sources :

Example 11 with HttpOperationFailedException

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

the class JettySuspendResumeTest method testJettySuspendResume.

@Test
public void testJettySuspendResume() throws Exception {
    context.getShutdownStrategy().setTimeout(50);
    String reply = template.requestBody(serverUri, "World", String.class);
    assertEquals("Bye World", reply);
    // now suspend jetty
    HttpConsumer consumer = (HttpConsumer) context.getRoute("route1").getConsumer();
    assertNotNull(consumer);
    // suspend
    consumer.suspend();
    try {
        template.requestBody(serverUri, "Moon", String.class);
        fail("Should throw exception");
    } catch (Exception e) {
        HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause());
        assertEquals(503, cause.getStatusCode());
    }
    // resume
    consumer.resume();
    // and send request which should be processed
    reply = template.requestBody(serverUri, "Moon", String.class);
    assertEquals("Bye Moon", reply);
}
Also used : HttpOperationFailedException(org.apache.camel.http.common.HttpOperationFailedException) HttpConsumer(org.apache.camel.http.common.HttpConsumer) HttpOperationFailedException(org.apache.camel.http.common.HttpOperationFailedException) Test(org.junit.Test)

Example 12 with HttpOperationFailedException

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

the class JettySuspendTest method testJettySuspend.

@Test
public void testJettySuspend() throws Exception {
    context.getShutdownStrategy().setTimeout(50);
    String reply = template.requestBody(serverUri, "World", String.class);
    assertEquals("Bye World", reply);
    // now suspend jetty
    HttpConsumer consumer = (HttpConsumer) context.getRoute("route1").getConsumer();
    assertNotNull(consumer);
    // suspend
    consumer.suspend();
    try {
        template.requestBody(serverUri, "Moon", String.class);
        fail("Should throw exception");
    } catch (Exception e) {
        HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause());
        assertEquals(503, cause.getStatusCode());
    }
}
Also used : HttpOperationFailedException(org.apache.camel.http.common.HttpOperationFailedException) HttpConsumer(org.apache.camel.http.common.HttpConsumer) HttpOperationFailedException(org.apache.camel.http.common.HttpOperationFailedException) Test(org.junit.Test)

Example 13 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 14 with HttpOperationFailedException

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

the class JmsToHttpWithOnExceptionAndNoTransactionErrorHandlerConfiguredRoute method configure.

public void configure() throws Exception {
    port = AvailablePortFinder.getNextAvailable(8000);
    // 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 15 with HttpOperationFailedException

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

the class HttpProducer method populateHttpOperationFailedException.

protected Exception populateHttpOperationFailedException(Exchange exchange, HttpMethod method, int responseCode) throws IOException, ClassNotFoundException, URISyntaxException {
    Exception answer;
    String uri = method.getURI().toString();
    String statusText = method.getStatusLine() != null ? method.getStatusLine().getReasonPhrase() : null;
    Map<String, String> headers = extractResponseHeaders(method.getResponseHeaders());
    // handle cookies
    if (getEndpoint().getCookieHandler() != null) {
        Map<String, List<String>> m = new HashMap<String, List<String>>();
        for (Entry<String, String> e : headers.entrySet()) {
            m.put(e.getKey(), Collections.singletonList(e.getValue()));
        }
        getEndpoint().getCookieHandler().storeCookies(exchange, new URI(method.getURI().getEscapedURI()), m);
    }
    Object responseBody = extractResponseBody(method, exchange, getEndpoint().isIgnoreResponseBody());
    if (transferException && responseBody != null && responseBody instanceof Exception) {
        // if the response was a serialized exception then use that
        return (Exception) responseBody;
    }
    // make a defensive copy of the response body in the exception so its detached from the cache
    String copy = null;
    if (responseBody != null) {
        copy = exchange.getContext().getTypeConverter().convertTo(String.class, exchange, responseBody);
    }
    if (responseCode >= 300 && responseCode < 400) {
        String redirectLocation;
        Header locationHeader = method.getResponseHeader("location");
        if (locationHeader != null) {
            redirectLocation = locationHeader.getValue();
            answer = new HttpOperationFailedException(uri, responseCode, statusText, redirectLocation, headers, copy);
        } else {
            // no redirect location
            answer = new HttpOperationFailedException(uri, responseCode, statusText, null, headers, copy);
        }
    } else {
        // internal server error (error code 500)
        answer = new HttpOperationFailedException(uri, responseCode, statusText, null, headers, copy);
    }
    return answer;
}
Also used : Header(org.apache.commons.httpclient.Header) HashMap(java.util.HashMap) HttpOperationFailedException(org.apache.camel.http.common.HttpOperationFailedException) ArrayList(java.util.ArrayList) List(java.util.List) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) HttpOperationFailedException(org.apache.camel.http.common.HttpOperationFailedException) CamelExchangeException(org.apache.camel.CamelExchangeException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

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