001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.dispatcher.multipart;
006:
007: import com.oreilly.servlet.MultipartRequest;
008:
009: import javax.servlet.http.HttpServletRequest;
010: import java.io.File;
011: import java.io.IOException;
012: import java.util.Collections;
013: import java.util.Enumeration;
014: import java.util.List;
015:
016: /**
017: * Multipart form data request adapter for Jason Hunter's
018: * <a href="http://www.servlets.com/cos/index.html" target="_blank">multipart utils</a>
019: * (COS == com.oreilly.servlet).
020: *
021: * @author <a href="mailto:matt@smallleap.com">Matt Baldree</a> (modified for WW's use)
022: * @author <a href="mailto:scott@atlassian.com">Scott Farquhar</a> (added i18n handling (WW-109))
023: */
024: public class CosMultiPartRequest extends MultiPartRequest {
025:
026: private MultipartRequest multi;
027:
028: /**
029: * Creates a new request wrapper to handle multi-part data using methods adapted from the COS
030: * multipart classes (see class description).
031: *
032: * @param maxSize maximum size post allowed
033: * @param saveDir the directory to save off the file
034: * @param servletRequest the request containing the multipart
035: */
036: public CosMultiPartRequest(HttpServletRequest servletRequest,
037: String saveDir, int maxSize) throws IOException {
038: String encoding = getEncoding();
039:
040: if (encoding != null) {
041: multi = new MultipartRequest(servletRequest, saveDir,
042: maxSize, encoding);
043: } else {
044: multi = new MultipartRequest(servletRequest, saveDir,
045: maxSize);
046: }
047: }
048:
049: public Enumeration getFileParameterNames() {
050: return multi.getFileNames();
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: return new String[] { multi.getFile(fieldName).getName() };
063: }
064:
065: public String[] getFilesystemName(String name) {
066: return new String[] { multi.getFilesystemName(name) };
067: }
068:
069: public String getParameter(String name) {
070: return multi.getParameter(name);
071: }
072:
073: public Enumeration getParameterNames() {
074: return multi.getParameterNames();
075: }
076:
077: public String[] getParameterValues(String name) {
078: return multi.getParameterValues(name);
079: }
080:
081: public List getErrors() {
082: return Collections.EMPTY_LIST;
083: }
084:
085: /**
086: * Set the encoding for the uploaded parameters. This needs to be set if you are using character sets
087: * other than ASCII.<p>
088: * <p/>
089: * The encoding is looked up from the configuration setting 'webwork.i18n.encoding'. This is usually set in
090: * default.properties and webwork.properties.
091: */
092: private static String getEncoding() {
093: return "utf-8";
094:
095: //todo: configuration in xwork needs to support non-action level config
096:
097: /*
098: String encoding = null;
099: try {
100:
101: //encoding = Configuration.getString("webwork.i18n.encoding");
102: } catch (IllegalArgumentException e) {
103: LogFactory.getLog(PellMultiPartRequest.class).info("Could not get encoding property 'webwork.i18n.encoding' for file upload. Using system default");
104: }
105: return encoding;
106: */
107: }
108: }
|