Return-Path: Mailing-List: contact commons-user-help@jakarta.apache.org; run by ezmlm Delivered-To: mailing list commons-user@jakarta.apache.org Received: (qmail 2552 invoked from network); 9 Feb 2003 17:41:21 -0000 Received: from roadrunner.proinet.pl (HELO proinet.pl) (213.25.190.113) by daedalus.apache.org with SMTP; 9 Feb 2003 17:41:21 -0000 Received: from celeron (mimo-local [192.168.4.117]) by proinet.pl (8.11.6/8.9.3) with SMTP id h19HgS013137 for ; Sun, 9 Feb 2003 18:42:29 +0100 Message-ID: <002201c2d062$9f765520$7504a8c0@celeron> From: =?windows-1250?Q?Micha=B3_Mosiewicz?= To: Subject: Commons-fileupload patch: Added java.servlet.Filter implementation Date: Sun, 9 Feb 2003 18:42:31 +0100 MIME-Version: 1.0 Content-Type: text/plain; charset="windows-1250" Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2600.0000 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000 X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N Index: org/apache/commons/fileupload/MultipartFilter.java =================================================================== RCS file: org/apache/commons/fileupload/MultipartFilter.java diff -N org/apache/commons/fileupload/MultipartFilter.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ org/apache/commons/fileupload/MultipartFilter.java 9 Feb 2003 17:39:39 -0000 @@ -0,0 +1,109 @@ +package org.apache.commons.fileupload; + +import java.io.File; +import java.io.IOException; +import java.util.Enumeration; + +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletRequestWrapper; +import javax.servlet.http.HttpServletResponse; + + +/** + * + *

Filter for automatic file upload processing. it + * wrapper HttpServletRequest to pass any form parameters + * in usuall form (normally they are not accessible cause JSDK + * web container are not required to implement RFC1867).

+ * + *

FileItem access is done through request attributes. By default + * they are named after the form field name. However you are allowed + * to supply attributePrefix and/or attributeSuffix that will + * be used to "decorate" the attribute name.

+ * + * @author Mike Mosiewicz + */ +public class MultipartFilter implements Filter { + + + FilterConfig config = null; + + FileUpload fileUpload = new FileUpload(); + + String attribPrefix=null, attribSuffix = null; + + + /** + * @see javax.servlet.Filter#init(FilterConfig) + */ + public void init(FilterConfig arg0) throws ServletException { + config = arg0; + File tdir = (java.io.File) config.getServletContext().getAttribute("javax.servlet.context.tempdir"); + if( tdir == null) { + throw new ServletException("This web container is not JSDK 2.2 compliant. It doesn't define javax.servlet.context.tempdir mandatory attribute."); + } + tdir = new File(tdir, "upload.tmp"); + if( ! tdir.exists()) + tdir.mkdirs(); + fileUpload.setRepositoryPath( + tdir.getAbsolutePath() + ); + String sizeMax = config.getInitParameter("sizeMax"); + if( sizeMax != null) + try { + fileUpload.setSizeMax(Integer.parseInt(sizeMax)); + } catch (Exception fooOnAnyProblems) { + } + else + fileUpload.setSizeMax(2048000); + + String threshold = config.getInitParameter("sizeThreshold"); + if( threshold != null) + try { + fileUpload.setSizeThreshold(Integer.parseInt(threshold)); + } catch (Exception fooOnAnyProblems) { + } + else + fileUpload.setSizeThreshold(4096); + + attribPrefix = config.getInitParameter("attributePrefix"); + attribSuffix = config.getInitParameter("attributeSuffix"); + + } + + /** + * @see javax.servlet.Filter#doFilter(ServletRequest, ServletResponse, FilterChain) + */ + public void doFilter( + ServletRequest req, + ServletResponse res, + FilterChain chain) + throws ServletException, IOException { + HttpServletRequest request = (HttpServletRequest) req; + HttpServletResponse response = (HttpServletResponse) res; + if( FileUpload.isMultipartContent(request)) { + try { + request = new RequestWrapper(request, fileUpload.parseRequest(request), attribPrefix, attribSuffix); + } catch (FileUploadException fx) { + throw new ServletException("Upload exception caught", fx); + } + } + chain.doFilter(request,response); + + } + + + + /** + * @see javax.servlet.Filter#destroy() + */ + public void destroy() { + } + +} Index: org/apache/commons/fileupload/RequestWrapper.java =================================================================== RCS file: org/apache/commons/fileupload/RequestWrapper.java diff -N org/apache/commons/fileupload/RequestWrapper.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ org/apache/commons/fileupload/RequestWrapper.java 9 Feb 2003 17:39:39 -0000 @@ -0,0 +1,103 @@ +package org.apache.commons.fileupload; + +import java.text.MessageFormat; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletRequestWrapper; + +/** + * + *

This class is a simple request wrapper to provide standard access + * to parameters passed in multipart request.

+ *

It is used internally by filter.

+ * + * @author Mike Mosiewicz + */ +public class RequestWrapper extends HttpServletRequestWrapper { + + List items = null; + HashMap params = new HashMap(); + private void addValue(String name, String value) { + String[] x = (String[]) params.get(name); + if( x == null) { + params.put( name, new String[] {value}); + } else { + //in real life it doesn't happen to frequently + //so we can recreate arrays in case of multivalued + //field - in summary it should be cheapper than + //using collections + String[] n = new String[x.length+1]; + System.arraycopy(x,0,n,0,x.length); + n[x.length] = value; + params.put( name, n); + } + } + + protected RequestWrapper( + final HttpServletRequest request, + final List items, + String attribPrefix, + String attribSuffix) { + super(request); + this.items = items; + if( attribPrefix == null) + attribPrefix = ""; + if( attribSuffix == null) + attribSuffix = ""; + for(Iterator it = items.iterator(); + it.hasNext(); ) { + FileItem item = (FileItem) it.next(); + if( item.isFormField()) { + addValue( item.getFieldName(), item.getString() ); + } else { + request.setAttribute(attribPrefix + item.getFieldName() + attribSuffix, item); + } + + } + } + + /** + * @see javax.servlet.ServletRequest#getParameter(String) + */ + public String getParameter(String arg0) { + String[] values = (String[]) params.get(arg0); + if( values != null) + return values[0]; + return null; + } + + /** + * @see javax.servlet.ServletRequest#getParameterMap() + */ + public Map getParameterMap() { + return params; + } + + /** + * @see javax.servlet.ServletRequest#getParameterNames() + */ + public Enumeration getParameterNames() { + return + java.util.Collections.enumeration( + params.keySet() + ); + } + + + + /** + * @see javax.servlet.ServletRequest#getParameterValues(String) + */ + public String[] getParameterValues(String arg0) { + return (String[]) params.get(arg0); + } + +} Index: org/apache/commons/fileupload/package.html =================================================================== RCS file: /home/cvspublic/jakarta-commons/fileupload/src/java/org/apache/commons/fileu pload/package.html,v retrieving revision 1.1 diff -u -r1.1 package.html --- org/apache/commons/fileupload/package.html 11 Apr 2002 06:03:19 -0000 1.1 +++ org/apache/commons/fileupload/package.html 9 Feb 2003 17:39:39 -0000 @@ -62,5 +62,18 @@ the given location. If it cannot be renamed, it is streamed to the new location.

+ +

MultipartFilter

+ +

{@link org.apache.commons.fileupload.MultipartFilter MultipartFilter} class + is a JSDK 2.3+ compliant filter. Basic usage pattern is placing it into your + web.xml config. Parameters:

+ +
    +
  • sizeMax maximum allowed size of uploaded file +
  • sizeThreshold maximum allowed size of in-memory file, if greater + it is stored on disk +
+