KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > za > org > coefficient > util > common > MultipartRequest


1 /*
2  * Coefficient - facilitates project based collaboration
3  * Copyright (C) 2003, Dylan Etkin, CSIR icomtek
4  * PO Box 395
5  * Pretoria 0001, RSA
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package za.org.coefficient.util.common;
21
22 import java.util.Arrays JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.Enumeration JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30
31 import org.apache.commons.fileupload.DiskFileUpload;
32 import org.apache.commons.fileupload.FileItem;
33 import org.apache.commons.fileupload.FileUploadException;
34
35
36 /**
37  * An implementation of multipart request that uses servlet
38  * requests to construct its data.
39  */

40 public class MultipartRequest extends BaseMultipartRequest {
41     //~ Static fields/initializers =============================================
42

43     public static final int DEFAULT_SIZE_MAX = 4 * 1024 * 1024;
44
45     //~ Instance fields ========================================================
46

47     private final Map JavaDoc parameters = new HashMap JavaDoc();
48
49     //~ Constructors ===========================================================
50

51     /**
52      * Creates a new MultipartRequest with default size max.
53      * @param request the incoming http request
54      * @throws FileUploadException
55      */

56     public MultipartRequest(HttpServletRequest JavaDoc request)
57         throws FileUploadException {
58         this(request, DEFAULT_SIZE_MAX);
59     }
60
61     /**
62      * Creates a new MultipartRequest.
63      * @param request the incoming http request
64      * @param sizeMax the maximum size a file can have
65      * @throws FileUploadException
66      */

67     public MultipartRequest(HttpServletRequest JavaDoc request, int sizeMax)
68         throws FileUploadException {
69
70         DiskFileUpload upload = new DiskFileUpload();
71
72         // Set upload parameters
73
upload.setSizeMax(sizeMax);
74
75         // get multipart form and file elements
76
for (Iterator JavaDoc i = upload.parseRequest(request).iterator(); i.hasNext();) {
77             FileItem item = (FileItem) i.next();
78             if (item.isFormField()) {
79                 addParameter(item.getFieldName(), item.getString());
80             } else {
81                 parameters.put(item.getFieldName(),
82                                new UploadedFile(item.getContentType(), item.get(),
83                                                 item.getName()));
84             }
85         }
86     }
87     
88     /**
89      * Adds a new parameter to the parameters map. If the name already exists
90      * an element is added to the back of the paramters array.
91      * @param name
92      * @param value
93      */

94     protected void addParameter (String JavaDoc name, String JavaDoc value) {
95         String JavaDoc[] existing = (String JavaDoc[])parameters.get(name);
96         if (existing == null) {
97             parameters.put (name,new String JavaDoc[]{value});
98         } else {
99             String JavaDoc[] newValue = new String JavaDoc[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 JavaDoc name)
116         throws FileUploadException {
117         Object JavaDoc o = parameters.get(name);
118
119         return (o instanceof UploadedFile) ? (UploadedFile) o : null;
120     }
121
122     public String JavaDoc getParameter(String JavaDoc name) {
123         Object JavaDoc o = parameters.get(name);
124
125         return (o instanceof String JavaDoc[]) ? ((String JavaDoc[]) o)[0] : null;
126     }
127
128     public Map JavaDoc getParameterMap() {
129         return Collections.unmodifiableMap(parameters);
130     }
131
132     public Enumeration JavaDoc getParameterNames() {
133         return Collections.enumeration(parameters.keySet());
134     }
135
136     public String JavaDoc[] getParameterValues(String JavaDoc name) {
137         Object JavaDoc o = parameters.get(name);
138
139         return (o instanceof String JavaDoc[]) ? (String JavaDoc[]) o : null;
140     }
141 }
142
Popular Tags