KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > controller > MultiPartEnabledRequest


1 /*
2  * ====================================================================
3  * Copyright (c) 2003 TONBELLER AG.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by
21  * TONBELLER AG (http://www.tonbeller.com)"
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
26  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28  * DISCLAIMED. IN NO EVENT SHALL THE TON BELLER AG OR
29  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
32  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
33  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
34  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
35  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  * ====================================================================
38  *
39  *
40  */

41 package com.tonbeller.wcf.controller;
42
43 import java.io.UnsupportedEncodingException JavaDoc;
44 import java.util.ArrayList JavaDoc;
45 import java.util.Enumeration JavaDoc;
46 import java.util.HashMap JavaDoc;
47 import java.util.Iterator JavaDoc;
48 import java.util.List JavaDoc;
49 import java.util.Map JavaDoc;
50 import java.util.MissingResourceException JavaDoc;
51 import java.util.Vector JavaDoc;
52
53 import javax.servlet.http.HttpServletRequest JavaDoc;
54 import javax.servlet.http.HttpServletRequestWrapper JavaDoc;
55
56 import org.apache.commons.fileupload.DiskFileUpload;
57 import org.apache.commons.fileupload.FileItem;
58 import org.apache.commons.fileupload.FileUpload;
59 import org.apache.commons.fileupload.FileUploadException;
60 import org.apache.log4j.Logger;
61
62 import com.tonbeller.tbutils.res.Resources;
63
64 /**
65  * This request supports also forms with encoding "multipart/form-data"
66  * that is used for file upload. For this kind of forms tomcat (J2EE reference)
67  * request implementations always returns null from function getParameter()
68  * <p>
69  * MultiPartEnabledRequest is a decorator
70  */

71 public class MultiPartEnabledRequest extends HttpServletRequestWrapper JavaDoc {
72
73   private static Logger logger = Logger.getLogger(MultiPartEnabledRequest.class);
74   private static Resources res = Resources.instance(MultiPartEnabledRequest.class);
75
76   private boolean multipart;
77
78   private Map JavaDoc fileItems = new HashMap JavaDoc();
79   private Map JavaDoc httpParams = new HashMap JavaDoc();
80
81   public MultiPartEnabledRequest(HttpServletRequest JavaDoc req) {
82     super(req);
83     this.multipart = FileUpload.isMultipartContent(req);
84     if (multipart) {
85       try {
86         readHttpParams(req);
87       } catch (FileUploadException e) {
88         logger.error("", e);
89         e.printStackTrace();
90       }
91     }
92   }
93
94   private void readHttpParams(HttpServletRequest JavaDoc req) throws FileUploadException {
95     List JavaDoc all = uploadFiles(req);
96
97     // read form fields
98
for (Iterator JavaDoc it = all.iterator(); it.hasNext();) {
99       FileItem item = (FileItem) it.next();
100
101       if (item.isFormField()) {
102         List JavaDoc valList = valueList(httpParams, item.getFieldName());
103         if (req.getCharacterEncoding() != null) {
104           try {
105             valList.add(item.getString(req.getCharacterEncoding()));
106           } catch (UnsupportedEncodingException JavaDoc e) {
107             logger.error(null, e);
108             valList.add(item.getString(/*encoding?*/));
109           }
110         }
111         else
112           valList.add(item.getString(/*encoding?*/));
113       } else {
114         List JavaDoc valList = valueList(fileItems, item.getFieldName());
115         valList.add(item);
116       }
117     }
118
119     // convert lists of values to arrays
120
for (Iterator JavaDoc it = httpParams.keySet().iterator(); it.hasNext();) {
121       String JavaDoc name = (String JavaDoc) it.next();
122       List JavaDoc valList = (List JavaDoc) httpParams.get(name);
123       httpParams.put(name, toStringArray(valList));
124     }
125
126     for (Iterator JavaDoc it = fileItems.keySet().iterator(); it.hasNext();) {
127       String JavaDoc name = (String JavaDoc) it.next();
128       List JavaDoc valList = (List JavaDoc) fileItems.get(name);
129       fileItems.put(name, toFileItemArray(valList));
130     }
131   }
132
133   private List JavaDoc valueList(Map JavaDoc params, String JavaDoc name) {
134     List JavaDoc valList = (List JavaDoc) params.get(name);
135     if (valList == null) {
136       valList = new ArrayList JavaDoc();
137       params.put(name, valList);
138     }
139     return valList;
140   }
141
142   private String JavaDoc[] toStringArray(List JavaDoc valList) {
143     String JavaDoc[] vals = new String JavaDoc[valList.size()];
144     for (int i = 0; i < vals.length; i++)
145       vals[i] = (String JavaDoc) valList.get(i);
146     return vals;
147   }
148
149   private FileItem[] toFileItemArray(List JavaDoc valList) {
150     FileItem[] vals = new FileItem[valList.size()];
151     for (int i = 0; i < vals.length; i++)
152       vals[i] = (FileItem) valList.get(i);
153     return vals;
154   }
155
156   private List JavaDoc uploadFiles(HttpServletRequest JavaDoc req) throws FileUploadException {
157
158     DiskFileUpload upload = new DiskFileUpload();
159
160     try {
161       upload.setSizeThreshold(res.getInteger("file.upload.size.threshold"));
162     } catch (MissingResourceException JavaDoc e) {
163       // use defaults
164
}
165
166     try {
167       upload.setSizeMax(res.getInteger("file.upload.size.max"));
168     } catch (MissingResourceException JavaDoc e) {
169       // use defaults
170
}
171
172     try {
173       upload.setRepositoryPath(res.getString("file.upload.repository"));
174     } catch (MissingResourceException JavaDoc e) {
175       // use defaults
176
}
177
178     List JavaDoc all = new DiskFileUpload().parseRequest(req);
179     return all;
180   }
181
182   public boolean isMultipart() {
183     return multipart;
184   }
185
186   public String JavaDoc getParameter(String JavaDoc name) {
187     if (!isMultipart())
188       return super.getParameter(name);
189
190     String JavaDoc[] vals = (String JavaDoc[]) httpParams.get(name);
191     if (vals == null)
192       return null;
193
194     return vals[0];
195   }
196
197   public FileItem getFileParameter(String JavaDoc name) {
198     FileItem[] vals = (FileItem[]) fileItems.get(name);
199     if (vals == null)
200       return null;
201
202     return vals[0];
203   }
204
205   public Map JavaDoc getParameterMap() {
206     if (!isMultipart())
207       return super.getParameterMap();
208
209     return new HashMap JavaDoc(httpParams);
210   }
211
212   public Map JavaDoc getFileParameterMap() {
213     return new HashMap JavaDoc(fileItems);
214   }
215
216   public Enumeration JavaDoc getParameterNames() {
217     if (!isMultipart())
218       return super.getParameterNames();
219
220     return new Vector JavaDoc(httpParams.keySet()).elements();
221   }
222
223   public Enumeration JavaDoc getFileParameterNames() {
224     return new Vector JavaDoc(fileItems.keySet()).elements();
225   }
226
227   public String JavaDoc[] getParameterValues(String JavaDoc name) {
228     if (!isMultipart())
229       return super.getParameterValues(name);
230
231     return (String JavaDoc[]) httpParams.get(name);
232   }
233
234   public FileItem[] getFileParameterValues(String JavaDoc name) {
235     return (FileItem[]) fileItems.get(name);
236   }
237
238 }
Popular Tags