View Javadoc

1   package com.atlassian.xwork;
2   
3   import com.opensymphony.webwork.config.Configuration;
4   import com.opensymphony.webwork.dispatcher.multipart.MultiPartRequest;
5   import http.utils.multipartrequest.ServletMultipartRequest;
6   
7   import javax.servlet.http.HttpServletRequest;
8   import java.io.File;
9   import java.io.IOException;
10  import java.io.UnsupportedEncodingException;
11  import java.util.ArrayList;
12  import java.util.Enumeration;
13  import java.util.List;
14  
15  /**
16   * Patched version of {@link com.opensymphony.webwork.dispatcher.multipart.PellMultiPartRequest}.
17   * The original implementation would throw a {@link NullPointerException} if getFileNames() was called for
18   * a non-existent fieldname.
19   *
20   * The entire class had to be duplicated because of the private ServletMultipartRequest
21   */
22  public class PellMultiPartRequest extends MultiPartRequest
23  {
24          //~ Instance fields ////////////////////////////////////////////////////////
25  
26          private ServletMultipartRequest multi;
27  
28          //~ Constructors ///////////////////////////////////////////////////////////
29  
30          /**
31           * Creates a new request wrapper to handle multi-part data using methods adapted from Jason Pell's
32           * multipart classes (see class description).
33           *
34           * @param maxSize        maximum size post allowed
35           * @param saveDir        the directory to save off the file
36           * @param servletRequest the request containing the multipart
37           */
38          public PellMultiPartRequest(HttpServletRequest servletRequest, String saveDir, int maxSize) throws IOException {
39              //this needs to be synchronised, as we should not change the encoding at the same time as
40              //calling the constructor.  See javadoc for MultipartRequest.setEncoding().
41              synchronized (this) {
42                  setEncoding();
43                  multi = new ServletMultipartRequest(servletRequest, saveDir, maxSize);
44              }
45          }
46  
47          //~ Methods ////////////////////////////////////////////////////////////////
48  
49          public Enumeration getFileParameterNames() {
50              return multi.getFileParameterNames();
51          }
52  
53          public String[] getContentType(String fieldName) {
54              return new String[]{multi.getContentType(fieldName)};
55          }
56  
57          public File[] getFile(String fieldName) {
58              return new File[]{multi.getFile(fieldName)};
59          }
60  
61          public String[] getFileNames(String fieldName) {
62              // This is a bug fix from the com.opensymphony.webwork.dispatcher.multipart.PellMultiPartRequest
63              // The original implementation did not check for a null return from multi.getFile()
64              File file = multi.getFile(fieldName);
65              if (file == null)
66                  return null;
67  
68              return new String[]{file.getName()};
69          }
70  
71          public String[] getFilesystemName(String fieldName) {
72              return new String[]{multi.getFileSystemName(fieldName)};
73          }
74  
75          public String getParameter(String name) {
76              return multi.getURLParameter(name);
77          }
78  
79          public Enumeration getParameterNames() {
80              return multi.getParameterNames();
81          }
82  
83          public String[] getParameterValues(String name) {
84              Enumeration enumeration = multi.getURLParameters(name);
85  
86              if (!enumeration.hasMoreElements()) {
87                  return null;
88              }
89  
90              List values = new ArrayList();
91  
92              while (enumeration.hasMoreElements()) {
93                  values.add(enumeration.nextElement());
94              }
95  
96              return (String[]) values.toArray(new String[values.size()]);
97          }
98  
99          /**
100          * Sets the encoding for the uploaded params.  This needs to be set if you are using character sets other than
101          * ASCII.
102          * <p/>
103          * The encoding is looked up from the configuration setting 'webwork.i18n.encoding'.  This is usually set in
104          * default.properties & webwork.properties.
105          */
106         private static void setEncoding() {
107             String encoding = null;
108 
109             try {
110                 encoding = Configuration.getString("webwork.i18n.encoding");
111 
112                 if (encoding != null) {
113                     //NB: This should never be called at the same time as the constructor for
114                     //ServletMultiPartRequest, as it can cause problems.
115                     //See javadoc for MultipartRequest.setEncoding()
116                     http.utils.multipartrequest.MultipartRequest.setEncoding(encoding);
117                 } else {
118                     http.utils.multipartrequest.MultipartRequest.setEncoding("UTF-8");
119                 }
120             } catch (IllegalArgumentException e) {
121                 log.info("Could not get encoding property 'webwork.i18n.encoding' for file upload.  Using system default");
122             } catch (UnsupportedEncodingException e) {
123                 log.error("Encoding " + encoding + " is not a valid encoding.  Please check your webwork.properties file.");
124             }
125         }
126     }
127