2 Replies Latest reply on May 30, 2016 12:10 AM by sonyjop

    cxf:rsServer - to respond with custom http code

    sonyjop

      Hello

       

      My configuration is as below.

       

      JBoss Fuse 6.2.1 - I run a bundle which exposes a rest server using cxf:rsServer.

       

      I want to respond with a custom HTTP response code in case if there is an exception in my route.

       

      I tried setting "CamelHttpServletResponse" header to my own response coee, say 1001. But it does not have any particular effect on the response from the component.

       

      Is it possible to send custom htt response code from camel route?

      If possible, is there a particular way that I accomplish this?

        • 1. Re: cxf:rsServer - to respond with custom http code
          vrlgohel

          Did you try setting the Exchange headers as,

          exchange.getOut.setHeader.(Exchange.HTTP_RESPONSE_CODE, 1001) ?

          • 2. Re: cxf:rsServer - to respond with custom http code
            sonyjop

            Yes I tried. I think that does not work. Instead, there is another approach that worked.

             

            I wrote a custom processor as below: Improvements are required which I am yet to do

             

            public class RSResponseProcessor implements Processor {


            @Override

            public void process(Exchange exchange) throws Exception {

            Message message = exchange.getIn();

                    Response response = convertToJaxRs(message);

                    exchange.getIn().setBody(response);

                    exchange.getIn().setHeader("Test", "Won't work unless DefaultCxfRsBinding is not replaced with a custom one");



            }

            private Response convertToJaxRs(Message message)

                {

                   // ResponseBuilder jaxrsResponseBuilder = Response.ok(message.getBody(), MediaType.APPLICATION_JSON);

            ResponseBuilder jaxrsResponseBuilder = Response.status(400);

            jaxrsResponseBuilder.entity(" { \"message\" : \"There was a problem with the incoming message\" }");

            jaxrsResponseBuilder.type(MediaType.APPLICATION_JSON);

            //Response.

            //jaxrsResponseBuilder.

                   // jaxrsResponseBuilder.header("header1", "you'll see this");

                    Response response = jaxrsResponseBuilder.build();

                    return response;

                }

             

            }