You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Maynard Demmon <ma...@organic.com> on 2003/08/05 20:45:08 UTC

HttpServletRequestWrapper for FileUpload

I've written an HttpServletRequestWrapper for use with the file upload package. I didn't find a request wrapper in the file upload package itself, so I figure this might be of some use to others. I've included the code for the class below. If this seems useful to others, perhaps it might be included in the package itself.

Maynard

package rename.to.something.else;

import org.apache.commons.fileupload.*;
import java.util.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.log4j.Category;

/**
 * Wrapper for "multipart/form-data" requests.
 */
public class MultipartWrapper extends HttpServletRequestWrapper {
	
	// Logging
	/** Used by Log4j for logging. */
	static Category log = Category.getInstance(MultipartWrapper.class.getName());
	
	
	// Constants
	/** The size threshold beyond which files are written directly to disk in bytes. */
	private static final int SIZE_THRESHOLD = 0;
	
	/** The maximum allowed upload size in bytes. If negative, there is no maximum. */
	private static final long SIZE_MAX = 1024 * 1024 * 5;
	
	/** The location used to temporarily store files that are larger than the configured size threshold. */
	private static final String REPOSITORY_PATH = "/";
	
	
	// Instance Fields
	/** Stores a reference to the underlying Request object. */
	private HttpServletRequest request = null;
	
	/** A Map of Request parameter String and String[] objects. */
	private Hashtable parameters = null;
	
	/** A Map of FileItem objects. */
	private Hashtable file_items = null;
	
	
	// Constructors
	public MultipartWrapper(HttpServletRequest request) throws IOException {
		this(request, SIZE_THRESHOLD, SIZE_MAX, REPOSITORY_PATH);
	}
	
	public MultipartWrapper(
		HttpServletRequest request,
		int size_threshold,
		long size_max,
		String repository_path
	) throws IOException {
		super(request);
		this.request = request;
		
		DiskFileUpload upload = new DiskFileUpload();
		parameters = new Hashtable();
		file_items = new Hashtable();
		
		// Set upload parameters
		upload.setSizeThreshold(size_threshold);
		upload.setSizeMax(size_max);
		upload.setRepositoryPath(repository_path);
		
		// Parse the request
		try {
			List items = upload.parseRequest(request);
			Iterator iter = items.iterator();
			while (iter.hasNext()) {
					FileItem item = (FileItem) iter.next();
					
					if (item.isFormField()) {
							processFormField(item);
					} else {
							processUploadedFile(item);
					}
			}
		} catch (UnsupportedEncodingException uee) {
			log.error(uee.getMessage());
		} catch (FileUploadException fue) {
			log.error(fue.getMessage());
		}
	}
	
	private void processFormField(FileItem item) throws UnsupportedEncodingException {
		String field_name = item.getFieldName();
		String value = item.getString(request.getCharacterEncoding());
		
		Object obj = parameters.get(field_name);
		
		if (obj == null) {
			parameters.put(field_name, value);
		} else if (obj instanceof String) {
			String[] value_array = new String[2];
			value_array[0] = (String) obj;
			value_array[1] = value;
			parameters.put(field_name, value_array);
		} else if (obj instanceof String[]) {
			String[] old_value_array = (String[]) obj;
			String[] value_array = new String[old_value_array.length + 1];
			System.arraycopy(old_value_array, 0, value_array, 0, old_value_array.length);
			value_array[old_value_array.length] = value;
			parameters.put(field_name, value_array);
		}
	}
	
	private void processUploadedFile(FileItem item) {
		String field_name = item.getFieldName();
		File file_name = new File(item.getName());
		
		parameters.put(field_name, file_name.getPath());
		file_items.put(field_name, item);
	}
	
	
	// Overridden methods from HttpServletRequest
	public Enumeration getParameterNames() {
		return parameters.keys();
	}
	
	public String getParameter(String name) {
		Object obj = parameters.get(name);
		
		if (obj instanceof String) {
			return (String) obj;
		} else if (obj instanceof String[]) {
			// Might as well return the first item in the array rather than null.
			return ((String[]) obj)[0];
		} else {
			// Try the underlying request object since it may have query params
			return request.getParameter(name);
		}
	}
	
	public String[] getParameterValues(String name) {
		Object obj = parameters.get(name);
		
		if (obj instanceof String) {
			// Might as well return the String as a String[] rather than null.
			String[] retval = {(String) obj};
			return retval;
		} else if (obj instanceof String[]) {
			return (String[]) obj;
		} else {
			// Try the underlying request object since it may have query params
			return request.getParameterValues(name);
		}
	}
	
	public Map getParameterMap() {
		return parameters;
	}
	
	
	// File Accessors
	/**
	 * Gets the FileItem associated with the provided field name if it exists.
	 *
	 * @param field_name The name of the field to get the FileItem for.
	 *
	 * @return The associated FileItem if it exists, or Null if none exists.
	 */
	public FileItem getFileItem(String field_name) {
		return (FileItem) file_items.get(field_name);
	}
}

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