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