001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.util.servlet;
022:
023: import com.liferay.portal.kernel.util.GetterUtil;
024: import com.liferay.util.SystemProperties;
025: import com.liferay.util.servlet.fileupload.LiferayFileItem;
026: import com.liferay.util.servlet.fileupload.LiferayFileItemFactory;
027: import com.liferay.util.servlet.fileupload.LiferayFileUpload;
028: import com.liferay.util.servlet.fileupload.LiferayServletRequest;
029:
030: import java.io.File;
031: import java.io.IOException;
032:
033: import java.util.ArrayList;
034: import java.util.Collections;
035: import java.util.Enumeration;
036: import java.util.HashMap;
037: import java.util.Iterator;
038: import java.util.LinkedHashMap;
039: import java.util.List;
040: import java.util.Map;
041:
042: import javax.servlet.ServletInputStream;
043: import javax.servlet.http.HttpServletRequest;
044: import javax.servlet.http.HttpServletRequestWrapper;
045:
046: import org.apache.commons.fileupload.FileUploadException;
047: import org.apache.commons.fileupload.servlet.ServletFileUpload;
048: import org.apache.commons.logging.Log;
049: import org.apache.commons.logging.LogFactory;
050:
051: /**
052: * <a href="UploadServletRequest.java.html"><b><i>View Source</i></b></a>
053: *
054: * @author Brian Wing Shun Chan
055: * @author Zongliang Li
056: * @author Harry Mark
057: *
058: */
059: public class UploadServletRequest extends HttpServletRequestWrapper {
060:
061: public static final int DEFAULT_SIZE_MAX = GetterUtil
062: .getInteger(SystemProperties.get(UploadServletRequest.class
063: .getName()
064: + ".max.size"));
065:
066: public static final File DEFAULT_TEMP_DIR = new File(GetterUtil
067: .getString(SystemProperties.get(UploadServletRequest.class
068: .getName()
069: + ".temp.dir"), SystemProperties
070: .get(SystemProperties.TMP_DIR)));
071:
072: public UploadServletRequest(HttpServletRequest req)
073: throws IOException {
074:
075: super (req);
076:
077: _params = new LinkedHashMap();
078:
079: try {
080: ServletFileUpload servletFileUpload = new LiferayFileUpload(
081: new LiferayFileItemFactory(DEFAULT_TEMP_DIR), req);
082:
083: servletFileUpload.setSizeMax(DEFAULT_SIZE_MAX);
084:
085: _lsr = new LiferayServletRequest(req);
086:
087: List list = servletFileUpload.parseRequest(_lsr);
088:
089: for (int i = 0; i < list.size(); i++) {
090: LiferayFileItem fileItem = (LiferayFileItem) list
091: .get(i);
092:
093: if (fileItem.isFormField()) {
094: fileItem.setString(req.getCharacterEncoding());
095: }
096:
097: LiferayFileItem[] fileItems = (LiferayFileItem[]) _params
098: .get(fileItem.getFieldName());
099:
100: if (fileItems == null) {
101: fileItems = new LiferayFileItem[] { fileItem };
102: } else {
103: LiferayFileItem[] newFileItems = new LiferayFileItem[fileItems.length + 1];
104:
105: System.arraycopy(fileItems, 0, newFileItems, 0,
106: fileItems.length);
107:
108: newFileItems[newFileItems.length - 1] = fileItem;
109:
110: fileItems = newFileItems;
111: }
112:
113: _params.put(fileItem.getFieldName(), fileItems);
114: }
115: } catch (FileUploadException fue) {
116: _log.error(fue.getMessage());
117: }
118: }
119:
120: public String getContentType(String name) {
121: LiferayFileItem[] fileItems = (LiferayFileItem[]) _params
122: .get(name);
123:
124: if ((fileItems != null) && (fileItems.length > 0)) {
125: return fileItems[0].getContentType();
126: } else {
127: return null;
128: }
129: }
130:
131: public File getFile(String name) {
132: if (getFileName(name) == null) {
133: return null;
134: }
135:
136: LiferayFileItem[] fileItems = (LiferayFileItem[]) _params
137: .get(name);
138:
139: if ((fileItems != null) && (fileItems.length > 0)) {
140: return fileItems[0].getStoreLocation();
141: } else {
142: return null;
143: }
144: }
145:
146: public String getFileName(String name) {
147: LiferayFileItem[] fileItems = (LiferayFileItem[]) _params
148: .get(name);
149:
150: if ((fileItems != null) && (fileItems.length > 0)) {
151: return fileItems[0].getFileName();
152: } else {
153: return null;
154: }
155: }
156:
157: public String getFullFileName(String name) {
158: LiferayFileItem[] fileItems = (LiferayFileItem[]) _params
159: .get(name);
160:
161: if ((fileItems != null) && (fileItems.length > 0)) {
162: return fileItems[0].getFullFileName();
163: } else {
164: return null;
165: }
166: }
167:
168: public ServletInputStream getInputStream() throws IOException {
169: return _lsr.getInputStream();
170: }
171:
172: public String getParameter(String name) {
173: LiferayFileItem[] fileItems = (LiferayFileItem[]) _params
174: .get(name);
175:
176: if ((fileItems != null) && (fileItems.length > 0)) {
177: return fileItems[0].getString();
178: } else {
179: return super .getParameter(name);
180: }
181: }
182:
183: public Map getParameterMap() {
184: Map map = new HashMap();
185:
186: Enumeration enu = getParameterNames();
187:
188: while (enu.hasMoreElements()) {
189: String name = (String) enu.nextElement();
190:
191: map.put(name, getParameterValues(name));
192: }
193:
194: return map;
195: }
196:
197: public Enumeration getParameterNames() {
198: List parameterNames = new ArrayList();
199:
200: Enumeration enu = super .getParameterNames();
201:
202: while (enu.hasMoreElements()) {
203: String name = (String) enu.nextElement();
204:
205: if (!_params.containsKey(name)) {
206: parameterNames.add(name);
207: }
208: }
209:
210: Iterator itr = _params.keySet().iterator();
211:
212: while (itr.hasNext()) {
213: String name = (String) itr.next();
214:
215: parameterNames.add(name);
216: }
217:
218: return Collections.enumeration(parameterNames);
219: }
220:
221: public String[] getParameterValues(String name) {
222: String[] parentValues = super .getParameterValues(name);
223:
224: LiferayFileItem[] fileItems = (LiferayFileItem[]) _params
225: .get(name);
226:
227: if ((fileItems == null) || (fileItems.length == 0)) {
228: return parentValues;
229: } else if ((parentValues == null) || (parentValues.length == 0)) {
230: String[] values = new String[fileItems.length];
231:
232: for (int i = 0; i < values.length; i++) {
233: values[i] = fileItems[i].getString();
234: }
235:
236: return values;
237: } else {
238: String[] values = new String[parentValues.length
239: + fileItems.length];
240:
241: System.arraycopy(parentValues, 0, values, 0,
242: parentValues.length);
243:
244: for (int i = parentValues.length; i < values.length; i++) {
245: values[i] = fileItems[i - parentValues.length]
246: .getString();
247: }
248:
249: return values;
250: }
251: }
252:
253: public Map getMultipartParameterMap() {
254: return _params;
255: }
256:
257: public Boolean isFormField(String name) {
258: LiferayFileItem[] fileItems = (LiferayFileItem[]) _params
259: .get(name);
260:
261: if ((fileItems != null) && (fileItems.length > 0)) {
262: return new Boolean(fileItems[0].isFormField());
263: } else {
264: return null;
265: }
266: }
267:
268: public void cleanUp() {
269: if ((_params != null) && !_params.isEmpty()) {
270: Iterator itr = _params.values().iterator();
271:
272: while (itr.hasNext()) {
273: LiferayFileItem[] fileItems = (LiferayFileItem[]) itr
274: .next();
275:
276: for (int i = 0; i < fileItems.length; i++) {
277: fileItems[i].delete();
278: }
279: }
280: }
281: }
282:
283: private static Log _log = LogFactory
284: .getLog(UploadServletRequest.class);
285:
286: private LiferayServletRequest _lsr;
287: private Map _params;
288:
289: }
|