001: /*
002: * (C) Copyright 2004 Nabh Information Systems, Inc.
003: *
004: * All copyright notices regarding Nabh's products MUST remain
005: * intact in the scripts and in the outputted HTML.
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021: package com.nabhinc.portlet.mvcportlet.core;
022:
023: import java.io.BufferedReader;
024: import java.io.File;
025: import java.io.IOException;
026: import java.io.InputStream;
027: import java.io.UnsupportedEncodingException;
028: import java.security.Principal;
029: import java.util.Enumeration;
030: import java.util.Hashtable;
031: import java.util.List;
032: import java.util.Locale;
033: import java.util.Map;
034:
035: import javax.portlet.ActionRequest;
036: import javax.portlet.PortalContext;
037: import javax.portlet.PortletMode;
038: import javax.portlet.PortletPreferences;
039: import javax.portlet.PortletSession;
040: import javax.portlet.WindowState;
041:
042: import org.apache.commons.fileupload.disk.DiskFileItemFactory;
043: import org.apache.commons.fileupload.portlet.PortletFileUpload;
044: import org.apache.commons.fileupload.FileItem;
045: import org.apache.commons.fileupload.FileUploadException;
046:
047: /**
048: * This is a convenience class used to process file upload request. It parses a multipart
049: * request and provides easy access to form fields and uploaded files. Form fields can be
050: * accessed using standard getParameter* methods. Uploaded FileItem objects can be accessed
051: * via request attributes "mvcportlet.file.<file name>".
052: *
053: * @author Padmanabh Dabke
054: * (c) 2004 Nabh Information Systems, Inc. All Rights Reserved.
055: */
056: public class MultipartRequestWrapper implements ActionRequest {
057:
058: private ActionRequest arwRequest = null;
059: private Hashtable arwParams = new Hashtable();
060: private long arwMaxFileSize = 1000000;
061: private int arwThreshold = 500000;
062: private String arwRepositoryPath = null;
063:
064: public MultipartRequestWrapper(ActionRequest aRequest,
065: long maxFileSize, int threshold, String repositoryPath)
066: throws FileUploadException {
067: arwRequest = aRequest;
068: arwMaxFileSize = maxFileSize;
069: arwThreshold = threshold;
070: arwRepositoryPath = repositoryPath;
071: populateParameters();
072: }
073:
074: public void setMaxFileSize(long fileSize) {
075: arwMaxFileSize = fileSize;
076: }
077:
078: public void setThreshold(int threshold) {
079: arwThreshold = threshold;
080: }
081:
082: /**
083: * Parses a multipart request submission to figure out form inputs
084: * and uploaded files.
085: * @throws FileUploadException
086: */
087: @SuppressWarnings("unchecked")
088: public void populateParameters() throws FileUploadException {
089: // First populate the param table with original request
090: // parameters
091: Enumeration paramList = arwRequest.getParameterNames();
092: while (paramList.hasMoreElements()) {
093: String paramName = (String) paramList.nextElement();
094: String[] paramValues = arwRequest
095: .getParameterValues(paramName);
096: arwParams.put(paramName, paramValues);
097: }
098:
099: // Now parse the multi-part file content and set additional text
100: // parameters. File parameters are set as attributes prefixed
101: // with "mvcportlet.file."
102: //DiskFileUpload fileUpload = new DiskFileUpload();
103: DiskFileItemFactory factory = new DiskFileItemFactory(
104: arwThreshold, new File(arwRepositoryPath));
105: PortletFileUpload fileUpload = new PortletFileUpload(factory);
106: fileUpload.setSizeMax(arwMaxFileSize);
107: List itemList = fileUpload.parseRequest(arwRequest);
108: for (int i = 0; i < itemList.size(); i++) {
109: FileItem item = (FileItem) itemList.get(i);
110: if (item.isFormField()) {
111: String paramName = item.getFieldName();
112: String paramValue = null;
113: try {
114: paramValue = item.getString(arwRequest
115: .getCharacterEncoding());
116: } catch (Exception e) {
117: paramValue = item.getString();
118: }
119: String[] oldValue = (String[]) arwParams.get(paramName);
120: if (oldValue == null) {
121: arwParams.put(paramName,
122: new String[] { paramValue });
123: } else {
124: String[] newValue = new String[oldValue.length + 1];
125: for (int j = 0; j < oldValue.length; j++) {
126: newValue[j] = oldValue[j];
127: }
128: newValue[oldValue.length] = paramValue;
129: arwParams.put(paramName, newValue);
130: }
131: } else {
132: // Set attribute corresponding the uploaded file item
133: arwRequest.setAttribute("mvcportlet.file."
134: + item.getFieldName(), item);
135: }
136: }
137: }
138:
139: /**
140: * @see javax.portlet.ActionRequest#getPortletInputStream()
141: */
142: public InputStream getPortletInputStream() throws IOException {
143: return arwRequest.getPortletInputStream();
144: }
145:
146: /**
147: * @see javax.portlet.ActionRequest#setCharacterEncoding(java.lang.String)
148: */
149: public void setCharacterEncoding(String arg0)
150: throws UnsupportedEncodingException {
151: arwRequest.setCharacterEncoding(arg0);
152: }
153:
154: /**
155: * @see javax.portlet.ActionRequest#getReader()
156: */
157: public BufferedReader getReader()
158: throws UnsupportedEncodingException, IOException {
159: return arwRequest.getReader();
160: }
161:
162: /**
163: * @see javax.portlet.ActionRequest#getCharacterEncoding()
164: */
165: public String getCharacterEncoding() {
166: return arwRequest.getCharacterEncoding();
167: }
168:
169: /**
170: * @see javax.portlet.ActionRequest#getContentType()
171: */
172: public String getContentType() {
173: return arwRequest.getContentType();
174: }
175:
176: /**
177: * @see javax.portlet.ActionRequest#getContentLength()
178: */
179: public int getContentLength() {
180: return arwRequest.getContentLength();
181: }
182:
183: /**
184: * @see javax.portlet.PortletRequest#isWindowStateAllowed(javax.portlet.WindowState)
185: */
186: public boolean isWindowStateAllowed(WindowState arg0) {
187: return arwRequest.isWindowStateAllowed(arg0);
188: }
189:
190: /**
191: * @see javax.portlet.PortletRequest#isPortletModeAllowed(javax.portlet.PortletMode)
192: */
193: public boolean isPortletModeAllowed(PortletMode arg0) {
194: return arwRequest.isPortletModeAllowed(arg0);
195: }
196:
197: /**
198: * @see javax.portlet.PortletRequest#getPortletMode()
199: */
200: public PortletMode getPortletMode() {
201: return arwRequest.getPortletMode();
202: }
203:
204: /**
205: * @see javax.portlet.PortletRequest#getWindowState()
206: */
207: public WindowState getWindowState() {
208: return arwRequest.getWindowState();
209: }
210:
211: /**
212: * @see javax.portlet.PortletRequest#getPreferences()
213: */
214: public PortletPreferences getPreferences() {
215: return arwRequest.getPreferences();
216: }
217:
218: /**
219: * @see javax.portlet.PortletRequest#getPortletSession()
220: */
221: public PortletSession getPortletSession() {
222: return arwRequest.getPortletSession();
223: }
224:
225: /**
226: * @see javax.portlet.PortletRequest#getPortletSession(boolean)
227: */
228: public PortletSession getPortletSession(boolean arg0) {
229: return arwRequest.getPortletSession(arg0);
230: }
231:
232: /**
233: * @see javax.portlet.PortletRequest#getProperty(java.lang.String)
234: */
235: public String getProperty(String arg0) {
236: return arwRequest.getProperty(arg0);
237: }
238:
239: /**
240: * @see javax.portlet.PortletRequest#getProperties(java.lang.String)
241: */
242: public Enumeration getProperties(String arg0) {
243: return arwRequest.getProperties(arg0);
244: }
245:
246: /**
247: * @see javax.portlet.PortletRequest#getPropertyNames()
248: */
249: public Enumeration getPropertyNames() {
250: return arwRequest.getPropertyNames();
251: }
252:
253: /**
254: * @see javax.portlet.PortletRequest#getPortalContext()
255: */
256: public PortalContext getPortalContext() {
257: return arwRequest.getPortalContext();
258: }
259:
260: /**
261: * @see javax.portlet.PortletRequest#getAuthType()
262: */
263: public String getAuthType() {
264: return arwRequest.getAuthType();
265: }
266:
267: /**
268: * @see javax.portlet.PortletRequest#getContextPath()
269: */
270: public String getContextPath() {
271: return arwRequest.getContextPath();
272: }
273:
274: /**
275: * @see javax.portlet.PortletRequest#getRemoteUser()
276: */
277: public String getRemoteUser() {
278: return arwRequest.getRemoteUser();
279: }
280:
281: /**
282: * @see javax.portlet.PortletRequest#getUserPrincipal()
283: */
284: public Principal getUserPrincipal() {
285: return arwRequest.getUserPrincipal();
286: }
287:
288: /**
289: * @see javax.portlet.PortletRequest#isUserInRole(java.lang.String)
290: */
291: public boolean isUserInRole(String arg0) {
292: return arwRequest.isUserInRole(arg0);
293: }
294:
295: /**
296: * @see javax.portlet.PortletRequest#getAttribute(java.lang.String)
297: */
298: public Object getAttribute(String arg0) {
299: return arwRequest.getAttribute(arg0);
300: }
301:
302: /**
303: * @see javax.portlet.PortletRequest#getAttributeNames()
304: */
305: public Enumeration getAttributeNames() {
306: return arwRequest.getAttributeNames();
307: }
308:
309: /**
310: * @see javax.portlet.PortletRequest#getParameter(java.lang.String)
311: */
312: public String getParameter(String arg0) {
313: String[] paramValues = (String[]) arwParams.get(arg0);
314: if (paramValues == null) {
315: return null;
316: } else {
317: return paramValues[0];
318: }
319: }
320:
321: /**
322: * @see javax.portlet.PortletRequest#getParameterNames()
323: */
324: public Enumeration getParameterNames() {
325: return arwParams.keys();
326: }
327:
328: /**
329: * @see javax.portlet.PortletRequest#getParameterValues(java.lang.String)
330: */
331: public String[] getParameterValues(String arg0) {
332: return (String[]) arwParams.get(arg0);
333: }
334:
335: /**
336: * @see javax.portlet.PortletRequest#getParameterMap()
337: */
338: public Map getParameterMap() {
339: return arwParams;
340: }
341:
342: /**
343: * @see javax.portlet.PortletRequest#isSecure()
344: */
345: public boolean isSecure() {
346: return arwRequest.isSecure();
347: }
348:
349: /**
350: * @see javax.portlet.PortletRequest#setAttribute(java.lang.String, java.lang.Object)
351: */
352: public void setAttribute(String arg0, Object arg1) {
353: arwRequest.setAttribute(arg0, arg1);
354:
355: }
356:
357: /**
358: * @see javax.portlet.PortletRequest#removeAttribute(java.lang.String)
359: */
360: public void removeAttribute(String arg0) {
361: arwRequest.removeAttribute(arg0);
362:
363: }
364:
365: /**
366: * @see javax.portlet.PortletRequest#getRequestedSessionId()
367: */
368: public String getRequestedSessionId() {
369: return arwRequest.getRequestedSessionId();
370: }
371:
372: /**
373: * @see javax.portlet.PortletRequest#isRequestedSessionIdValid()
374: */
375: public boolean isRequestedSessionIdValid() {
376: return arwRequest.isRequestedSessionIdValid();
377: }
378:
379: /**
380: * @see javax.portlet.PortletRequest#getResponseContentType()
381: */
382: public String getResponseContentType() {
383: return arwRequest.getResponseContentType();
384: }
385:
386: /**
387: * @see javax.portlet.PortletRequest#getResponseContentTypes()
388: */
389: public Enumeration getResponseContentTypes() {
390: return arwRequest.getResponseContentTypes();
391: }
392:
393: /**
394: * @see javax.portlet.PortletRequest#getLocale()
395: */
396: public Locale getLocale() {
397: return arwRequest.getLocale();
398: }
399:
400: /**
401: * @see javax.portlet.PortletRequest#getLocales()
402: */
403: public Enumeration getLocales() {
404: return arwRequest.getLocales();
405: }
406:
407: /**
408: * @see javax.portlet.PortletRequest#getScheme()
409: */
410: public String getScheme() {
411: return arwRequest.getScheme();
412: }
413:
414: /**
415: * @see javax.portlet.PortletRequest#getServerName()
416: */
417: public String getServerName() {
418: return arwRequest.getServerName();
419: }
420:
421: /**
422: * @see javax.portlet.PortletRequest#getServerPort()
423: */
424: public int getServerPort() {
425: return arwRequest.getServerPort();
426: }
427:
428: }
|