org.barracudamvc.core.helper.servlet
Class MultipartRequestWrapper

java.lang.Object
  extended by javax.servlet.ServletRequestWrapper
      extended by javax.servlet.http.HttpServletRequestWrapper
          extended by org.barracudamvc.core.helper.servlet.DefaultServletRequestWrapper
              extended by org.barracudamvc.core.helper.servlet.MultipartRequestWrapper
All Implemented Interfaces:
javax.servlet.http.HttpServletRequest, javax.servlet.ServletRequest, BarracudaServletRequestWrapper

public class MultipartRequestWrapper
extends DefaultServletRequestWrapper

Handles both simple and multipart requests transparently

For handling multipart stuff, Jakarta Commons Fileupload is used. Otherwise this wrapper's behavior is identical to the superclass. getFileItem* methods are provided to get direct access to file items in a multipart request and an isMultipart() method is provided for querying whether the current request has multipart data in it or not. The only drawback to using this wrapper is the slight overhead involved in doing multipart checks when the request isn't multipart. The performance deficit should, however, be neglegible.

Author:
Stefan Armbruster (sam, 2003-11-15), Shawn A. Wilson, Jacob Kjome

Field Summary
protected  boolean isMultipart
          indicates if the current request is multipart
protected  long maxSize
          maximum allowed size of uploads
protected  List uploadItemList
          contains a list of file items
 
Fields inherited from class org.barracudamvc.core.helper.servlet.DefaultServletRequestWrapper
DEFAULT_ENCODING
 
Fields inherited from interface javax.servlet.http.HttpServletRequest
BASIC_AUTH, CLIENT_CERT_AUTH, DIGEST_AUTH, FORM_AUTH
 
Constructor Summary
MultipartRequestWrapper(javax.servlet.http.HttpServletRequest req)
          Simple constructor, accepts any file size and uses the default FileItemFactory
MultipartRequestWrapper(javax.servlet.http.HttpServletRequest req, long max)
           
MultipartRequestWrapper(javax.servlet.http.HttpServletRequest req, long max, org.apache.commons.fileupload.FileItemFactory fileItemFactory)
          Checks if request is multipart.
 
Method Summary
 org.apache.commons.fileupload.FileItem getFileItem(String name)
          Finds a matching item in uploadItems and returns it.
 Enumeration getFileParameterNames()
          Akin to getParameterNames(), except that only parameter names representing file parameters are applicable here.
 String getParameter(String name)
          Same behavior as ServletRequest.getParameter, but extended to handle parameters when request is multipart.
 Map getParameterMap()
          Same behavior as ServletRequest.getParameterMap, but extended to handle parameters when request is multipart.
 Enumeration getParameterNames()
          Same behavior as ServletRequest.getParameterNames, but extended to handle parameter names when request is multipart.
 String[] getParameterValues(String name)
          Same behavior as ServletRequest.getParameterValues, but extended to handle parameters when request is multipart.
protected  String getUploadItemValueByName(String name)
          Finds a matching item in uploadItems and returns its value as a string.
 boolean isMultipart()
          Specifies whether the current request is multipart
 
Methods inherited from class org.barracudamvc.core.helper.servlet.DefaultServletRequestWrapper
addParameter, getCoreRequest, getMethod, removeAllParameters, removeParameter, resetParameters, setMethod, setRequest
 
Methods inherited from class javax.servlet.http.HttpServletRequestWrapper
getAuthType, getContextPath, getCookies, getDateHeader, getHeader, getHeaderNames, getHeaders, getIntHeader, getPathInfo, getPathTranslated, getQueryString, getRemoteUser, getRequestedSessionId, getRequestURI, getRequestURL, getServletPath, getSession, getSession, getUserPrincipal, isRequestedSessionIdFromCookie, isRequestedSessionIdFromUrl, isRequestedSessionIdFromURL, isRequestedSessionIdValid, isUserInRole
 
Methods inherited from class javax.servlet.ServletRequestWrapper
getAttribute, getAttributeNames, getCharacterEncoding, getContentLength, getContentType, getInputStream, getLocale, getLocales, getProtocol, getReader, getRealPath, getRemoteAddr, getRemoteHost, getRequest, getRequestDispatcher, getScheme, getServerName, getServerPort, isSecure, removeAttribute, setAttribute, setCharacterEncoding
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface javax.servlet.http.HttpServletRequest
getAuthType, getContextPath, getCookies, getDateHeader, getHeader, getHeaderNames, getHeaders, getIntHeader, getPathInfo, getPathTranslated, getQueryString, getRemoteUser, getRequestedSessionId, getRequestURI, getRequestURL, getServletPath, getSession, getSession, getUserPrincipal, isRequestedSessionIdFromCookie, isRequestedSessionIdFromUrl, isRequestedSessionIdFromURL, isRequestedSessionIdValid, isUserInRole
 
Methods inherited from interface javax.servlet.ServletRequest
getAttribute, getAttributeNames, getCharacterEncoding, getContentLength, getContentType, getInputStream, getLocale, getLocales, getProtocol, getReader, getRealPath, getRemoteAddr, getRemoteHost, getRequestDispatcher, getScheme, getServerName, getServerPort, isSecure, removeAttribute, setAttribute, setCharacterEncoding
 

Field Detail

maxSize

protected long maxSize
maximum allowed size of uploads


isMultipart

protected boolean isMultipart
indicates if the current request is multipart


uploadItemList

protected List uploadItemList
contains a list of file items

Constructor Detail

MultipartRequestWrapper

public MultipartRequestWrapper(javax.servlet.http.HttpServletRequest req,
                               long max,
                               org.apache.commons.fileupload.FileItemFactory fileItemFactory)
Checks if request is multipart. If so, initializes upload items.

Parameters:
req - the request
max - the maximum allowed size of uploads, -1 signifies no limit
fileItemFactory - some FileItemFactory implementation, defaults to DiskFileItemFactory if null

MultipartRequestWrapper

public MultipartRequestWrapper(javax.servlet.http.HttpServletRequest req,
                               long max)
Parameters:
req - the request
max - Maximum allowed size of uploads
See Also:
#this(HttpServletRequest, long, FileItemFactory)

MultipartRequestWrapper

public MultipartRequestWrapper(javax.servlet.http.HttpServletRequest req)
Simple constructor, accepts any file size and uses the default FileItemFactory

Parameters:
req -
See Also:
#this(HttpServletRequest, long, FileItemFactory)
Method Detail

getParameter

public String getParameter(String name)
Same behavior as ServletRequest.getParameter, but extended to handle parameters when request is multipart. Only non-file parameters are applicable here.

Specified by:
getParameter in interface javax.servlet.ServletRequest
Overrides:
getParameter in class DefaultServletRequestWrapper
Parameters:
name - a String specifying the name of the parameter
Returns:
a String representing the single value of the parameter
See Also:
ServletRequest.getParameter(String)

getParameterNames

public Enumeration getParameterNames()
Same behavior as ServletRequest.getParameterNames, but extended to handle parameter names when request is multipart. Only parameter names representing non-file parameters are applicable here.

Specified by:
getParameterNames in interface javax.servlet.ServletRequest
Overrides:
getParameterNames in class DefaultServletRequestWrapper
Returns:
an Enumeration of String objects, each String containing the name of a request parameter; or an empty Enumeration if the request has no parameters
See Also:
ServletRequest.getParameterNames()

getFileParameterNames

public Enumeration getFileParameterNames()
Akin to getParameterNames(), except that only parameter names representing file parameters are applicable here. If the request is not multipart or it is but there are no file fields to speak of, an empty Enumeration will be returned.

Returns:
an Enumeration of String objects, each String containing the name of a file request parameter; or an empty Enumeration if the request has no file parameters
See Also:
getParameterNames()

getParameterValues

public String[] getParameterValues(String name)
Same behavior as ServletRequest.getParameterValues, but extended to handle parameters when request is multipart. Only non-file parameters are applicable here.

Specified by:
getParameterValues in interface javax.servlet.ServletRequest
Overrides:
getParameterValues in class DefaultServletRequestWrapper
Parameters:
name - a String containing the name of the parameter whose value is requested
Returns:
an array of String objects containing the parameter's values
See Also:
ServletRequest.getParameterValues(String)

getParameterMap

public Map getParameterMap()
Same behavior as ServletRequest.getParameterMap, but extended to handle parameters when request is multipart. Only non-file parameters are applicable here.

Specified by:
getParameterMap in interface javax.servlet.ServletRequest
Overrides:
getParameterMap in class DefaultServletRequestWrapper
Returns:
an immutable java.util.Map containing parameter names as keys and parameter values as map values. The keys in the parameter map are of type String. The values in the parameter map are of type String array
See Also:
ServletRequest.getParameterMap()

getUploadItemValueByName

protected String getUploadItemValueByName(String name)
Finds a matching item in uploadItems and returns its value as a string. Returns null if the 'name' parameter is null, the named file item doesn't exist, the file item does exist but it isn't a simple form field (in which case the value is not provided as parameter data, only as file data), or the request is not multipart.

Parameters:
name - a String specifying the name of the parameter
Returns:
the result of FileItem.getString(), first trying the encoding of the request and falling back to the system default if that fails
See Also:
getFileItem(String), FileItem.getString(String)

getFileItem

public org.apache.commons.fileupload.FileItem getFileItem(String name)
Finds a matching item in uploadItems and returns it. Returns null if the 'name' parameter is null or the request is not multipart.

Parameters:
name - a String specifying the name of the parameter
Returns:
the matching FileItem

isMultipart

public boolean isMultipart()
Specifies whether the current request is multipart

Returns:
true if multipart, false if not


Copyright © 2006 BarracudaMVC.org All Rights Reserved.