2 * Copyright (C) 2001, 2002 The Mir-coders group
\r
4 * This file is part of Mir.
\r
6 * Mir is free software; you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation; either version 2 of the License, or
\r
9 * (at your option) any later version.
\r
11 * Mir is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with Mir; if not, write to the Free Software
\r
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 * In addition, as a special exception, The Mir-coders gives permission to link
\r
21 * the code of this program with the com.oreilly.servlet library, any library
\r
22 * licensed under the Apache Software License, The Sun (tm) Java Advanced
\r
23 * Imaging library (JAI), The Sun JIMI library (or with modified versions of
\r
24 * the above that use the same license as the above), and distribute linked
\r
25 * combinations including the two. You must obey the GNU General Public
\r
26 * License in all respects for all of the code used other than the above
\r
27 * mentioned libraries. If you modify this file, you may extend this exception
\r
28 * to your version of the file, but you are not obligated to do so. If you do
\r
29 * not wish to do so, delete this exception statement from your version.
\r
34 import java.util.Arrays;
\r
35 import java.util.Enumeration;
\r
36 import java.util.HashMap;
\r
37 import java.util.Iterator;
\r
38 import java.util.List;
\r
39 import java.util.Map;
\r
40 import java.util.Vector;
\r
41 import javax.servlet.http.HttpServletRequest;
\r
43 import org.apache.commons.fileupload.FileItem;
\r
44 import org.apache.commons.fileupload.FileUpload;
\r
45 import mir.log.LoggerWrapper;
\r
47 public class HTTPParsedRequest {
\r
48 static final String MULTIPART_FORMDATA_CONTENTTYPE = "multipart/form-data";
\r
50 private HttpServletRequest request;
\r
51 private String encoding;
\r
52 private int maxUploadSize;
\r
53 private String tempDir;
\r
55 private Map stringValues;
\r
56 private Map listValues;
\r
59 private LoggerWrapper logger;
\r
61 public HTTPParsedRequest(HttpServletRequest aRequest, String anEncoding, int aMaxUploadSize, String aTempDir) throws UtilExc, UtilFailure {
\r
62 logger = new LoggerWrapper("Utility,HTTPParsedRequest");
\r
65 encoding = anEncoding;
\r
66 maxUploadSize = aMaxUploadSize;
\r
69 stringValues = new HashMap();
\r
70 listValues = new HashMap();
\r
71 files = new Vector();
\r
73 parseRequest(aRequest);
\r
76 public HTTPParsedRequest(HttpServletRequest aRequest, int aMaxUploadSize, String aTempDir) throws UtilExc, UtilFailure {
\r
77 this(aRequest, aRequest.getCharacterEncoding(), aMaxUploadSize, aTempDir);
\r
80 public HttpServletRequest getRequest() {
\r
84 public String getParameter(String aName) {
\r
85 return (String) stringValues.get(aName);
\r
88 public List getFiles() {
\r
92 public List getParameterList(String aName) {
\r
93 return (List) listValues.get(aName);
\r
96 protected void parseRequest(HttpServletRequest aRequest) throws UtilExc, UtilFailure {
\r
99 String contentType = aRequest.getContentType();
\r
100 List parts = StringRoutines.splitString(contentType, ";");
\r
102 Enumeration e = aRequest.getParameterNames();
\r
104 while (e.hasMoreElements()) {
\r
105 String name = (String) e.nextElement();
\r
107 stringValues.put(name, aRequest.getParameter(name));
\r
108 List listValue = new Vector(Arrays.asList(aRequest.getParameterValues(name)));
\r
109 listValues.put(name, listValue);
\r
112 if (parts.size()>0 && ((String) parts.get(0)).trim().toLowerCase().equals(MULTIPART_FORMDATA_CONTENTTYPE)) {
\r
113 parseMultipartRequest();
\r
116 catch (Throwable t) {
\r
117 t.printStackTrace();
\r
119 throw new UtilFailure(t);
\r
123 protected void parseMultipartRequest() throws UtilExc, UtilFailure {
\r
125 FileUpload upload = new FileUpload();
\r
127 upload.setSizeMax(maxUploadSize);
\r
128 upload.setSizeThreshold(4096);
\r
129 upload.setRepositoryPath(tempDir);
\r
131 List items = upload.parseRequest(request);
\r
133 Iterator i = items.iterator();
\r
134 while (i.hasNext()) {
\r
135 FileItem item = (FileItem) i.next();
\r
137 if (item.isFormField()) {
\r
138 if (!stringValues.containsKey(item.getName())) {
\r
139 stringValues.put(item.getFieldName(), item.getString());
\r
142 List listValue = (List) listValues.get(item.getFieldName());
\r
143 if (listValue == null) {
\r
144 listValue = new Vector();
\r
145 listValues.put(item.getFieldName(), listValue);
\r
147 listValue.add(item.getString());
\r
150 if (item.getSize()>0)
\r
155 catch (Throwable t) {
\r
156 throw new UtilFailure(t);
\r