camel-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mark Mindenhall <mark.mindenh...@machineshop.io>
Subject Re: camel-netty4-http access to response
Date Wed, 18 Feb 2015 02:46:09 GMT
Figured it out; just replying to my own message in case someone else runs into the same problem
(or in case I forget later ;)  ).

I dug around in the code, and discovered that the HTTP response code can be set via the CamelHttpResponseCode
message header (defined as a constant: Exchange.HTTP_RESPONSE_CODE).  There’s no mention
of that header in the camel-netty-http or camel-netty4-http documentation (I opened https://issues.apache.org/jira/browse/CAMEL-8370
to address that).  So the following code works to set the response code and message body:

      exchange.getOut().setBody(myResponseBody);
      exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, 401);


On Feb 17, 2015, at 11:34 AM, Mark Mindenhall <mark.mindenhall@machineshop.io> wrote:

> I’m using the camel-netty4-http (latest 2.14.2-SNAPSHOT) component to create an endpoint
that receives an HTTP POST from a device, translates the message from binary to JSON, then
sends it along to a kafka topic for further processing.  When there are errors in validating
or translating the incoming message, I need to be able to return a HTTP response code and
response body.
> 
> The camel-netty4-http documentation has an “Access to Netty types” section, which
says that I should be able to do the following to access the instance of io.netty.handler.codec.http.HttpResponse:
> 
>     HttpResponse response = exchange.getIn(NettyHttpMessage.class).getHttpResponse();
> 
> Regardless of where I access the exchange within the route, getHttpResponse() is always
returning null.  On the "happy path” I can return 200 by calling exchange.getOut().setBody(myResponse),
but I have been unable to figure out how to return another response code.
> 
> Is this a bug?  Or is there another way to accomplish what I’m trying to do?
> 
> Thanks,
> Mark
> 


Mime
View raw message