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());
}
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);
}
});
}
};
}
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);
}
Aggregations