commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From MichaƂ Mosiewicz <m...@proinet.pl>
Subject Commons-fileupload patch: Added java.servlet.Filter implementation
Date Sun, 09 Feb 2003 17:42:31 GMT
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;
+
+
+/**
+ *
+ * <p>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). </p>
+ *
+ * <p>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.</p>
+ *
+ * @author Mike Mosiewicz <mimo@proinet.pl>
+ */
+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;
+
+/**
+ *
+ * <p>This class is a simple request wrapper to provide standard access
+ * to parameters passed in multipart request.</p>
+ * <p>It is used internally by filter.</p>
+ *
+ * @author Mike Mosiewicz <mimo@proinet.pl>
+ */
+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.
       </p>
+
+      <h4>MultipartFilter</h4>
+
+      <p>{@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:</p>
+
+      <ul>
+       <li> <b>sizeMax </b> maximum allowed size of uploaded file
+       <li> <b>sizeThreshold</b> maximum allowed size of in-memory file,
if
greater
+       it is stored on disk
+      </ul>
+
    </body>
 </html>



Mime
View raw message