users@jersey.java.net

Re: [Jersey] _at_FormParam, @Context HttpContext and @Context HttpServletRequest

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Wed, 31 Mar 2010 10:21:57 +0200

On Mar 30, 2010, at 5:38 PM, Marc Hadley wrote:

> On Mar 30, 2010, at 11:27 AM, Alexandru Popescu ☀ wrote:
>
>> I guess that means it is a custom Jersey Entity @Provider. I'd prefer
>> to continue using as much as possible spec only features and see how
>> that goes.
>>
> This is required by JAX-RS, see:
>
> https://jsr311.dev.java.net/nonav/releases/1.0/spec/
> spec3.html#x3-460004.2.4
>
> Provided you are using the application/x-www-form-urlencoded media
> type then MultivaluedMap<String, String> is supported as a type for
> the entity parameter.
>

Yes.

The following should also work, but getRequest().getFormParameters()
is really designed for use with filters:

    @POST
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    public Response mset(@Context HttpContext httpContext,
@FormParam("nx") String nx) {
        Form form= httpContext.getRequest().getFormParameters();

        assertNotEmpty(form); // <== this FAILS, even if I'm 100% form
data is submitted
    }

Are you using the latest version of Jersey?

Paul.

> Marc.
>
>> Continuing my experiment I have discovered that removing the
>> @FormParam and keeping any of the @Context approaches seems to leave
>> the parameters on the request.
>>
>> I kind of correlated this behavior with the Chapter 6, 6.1. Servlet
>> Container details:
>>
>> An injected HttpServletRequest allows a resource method to stream the
>> contents of a request entity.
>> If the resource method has a parameter whose value is derived from
>> the
>> request entity then the stream will have already been consumed and an
>> attempt to access it MAY result in an exception.
>>
>> Servlet filters may trigger consumption of a request body by
>> accessing
>> request parameters. In a servlet container the @FormParam annotation
>> and the standard entity provider for application/x-www-form-
>> urlencoded
>> MUST obtain their values from the servlet request parameters if the
>> request body has already been consumed.
>>
>> Now, I must confess that there are a couple of things that are not
>> clear in the above paragraph:
>>
>> 1. what is the standard entity provider for application/x-www-form-
>> urlencoded?
>>
>> 2. the spec mentioned how @FormParam and entity provider must behave,
>> but there's no mention of the impact on the request. Should I
>> understand from the behavior I am seeing that the request body has
>> been consumed? Is this specific to Jersey impl?
>>
>> :- alex
>>
>>
>> 2010/3/30 Jose Javier García Zornoza <josejavier.garcia.zornoza_at_gmail.co
>> m>:
>>> I found it searching Jersey test code.
>>> You can see this same aproach here http://bit.ly/bMXJhU
>>> <https://jersey.dev.java.net/source/browse/jersey/>
>>>
>>> Alexandru Popescu ☀ escribió:
>>>> Thanks I'll try that. But I'm wondering where can I read about this
>>>> approach you are suggesting? I don't remember seeing it anywhere in
>>>> the spec and it doesn't look like using any of the @Context or
>>>> @XParam...
>>>>
>>>> :- alex
>>>>
>>>> 2010/3/30 Jose Javier García Zornoza <josejavier.garcia.zornoza_at_gmail.co
>>>> m>:
>>>>
>>>>> Hello.
>>>>> Try this:
>>>>>
>>>>> @POST
>>>>> @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
>>>>> public Response mset(MultivaluedMap<String, String> formParams) {
>>>>> ....
>>>>> }
>>>>>
>>>>>
>>>>>
>>>>> Alexandru Popescu ☀ escribió:
>>>>>
>>>>>> Hi all,
>>>>>>
>>>>>> I am not really sure if it is something wrong with the below
>>>>>> code or
>>>>>> just something I don't understand:
>>>>>>
>>>>>> @POST
>>>>>> @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
>>>>>> public Response mset(@Context HttpContext httpContext,
>>>>>> @FormParam("nx") String nx) {
>>>>>> Form form= httpContext.getRequest().getFormParameters();
>>>>>>
>>>>>> assertNotEmpty(form); // <== this FAILS, even if I'm 100%
>>>>>> form
>>>>>> data is submitted
>>>>>> }
>>>>>>
>>>>>>
>>>>>> @POST
>>>>>> @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
>>>>>> public Response mset(@Context HttpServletRequest request,
>>>>>> @FormParam("nx") String nx) {
>>>>>> Map params= request.getParameterMap();
>>>>>>
>>>>>> assertNotEmpty(form); // <== this FAILS, even if I'm 100%
>>>>>> form
>>>>>> data is submitted
>>>>>> }
>>>>>>
>>>>>>
>>>>>> For both above cases, if I'm using @FormParam-s the form
>>>>>> parameters
>>>>>> are correctly retrieved. The problem I'm trying to solve is that
>>>>>> my service is not aware beforehand of all form parameters so it
>>>>>> needs
>>>>>> to retrieve all of them.
>>>>>>
>>>>>>
>>>>>> For current code I am using Grizzly 1.9.18 with the following
>>>>>> setup:
>>>>>>
>>>>>> SelectorThread threadSelector =
>>>>>> GrizzlyWebContainerFactory.create(BASE_URI, initParams);
>>>>>>
>>>>>>
>>>>>> Any ideas what I'm doing wrong?
>>>>>>
>>>>>> Many thanks in advance,
>>>>>>
>>>>>> :- alex
>>>>>>
>>>>>> ---------------------------------------------------------------------
>>>>>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>>>>>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>> ---------------------------------------------------------------------
>>>>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>>>>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>>>>
>>>>>
>>>>>
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>>>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>>>
>>>>
>>>>
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>>
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>