exceptionapache-camelrestlethttp-status-codes

Set HTTP code onException with Camel and Restlet


Using the Restlet component in an Apache Camel project, I am not able to set the HTTP status code on the response in onException.

Consider a Camel RouteBuilder configured like this:

onException(Exception.class)
  .handled(true)
  .process(new FailureResponseProcessor());

from("restlet:http://example.com/foo")
  .process(fooProcessor)
  .to("file:data/outbox");

In the FailureResponseProcessor I try to set the HTTP response code for Restlet:

public void process(Exchange exchange) throws Exception {
  Response response = exchange.getIn()
                              .getHeader(RestletConstants.RESTLET_RESPONSE,
                                         Response.class);
    response.setStatus(Status.SERVER_ERROR_INTERNAL);
}

This does not work. The status of the the HTTP response is always 200 OK.

What is necessary to effecively set the HTTP status code?


Solution

  • Here you are just retrieving the response object and changing it. This does not propagate down the route. You need to set the header after modifying the object.

    exchange.getIn().setHeader(RestletConstants.RESTLET_RESPONSE, response);
    

    Thanks.