@Override public void configure() throws Exception { getContext().setTracing(true); // general error handler errorHandler(defaultErrorHandler() .maximumRedeliveries(5) .redeliveryDelay(2000) .retryAttemptedLogLevel(LoggingLevel.WARN)); // in case of a http exception then retry at most 3 times // and if exhausted then upload using ftp instead onException(IOException.class, HttpOperationFailedException.class) .maximumRedeliveries(3) .handled(true) .to(ftp); // poll files send them to the HTTP server from(file).to(http); }
@Override public void configure() throws Exception { getContext().setTracing(true); // general error handler errorHandler(defaultErrorHandler() .maximumRedeliveries(5) .redeliveryDelay(2000) .retryAttemptedLogLevel(LoggingLevel.WARN)); // in case of a http exception then retry at most 3 times // and if exhausted then upload using ftp instead onException(HttpOperationFailedException.class).maximumRedeliveries(3) .handled(true) // use file instead of ftp as its easier to play with .to("file:target/ftp/upload"); // poll files every 5th second and send them to the HTTP server from("file:target/rider?delay=5000&readLock=none") .to("http://localhost:8765/rider"); }
@SuppressWarnings("unchecked") @Override public void process(Exchange exchange) { Throwable t = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class); if (!(t instanceof HttpOperationFailedException)) return; HttpOperationFailedException httpException = (HttpOperationFailedException) t; String responseBody = httpException.getResponseBody(); Map<String, String> responseHeaders = httpException.getResponseHeaders(); exchange.getIn().setBody(responseBody); exchange.getIn().getHeaders().putAll(responseHeaders); exchange.getIn().setHeader(Exchange.HTTP_RESPONSE_CODE, httpException.getStatusCode()); StringBuilder builder = new StringBuilder(); for (String key : responseHeaders.keySet()) { builder.append(key).append(":").append(responseHeaders.get(key)).append(","); } logger.debug("Received error response from endpoint: {}, body: {}, headers: {}", new String[]{(exchange.getFromEndpoint() != null ? exchange.getFromEndpoint().getEndpointUri() : "unknown"), responseBody, builder.toString()}); }
@Test public void testFoundExceptionGe400Match() throws Exception { HttpStatusCodeTestResource resource = new HttpStatusCodeTestResource(500); Exchange e = new DefaultExchange(new DefaultCamelContext()); e.getIn().setHeader(Exchange.HTTP_RESPONSE_CODE, 505); e.setProperty(Exchange.EXCEPTION_CAUGHT, new HttpOperationFailedException(null, 505, null, null, null, null)); assertFalse(resource.matches(e)); }
@Test public void testFoundExceptionGe400MisMatch() throws Exception { HttpStatusCodeTestResource resource = new HttpStatusCodeTestResource(505); Exchange e = new DefaultExchange(new DefaultCamelContext()); e.getIn().setHeader(Exchange.HTTP_RESPONSE_CODE, 505); e.setProperty(Exchange.EXCEPTION_CAUGHT, new HttpOperationFailedException(null, 505, null, null, null, null)); assertTrue(resource.matches(e)); }
@Test public void testSettingContent() throws Exception { Exchange e = new DefaultExchange(new DefaultCamelContext()); //check headers are appended e.getIn().setHeader("foo", "baz"); HttpExceptionResponseProcessor processor = new HttpExceptionResponseProcessor(); Map<String, String> headers = new HashMap<>(); headers.put("a", "b"); headers.put("c", "d"); Exception ex = new HttpOperationFailedException("foo", 505, "failbot", "somewhere", headers, "responsebody"); e.setProperty(Exchange.EXCEPTION_CAUGHT, ex); processor.process(e); assertEquals("responsebody", e.getIn().getBody(String.class)); assertEquals(505, e.getIn().getHeader(Exchange.HTTP_RESPONSE_CODE)); assertEquals("b", e.getIn().getHeader("a")); assertEquals("d", e.getIn().getHeader("c")); assertEquals("baz", e.getIn().getHeader("foo")); }
private int retrieveStatusCode(ReferenceInvocation invocation) { Exception exception = invocation.getContext().getPropertyValue(org.apache.camel.Exchange.EXCEPTION_CAUGHT); if (!(exception instanceof HandlerException)) { return 500; } HandlerException handlerEx = (HandlerException) exception; HttpOperationFailedException httpOperationFailed = (HttpOperationFailedException) handlerEx.getCause(); return httpOperationFailed.getStatusCode(); }
public void process(Exchange exchange) throws Exception { Exception exception = exchange.getProperty(org.apache.camel.Exchange.EXCEPTION_CAUGHT, Exception.class); LOGGER.error(exception.getMessage(), exception); int statusCode = 500; if (exception instanceof HandlerException) { HandlerException handlerEx = (HandlerException) exception; HttpOperationFailedException httpOperationFailed = (HttpOperationFailedException) handlerEx.getCause(); statusCode = httpOperationFailed.getStatusCode(); } exchange.getIn().setHeader(HttpContextMapper.HTTP_RESPONSE_STATUS, statusCode); }
@Override public boolean matches(Exchange exchange) { if (exchange == null) return false; if (exchange.getIn().getHeader(Exchange.HTTP_RESPONSE_CODE) == null) { logger.warn("The HTTP response code does not exist"); return false; } int receivedStatusCode = exchange.getIn().getHeader(Exchange.HTTP_RESPONSE_CODE, Integer.class); if (statusCode != receivedStatusCode) { logger.warn("HTTP Status Code is not expected, received: {}, expected: {}", receivedStatusCode, this.statusCode); return false; } if (statusCode >= 400) { Throwable t = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class); if (!(t instanceof HttpOperationFailedException)) { logger.error("An HttpOperationFailedException response is expected for status code {} but received {}", statusCode, t); //noting that there is other code that yoinks out the responseBody/responseHeaders and validates //against these return false; } HttpOperationFailedException httpException = (HttpOperationFailedException) t; logger.debug("Validated exception {} on endpoint {}", httpException, (exchange.getFromEndpoint() != null ? exchange.getFromEndpoint().getEndpointUri() : "null")); } return true; }