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.servlet.multipart;
018:
019: import java.io.BufferedReader;
020: import java.io.IOException;
021: import java.io.UnsupportedEncodingException;
022: import java.security.Principal;
023: import java.util.Enumeration;
024: import java.util.Hashtable;
025: import java.util.Locale;
026: import java.util.Map;
027: import java.util.Vector;
028:
029: import javax.servlet.RequestDispatcher;
030: import javax.servlet.ServletInputStream;
031: import javax.servlet.http.Cookie;
032: import javax.servlet.http.HttpServletRequest;
033: import javax.servlet.http.HttpSession;
034:
035: /**
036: * Servlet request wrapper for multipart parser.
037: *
038: * @author <a href="mailto:j.tervoorde@home.nl">Jeroen ter Voorde</a>
039: * @author Stefano Mazzocchi
040: * @version CVS $Id: MultipartHttpServletRequest.java 433543 2006-08-22 06:22:54Z crossley $
041: */
042: public class MultipartHttpServletRequest implements HttpServletRequest {
043:
044: /** The wrapped request */
045: private HttpServletRequest request = null;
046:
047: /** The submitted parts */
048: private Hashtable values = null;
049:
050: /**
051: * Create this wrapper around the given request and including the given
052: * parts.
053: */
054: public MultipartHttpServletRequest(HttpServletRequest request,
055: Hashtable values) {
056: this .request = request;
057: this .values = values;
058: }
059:
060: /**
061: * Cleanup eventually uploaded parts that were saved on disk
062: */
063: public void cleanup() throws IOException {
064: Enumeration e = getParameterNames();
065: while (e.hasMoreElements()) {
066: Object o = get((String) e.nextElement());
067: if (o instanceof Part) {
068: Part part = (Part) o;
069: if (part.disposeWithRequest()) {
070: part.dispose();
071: }
072: }
073: }
074: }
075:
076: /**
077: * Method get
078: *
079: * @param name
080: *
081: */
082: public Object get(String name) {
083: Object result = null;
084:
085: if (values != null) {
086: result = values.get(name);
087:
088: if (result instanceof Vector) {
089: if (((Vector) result).size() == 1) {
090: return ((Vector) result).elementAt(0);
091: } else {
092: return result;
093: }
094: }
095: } else {
096: String[] array = request.getParameterValues(name);
097: Vector vec = new Vector();
098:
099: if (array != null) {
100: for (int i = 0; i < array.length; i++) {
101: vec.addElement(array[i]);
102: }
103:
104: if (vec.size() == 1) {
105: result = vec.elementAt(0);
106: } else {
107: result = vec;
108: }
109: }
110: }
111:
112: return result;
113: }
114:
115: /**
116: * Method getParameterNames
117: *
118: */
119: public Enumeration getParameterNames() {
120: if (values != null) {
121: return values.keys();
122: } else {
123: return request.getParameterNames();
124: }
125: }
126:
127: /**
128: * Method getParameter
129: *
130: * @param name
131: *
132: */
133: public String getParameter(String name) {
134: if (values != null) {
135: Object value = get(name);
136: String result = null;
137:
138: if (value != null) {
139: if (value instanceof Vector) {
140: value = ((Vector) value).elementAt(0);
141: }
142:
143: result = value.toString();
144: }
145: return result;
146: } else {
147: return this .request.getParameter(name);
148: }
149: }
150:
151: /**
152: * Method getParameterValues
153: *
154: * @param name
155: *
156: */
157: public String[] getParameterValues(String name) {
158: if (values != null) {
159: Object value = get(name);
160:
161: if (value != null) {
162: if (value instanceof Vector) {
163: String[] results = new String[((Vector) value)
164: .size()];
165: for (int i = 0; i < ((Vector) value).size(); i++) {
166: results[i] = ((Vector) value).elementAt(i)
167: .toString();
168: }
169: return results;
170:
171: } else {
172: return new String[] { value.toString() };
173: }
174: }
175:
176: return null;
177: } else {
178: return request.getParameterValues(name);
179: }
180: }
181:
182: /**
183: * Method getAttribute
184: *
185: * @param name
186: *
187: */
188: public Object getAttribute(String name) {
189: return request.getAttribute(name);
190: }
191:
192: /**
193: * Method getAttributeNames
194: *
195: */
196: public Enumeration getAttributeNames() {
197: return request.getAttributeNames();
198: }
199:
200: /**
201: * Method getCharacterEncoding
202: *
203: */
204: public String getCharacterEncoding() {
205: return request.getCharacterEncoding();
206: }
207:
208: /**
209: * Method getContentLength
210: *
211: */
212: public int getContentLength() {
213: return request.getContentLength();
214: }
215:
216: /**
217: * Method getContentType
218: *
219: */
220: public String getContentType() {
221: return request.getContentType();
222: }
223:
224: /**
225: * Method getInputStream
226: *
227: *
228: * @throws IOException
229: */
230: public ServletInputStream getInputStream() throws IOException {
231: return request.getInputStream();
232: }
233:
234: /**
235: * Method getProtocol
236: *
237: */
238: public String getProtocol() {
239: return request.getProtocol();
240: }
241:
242: /**
243: * Method getScheme
244: *
245: */
246: public String getScheme() {
247: return request.getScheme();
248: }
249:
250: /**
251: * Method getServerName
252: *
253: */
254: public String getServerName() {
255: return request.getServerName();
256: }
257:
258: /**
259: * Method getServerPort
260: *
261: */
262: public int getServerPort() {
263: return request.getServerPort();
264: }
265:
266: /**
267: * Method getReader
268: *
269: *
270: * @throws IOException
271: */
272: public BufferedReader getReader() throws IOException {
273: return request.getReader();
274: }
275:
276: /**
277: * Method getRemoteAddr
278: *
279: */
280: public String getRemoteAddr() {
281: return request.getRemoteAddr();
282: }
283:
284: /**
285: * Method getRemoteHost
286: *
287: */
288: public String getRemoteHost() {
289: return request.getRemoteHost();
290: }
291:
292: /**
293: * Method setAttribute
294: *
295: * @param name
296: * @param o
297: */
298: public void setAttribute(String name, Object o) {
299: request.setAttribute(name, o);
300: }
301:
302: /**
303: * Method removeAttribute
304: *
305: * @param name
306: */
307: public void removeAttribute(String name) {
308: request.removeAttribute(name);
309: }
310:
311: /**
312: * Method getLocale
313: *
314: */
315: public Locale getLocale() {
316: return request.getLocale();
317: }
318:
319: /**
320: * Method getLocales
321: *
322: */
323: public Enumeration getLocales() {
324: return request.getLocales();
325: }
326:
327: /**
328: * Method isSecure
329: *
330: */
331: public boolean isSecure() {
332: return request.isSecure();
333: }
334:
335: /**
336: * Method getRequestDispatcher
337: *
338: * @param path
339: *
340: */
341: public RequestDispatcher getRequestDispatcher(String path) {
342: return request.getRequestDispatcher(path);
343: }
344:
345: /**
346: * Method getRealPath
347: *
348: * @param path
349: *
350: */
351: public String getRealPath(String path) {
352: return request.getRealPath(path);
353: }
354:
355: /**
356: * Method getAuthType
357: *
358: */
359: public String getAuthType() {
360: return request.getAuthType();
361: }
362:
363: /**
364: * Method getCookies
365: *
366: */
367: public Cookie[] getCookies() {
368: return request.getCookies();
369: }
370:
371: /**
372: * Method getDateHeader
373: *
374: * @param name
375: *
376: */
377: public long getDateHeader(String name) {
378: return request.getDateHeader(name);
379: }
380:
381: /**
382: * Method getHeader
383: *
384: * @param name
385: *
386: */
387: public String getHeader(String name) {
388: return request.getHeader(name);
389: }
390:
391: /**
392: * Method getHeaders
393: *
394: * @param name
395: *
396: */
397: public Enumeration getHeaders(String name) {
398: return request.getHeaders(name);
399: }
400:
401: /**
402: * Method getHeaderNames
403: *
404: */
405: public Enumeration getHeaderNames() {
406: return request.getHeaderNames();
407: }
408:
409: /**
410: * Method getIntHeader
411: *
412: * @param name
413: *
414: */
415: public int getIntHeader(String name) {
416: return request.getIntHeader(name);
417: }
418:
419: /**
420: * Method getMethod
421: *
422: */
423: public String getMethod() {
424: return request.getMethod();
425: }
426:
427: /**
428: * Method getPathInfo
429: *
430: */
431: public String getPathInfo() {
432: return request.getPathInfo();
433: }
434:
435: /**
436: * Method getPathTranslated
437: *
438: */
439: public String getPathTranslated() {
440: return request.getPathTranslated();
441: }
442:
443: /**
444: * Method getContextPath
445: *
446: */
447: public String getContextPath() {
448: return request.getContextPath();
449: }
450:
451: /**
452: * Method getQueryString
453: *
454: */
455: public String getQueryString() {
456: return request.getQueryString();
457: }
458:
459: /**
460: * Method getRemoteUser
461: *
462: */
463: public String getRemoteUser() {
464: return request.getRemoteUser();
465: }
466:
467: /**
468: * Method isUserInRole
469: *
470: * @param role
471: *
472: */
473: public boolean isUserInRole(String role) {
474: return request.isUserInRole(role);
475: }
476:
477: /**
478: * Method getUserPrincipal
479: *
480: */
481: public Principal getUserPrincipal() {
482: return request.getUserPrincipal();
483: }
484:
485: /**
486: * Method getRequestedSessionId
487: *
488: */
489: public String getRequestedSessionId() {
490: return request.getRequestedSessionId();
491: }
492:
493: /**
494: * Method getRequestURI
495: *
496: */
497: public String getRequestURI() {
498: return request.getRequestURI();
499: }
500:
501: /**
502: * Method getServletPath
503: *
504: */
505: public String getServletPath() {
506: return request.getServletPath();
507: }
508:
509: /**
510: * Method getSession
511: *
512: * @param create
513: *
514: */
515: public HttpSession getSession(boolean create) {
516: return request.getSession(create);
517: }
518:
519: /**
520: * Method getSession
521: *
522: */
523: public HttpSession getSession() {
524: return request.getSession();
525: }
526:
527: /**
528: * Method isRequestedSessionIdValid
529: *
530: */
531: public boolean isRequestedSessionIdValid() {
532: return request.isRequestedSessionIdValid();
533: }
534:
535: /**
536: * Method isRequestedSessionIdFromCookie
537: *
538: */
539: public boolean isRequestedSessionIdFromCookie() {
540: return request.isRequestedSessionIdFromCookie();
541: }
542:
543: /**
544: * Method isRequestedSessionIdFromURL
545: *
546: */
547: public boolean isRequestedSessionIdFromURL() {
548: return request.isRequestedSessionIdFromURL();
549: }
550:
551: /**
552: * Method isRequestedSessionIdFromUrl
553: * @deprecated use {@link #isRequestedSessionIdFromURL()} instead
554: */
555: public boolean isRequestedSessionIdFromUrl() {
556: return request.isRequestedSessionIdFromURL();
557: }
558:
559: /* (non-Javadoc)
560: * @see javax.servlet.http.HttpServletRequest#getRequestURL()
561: */
562: public StringBuffer getRequestURL() {
563: // TODO Auto-generated method stub
564: return null;
565: }
566:
567: /* (non-Javadoc)
568: * @see javax.servlet.ServletRequest#getParameterMap()
569: */
570: public Map getParameterMap() {
571: // TODO Auto-generated method stub
572: return null;
573: }
574:
575: /* (non-Javadoc)
576: * @see javax.servlet.ServletRequest#setCharacterEncoding(java.lang.String)
577: */
578: public void setCharacterEncoding(String arg0)
579: throws UnsupportedEncodingException {
580: // TODO Auto-generated method stub
581:
582: }
583:
584: }
|