users@jersey.java.net

Re: [Jersey] need help with parsing multipart/form-data input ( NullPointerException in 1.0.1-SNAPSHOT )

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Fri, 07 Nov 2008 18:01:30 +0100

Hi,

Switching over to the users list as that is where all the action
happens.

As a work around change the following:

   @FormParam( "protocol" ) int protocol

to

   @FormParam( "protocol" ) String protocol

The problem is that when the "multipart/form-data" media type is used
the registered message body readers are utilized and by default there
are none capable of reading bytes to Java primitive types.


I am working on a fix so that if a message body reader is not found
the following happens:

1) create an "extractor" following the rules of @*Param; and

2) if an extractor in 1) exists apply that to the UTF-8 decoded String
of the body part.


BTW even with the proposed fix in place it will fail for your set up
because the body part of "protocol" is "0.1" and thus parsing it as an
integer will result in a number format exception.

Paul.

On Nov 7, 2008, at 5:19 PM, moiz_at_dohadwala.net wrote:

> Hello,
>
> I am working on migrating a (legacy!) PHP server to a RESTful one
> using jersey. However, the server needs to be backwards compatible
> with existing clients ( 1000s ) until they upgrade. The current
> client perform a POST operation, listed below. The problem I have is
> that with Jersey 1.0, I get null values for the @FormData annotated
> method parameters. With jersey 1.0.1-SNAPSHOT deployed yesterday, I
> get a NullPointerException. I have also listed my resource below
> along with the stacktrace from the 1.0.1-SNAPSHOT.
>
> Post sample:
>
> POST /Subscribe/list1 HTTP/1.1
> User-Agent: Player|Windows|1.5.0.17874|https://staging.ivy-qa.com/
> Host: staging.ivy-qa.com
> Accept: */*
> Content-Length: 395
> Expect: 100-continue
> Content-Type: multipart/form-data;
> boundary=----------------------------7aaff3ab83ee
>
> HTTP/1.1 100 Continue
> ------------------------------7aaff3ab83ee
> Content-Disposition: form-data; name="engine_id"
>
> e23f219e-a1e4-4037-b257-f1c488b49a5d
> ------------------------------7aaff3ab83ee
> Content-Disposition: form-data; name="list"
>
> list1
> ------------------------------7aaff3ab83ee
> Content-Disposition: form-data; name="protocol"
>
> 0.1
> ------------------------------7aaff3ab83ee--
>
>
> Resource source:
> package com.corp.m5es.rest.resources.list;
>
> import javax.ws.rs.Consumes;
> import javax.ws.rs.FormParam;
> import javax.ws.rs.POST;
> import javax.ws.rs.Path;
> import javax.ws.rs.PathParam;
> import javax.ws.rs.Produces;
>
> import
> com
> .sun
> .xml.internal.messaging.saaj.packaging.mime.internet.MimeMultipart;
>
> @Path("/SubscribeList/{listId}")
> @Consumes("multipart/form-data")
> @Produces("application/x-www-form-urlencoded")
> public class ListSubscription
> {
> @POST
> @Consumes("multipart/form-data")
> public String createSubscription(
> @PathParam("listId") String listId,
> @FormParam( "engine_id" ) String engineId,
> @FormParam( "list" ) String formListId,
> @FormParam( "protocol" ) int protocol
> )
> {
> System.out.println(listId);
> System.out.println(engineId);
> System.out.println(formListId);
> System.out.println(protocol);
> return "test";
> }
> }
> Only the @PathParam argument has the right value. All other
> @FormParam annotated arguments are nulls or 0s.
>
> Here's the exception, if I use 1.0.1-SNAPSHOT version of jersey-
> multipart:
>
> Starting grizzly...
> Nov 7, 2008 8:16:34 AM com.sun.grizzly.http.servlet.ServletAdapter
> service
> SEVERE: service exception:
> javax.servlet.ServletException:
> com.sun.jersey.api.container.ContainerException: Exception injecting
> parameters to Web resource method
> at
> com
> .sun
> .jersey
> .spi
> .container.servlet.ServletContainer.service(ServletContainer.java:351)
> at javax.servlet.http.HttpServlet.service(HttpServlet.java:831)
> at
> com
> .sun
> .grizzly.http.servlet.FilterChainImpl.doFilter(FilterChainImpl.java:
> 141)
> at
> com
> .sun.grizzly.http.servlet.ServletAdapter.service(ServletAdapter.java:
> 220)
> at
> com
> .sun.grizzly.tcp.http11.GrizzlyAdapter.service(GrizzlyAdapter.java:
> 148)
> at
> com
> .sun
> .grizzly
> .http.DefaultProcessorTask.invokeAdapter(DefaultProcessorTask.java:
> 621)
> at
> com
> .sun
> .grizzly
> .http.DefaultProcessorTask.doProcess(DefaultProcessorTask.java:552)
> at
> com
> .sun
> .grizzly.http.DefaultProcessorTask.process(DefaultProcessorTask.java:
> 800)
> at
> com
> .sun
> .grizzly
> .http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:152)
> at
> com
> .sun
> .grizzly
> .DefaultProtocolChain
> .executeProtocolFilter(DefaultProtocolChain.java:137)
> at
> com
> .sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:
> 104)
> at
> com
> .sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:
> 90)
> at com.sun.grizzly.http.SelectorThread
> $1.execute(SelectorThread.java:649)
> at
> com
> .sun
> .grizzly
> .ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:67)
> at
> com
> .sun
> .grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:56)
> at
> com.sun.grizzly.util.WorkerThreadImpl.run(WorkerThreadImpl.java:169)
> Caused by: com.sun.jersey.api.container.ContainerException:
> Exception injecting parameters to Web resource method
> at com.sun.jersey.impl.model.method.dispatch.FormDispatchProvider
> $FormParamInInvoker.getParams(FormDispatchProvider.java:99)
> at com.sun.jersey.impl.model.method.dispatch.FormDispatchProvider
> $TypeOutInvoker._dispatch(FormDispatchProvider.java:128)
> at
> com
> .sun
> .jersey
> .impl
> .model
> .method
> .dispatch
> .ResourceJavaMethodDispatcher
> .dispatch(ResourceJavaMethodDispatcher.java:67)
> at
> com
> .sun.jersey.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:
> 124)
> at
> com
> .sun
> .jersey
> .impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:71)
> at
> com
> .sun
> .jersey
> .impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:111)
> at
> com
> .sun
> .jersey
> .impl
> .uri
> .rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:63)
> at
> com
> .sun
> .jersey
> .impl
> .application
> .WebApplicationImpl._handleRequest(WebApplicationImpl.java:551)
> at
> com
> .sun
> .jersey
> .impl
> .application
> .WebApplicationImpl.handleRequest(WebApplicationImpl.java:510)
> at
> com
> .sun
> .jersey
> .impl
> .application
> .WebApplicationImpl.handleRequest(WebApplicationImpl.java:501)
> at
> com
> .sun
> .jersey
> .spi
> .container.servlet.ServletContainer.service(ServletContainer.java:349)
> ... 15 more
> Caused by: com.sun.jersey.api.container.ContainerException:
> java.lang.NullPointerException
> at
> com
> .sun.jersey.impl.model.method.dispatch.MultipartFormDispatchProvider
> $
> MultipartFormParamInjectable
> .getValue(MultipartFormDispatchProvider.java:146)
> at com.sun.jersey.impl.model.method.dispatch.FormDispatchProvider
> $FormParamInInvoker.getParams(FormDispatchProvider.java:93)
> ... 25 more
> Caused by: java.lang.NullPointerException
> at
> com
> .sun.jersey.impl.model.method.dispatch.MultipartFormDispatchProvider
> $
> MultipartFormParamInjectable
> .getAsMultipartFormData(MultipartFormDispatchProvider.java:186)
> at
> com
> .sun.jersey.impl.model.method.dispatch.MultipartFormDispatchProvider
> $
> MultipartFormParamInjectable
> .getValue(MultipartFormDispatchProvider.java:143)
> ... 26 more
>
> Thanks,
>
> -Moiz
>
>