001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.dispatcher.multipart;
006:
007: import com.opensymphony.webwork.config.Configuration;
008: import com.opensymphony.webwork.WebWorkConstants;
009: import http.utils.multipartrequest.ServletMultipartRequest;
010:
011: import javax.servlet.http.HttpServletRequest;
012: import java.io.File;
013: import java.io.IOException;
014: import java.io.UnsupportedEncodingException;
015: import java.util.ArrayList;
016: import java.util.Collections;
017: import java.util.Enumeration;
018: import java.util.List;
019:
020: /**
021: * Multipart form data request adapter for Jason Pell's multipart utils package.
022: *
023: * @author <a href="matt@smallleap.com">Matt Baldree</a> (modified for WW's use)
024: * @author <a href="scott@atlassian.com">Scott Farquhar</a> (added i18n handling (WW-109))
025: */
026: public class PellMultiPartRequest extends MultiPartRequest {
027:
028: private ServletMultipartRequest multi;
029:
030: /**
031: * Creates a new request wrapper to handle multi-part data using methods adapted from Jason Pell's
032: * multipart classes (see class description).
033: *
034: * @param maxSize maximum size post allowed
035: * @param saveDir the directory to save off the file
036: * @param servletRequest the request containing the multipart
037: */
038: public PellMultiPartRequest(HttpServletRequest servletRequest,
039: String saveDir, int maxSize) throws IOException {
040: //this needs to be synchronised, as we should not change the encoding at the same time as
041: //calling the constructor. See javadoc for MultipartRequest.setEncoding().
042: synchronized (this ) {
043: setEncoding();
044: multi = new ServletMultipartRequest(servletRequest,
045: saveDir, maxSize);
046: }
047: }
048:
049: public Enumeration getFileParameterNames() {
050: return multi.getFileParameterNames();
051: }
052:
053: public String[] getContentType(String fieldName) {
054: return new String[] { multi.getContentType(fieldName) };
055: }
056:
057: public File[] getFile(String fieldName) {
058: return new File[] { multi.getFile(fieldName) };
059: }
060:
061: public String[] getFileNames(String fieldName) {
062:
063: // TODO - not sure about this - is this the filename of the actual file or
064: // TODO - the uploaded filename as provided by the browser?
065: // TODO - Not sure what version of Pell this class uses as it doesn't seem to be the latest
066: return new String[] { multi.getFile(fieldName).getName() };
067: }
068:
069: public String[] getFilesystemName(String fieldName) {
070: return new String[] { multi.getFileSystemName(fieldName) };
071: }
072:
073: public String getParameter(String name) {
074: return multi.getURLParameter(name);
075: }
076:
077: public Enumeration getParameterNames() {
078: return multi.getParameterNames();
079: }
080:
081: public String[] getParameterValues(String name) {
082: Enumeration enumeration = multi.getURLParameters(name);
083:
084: if (!enumeration.hasMoreElements()) {
085: return null;
086: }
087:
088: List values = new ArrayList();
089:
090: while (enumeration.hasMoreElements()) {
091: values.add(enumeration.nextElement());
092: }
093:
094: return (String[]) values.toArray(new String[values.size()]);
095: }
096:
097: public List getErrors() {
098: return Collections.EMPTY_LIST;
099: }
100:
101: /**
102: * Sets the encoding for the uploaded params. This needs to be set if you are using character sets other than
103: * ASCII.
104: * <p/>
105: * The encoding is looked up from the configuration setting 'webwork.i18n.encoding'. This is usually set in
106: * default.properties & webwork.properties.
107: */
108: private static void setEncoding() {
109: String encoding = null;
110:
111: try {
112: encoding = Configuration
113: .getString(WebWorkConstants.WEBWORK_I18N_ENCODING);
114:
115: if (encoding != null) {
116: //NB: This should never be called at the same time as the constructor for
117: //ServletMultiPartRequest, as it can cause problems.
118: //See javadoc for MultipartRequest.setEncoding()
119: http.utils.multipartrequest.MultipartRequest
120: .setEncoding(encoding);
121: } else {
122: http.utils.multipartrequest.MultipartRequest
123: .setEncoding("UTF-8");
124: }
125: } catch (IllegalArgumentException e) {
126: log
127: .info("Could not get encoding property 'webwork.i18n.encoding' for file upload. Using system default");
128: } catch (UnsupportedEncodingException e) {
129: log
130: .error("Encoding "
131: + encoding
132: + " is not a valid encoding. Please check your webwork.properties file.");
133: }
134: }
135: }
|