camel-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Willem Jiang <willem.ji...@gmail.com>
Subject Re: How to handle http status code
Date Tue, 24 Jul 2012 01:30:37 GMT
Here is some tricks for accessing message header and body.
In the Processor, you can access the message body from InMessage, and You
can also set the  OutMessage body and header. If you doen't set the
OutMessage, Camel will take the InMessage as the InMessage for next
processor, otherwise it will take the OutMessage as the InMessage.
If you set the OutMessage body, you should also copy the header of
InMessage to the OutMessage.

On Tue, Jul 24, 2012 at 3:12 AM, bitter geek <bittergeek@gmail.com> wrote:

> Hi Christoph,
>
> Thanks for your help!
>
> I managed to get it to work based on you code with some changes as shown
> below.
>
> One change is to call process after to(endpoint). The reponse I need
> to handle is that from to(endpoint).
> After this change, the getOut() on exchange returns null, so I have to
> set the body on in. (why is it null?)
>
> I then create a responseEntity object so the return value from
> requestBodyAndHeaders will have both the status and response text
> ready to use.
>
> Do you think if this is the right approach to my problem?
>
> Thanks again!
> ////////////////////////////////////////////////////////////////////
> @Override
>         public void configure() throws Exception {
>                 from("direct:GetCustomer")
>                 .setHeader("Content-Type",
> constant("application/x-www-form-urlencoded"))
>                 .setHeader(Exchange.HTTP_METHOD, constant("GET"))
>                 .setHeader(CxfConstants.CAMEL_CXF_RS_USING_HTTP_API,
> constant(Boolean.TRUE))
>                 .setHeader(Exchange.HTTP_PATH,
> simple("${header.httpPath}"))
>                 .setHeader(Exchange.HTTP_QUERY,
>                                 simple(getURIComponentQuery()))
>                 .to(endpoint)
>                 .process(new Processor() {
>                         @Override
>                         public void process(final Exchange exchange)
> throws Exception {
>                                 int responseCode =
> exchange.getIn().getHeader(Exchange.HTTP_RESPONSE_CODE, int.class);
>                                 String body =
> exchange.getIn().getBody(String.class);
>                                 ResponseEntity<String> response = new
> ResponseEntity<String>(body,
> HttpStatus.valueOf(responseCode));
>                                 exchange.getIn().setBody(response);
>                         }
>                 });
>         }
> //////////////////////////////////////////////////////
> The calling code:
>
> ResponseEntity<String> response = (ResponseEntity<String>)
> producerTemplate.requestBodyAndHeaders(null, headers);
>                 return getDto(response);
>
> ///////////////////////////////////////////////////////
>
>
> On 7/23/12, Christoph Emmersberger <cemmersb@googlemail.com> wrote:
> > Hi Bing,
> >
> > simple question, why don't you write e.g. a processor that is configured
> to
> > your route. Your route could look like thos:
> >
> > /** Processor defined as spring bean, e.g. via @Named tag */
> > @Inject
> > MyHeaderExtractor myHeaderExtractor;
> >
> > public void configure() throws Exceltion {
> >       from("direct:GetCustomer")
> >               .setHeader("direct:GetCustomer")
> >               // ... continue with your setter to the header
> >       .process(myHeaderExtractor)
> >       .to(endpoint);
> > }
> >
> > Your header extractor can than implement the processor interface which
> > forces you to implement a process(Exchange exchange) method.
> >
> > You could also decide simply using the bean processing capabilities of
> camel
> > as an alternative approach.
> >
> > @Named
> > public class MyHeaderExtractor extends Processor {
> >
> >       public void process(Exchange exchange) {
> >               exchange.getIn().getHeaders(); // returns the header map
> on the in
> > exchange
> >               exchange.getIn().getBody(); // returns the body on the in
> exchange
> >               // Here you can add some specific header extractions for
> whatever you need
> > it
> >               exchange.getOut().getHeaders(); // Would give you the out
> header
> >       }
> > }
> >
> > Hope this helps, best regards,
> >
> > Christoph
> >
> >
> >
> > On Jul 23, 2012, at 7:06 PM, bitter geek wrote:
> >
> >> Hi Sergey,
> >>
> >> Thanks for the reply.
> >>
> >> I'm quite new to Camel. I knew you can get it from the out message but
> >> how do I access the out message from the code snippet I provided?
> >>
> >> I'd like to know what is the best way to access both the status and
> >> the response message as a string.
> >>
> >> On 7/23/12, Sergey Beryozkin <sberyozkin@gmail.com> wrote:
> >>> Hi
> >>> On 23/07/12 16:43, bitter geek wrote:
> >>>> Hi All,
> >>>>
> >>>> I have a router builder for a rest service with configure override
> >>>> like the following:
> >>>> ...
> >>>> @Override
> >>>>    public void configure() throws Exception {
> >>>>            from("direct:GetCustomer")
> >>>>            .setHeader("Content-Type",
> >>>> constant("application/x-www-form-urlencoded"))
> >>>>            .setHeader(Exchange.HTTP_METHOD, constant("GET"))
> >>>>            .setHeader(CxfConstants.CAMEL_CXF_RS_USING_HTTP_API,
> >>>> constant(Boolean.TRUE))
> >>>>            .setHeader(Exchange.HTTP_PATH,
> simple("${header.httpPath}"))
> >>>>            .setHeader(Exchange.HTTP_QUERY,
> >>>>                            simple(getURIComponentQuery()))
> >>>>            .to(endpoint);
> >>>>    }
> >>>> ...
> >>>>
> >>>>
> >>>> The rest service returns an xml document. I don't have the xsd for it
> >>>> so would like to set the type of the response to String via
> >>>> convertBodyTo(String.class).
> >>>>
> >>>> But once I do this, producerTemplate.requestBodyAndHeaders(null,
> >>>> headers) returns a String object. So I lose the status code.
> >>>>
> >>>> Noting converting it to string will let me access the status code but
> >>>> then I no longer have the text body available.
> >>>>
> >>>> Any idea how to get both the status code and the text body properly?
> >>>>
> >>>> Thanks a lot for the help!
> >>> I can see from the code that a status code is set as
> >>> Exchange.HTTP_RESPONSE_CODE header on the out message, so you should be
> >>> able to get it from there
> >>>
> >>> Cheers, Sergey
> >>>
> >>>>
> >>>> Bing
> >>>
> >>>
> >
> >
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message