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