KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > servlet > upload > MultipartRequestWrapper


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.servlet.upload;
4
5 import jodd.servlet.ServletUtil;
6
7 import javax.servlet.http.HttpServletRequestWrapper JavaDoc;
8 import javax.servlet.http.HttpServletRequest JavaDoc;
9 import java.util.Map JavaDoc;
10 import java.util.HashMap JavaDoc;
11 import java.util.Enumeration JavaDoc;
12 import java.util.Collections JavaDoc;
13 import java.io.IOException JavaDoc;
14
15 /**
16  * Multipart HTTP servlet request wrapper.
17  * @see MultipartRequest
18  */

19 public class MultipartRequestWrapper extends HttpServletRequestWrapper JavaDoc {
20
21     // ---------------------------------------------------------------- construction
22

23     MultipartRequest mreq;
24     HttpServletRequest JavaDoc req;
25
26     public MultipartRequestWrapper(HttpServletRequest JavaDoc request, FileUploadFactory fileUploadFactory, String JavaDoc encoding) throws IOException JavaDoc {
27         super(request);
28         req = request;
29         if (ServletUtil.isMultipartRequest(request)) {
30             mreq = MultipartRequest.getParsedInstance(request, fileUploadFactory, encoding);
31         }
32     }
33
34     public MultipartRequestWrapper(HttpServletRequest JavaDoc request, FileUploadFactory fileUploadFactory) throws IOException JavaDoc {
35         super(request);
36         if (ServletUtil.isMultipartRequest(request)) {
37             mreq = MultipartRequest.getParsedInstance(request, fileUploadFactory);
38         }
39     }
40
41     public MultipartRequestWrapper(MultipartRequest mpreq) {
42         super(mpreq.getServletRequest());
43         mreq = mpreq;
44     }
45
46     /**
47      * Returns {@link MultipartRequest} instance or <code>null</code> if request is not
48      * multipart.
49      */

50     public MultipartRequest getMultipartRequest() {
51         return mreq;
52     }
53
54     /**
55      * Returns <code>true</code> if request is multipart.
56      */

57     public boolean isMultipart() {
58         return mreq != null;
59     }
60
61     // ---------------------------------------------------------------- methods
62

63     /**
64      * Get an enumeration of the parameter names for uploaded files
65      */

66     public Enumeration JavaDoc getFileParameterNames() {
67         if (mreq == null) {
68             return null;
69         }
70         return Collections.enumeration(mreq.getFileParameterNames());
71     }
72
73     /**
74      * Get a {@link FileUpload} array for the given input field name.
75      *
76      * @param fieldName input field name
77      * @return a File[] object for files associated with the specified input field name
78      */

79     public FileUpload[] getFiles(String JavaDoc fieldName) {
80         if (mreq == null) {
81             return null;
82         }
83         return mreq.getFiles(fieldName);
84     }
85
86     public FileUpload getFile(String JavaDoc fieldName) {
87         if (mreq == null) {
88             return null;
89         }
90         return mreq.getFile(fieldName);
91     }
92
93
94     /**
95      * @see javax.servlet.http.HttpServletRequest#getParameter(String)
96      */

97     public String JavaDoc getParameter(String JavaDoc name) {
98         if (mreq == null) {
99             return super.getParameter(name);
100         }
101         return mreq.getParameter(name);
102     }
103
104     /**
105      * @see javax.servlet.http.HttpServletRequest#getParameterMap()
106      */

107     public Map JavaDoc getParameterMap() {
108         if (mreq == null) {
109             return super.getParameterMap();
110         }
111         Map JavaDoc map = new HashMap JavaDoc();
112         Enumeration JavaDoc enumeration = getParameterNames();
113         while (enumeration.hasMoreElements()) {
114             String JavaDoc name = (String JavaDoc) enumeration.nextElement();
115             map.put(name, this.getParameterValues(name));
116         }
117         return map;
118     }
119
120     /**
121      * @see javax.servlet.http.HttpServletRequest#getParameterNames()
122      */

123     public Enumeration JavaDoc getParameterNames() {
124         if (mreq == null) {
125             return super.getParameterNames();
126         }
127         return Collections.enumeration(mreq.getParameterNames());
128     }
129
130     /**
131      * @see javax.servlet.http.HttpServletRequest#getParameterValues(String)
132      */

133     public String JavaDoc[] getParameterValues(String JavaDoc name) {
134         if (mreq == null) {
135             return super.getParameterValues(name);
136         }
137         return mreq.getParameterValues(name);
138     }
139
140 }
141
Popular Tags