You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Piotr Gaertig <ge...@o2.pl> on 2005/04/25 00:59:35 UTC

[fileupload] class contribution - HttpServletRequest wrapping

Hello,

Current FileUpload component involves user in current code changes or
usage of the nonstandard semantics of the parameter retrieval. That would be
nightmare for projects based on huge forms, form digesters (struts) or 
just maintenanced projects.
So I developed small class, which is remedy for presented problems.

http://gertas.art.pl/res/files/UploadHttpServletRequest.java

It just simply inherits the HttpServletRequestWrapper class,
so it's Servlets version independent (2.3+).

I'm also using latest FileUpload codebase from the ASF public SVN.

A brief description:

 * <p>This class is "multipart" extension to servlets'
 * <code>HttpServletRequest</code>, each instance wraps a genuine 
request object
 * containing multipart data and acts as a traditional POST request allowing
 * access to parameters in standarized way.</p>

.. the other details in the class file.


An usage example:

        File tmp = new File(System.getProperty("java.io.tmpdir"));
        FileItemFactory fif = new DiskFileItemFactory(1*1024*1024,tmp);
        ServletFileUpload uploader = new ServletFileUpload(fif);
        uploader.setSizeMax(10*1024*1024);
        UploadHttpServletRequest request = new 
UploadHttpServletRequest(req,uploader);

You can even integrate it with the ServletFileUpload through following 
method:

    public UploadHttpServletRequest wrapRequest(HttpServletRequest request)
            throws FileUploadException {
        return new UploadHttpServletRequest(request, this);
    }
    //wrapRequest, convertRequest, extendRequest or whatever

Here are few use cases:
 * <ul>
 *  <li> wrap a request at the begining of servlet's doPost and use
 *     the extended request</li>
 *  <li> wrap a request in a servlet filter, so all following servlets 
will be given
 *    the extended request with no code change, casting is necessary
 *   only in code processing files</li>
 *  <li> wrap before RequestDispatcher invocation, this is similar
      to the filter use case </li>
 * </ul>

... thus wrapper gains code reuse and reduces current code modifications.
I think this class can come in useful to many developers (as it was for me),
so it is my contribution to the Commons FileUpload component project.
The class and examples on this mail are licensed under the Apache License.

I will make some test cases for this class if you decide it's worth to
add to the project.

Best regards,
Piotr Gaertig



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org