You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Sebastian Münz <se...@catify.com> on 2010/10/28 11:24:25 UTC

Jetty Component: HttpServletRequest losts HTTP Post parameters

Hi,

by using Jetty Component and the receive of an HttpServletRequest, the 
request object losts the parameters submitted by the form by using http 
POST method.

I know the parameters are written to message header, but the 
HttpServletRequest instance losts the parameters in the parameters map 
=> httpServletRequest.getParameterMap(). In my case, the map is empty :(

If i know which parameters are sent, i can grap it from message header 
=> all fine!

But what should i do, if i don't know with parameters will be submitted 
by the form?

By the way, if i use http GET method in the form, all works fine and the 
map contains the parameters sent.

*Any hint to get know which parameters are sent by form, like 
httpServletRequest.getParameterNames()?*

Here is my test to attest my state:

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.spring.SpringRouteBuilder;
import org.apache.camel.test.CamelSpringTestSupport;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.springframework.context.support.AbstractXmlApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class JettyTest extends CamelSpringTestSupport {

    @Override
    protected AbstractXmlApplicationContext createApplicationContext() {
        return new 
ClassPathXmlApplicationContext("META-INF/spring/test-context.xml");
    }
   
    public void testReceivePostData() throws InterruptedException {
               
        final MockEndpoint mockEndpoint = 
context.getEndpoint("mock:httpRequestReceive", MockEndpoint.class);
       
        RouteBuilder rb = new SpringRouteBuilder() {
           
            @Override
            public void configure() throws Exception {
               
                from("jetty:http://localhost:8888/testreceivepostdata")
                .process(new Processor() {
                   
                    @Override
                    public void process(Exchange exchange) throws 
Exception {
                        HttpServletRequest request = 
exchange.getIn().getBody(HttpServletRequest.class);
                        Map parameters = request.getParameterMap();
                       
                        
context.createProducerTemplate().sendBody(mockEndpoint, parameters);
                    }
                });
            }
        };
       
        try {
            context.addRoutes(rb);
        } catch (Exception e) {
            e.printStackTrace();
            fail("Failed to add test route.");
        }
       
        HttpClient httpClient = new HttpClient();
       
        HttpMethod httpGetMethod = new 
GetMethod("http://localhost:8888/testreceivepostdata");       
        httpGetMethod.setQueryString("parameter1=testParam");
       
        try {
            httpClient.executeMethod(httpGetMethod);
        } catch (Exception e) {
            fail("Failed to execute method.");
        }
       
        assertEquals(1, mockEndpoint.getReceivedCounter());
        Exchange exchange = mockEndpoint.getExchanges().get(0);
        Map parameters = exchange.getIn().getBody(Map.class);
        assertNotNull(parameters);
        assertFalse("There are no parameters in HttpServletRequest.", 
parameters.isEmpty());
       
       
        HttpMethod httpPostMethod = new 
PostMethod("http://localhost:8888/testreceivepostdata");
        HttpMethodParams params = new HttpMethodParams();
        params.setParameter("parameter1", "testParam");
        httpPostMethod.setParams(params);
       
        try {
            httpClient.executeMethod(httpPostMethod);
        } catch (Exception e) {
            fail("Failed to execute method.");
        }
       
        assertEquals(2, mockEndpoint.getReceivedCounter());
        exchange = mockEndpoint.getExchanges().get(1);
        parameters = exchange.getIn().getBody(Map.class);
        assertNotNull(parameters);
        assertFalse("There are no parameters in HttpServletRequest.", 
parameters.isEmpty());
       
    }

}

Any hints are appreciated :) Thank you!

Sebastian

-- 
sebastian münz
__________________________________________

catify consulting
claus straube, sebastian münz

phone	+49-89-38157419-1
mobile  +49-171-1992074
web     http://www.catify.com
office  6. floor, heßstr. 56, 80798 munich
__________________________________________


Re: Jetty Component: HttpServletRequest losts HTTP Post parameters

Posted by Sebastian Münz <se...@catify.com>.
I use Camel in version 2.4.



Re: Jetty Component: HttpServletRequest losts HTTP Post parameters

Posted by Claus Ibsen <cl...@gmail.com>.
Hi

What version of Camel are you using? I think I recall something like
this fixed. I am not sure though.

If possible try a newer release and report back if its still a problem or fixed.


On Thu, Oct 28, 2010 at 12:45 PM, Sebastian Münz
<se...@catify.com> wrote:
> Okay, i get it!
>
>
> By converting the Exchange-Body I get the HTTP_QUERY String. So i get the
> information which parameters are sent.
>
> String exchangeBody = httpExchange.getIn().getBody(String.class);
>
> Result: property1=one&property2=two&property3=three&property4=four
>
>
> Nonetheless I think the parameter map should be in the HttpServletRequest
> after convertion! Isn't it a bug?
>
> Best regards - Sebastian
>



-- 
Claus Ibsen
-----------------
FuseSource
Email: cibsen@fusesource.com
Web: http://fusesource.com
Twitter: davsclaus
Blog: http://davsclaus.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/

Re: Jetty Component: HttpServletRequest losts HTTP Post parameters

Posted by Willem Jiang <wi...@gmail.com>.
You can't use the HttpServletRequest to get the request parameter map, 
as the HttpServletRequest input stream is consumed by camel if the 
request content type is "application/x-www-form-urlencoded".

It not a bug, you can consider that the HttpServletRequest's input 
stream can't be consumed twice.

On 10/28/10 6:45 PM, Sebastian Münz wrote:
> Okay, i get it!
>
>
> By converting the Exchange-Body I get the HTTP_QUERY String. So i get
> the information which parameters are sent.
>
> String exchangeBody = httpExchange.getIn().getBody(String.class);
>
> Result: property1=one&property2=two&property3=three&property4=four
>
>
> Nonetheless I think the parameter map should be in the
> HttpServletRequest after convertion! Isn't it a bug?
>
> Best regards - Sebastian
>


-- 
Willem
----------------------------------
FuseSource
Web: http://www.fusesource.com
Blog:    http://willemjiang.blogspot.com (English)
          http://jnn.javaeye.com (Chinese)
Twitter: willemjiang

Re: Jetty Component: HttpServletRequest losts HTTP Post parameters

Posted by Sebastian Münz <se...@catify.com>.
Okay, i get it!


By converting the Exchange-Body I get the HTTP_QUERY String. So i get 
the information which parameters are sent.

String exchangeBody = httpExchange.getIn().getBody(String.class);

Result: property1=one&property2=two&property3=three&property4=four


Nonetheless I think the parameter map should be in the 
HttpServletRequest after convertion! Isn't it a bug?

Best regards - Sebastian