001: /*
002: * Coefficient - facilitates project based collaboration
003: * Copyright (C) 2003, Dylan Etkin, CSIR icomtek
004: * PO Box 395
005: * Pretoria 0001, RSA
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019:
020: package za.org.coefficient.util.common;
021:
022: import java.util.Arrays;
023: import java.util.Collections;
024: import java.util.Enumeration;
025: import java.util.HashMap;
026: import java.util.Iterator;
027: import java.util.Map;
028:
029: import javax.servlet.http.HttpServletRequest;
030:
031: import org.apache.commons.fileupload.DiskFileUpload;
032: import org.apache.commons.fileupload.FileItem;
033: import org.apache.commons.fileupload.FileUploadException;
034:
035: /**
036: * An implementation of multipart request that uses servlet
037: * requests to construct its data.
038: */
039: public class MultipartRequest extends BaseMultipartRequest {
040: //~ Static fields/initializers =============================================
041:
042: public static final int DEFAULT_SIZE_MAX = 4 * 1024 * 1024;
043:
044: //~ Instance fields ========================================================
045:
046: private final Map parameters = new HashMap();
047:
048: //~ Constructors ===========================================================
049:
050: /**
051: * Creates a new MultipartRequest with default size max.
052: * @param request the incoming http request
053: * @throws FileUploadException
054: */
055: public MultipartRequest(HttpServletRequest request)
056: throws FileUploadException {
057: this (request, DEFAULT_SIZE_MAX);
058: }
059:
060: /**
061: * Creates a new MultipartRequest.
062: * @param request the incoming http request
063: * @param sizeMax the maximum size a file can have
064: * @throws FileUploadException
065: */
066: public MultipartRequest(HttpServletRequest request, int sizeMax)
067: throws FileUploadException {
068:
069: DiskFileUpload upload = new DiskFileUpload();
070:
071: // Set upload parameters
072: upload.setSizeMax(sizeMax);
073:
074: // get multipart form and file elements
075: for (Iterator i = upload.parseRequest(request).iterator(); i
076: .hasNext();) {
077: FileItem item = (FileItem) i.next();
078: if (item.isFormField()) {
079: addParameter(item.getFieldName(), item.getString());
080: } else {
081: parameters.put(item.getFieldName(), new UploadedFile(
082: item.getContentType(), item.get(), item
083: .getName()));
084: }
085: }
086: }
087:
088: /**
089: * Adds a new parameter to the parameters map. If the name already exists
090: * an element is added to the back of the paramters array.
091: * @param name
092: * @param value
093: */
094: protected void addParameter(String name, String value) {
095: String[] existing = (String[]) parameters.get(name);
096: if (existing == null) {
097: parameters.put(name, new String[] { value });
098: } else {
099: String[] newValue = new String[existing.length + 1];
100: for (int i = 0; i < existing.length; i++) {
101: newValue[i] = existing[i];
102: }
103: newValue[existing.length] = value;
104: parameters.put(name, newValue);
105: }
106: }
107:
108: //~ Methods ================================================================
109:
110: /**
111: * Returns an uploaded file.
112: * @param name the name of the parameter
113: * @return the file or null if it does not exists
114: */
115: public UploadedFile getFileParameter(String name)
116: throws FileUploadException {
117: Object o = parameters.get(name);
118:
119: return (o instanceof UploadedFile) ? (UploadedFile) o : null;
120: }
121:
122: public String getParameter(String name) {
123: Object o = parameters.get(name);
124:
125: return (o instanceof String[]) ? ((String[]) o)[0] : null;
126: }
127:
128: public Map getParameterMap() {
129: return Collections.unmodifiableMap(parameters);
130: }
131:
132: public Enumeration getParameterNames() {
133: return Collections.enumeration(parameters.keySet());
134: }
135:
136: public String[] getParameterValues(String name) {
137: Object o = parameters.get(name);
138:
139: return (o instanceof String[]) ? (String[]) o : null;
140: }
141: }
|