KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > dispatcher > multipart > PellMultiPartRequest


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.webwork.dispatcher.multipart;
6
7 import com.opensymphony.webwork.config.Configuration;
8 import http.utils.multipartrequest.ServletMultipartRequest;
9
10 import javax.servlet.http.HttpServletRequest JavaDoc;
11 import java.io.File JavaDoc;
12 import java.io.IOException JavaDoc;
13 import java.io.UnsupportedEncodingException JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Collections JavaDoc;
16 import java.util.Enumeration JavaDoc;
17 import java.util.List JavaDoc;
18
19
20 /**
21  * Multipart form data request adapter for Jason Pell's multipart utils package.
22  *
23  * @author <a HREF="matt@smallleap.com">Matt Baldree</a> (modified for WW's use)
24  * @author <a HREF="scott@atlassian.com">Scott Farquhar</a> (added i18n handling (WW-109))
25  */

26 public class PellMultiPartRequest extends MultiPartRequest {
27     //~ Instance fields ////////////////////////////////////////////////////////
28

29     private ServletMultipartRequest multi;
30
31     //~ Constructors ///////////////////////////////////////////////////////////
32

33     /**
34      * Creates a new request wrapper to handle multi-part data using methods adapted from Jason Pell's
35      * multipart classes (see class description).
36      *
37      * @param maxSize maximum size post allowed
38      * @param saveDir the directory to save off the file
39      * @param servletRequest the request containing the multipart
40      */

41     public PellMultiPartRequest(HttpServletRequest JavaDoc servletRequest, String JavaDoc saveDir, int maxSize) throws IOException JavaDoc {
42         //this needs to be synchronised, as we should not change the encoding at the same time as
43
//calling the constructor. See javadoc for MultipartRequest.setEncoding().
44
synchronized (this) {
45             setEncoding();
46             multi = new ServletMultipartRequest(servletRequest, saveDir, maxSize);
47         }
48     }
49
50     //~ Methods ////////////////////////////////////////////////////////////////
51

52     public Enumeration JavaDoc getFileParameterNames() {
53         return multi.getFileParameterNames();
54     }
55
56     public String JavaDoc[] getContentType(String JavaDoc fieldName) {
57         return new String JavaDoc[]{multi.getContentType(fieldName)};
58     }
59
60     public File JavaDoc[] getFile(String JavaDoc fieldName) {
61         return new File JavaDoc[]{multi.getFile(fieldName)};
62     }
63
64     public String JavaDoc[] getFileNames(String JavaDoc fieldName) {
65
66         // TODO - not sure about this - is this the filename of the actual file or
67
// TODO - the uploaded filename as provided by the browser?
68
// TODO - Not sure what version of Pell this class uses as it doesn't seem to be the latest
69
return new String JavaDoc[]{multi.getFile(fieldName).getName()};
70     }
71
72     public String JavaDoc[] getFilesystemName(String JavaDoc fieldName) {
73         return new String JavaDoc[]{multi.getFileSystemName(fieldName)};
74     }
75
76     public String JavaDoc getParameter(String JavaDoc name) {
77         return multi.getURLParameter(name);
78     }
79
80     public Enumeration JavaDoc getParameterNames() {
81         return multi.getParameterNames();
82     }
83
84     public String JavaDoc[] getParameterValues(String JavaDoc name) {
85         Enumeration JavaDoc enumeration = multi.getURLParameters(name);
86
87         if (!enumeration.hasMoreElements()) {
88             return null;
89         }
90
91         List JavaDoc values = new ArrayList JavaDoc();
92
93         while (enumeration.hasMoreElements()) {
94             values.add(enumeration.nextElement());
95         }
96
97         return (String JavaDoc[]) values.toArray(new String JavaDoc[values.size()]);
98     }
99
100     public List JavaDoc getErrors() {
101         return Collections.EMPTY_LIST;
102     }
103
104     /**
105      * Sets the encoding for the uploaded params. This needs to be set if you are using character sets other than
106      * ASCII.
107      * <p/>
108      * The encoding is looked up from the configuration setting 'webwork.i18n.encoding'. This is usually set in
109      * default.properties & webwork.properties.
110      */

111     private static void setEncoding() {
112         String JavaDoc encoding = null;
113
114         try {
115             encoding = Configuration.getString("webwork.i18n.encoding");
116
117             if (encoding != null) {
118                 //NB: This should never be called at the same time as the constructor for
119
//ServletMultiPartRequest, as it can cause problems.
120
//See javadoc for MultipartRequest.setEncoding()
121
http.utils.multipartrequest.MultipartRequest.setEncoding(encoding);
122             } else {
123                 http.utils.multipartrequest.MultipartRequest.setEncoding("UTF-8");
124             }
125         } catch (IllegalArgumentException JavaDoc e) {
126             log.info("Could not get encoding property 'webwork.i18n.encoding' for file upload. Using system default");
127         } catch (UnsupportedEncodingException JavaDoc e) {
128             log.error("Encoding " + encoding + " is not a valid encoding. Please check your webwork.properties file.");
129         }
130     }
131 }
132
Popular Tags