001: /*
002: * $Id: MultiPartRequestWrapper.java 474787 2006-11-14 13:51:15Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.dispatcher.multipart;
022:
023: import java.io.File;
024: import java.io.IOException;
025: import java.util.ArrayList;
026: import java.util.Collection;
027: import java.util.Enumeration;
028: import java.util.HashMap;
029: import java.util.Map;
030: import java.util.Vector;
031:
032: import javax.servlet.http.HttpServletRequest;
033:
034: import org.apache.commons.logging.Log;
035: import org.apache.commons.logging.LogFactory;
036: import org.apache.struts2.dispatcher.StrutsRequestWrapper;
037:
038: /**
039: * Parse a multipart request and provide a wrapper around the request. The parsing implementation used
040: * depends on the <tt>struts.multipart.parser</tt> setting. It should be set to a class which
041: * extends {@link org.apache.struts2.dispatcher.multipart.MultiPartRequest}.
042: * <p/>
043: * The <tt>struts.multipart.parser</tt> property should be set to <tt>jakarta</tt> for the
044: * Jakarta implementation, <tt>pell</tt> for the Pell implementation and <tt>cos</tt> for the Jason Hunter
045: * implementation.
046: * <p/>
047: * The files are uploaded when the object is instantiated. If there are any errors they are logged using
048: * {@link #addError(String)}. An action handling a multipart form should first check {@link #hasErrors()}
049: * before doing any other processing.
050: * <p/>
051: * An alternate implementation, PellMultiPartRequest, is provided as a plugin.
052: *
053: */
054: public class MultiPartRequestWrapper extends StrutsRequestWrapper {
055: protected static final Log log = LogFactory
056: .getLog(MultiPartRequestWrapper.class);
057:
058: Collection<String> errors;
059: MultiPartRequest multi;
060:
061: /**
062: * Process file downloads and log any errors.
063: *
064: * @param request Our HttpServletRequest object
065: * @param saveDir Target directory for any files that we save
066: * @param multiPartRequest Our MultiPartRequest object
067: */
068: public MultiPartRequestWrapper(MultiPartRequest multiPartRequest,
069: HttpServletRequest request, String saveDir) {
070: super (request);
071:
072: multi = multiPartRequest;
073: try {
074: multi.parse(request, saveDir);
075: for (Object o : multi.getErrors()) {
076: String error = (String) o;
077: addError(error);
078: }
079: } catch (IOException e) {
080: addError("Cannot parse request: " + e.toString());
081: }
082: }
083:
084: /**
085: * Get an enumeration of the parameter names for uploaded files
086: *
087: * @return enumeration of parameter names for uploaded files
088: */
089: public Enumeration<String> getFileParameterNames() {
090: if (multi == null) {
091: return null;
092: }
093:
094: return multi.getFileParameterNames();
095: }
096:
097: /**
098: * Get an array of content encoding for the specified input field name or <tt>null</tt> if
099: * no content type was specified.
100: *
101: * @param name input field name
102: * @return an array of content encoding for the specified input field name
103: */
104: public String[] getContentTypes(String name) {
105: if (multi == null) {
106: return null;
107: }
108:
109: return multi.getContentType(name);
110: }
111:
112: /**
113: * Get a {@link java.io.File[]} for the given input field name.
114: *
115: * @param fieldName input field name
116: * @return a File[] object for files associated with the specified input field name
117: */
118: public File[] getFiles(String fieldName) {
119: if (multi == null) {
120: return null;
121: }
122:
123: return multi.getFile(fieldName);
124: }
125:
126: /**
127: * Get a String array of the file names for uploaded files
128: *
129: * @param fieldName Field to check for file names.
130: * @return a String[] of file names for uploaded files
131: */
132: public String[] getFileNames(String fieldName) {
133: if (multi == null) {
134: return null;
135: }
136:
137: return multi.getFileNames(fieldName);
138: }
139:
140: /**
141: * Get the filename(s) of the file(s) uploaded for the given input field name.
142: * Returns <tt>null</tt> if the file is not found.
143: *
144: * @param fieldName input field name
145: * @return the filename(s) of the file(s) uploaded for the given input field name or
146: * <tt>null</tt> if name not found.
147: */
148: public String[] getFileSystemNames(String fieldName) {
149: if (multi == null) {
150: return null;
151: }
152:
153: return multi.getFilesystemName(fieldName);
154: }
155:
156: /**
157: * @see javax.servlet.http.HttpServletRequest#getParameter(String)
158: */
159: public String getParameter(String name) {
160: return ((multi == null) || (multi.getParameter(name) == null)) ? super
161: .getParameter(name)
162: : multi.getParameter(name);
163: }
164:
165: /**
166: * @see javax.servlet.http.HttpServletRequest#getParameterMap()
167: */
168: public Map getParameterMap() {
169: Map<String, String[]> map = new HashMap<String, String[]>();
170: Enumeration enumeration = getParameterNames();
171:
172: while (enumeration.hasMoreElements()) {
173: String name = (String) enumeration.nextElement();
174: map.put(name, this .getParameterValues(name));
175: }
176:
177: return map;
178: }
179:
180: /**
181: * @see javax.servlet.http.HttpServletRequest#getParameterNames()
182: */
183: public Enumeration getParameterNames() {
184: if (multi == null) {
185: return super .getParameterNames();
186: } else {
187: return mergeParams(multi.getParameterNames(), super
188: .getParameterNames());
189: }
190: }
191:
192: /**
193: * @see javax.servlet.http.HttpServletRequest#getParameterValues(String)
194: */
195: public String[] getParameterValues(String name) {
196: return ((multi == null) || (multi.getParameterValues(name) == null)) ? super
197: .getParameterValues(name)
198: : multi.getParameterValues(name);
199: }
200:
201: /**
202: * Returns <tt>true</tt> if any errors occured when parsing the HTTP multipart request, <tt>false</tt> otherwise.
203: *
204: * @return <tt>true</tt> if any errors occured when parsing the HTTP multipart request, <tt>false</tt> otherwise.
205: */
206: public boolean hasErrors() {
207: return !((errors == null) || errors.isEmpty());
208: }
209:
210: /**
211: * Returns a collection of any errors generated when parsing the multipart request.
212: *
213: * @return the error Collection.
214: */
215: public Collection<String> getErrors() {
216: return errors;
217: }
218:
219: /**
220: * Adds an error message.
221: *
222: * @param anErrorMessage the error message to report.
223: */
224: protected void addError(String anErrorMessage) {
225: if (errors == null) {
226: errors = new ArrayList<String>();
227: }
228:
229: errors.add(anErrorMessage);
230: }
231:
232: /**
233: * Merges 2 enumeration of parameters as one.
234: *
235: * @param params1 the first enumeration.
236: * @param params2 the second enumeration.
237: * @return a single Enumeration of all elements from both Enumerations.
238: */
239: protected Enumeration mergeParams(Enumeration params1,
240: Enumeration params2) {
241: Vector temp = new Vector();
242:
243: while (params1.hasMoreElements()) {
244: temp.add(params1.nextElement());
245: }
246:
247: while (params2.hasMoreElements()) {
248: temp.add(params2.nextElement());
249: }
250:
251: return temp.elements();
252: }
253: }
|