1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.struts2.dispatcher.multipart;
19
20 import org.apache.struts2.config.Settings;
21 import org.apache.struts2.StrutsConstants;
22 import http.utils.multipartrequest.ServletMultipartRequest;
23
24 import javax.servlet.http.HttpServletRequest;
25 import java.io.File;
26 import java.io.IOException;
27 import java.io.UnsupportedEncodingException;
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.Enumeration;
31 import java.util.List;
32
33
34 /***
35 * Multipart form data request adapter for Jason Pell's multipart utils package.
36 *
37 */
38 public class PellMultiPartRequest extends MultiPartRequest {
39
40 private ServletMultipartRequest multi;
41
42
43 /***
44 * Creates a new request wrapper to handle multi-part data using methods adapted from Jason Pell's
45 * multipart classes (see class description).
46 *
47 * @param maxSize maximum size post allowed
48 * @param saveDir the directory to save off the file
49 * @param servletRequest the request containing the multipart
50 */
51 public PellMultiPartRequest(HttpServletRequest servletRequest, String saveDir, int maxSize) throws IOException {
52
53
54 synchronized (this) {
55 setEncoding();
56 multi = new ServletMultipartRequest(servletRequest, saveDir, maxSize);
57 }
58 }
59
60
61 public Enumeration getFileParameterNames() {
62 return multi.getFileParameterNames();
63 }
64
65 public String[] getContentType(String fieldName) {
66 return new String[]{multi.getContentType(fieldName)};
67 }
68
69 public File[] getFile(String fieldName) {
70 return new File[]{multi.getFile(fieldName)};
71 }
72
73 public String[] getFileNames(String fieldName) {
74
75
76
77
78 return new String[]{multi.getFile(fieldName).getName()};
79 }
80
81 public String[] getFilesystemName(String fieldName) {
82 return new String[]{multi.getFileSystemName(fieldName)};
83 }
84
85 public String getParameter(String name) {
86 return multi.getURLParameter(name);
87 }
88
89 public Enumeration getParameterNames() {
90 return multi.getParameterNames();
91 }
92
93 public String[] getParameterValues(String name) {
94 Enumeration enumeration = multi.getURLParameters(name);
95
96 if (!enumeration.hasMoreElements()) {
97 return null;
98 }
99
100 List values = new ArrayList();
101
102 while (enumeration.hasMoreElements()) {
103 values.add(enumeration.nextElement());
104 }
105
106 return (String[]) values.toArray(new String[values.size()]);
107 }
108
109 public List getErrors() {
110 return Collections.EMPTY_LIST;
111 }
112
113 /***
114 * Sets the encoding for the uploaded params. This needs to be set if you are using character sets other than
115 * ASCII.
116 * <p/>
117 * The encoding is looked up from the configuration setting 'struts.i18n.encoding'. This is usually set in
118 * default.properties & struts.properties.
119 */
120 private static void setEncoding() {
121 String encoding = null;
122
123 try {
124 encoding = Settings.get(StrutsConstants.STRUTS_I18N_ENCODING);
125
126 if (encoding != null) {
127
128
129
130 http.utils.multipartrequest.MultipartRequest.setEncoding(encoding);
131 } else {
132 http.utils.multipartrequest.MultipartRequest.setEncoding("UTF-8");
133 }
134 } catch (IllegalArgumentException e) {
135 log.info("Could not get encoding property 'struts.i18n.encoding' for file upload. Using system default");
136 } catch (UnsupportedEncodingException e) {
137 log.error("Encoding " + encoding + " is not a valid encoding. Please check your struts.properties file.");
138 }
139 }
140 }