Return-Path: X-Original-To: apmail-camel-users-archive@www.apache.org Delivered-To: apmail-camel-users-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 0F252D257 for ; Tue, 24 Jul 2012 01:31:05 +0000 (UTC) Received: (qmail 52471 invoked by uid 500); 24 Jul 2012 01:31:04 -0000 Delivered-To: apmail-camel-users-archive@camel.apache.org Received: (qmail 52442 invoked by uid 500); 24 Jul 2012 01:31:04 -0000 Mailing-List: contact users-help@camel.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: users@camel.apache.org Delivered-To: mailing list users@camel.apache.org Received: (qmail 52433 invoked by uid 99); 24 Jul 2012 01:31:04 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 24 Jul 2012 01:31:04 +0000 X-ASF-Spam-Status: No, hits=1.5 required=5.0 tests=HTML_MESSAGE,RCVD_IN_DNSWL_LOW,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of willem.jiang@gmail.com designates 209.85.214.45 as permitted sender) Received: from [209.85.214.45] (HELO mail-bk0-f45.google.com) (209.85.214.45) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 24 Jul 2012 01:30:59 +0000 Received: by bkcji1 with SMTP id ji1so5556751bkc.32 for ; Mon, 23 Jul 2012 18:30:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=p/Ac/Hr8sK/ZCImUUsuZdc/fWKXS3O3DPWtTCdL9uKk=; b=iRxVDh9FJpjWDg7A0lTDlLii/bRM7AR5ZPjm9pFmSfYLMOmYuGW1i1PJ50DK5jL7Ld 9S4lHGBtDF/lpWLwrlXCgYfeYmDXy0j5O1L3nCekJ/pW/kbzVKYrDxa3UW/Go9qzbkoD Oj1yutqkTE7RRsQl4XePTNkHsAJaH43eSN/awYHEO1IQH5dnac/23HaQYWsujAwakKL9 8JFEVzqiTqMfFX9iApVvW2K4eVnTeAr405ZyNbjNWIYG8m8jzO4qF0Zla7kh610QQdfG hHYzjgifPgyMBa4qbT5BnhGPa7+R/fnXdsTKROHXP7xbWcOUP2CBn0dQHkKqH2VK3ddm WmPw== MIME-Version: 1.0 Received: by 10.205.126.15 with SMTP id gu15mr8780658bkc.134.1343093437948; Mon, 23 Jul 2012 18:30:37 -0700 (PDT) Received: by 10.204.231.194 with HTTP; Mon, 23 Jul 2012 18:30:37 -0700 (PDT) In-Reply-To: References: <500D795F.2030603@gmail.com> Date: Tue, 24 Jul 2012 09:30:37 +0800 Message-ID: Subject: Re: How to handle http status code From: Willem Jiang To: users@camel.apache.org Content-Type: multipart/alternative; boundary=000e0ce0d26a44e2be04c5894f8c X-Virus-Checked: Checked by ClamAV on apache.org --000e0ce0d26a44e2be04c5894f8c Content-Type: text/plain; charset=ISO-8859-1 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 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 response = new > ResponseEntity(body, > HttpStatus.valueOf(responseCode)); > exchange.getIn().setBody(response); > } > }); > } > ////////////////////////////////////////////////////// > The calling code: > > ResponseEntity response = (ResponseEntity) > producerTemplate.requestBodyAndHeaders(null, headers); > return getDto(response); > > /////////////////////////////////////////////////////// > > > On 7/23/12, Christoph Emmersberger 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 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 > >>> > >>> > > > > > --000e0ce0d26a44e2be04c5894f8c--