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