- experimental opensessions
[mir.git] / source / mir / util / HTTPParsedRequest.java
1 /*\r
2  * Copyright (C) 2001, 2002  The Mir-coders group\r
3  *\r
4  * This file is part of Mir.\r
5  *\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
10  *\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
15  *\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
19  *\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
30  */\r
31 \r
32 package mir.util;\r
33 \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
42 \r
43 import org.apache.commons.fileupload.FileItem;\r
44 import org.apache.commons.fileupload.FileUpload;\r
45 import mir.log.LoggerWrapper;\r
46 \r
47 public class HTTPParsedRequest {\r
48   static final String MULTIPART_FORMDATA_CONTENTTYPE = "multipart/form-data";\r
49 \r
50   private HttpServletRequest request;\r
51   private String encoding;\r
52   private int maxUploadSize;\r
53   private String tempDir;\r
54 \r
55   private Map stringValues;\r
56   private Map listValues;\r
57   private List files;\r
58 \r
59   private LoggerWrapper logger;\r
60 \r
61   public HTTPParsedRequest(HttpServletRequest aRequest, String anEncoding, int aMaxUploadSize, String aTempDir) throws UtilExc, UtilFailure {\r
62     logger = new LoggerWrapper("Utility,HTTPParsedRequest");\r
63 \r
64     request = aRequest;\r
65     encoding = anEncoding;\r
66     maxUploadSize = aMaxUploadSize;\r
67     tempDir = aTempDir;\r
68 \r
69     stringValues = new HashMap();\r
70     listValues = new HashMap();\r
71     files = new Vector();\r
72 \r
73     parseRequest(aRequest);\r
74   }\r
75 \r
76   public HTTPParsedRequest(HttpServletRequest aRequest, int aMaxUploadSize, String aTempDir) throws UtilExc, UtilFailure {\r
77     this(aRequest, aRequest.getCharacterEncoding(), aMaxUploadSize, aTempDir);\r
78   }\r
79 \r
80   public HttpServletRequest getRequest() {\r
81     return request;\r
82   }\r
83 \r
84   public String getParameter(String aName) {\r
85     return (String) stringValues.get(aName);\r
86   }\r
87 \r
88   public List getFiles() {\r
89     return files;\r
90   }\r
91 \r
92   public List getParameterList(String aName) {\r
93     return (List) listValues.get(aName);\r
94   }\r
95 \r
96   protected void parseRequest(HttpServletRequest aRequest) throws UtilExc, UtilFailure {\r
97 \r
98     try {\r
99       String contentType = aRequest.getContentType();\r
100       List parts = StringRoutines.splitString(contentType, ";");\r
101 \r
102       Enumeration e = aRequest.getParameterNames();\r
103 \r
104       while (e.hasMoreElements()) {\r
105         String name = (String) e.nextElement();\r
106 \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
110       }\r
111 \r
112       if (parts.size()>0 && ((String) parts.get(0)).trim().toLowerCase().equals(MULTIPART_FORMDATA_CONTENTTYPE)) {\r
113         parseMultipartRequest();\r
114       }\r
115     }\r
116     catch (Throwable t) {\r
117       t.printStackTrace();\r
118 \r
119       throw new UtilFailure(t);\r
120     }\r
121   }\r
122 \r
123   protected void parseMultipartRequest() throws UtilExc, UtilFailure {\r
124     try {\r
125       FileUpload upload = new FileUpload();\r
126 \r
127       upload.setSizeMax(maxUploadSize);\r
128       upload.setSizeThreshold(4096);\r
129       upload.setRepositoryPath(tempDir);\r
130 \r
131       List items = upload.parseRequest(request);\r
132 \r
133       Iterator i = items.iterator();\r
134       while (i.hasNext()) {\r
135         FileItem item = (FileItem) i.next();\r
136 \r
137         if (item.isFormField()) {\r
138           if (!stringValues.containsKey(item.getName())) {\r
139             stringValues.put(item.getFieldName(), item.getString());\r
140           }\r
141 \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
146           }\r
147           listValue.add(item.getString());\r
148         }\r
149         else {\r
150           if (item.getSize()>0)\r
151             files.add(item);\r
152         }\r
153       }\r
154     }\r
155     catch (Throwable t) {\r
156       throw new UtilFailure(t);\r
157     }\r
158   }\r
159 }\r