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