user1907906
user1907906

Reputation:

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?

Upvotes: 0

Views: 4342

Answers (2)

Endeios
Endeios

Reputation: 911

You can force the status code for your exception by setting

.onException(MyException.class).process(SetFailureResponse::new).handled(true)

and in a separated SetFailureResponse class set

@Override
    public void process(Exchange exchange) throws Exception {
        exchange.getMessage().setHeader(Exchange.HTTP_RESPONSE_CODE,400);
    }

Upvotes: 1

U2one
U2one

Reputation: 381

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.

Upvotes: 2

Related Questions