KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portlet > multipart > MultipartActionRequest


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.portlet.multipart;
17
18 import org.apache.cocoon.servlet.multipart.PartOnDisk;
19
20 import javax.portlet.ActionRequest;
21 import javax.portlet.PortalContext;
22 import javax.portlet.PortletMode;
23 import javax.portlet.PortletPreferences;
24 import javax.portlet.PortletSession;
25 import javax.portlet.WindowState;
26
27 import java.io.BufferedReader JavaDoc;
28 import java.io.File JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.InputStream JavaDoc;
31 import java.io.UnsupportedEncodingException JavaDoc;
32 import java.security.Principal JavaDoc;
33 import java.util.Enumeration JavaDoc;
34 import java.util.Hashtable JavaDoc;
35 import java.util.Locale JavaDoc;
36 import java.util.Map JavaDoc;
37 import java.util.Vector JavaDoc;
38
39 /**
40  * Portlet action request wrapper for multipart parser.
41  *
42  * @author <a HREF="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
43  * @version CVS $Id: MultipartActionRequest.java 30932 2004-07-29 17:35:38Z vgritsenko $
44  */

45 public class MultipartActionRequest implements ActionRequest {
46
47     /** The wrapped request */
48     private ActionRequest request = null;
49
50     /** The submitted parts */
51     private Hashtable JavaDoc values = null;
52
53     /**
54      * Create this wrapper around the given request and including the given
55      * parts.
56      */

57     public MultipartActionRequest(ActionRequest request, Hashtable JavaDoc values) {
58         this.request = request;
59         this.values = values;
60     }
61
62     /**
63      * Cleanup eventually uploaded parts that were saved on disk
64      */

65     public void cleanup() throws IOException JavaDoc {
66         Enumeration JavaDoc e = getParameterNames();
67         while (e.hasMoreElements()) {
68             Object JavaDoc o = get((String JavaDoc) e.nextElement());
69             if (o instanceof PartOnDisk) {
70                 File JavaDoc file = ((PartOnDisk) o).getFile();
71                 file.delete();
72             }
73         }
74     }
75
76     /**
77      * Method get
78      *
79      * @param name
80      */

81     public Object JavaDoc get(String JavaDoc name) {
82         Object JavaDoc result = null;
83
84         if (values != null) {
85             result = values.get(name);
86
87             if (result instanceof Vector JavaDoc) {
88                 if (((Vector JavaDoc) result).size() == 1) {
89                     return ((Vector JavaDoc) result).elementAt(0);
90                 } else {
91                     return result;
92                 }
93             }
94         }
95
96         // TODO: Test multipart form with parameter in action="" attribute
97
if (result == null) {
98             String JavaDoc[] array = request.getParameterValues(name);
99             Vector JavaDoc vec = new Vector JavaDoc();
100
101             if (array != null) {
102                 for (int i = 0; i < array.length; i++) {
103                     vec.addElement(array[i]);
104                 }
105
106                 if (vec.size() == 1) {
107                     result = vec.elementAt(0);
108                 } else {
109                     result = vec;
110                 }
111             }
112         }
113
114         return result;
115     }
116
117     /**
118      * Method getParameterNames
119      */

120     public Enumeration JavaDoc getParameterNames() {
121         if (values != null) {
122             return values.keys();
123         } else {
124             return request.getParameterNames();
125         }
126     }
127
128     /**
129      * Method getParameter
130      *
131      * @param name
132      */

133     public String JavaDoc getParameter(String JavaDoc name) {
134         Object JavaDoc value = get(name);
135         String JavaDoc result = null;
136
137         if (value != null) {
138             if (value instanceof Vector JavaDoc) {
139                 value = ((Vector JavaDoc) value).elementAt(0);
140             }
141
142             result = value.toString();
143         }
144
145         return result;
146     }
147
148     /**
149      * Method getParameterValues
150      *
151      * @param name
152      */

153     public String JavaDoc[] getParameterValues(String JavaDoc name) {
154         if (values != null) {
155             Object JavaDoc value = get(name);
156
157             if (value != null) {
158                 if (value instanceof Vector JavaDoc) {
159                     String JavaDoc[] results = new String JavaDoc[((Vector JavaDoc) value).size()];
160                     for (int i = 0; i < ((Vector JavaDoc) value).size(); i++) {
161                         results[i] = ((Vector JavaDoc) value).elementAt(i).toString();
162                     }
163                     return results;
164
165                 } else {
166                     return new String JavaDoc[]{value.toString()};
167                 }
168             }
169
170             return null;
171         } else {
172             return request.getParameterValues(name);
173         }
174     }
175
176     /**
177      * Method getAttribute
178      *
179      * @param name
180      */

181     public Object JavaDoc getAttribute(String JavaDoc name) {
182         return request.getAttribute(name);
183     }
184
185     /**
186      * Method getAttributeNames
187      */

188     public Enumeration JavaDoc getAttributeNames() {
189         return request.getAttributeNames();
190     }
191
192     /**
193      * Method getCharacterEncoding
194      */

195     public String JavaDoc getCharacterEncoding() {
196         return request.getCharacterEncoding();
197     }
198
199     /**
200      * Method getContentLength
201      */

202     public int getContentLength() {
203         return request.getContentLength();
204     }
205
206     /**
207      * Method getContentType
208      */

209     public String JavaDoc getContentType() {
210         return request.getContentType();
211     }
212
213     /**
214      * Method getInputStream
215      *
216      * @throws IOException
217      */

218     public InputStream JavaDoc getInputStream() throws IOException JavaDoc {
219         return request.getPortletInputStream();
220     }
221
222     /**
223      * Method getScheme
224      */

225     public String JavaDoc getScheme() {
226         return request.getScheme();
227     }
228
229     /**
230      * Method getServerName
231      */

232     public String JavaDoc getServerName() {
233         return request.getServerName();
234     }
235
236     /**
237      * Method getServerPort
238      */

239     public int getServerPort() {
240         return request.getServerPort();
241     }
242
243     /**
244      * Method getReader
245      *
246      * @throws IOException
247      */

248     public BufferedReader JavaDoc getReader() throws IOException JavaDoc {
249         return request.getReader();
250     }
251
252     /**
253      * Method setAttribute
254      *
255      * @param name
256      * @param o
257      */

258     public void setAttribute(String JavaDoc name, Object JavaDoc o) {
259         request.setAttribute(name, o);
260     }
261
262     /**
263      * Method removeAttribute
264      *
265      * @param name
266      */

267     public void removeAttribute(String JavaDoc name) {
268         request.removeAttribute(name);
269     }
270
271     /**
272      * Method getLocale
273      */

274     public Locale JavaDoc getLocale() {
275         return request.getLocale();
276     }
277
278     /**
279      * Method getLocales
280      */

281     public Enumeration JavaDoc getLocales() {
282         return request.getLocales();
283     }
284
285     /**
286      * Method isSecure
287      */

288     public boolean isSecure() {
289         return request.isSecure();
290     }
291
292     /**
293      * Method getAuthType
294      */

295     public String JavaDoc getAuthType() {
296         return request.getAuthType();
297     }
298
299     /**
300      * Method getContextPath
301      */

302     public String JavaDoc getContextPath() {
303         return request.getContextPath();
304     }
305
306     /**
307      * Method getRemoteUser
308      *
309      */

310     public String JavaDoc getRemoteUser() {
311         return request.getRemoteUser();
312     }
313
314     /**
315      * Method isUserInRole
316      *
317      * @param role
318      */

319     public boolean isUserInRole(String JavaDoc role) {
320         return request.isUserInRole(role);
321     }
322
323     /**
324      * Method getUserPrincipal
325      */

326     public Principal JavaDoc getUserPrincipal() {
327         return request.getUserPrincipal();
328     }
329
330     /**
331      * Method getRequestedSessionId
332      */

333     public String JavaDoc getRequestedSessionId() {
334         return request.getRequestedSessionId();
335     }
336
337     /**
338      * Method getSession
339      *
340      * @param create
341      */

342     public PortletSession getPortletSession(boolean create) {
343         return request.getPortletSession(create);
344     }
345
346     /**
347      * Method getSession
348      */

349     public PortletSession getPortletSession() {
350         return request.getPortletSession();
351     }
352
353     /**
354      * Method isRequestedSessionIdValid
355      */

356     public boolean isRequestedSessionIdValid() {
357         return request.isRequestedSessionIdValid();
358     }
359
360
361     /* (non-Javadoc)
362      * @see javax.portlet.ActionRequest#getPortletInputStream()
363      */

364     public InputStream JavaDoc getPortletInputStream() throws IOException JavaDoc {
365         return request.getPortletInputStream();
366     }
367
368     /* (non-Javadoc)
369      * @see javax.portlet.ActionRequest#setCharacterEncoding(java.lang.String)
370      */

371     public void setCharacterEncoding(String JavaDoc enc) throws UnsupportedEncodingException JavaDoc {
372         request.setCharacterEncoding(enc);
373     }
374
375     /* (non-Javadoc)
376      * @see javax.portlet.PortletRequest#getParameterMap()
377      */

378     public Map JavaDoc getParameterMap() {
379         return request.getParameterMap();
380     }
381
382     /* (non-Javadoc)
383      * @see javax.portlet.PortletRequest#getPortalContext()
384      */

385     public PortalContext getPortalContext() {
386         return request.getPortalContext();
387     }
388
389     /* (non-Javadoc)
390      * @see javax.portlet.PortletRequest#getPortletMode()
391      */

392     public PortletMode getPortletMode() {
393         return request.getPortletMode();
394     }
395
396     /* (non-Javadoc)
397      * @see javax.portlet.PortletRequest#getPreferences()
398      */

399     public PortletPreferences getPreferences() {
400         return request.getPreferences();
401     }
402
403     /* (non-Javadoc)
404      * @see javax.portlet.PortletRequest#getProperties(java.lang.String)
405      */

406     public Enumeration JavaDoc getProperties(String JavaDoc name) {
407         return request.getProperties(name);
408     }
409
410     /* (non-Javadoc)
411      * @see javax.portlet.PortletRequest#getProperty(java.lang.String)
412      */

413     public String JavaDoc getProperty(String JavaDoc name) {
414         return request.getProperty(name);
415     }
416
417     /* (non-Javadoc)
418      * @see javax.portlet.PortletRequest#getPropertyNames()
419      */

420     public Enumeration JavaDoc getPropertyNames() {
421         return request.getPropertyNames();
422     }
423
424     /* (non-Javadoc)
425      * @see javax.portlet.PortletRequest#getResponseContentType()
426      */

427     public String JavaDoc getResponseContentType() {
428         return request.getResponseContentType();
429     }
430
431     /* (non-Javadoc)
432      * @see javax.portlet.PortletRequest#getResponseContentTypes()
433      */

434     public Enumeration JavaDoc getResponseContentTypes() {
435         return request.getResponseContentTypes();
436     }
437
438     /* (non-Javadoc)
439      * @see javax.portlet.PortletRequest#getWindowState()
440      */

441     public WindowState getWindowState() {
442         return request.getWindowState();
443     }
444
445     /* (non-Javadoc)
446      * @see javax.portlet.PortletRequest#isPortletModeAllowed(javax.portlet.PortletMode)
447      */

448     public boolean isPortletModeAllowed(PortletMode mode) {
449         return request.isPortletModeAllowed(mode);
450     }
451
452     /* (non-Javadoc)
453      * @see javax.portlet.PortletRequest#isWindowStateAllowed(javax.portlet.WindowState)
454      */

455     public boolean isWindowStateAllowed(WindowState state) {
456         return request.isWindowStateAllowed(state);
457     }
458 }
459
Popular Tags