View Javadoc

1   /*
2    * $Id: PellMultiPartRequest.java 651946 2008-04-27 13:41:38Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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          //this needs to be synchronised, as we should not change the encoding at the same time as
76          //calling the constructor.  See javadoc for MultipartRequest.setEncoding().
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         // TODO - not sure about this - is this the filename of the actual file or
102         // TODO - the uploaded filename as provided by the browser?
103         // TODO - Not sure what version of Pell this class uses as it doesn't seem to be the latest
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                 //NB: This should never be called at the same time as the constructor for
154                 //ServletMultiPartRequest, as it can cause problems.
155                 //See javadoc for MultipartRequest.setEncoding()
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 }