users@jersey.java.net

[Jersey] Re: Problems handling multipart/form-data input

From: Marko Asplund <marko.asplund_at_gmail.com>
Date: Thu, 30 Sep 2010 23:15:19 +0300

Hi,

I managed to find out what was causing the problem:
mimepull.jar was missing from the classpath which resulted in the
dependent MessageBodyReader implementation
(com.sun.jersey.multipart.impl.MultiPartReaderServerSide) registration to
fail.
After adding mimepull.jar to the classpath things started working.
Reading through the Jersey 1.4 user guide again, I see that the dependency
was actually documented.

I think that the documentation could be improved, though, by adding a
section on troubleshooting and/or logging. Potential topics to include would
be:
- configuring java.util.logging for Jersey and changing the log levels
- running the resource / service using an embedded container (e.g. Grizzly
Web container [https://jersey.dev.java.net/use/getting-started.html] or the
test framework)

Another thing to consider is whether the log message that reports
registration issues like this should be logged using higher level. It seems
to me that a case like this would typically result from an error of some
sort and should not be silently ignored by default:

CONFIG: A dependent class, org/jvnet/mimepull/MIMEParsingException, of the
component class com.sun.jersey.multipart.impl.MultiPartReaderServerSide is
not found. The component is ignored.

I need to process multipart/form-data input that can contain both files as
well as simple, string parts.
What would be the best way to identify file body parts? Can I use the
MediaType.isCompatible method as in the code fragment below?
Is it safe to cast the entity to a BodyPartEntity below?
Can I get the file-based body part as an FileDataBodyPart instance somehow
(without manually creating one)?

private static final MediaType MIME_TYPE_FILE = new MediaType("application",
"octet-stream");
public String processMultipartPost(FormDataMultiPart mimeMultipartData)
throws IOException {
  for(BodyPart p : mimeMultipartData.getBodyParts()) {
    FormDataBodyPart bp = (FormDataBodyPart)p;
    if(MIME_TYPE_FILE.isCompatible(p.getMediaType())) { // identify
file-based body parts.
      BodyPartEntity bpe = (BodyPartEntity)p.getEntity(); // is this cast
safe?
      InputStream is = bpe.getInputStream();
    } else if(bp.isSimple()) {
      String name = bp.getName();
      String value = bp.getValue();
    }
  }
  return "ok";
}


thanks,

marko


Martin Matula wrote:
> Would you be able to send a test case we could debug?
> Martin