View Javadoc

1   /*
2    * $Id: PellMultiPartRequest.java 440597 2006-09-06 03:34:39Z wsmoak $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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          //this needs to be synchronised, as we should not change the encoding at the same time as
53          //calling the constructor.  See javadoc for MultipartRequest.setEncoding().
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          // TODO - not sure about this - is this the filename of the actual file or
76          // TODO - the uploaded filename as provided by the browser?
77          // TODO - Not sure what version of Pell this class uses as it doesn't seem to be the latest
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                 //NB: This should never be called at the same time as the constructor for
128                 //ServletMultiPartRequest, as it can cause problems.
129                 //See javadoc for MultipartRequest.setEncoding()
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 }