FileDocCategorySizeDatePackage
MultiPartRequestWrapper.javaAPI DocExample8246Mon Jul 23 13:26:36 BST 2007org.apache.struts2.dispatcher.multipart

MultiPartRequestWrapper

public class MultiPartRequestWrapper extends org.apache.struts2.dispatcher.StrutsRequestWrapper
Parse a multipart request and provide a wrapper around the request. The parsing implementation used depends on the struts.multipart.parser setting. It should be set to a class which extends {@link org.apache.struts2.dispatcher.multipart.MultiPartRequest}.

The struts.multipart.parser property should be set to jakarta for the Jakarta implementation, pell for the Pell implementation and cos for the Jason Hunter implementation.

The files are uploaded when the object is instantiated. If there are any errors they are logged using {@link #addError(String)}. An action handling a multipart form should first check {@link #hasErrors()} before doing any other processing.

An alternate implementation, PellMultiPartRequest, is provided as a plugin.

Fields Summary
protected static final Log
log
Collection
errors
MultiPartRequest
multi
Constructors Summary
public MultiPartRequestWrapper(MultiPartRequest multiPartRequest, HttpServletRequest request, String saveDir)
Process file downloads and log any errors.

param
request Our HttpServletRequest object
param
saveDir Target directory for any files that we save
param
multiPartRequest Our MultiPartRequest object


                                    
           
        super(request);
        
        multi = multiPartRequest;
        try {
            multi.parse(request, saveDir);
            for (Object o : multi.getErrors()) {
                String error = (String) o;
                addError(error);
            }
        } catch (IOException e) {
            addError("Cannot parse request: "+e.toString());
        } 
    
Methods Summary
protected voidaddError(java.lang.String anErrorMessage)
Adds an error message.

param
anErrorMessage the error message to report.

        if (errors == null) {
            errors = new ArrayList<String>();
        }

        errors.add(anErrorMessage);
    
public java.lang.String[]getContentTypes(java.lang.String name)
Get an array of content encoding for the specified input field name or null if no content type was specified.

param
name input field name
return
an array of content encoding for the specified input field name

        if (multi == null) {
            return null;
        }

        return multi.getContentType(name);
    
public java.util.CollectiongetErrors()
Returns a collection of any errors generated when parsing the multipart request.

return
the error Collection.

        return errors;
    
public java.lang.String[]getFileNames(java.lang.String fieldName)
Get a String array of the file names for uploaded files

param
fieldName Field to check for file names.
return
a String[] of file names for uploaded files

        if (multi == null) {
            return null;
        }

        return multi.getFileNames(fieldName);
    
public java.util.EnumerationgetFileParameterNames()
Get an enumeration of the parameter names for uploaded files

return
enumeration of parameter names for uploaded files

        if (multi == null) {
            return null;
        }

        return multi.getFileParameterNames();
    
public java.lang.String[]getFileSystemNames(java.lang.String fieldName)
Get the filename(s) of the file(s) uploaded for the given input field name. Returns null if the file is not found.

param
fieldName input field name
return
the filename(s) of the file(s) uploaded for the given input field name or null if name not found.

        if (multi == null) {
            return null;
        }

        return multi.getFilesystemName(fieldName);
    
public java.io.File[]getFiles(java.lang.String fieldName)
Get a {@link java.io.File[]} for the given input field name.

param
fieldName input field name
return
a File[] object for files associated with the specified input field name

        if (multi == null) {
            return null;
        }

        return multi.getFile(fieldName);
    
public java.lang.StringgetParameter(java.lang.String name)

see
javax.servlet.http.HttpServletRequest#getParameter(String)

        return ((multi == null) || (multi.getParameter(name) == null)) ? super.getParameter(name) : multi.getParameter(name);
    
public java.util.MapgetParameterMap()

see
javax.servlet.http.HttpServletRequest#getParameterMap()

        Map<String, String[]> map = new HashMap<String, String[]>();
        Enumeration enumeration = getParameterNames();

        while (enumeration.hasMoreElements()) {
            String name = (String) enumeration.nextElement();
            map.put(name, this.getParameterValues(name));
        }

        return map;
    
public java.util.EnumerationgetParameterNames()

see
javax.servlet.http.HttpServletRequest#getParameterNames()

        if (multi == null) {
            return super.getParameterNames();
        } else {
            return mergeParams(multi.getParameterNames(), super.getParameterNames());
        }
    
public java.lang.String[]getParameterValues(java.lang.String name)

see
javax.servlet.http.HttpServletRequest#getParameterValues(String)

        return ((multi == null) || (multi.getParameterValues(name) == null)) ? super.getParameterValues(name) : multi.getParameterValues(name);
    
public booleanhasErrors()
Returns true if any errors occured when parsing the HTTP multipart request, false otherwise.

return
true if any errors occured when parsing the HTTP multipart request, false otherwise.

        return !((errors == null) || errors.isEmpty());
    
protected java.util.EnumerationmergeParams(java.util.Enumeration params1, java.util.Enumeration params2)
Merges 2 enumeration of parameters as one.

param
params1 the first enumeration.
param
params2 the second enumeration.
return
a single Enumeration of all elements from both Enumerations.

        Vector temp = new Vector();

        while (params1.hasMoreElements()) {
            temp.add(params1.nextElement());
        }

        while (params2.hasMoreElements()) {
            temp.add(params2.nextElement());
        }

        return temp.elements();