Package org.apache.camel.component.http

Examples of org.apache.camel.component.http.HttpOperationFailedException


    public void testHttpRedirect() throws Exception {
        try {
            template.requestBody("http://localhost:9080/test", "Hello World", String.class);
            fail("Should have thrown an exception");
        } catch (RuntimeCamelException e) {
            HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause());
            assertEquals(301, cause.getStatusCode());
            assertEquals(true, cause.isRedirectError());
            assertEquals(true, cause.hasRedirectLocation());
            assertEquals("http://localhost:9080/newtest", cause.getRedirectLocation());
        }
    }
View Full Code Here


    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", body, s);
        }

        // give time for files to be deleted etc.
        Thread.sleep(2000);
View Full Code Here

    public void testHttpRedirectNoLocation() throws Exception {
        try {
            template.requestBody("http://localhost:9080/test", "Hello World", String.class);
            fail("Should have thrown an exception");
        } catch (RuntimeCamelException e) {
            HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause());
            assertEquals(302, cause.getStatusCode());
            assertEquals(true, cause.isRedirectError());
            assertEquals(false, cause.hasRedirectLocation());
            assertEquals(null, cause.getRedirectLocation());
        }
    }
View Full Code Here

                        .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();

View Full Code Here

    public void testResponseBodyWhenError() throws Exception {
        try {
            template.requestBody("http://localhost:9080/myapp/myservice", "bookid=123");
            fail("Should have thrown an exception");
        } catch (RuntimeCamelException e) {
            HttpOperationFailedException cause = (HttpOperationFailedException) e.getCause();
            assertEquals(500, cause.getStatusCode());
            String body = context.getTypeConverter().convertTo(String.class, cause.getResponseBody());
            assertTrue(body.indexOf("Damm") > -1);
            assertTrue(body.indexOf("IllegalArgumentException") > -1);
            assertNotNull(cause.getResponseHeaders());
            assertTrue("Should have http header with content type set", cause.getResponseHeaders()[0].getValue().indexOf("text/plain") > -1);
        }
    }
View Full Code Here

    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", body, s);
        }

        // the temporary files should have been deleted
        File file = new File("./target/cachedir");
View Full Code Here

        } catch (CamelExecutionException e) {
            log.info("Timeout hit and client got reply with failure status code");

            long taken = watch.stop();

            HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause());
            assertEquals(503, cause.getStatusCode());

            // should be approx 3-4 sec.
            assertTrue("Timeout should occur faster than " + taken, taken < 4500);
        }
View Full Code Here

                        .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();

View Full Code Here

        } catch (CamelExecutionException e) {
            log.info("Timeout hit and client got reply with failure status code");

            long taken = watch.stop();

            HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause());
            assertEquals(503, cause.getStatusCode());

            // should be approx 30-34 sec.
            assertTrue("Timeout should occur faster than " + taken, taken < 34000);
        }
View Full Code Here

        answer.setBody(extractResponseBody(exchange, httpExchange));
    }

    protected Exception populateHttpOperationFailedException(Exchange exchange, JettyContentExchange httpExchange,
                                                                                int responseCode) throws IOException {
        HttpOperationFailedException answer;
        String uri = httpExchange.getUrl();
        Map<String, String> headers = httpExchange.getHeaders();
        Object responseBody = extractResponseBody(exchange, httpExchange);

        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 locationHeader = httpExchange.getResponseFields().getStringField("location");
            if (locationHeader != null) {
                answer = new HttpOperationFailedException(uri, responseCode, null, locationHeader, headers, copy);
            } else {
                // no redirect location
                answer = new HttpOperationFailedException(uri, responseCode, null, null, headers, copy);
            }
        } else {
            // internal server error (error code 500)
            answer = new HttpOperationFailedException(uri, responseCode, null, null, headers, copy);
        }

        return answer;
    }
View Full Code Here

TOP

Related Classes of org.apache.camel.component.http.HttpOperationFailedException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.