View Javadoc

    1   /*
    2    * $Id: PellMultiPartRequest.java 474191 2006-11-13 08:30:40Z mrdon $
    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  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          //this needs to be synchronised, as we should not change the encoding at the same time as
    67          //calling the constructor.  See javadoc for MultipartRequest.setEncoding().
    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          // TODO - not sure about this - is this the filename of the actual file or
    89          // TODO - the uploaded filename as provided by the browser?
    90          // TODO - Not sure what version of Pell this class uses as it doesn't seem to be the latest
    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                 //NB: This should never be called at the same time as the constructor for
    141                 //ServletMultiPartRequest, as it can cause problems.
    142                 //See javadoc for MultipartRequest.setEncoding()
    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 }