From b3bd10ae8f9c768fba000f95febbd4e5b99a699b Mon Sep 17 00:00:00 2001 From: "Donald J. Brown" Date: Sat, 25 Mar 2006 03:49:18 +0000 Subject: [PATCH] Readding the Pell fileupload. I believe we can keep the LGPL code, just not ship its jars and mark them as optional git-svn-id: https://svn.apache.org/repos/asf/incubator/webwork2@388696 13f79535-47bb-0310-9956-ffa450edef68 --- ivy.xml | 3 + .../multipart/PellMultiPartRequest.java | 129 ++++++++++++++++++ 2 files changed, 132 insertions(+) create mode 100644 src/java/org/apache/struts/action2/dispatcher/multipart/PellMultiPartRequest.java diff --git a/ivy.xml b/ivy.xml index c57eecaba..26e22783f 100644 --- a/ivy.xml +++ b/ivy.xml @@ -88,6 +88,9 @@ + + + diff --git a/src/java/org/apache/struts/action2/dispatcher/multipart/PellMultiPartRequest.java b/src/java/org/apache/struts/action2/dispatcher/multipart/PellMultiPartRequest.java new file mode 100644 index 000000000..d2d1323fb --- /dev/null +++ b/src/java/org/apache/struts/action2/dispatcher/multipart/PellMultiPartRequest.java @@ -0,0 +1,129 @@ +/* + * Copyright (c) 2002-2003 by OpenSymphony + * All rights reserved. + */ +package org.apache.struts.action2.dispatcher.multipart; + +import org.apache.struts.action2.config.Configuration; +import org.apache.struts.action2.WebWorkConstants; +import http.utils.multipartrequest.ServletMultipartRequest; + +import javax.servlet.http.HttpServletRequest; +import java.io.File; +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Enumeration; +import java.util.List; + + +/** + * Multipart form data request adapter for Jason Pell's multipart utils package. + * + * @author Matt Baldree (modified for WW's use) + * @author Scott Farquhar (added i18n handling (WW-109)) + */ +public class PellMultiPartRequest extends MultiPartRequest { + + private ServletMultipartRequest multi; + + + /** + * Creates a new request wrapper to handle multi-part data using methods adapted from Jason Pell's + * multipart classes (see class description). + * + * @param maxSize maximum size post allowed + * @param saveDir the directory to save off the file + * @param servletRequest the request containing the multipart + */ + public PellMultiPartRequest(HttpServletRequest servletRequest, String saveDir, int maxSize) throws IOException { + //this needs to be synchronised, as we should not change the encoding at the same time as + //calling the constructor. See javadoc for MultipartRequest.setEncoding(). + synchronized (this) { + setEncoding(); + multi = new ServletMultipartRequest(servletRequest, saveDir, maxSize); + } + } + + + public Enumeration getFileParameterNames() { + return multi.getFileParameterNames(); + } + + public String[] getContentType(String fieldName) { + return new String[]{multi.getContentType(fieldName)}; + } + + public File[] getFile(String fieldName) { + return new File[]{multi.getFile(fieldName)}; + } + + public String[] getFileNames(String fieldName) { + + // TODO - not sure about this - is this the filename of the actual file or + // TODO - the uploaded filename as provided by the browser? + // TODO - Not sure what version of Pell this class uses as it doesn't seem to be the latest + return new String[]{multi.getFile(fieldName).getName()}; + } + + public String[] getFilesystemName(String fieldName) { + return new String[]{multi.getFileSystemName(fieldName)}; + } + + public String getParameter(String name) { + return multi.getURLParameter(name); + } + + public Enumeration getParameterNames() { + return multi.getParameterNames(); + } + + public String[] getParameterValues(String name) { + Enumeration enumeration = multi.getURLParameters(name); + + if (!enumeration.hasMoreElements()) { + return null; + } + + List values = new ArrayList(); + + while (enumeration.hasMoreElements()) { + values.add(enumeration.nextElement()); + } + + return (String[]) values.toArray(new String[values.size()]); + } + + public List getErrors() { + return Collections.EMPTY_LIST; + } + + /** + * Sets the encoding for the uploaded params. This needs to be set if you are using character sets other than + * ASCII. + *

+ * The encoding is looked up from the configuration setting 'webwork.i18n.encoding'. This is usually set in + * default.properties & webwork.properties. + */ + private static void setEncoding() { + String encoding = null; + + try { + encoding = Configuration.getString(WebWorkConstants.WEBWORK_I18N_ENCODING); + + if (encoding != null) { + //NB: This should never be called at the same time as the constructor for + //ServletMultiPartRequest, as it can cause problems. + //See javadoc for MultipartRequest.setEncoding() + http.utils.multipartrequest.MultipartRequest.setEncoding(encoding); + } else { + http.utils.multipartrequest.MultipartRequest.setEncoding("UTF-8"); + } + } catch (IllegalArgumentException e) { + log.info("Could not get encoding property 'webwork.i18n.encoding' for file upload. Using system default"); + } catch (UnsupportedEncodingException e) { + log.error("Encoding " + encoding + " is not a valid encoding. Please check your webwork.properties file."); + } + } +}