001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.portlet.multipart;
018:
019: import org.apache.cocoon.servlet.multipart.PartOnDisk;
020:
021: import javax.portlet.ActionRequest;
022: import javax.portlet.PortalContext;
023: import javax.portlet.PortletMode;
024: import javax.portlet.PortletPreferences;
025: import javax.portlet.PortletSession;
026: import javax.portlet.WindowState;
027:
028: import java.io.BufferedReader;
029: import java.io.File;
030: import java.io.IOException;
031: import java.io.InputStream;
032: import java.io.UnsupportedEncodingException;
033: import java.security.Principal;
034: import java.util.Enumeration;
035: import java.util.Hashtable;
036: import java.util.Locale;
037: import java.util.Map;
038: import java.util.Vector;
039:
040: /**
041: * Portlet action request wrapper for multipart parser.
042: *
043: * @author <a href="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
044: * @version CVS $Id: MultipartActionRequest.java 433543 2006-08-22 06:22:54Z crossley $
045: */
046: public class MultipartActionRequest implements ActionRequest {
047:
048: /** The wrapped request */
049: private ActionRequest request = null;
050:
051: /** The submitted parts */
052: private Hashtable values = null;
053:
054: /**
055: * Create this wrapper around the given request and including the given
056: * parts.
057: */
058: public MultipartActionRequest(ActionRequest request,
059: Hashtable values) {
060: this .request = request;
061: this .values = values;
062: }
063:
064: /**
065: * Cleanup eventually uploaded parts that were saved on disk
066: */
067: public void cleanup() throws IOException {
068: Enumeration e = getParameterNames();
069: while (e.hasMoreElements()) {
070: Object o = get((String) e.nextElement());
071: if (o instanceof PartOnDisk) {
072: File file = ((PartOnDisk) o).getFile();
073: file.delete();
074: }
075: }
076: }
077:
078: /**
079: * Method get
080: *
081: * @param name
082: */
083: public Object get(String name) {
084: Object result = null;
085:
086: if (values != null) {
087: result = values.get(name);
088:
089: if (result instanceof Vector) {
090: if (((Vector) result).size() == 1) {
091: return ((Vector) result).elementAt(0);
092: } else {
093: return result;
094: }
095: }
096: }
097:
098: // TODO: Test multipart form with parameter in action="" attribute
099: if (result == null) {
100: String[] array = request.getParameterValues(name);
101: Vector vec = new Vector();
102:
103: if (array != null) {
104: for (int i = 0; i < array.length; i++) {
105: vec.addElement(array[i]);
106: }
107:
108: if (vec.size() == 1) {
109: result = vec.elementAt(0);
110: } else {
111: result = vec;
112: }
113: }
114: }
115:
116: return result;
117: }
118:
119: /**
120: * Method getParameterNames
121: */
122: public Enumeration getParameterNames() {
123: if (values != null) {
124: return values.keys();
125: } else {
126: return request.getParameterNames();
127: }
128: }
129:
130: /**
131: * Method getParameter
132: *
133: * @param name
134: */
135: public String getParameter(String name) {
136: Object value = get(name);
137: String result = null;
138:
139: if (value != null) {
140: if (value instanceof Vector) {
141: value = ((Vector) value).elementAt(0);
142: }
143:
144: result = value.toString();
145: }
146:
147: return result;
148: }
149:
150: /**
151: * Method getParameterValues
152: *
153: * @param name
154: */
155: public String[] getParameterValues(String name) {
156: if (values != null) {
157: Object value = get(name);
158:
159: if (value != null) {
160: if (value instanceof Vector) {
161: String[] results = new String[((Vector) value)
162: .size()];
163: for (int i = 0; i < ((Vector) value).size(); i++) {
164: results[i] = ((Vector) value).elementAt(i)
165: .toString();
166: }
167: return results;
168:
169: } else {
170: return new String[] { value.toString() };
171: }
172: }
173:
174: return null;
175: } else {
176: return request.getParameterValues(name);
177: }
178: }
179:
180: /**
181: * Method getAttribute
182: *
183: * @param name
184: */
185: public Object getAttribute(String name) {
186: return request.getAttribute(name);
187: }
188:
189: /**
190: * Method getAttributeNames
191: */
192: public Enumeration getAttributeNames() {
193: return request.getAttributeNames();
194: }
195:
196: /**
197: * Method getCharacterEncoding
198: */
199: public String getCharacterEncoding() {
200: return request.getCharacterEncoding();
201: }
202:
203: /**
204: * Method getContentLength
205: */
206: public int getContentLength() {
207: return request.getContentLength();
208: }
209:
210: /**
211: * Method getContentType
212: */
213: public String getContentType() {
214: return request.getContentType();
215: }
216:
217: /**
218: * Method getInputStream
219: *
220: * @throws IOException
221: */
222: public InputStream getInputStream() throws IOException {
223: return request.getPortletInputStream();
224: }
225:
226: /**
227: * Method getScheme
228: */
229: public String getScheme() {
230: return request.getScheme();
231: }
232:
233: /**
234: * Method getServerName
235: */
236: public String getServerName() {
237: return request.getServerName();
238: }
239:
240: /**
241: * Method getServerPort
242: */
243: public int getServerPort() {
244: return request.getServerPort();
245: }
246:
247: /**
248: * Method getReader
249: *
250: * @throws IOException
251: */
252: public BufferedReader getReader() throws IOException {
253: return request.getReader();
254: }
255:
256: /**
257: * Method setAttribute
258: *
259: * @param name
260: * @param o
261: */
262: public void setAttribute(String name, Object o) {
263: request.setAttribute(name, o);
264: }
265:
266: /**
267: * Method removeAttribute
268: *
269: * @param name
270: */
271: public void removeAttribute(String name) {
272: request.removeAttribute(name);
273: }
274:
275: /**
276: * Method getLocale
277: */
278: public Locale getLocale() {
279: return request.getLocale();
280: }
281:
282: /**
283: * Method getLocales
284: */
285: public Enumeration getLocales() {
286: return request.getLocales();
287: }
288:
289: /**
290: * Method isSecure
291: */
292: public boolean isSecure() {
293: return request.isSecure();
294: }
295:
296: /**
297: * Method getAuthType
298: */
299: public String getAuthType() {
300: return request.getAuthType();
301: }
302:
303: /**
304: * Method getContextPath
305: */
306: public String getContextPath() {
307: return request.getContextPath();
308: }
309:
310: /**
311: * Method getRemoteUser
312: *
313: */
314: public String getRemoteUser() {
315: return request.getRemoteUser();
316: }
317:
318: /**
319: * Method isUserInRole
320: *
321: * @param role
322: */
323: public boolean isUserInRole(String role) {
324: return request.isUserInRole(role);
325: }
326:
327: /**
328: * Method getUserPrincipal
329: */
330: public Principal getUserPrincipal() {
331: return request.getUserPrincipal();
332: }
333:
334: /**
335: * Method getRequestedSessionId
336: */
337: public String getRequestedSessionId() {
338: return request.getRequestedSessionId();
339: }
340:
341: /**
342: * Method getSession
343: *
344: * @param create
345: */
346: public PortletSession getPortletSession(boolean create) {
347: return request.getPortletSession(create);
348: }
349:
350: /**
351: * Method getSession
352: */
353: public PortletSession getPortletSession() {
354: return request.getPortletSession();
355: }
356:
357: /**
358: * Method isRequestedSessionIdValid
359: */
360: public boolean isRequestedSessionIdValid() {
361: return request.isRequestedSessionIdValid();
362: }
363:
364: /* (non-Javadoc)
365: * @see javax.portlet.ActionRequest#getPortletInputStream()
366: */
367: public InputStream getPortletInputStream() throws IOException {
368: return request.getPortletInputStream();
369: }
370:
371: /* (non-Javadoc)
372: * @see javax.portlet.ActionRequest#setCharacterEncoding(java.lang.String)
373: */
374: public void setCharacterEncoding(String enc)
375: throws UnsupportedEncodingException {
376: request.setCharacterEncoding(enc);
377: }
378:
379: /* (non-Javadoc)
380: * @see javax.portlet.PortletRequest#getParameterMap()
381: */
382: public Map getParameterMap() {
383: return request.getParameterMap();
384: }
385:
386: /* (non-Javadoc)
387: * @see javax.portlet.PortletRequest#getPortalContext()
388: */
389: public PortalContext getPortalContext() {
390: return request.getPortalContext();
391: }
392:
393: /* (non-Javadoc)
394: * @see javax.portlet.PortletRequest#getPortletMode()
395: */
396: public PortletMode getPortletMode() {
397: return request.getPortletMode();
398: }
399:
400: /* (non-Javadoc)
401: * @see javax.portlet.PortletRequest#getPreferences()
402: */
403: public PortletPreferences getPreferences() {
404: return request.getPreferences();
405: }
406:
407: /* (non-Javadoc)
408: * @see javax.portlet.PortletRequest#getProperties(java.lang.String)
409: */
410: public Enumeration getProperties(String name) {
411: return request.getProperties(name);
412: }
413:
414: /* (non-Javadoc)
415: * @see javax.portlet.PortletRequest#getProperty(java.lang.String)
416: */
417: public String getProperty(String name) {
418: return request.getProperty(name);
419: }
420:
421: /* (non-Javadoc)
422: * @see javax.portlet.PortletRequest#getPropertyNames()
423: */
424: public Enumeration getPropertyNames() {
425: return request.getPropertyNames();
426: }
427:
428: /* (non-Javadoc)
429: * @see javax.portlet.PortletRequest#getResponseContentType()
430: */
431: public String getResponseContentType() {
432: return request.getResponseContentType();
433: }
434:
435: /* (non-Javadoc)
436: * @see javax.portlet.PortletRequest#getResponseContentTypes()
437: */
438: public Enumeration getResponseContentTypes() {
439: return request.getResponseContentTypes();
440: }
441:
442: /* (non-Javadoc)
443: * @see javax.portlet.PortletRequest#getWindowState()
444: */
445: public WindowState getWindowState() {
446: return request.getWindowState();
447: }
448:
449: /* (non-Javadoc)
450: * @see javax.portlet.PortletRequest#isPortletModeAllowed(javax.portlet.PortletMode)
451: */
452: public boolean isPortletModeAllowed(PortletMode mode) {
453: return request.isPortletModeAllowed(mode);
454: }
455:
456: /* (non-Javadoc)
457: * @see javax.portlet.PortletRequest#isWindowStateAllowed(javax.portlet.WindowState)
458: */
459: public boolean isWindowStateAllowed(WindowState state) {
460: return request.isWindowStateAllowed(state);
461: }
462: }
|