001: package org.gomba;
002:
003: import java.util.List;
004:
005: import javax.servlet.http.HttpServletRequest;
006: import javax.servlet.http.HttpServletRequestWrapper;
007:
008: import org.apache.commons.fileupload.DiskFileUpload;
009: import org.apache.commons.fileupload.FileUploadBase;
010: import org.apache.commons.fileupload.FileUploadException;
011:
012: /**
013: * Adds multipart form handling capabilities to an HTTPServletRequest using
014: * Jakarta Commons FileUpload.
015: *
016: * @author Flavio Tordini
017: * @version $Id: CustomHttpServletRequestWrapper.java,v 1.1 2004/09/28 15:58:22 flaviotordini Exp $
018: */
019: public class CustomHttpServletRequestWrapper extends
020: HttpServletRequestWrapper {
021:
022: private final int multipartSizeThreshold;
023:
024: private final int multipartMaxSize;
025:
026: private final String multipartRepositoryPath;
027:
028: private List multipartItems;
029:
030: /**
031: * Constructor.
032: */
033: public CustomHttpServletRequestWrapper(HttpServletRequest request,
034: int multipartSizeThreshold, int multipartMaxSize,
035: String multipartRepositoryPath) {
036: super (request);
037:
038: this .multipartSizeThreshold = multipartSizeThreshold;
039: this .multipartMaxSize = multipartMaxSize;
040: this .multipartRepositoryPath = multipartRepositoryPath;
041: }
042:
043: /**
044: * Get a List of multipart items. Use the FileUpload API to retrieve values
045: * from the items. <code>FileItem</code> has getter methods, so it's a
046: * snap to use it in the JSP EL.
047: *
048: * @return A list of <code>org.apache.commons.fileupload.FileItem</code>
049: * @throws FileUploadException
050: */
051: public List getMultipartItems() throws FileUploadException {
052: // lazily parse a multipart request
053: if (this .multipartItems == null) {
054: this .multipartItems = parseMultipartRequest();
055: }
056: return this .multipartItems;
057: }
058:
059: /**
060: * @return <code>true</code> if the request is of type
061: * 'multipart/form-data'.
062: */
063: public boolean isMultipart() {
064: return FileUploadBase.isMultipartContent(this );
065: }
066:
067: /**
068: * Parse the multipart data.
069: *
070: * @return A list of <code>org.apache.commons.fileupload.FileItem</code>
071: * @throws FileUploadException
072: * when the request is not multipart or there is a problem
073: * parsing the request.
074: */
075: private List parseMultipartRequest() throws FileUploadException {
076:
077: // Check that we have a file upload request
078: boolean isMultipart = FileUploadBase.isMultipartContent(this );
079: if (!isMultipart) {
080: throw new FileUploadException("Not a multipart request: "
081: + this .getContentType());
082: }
083:
084: // Create a new file upload handler
085: DiskFileUpload upload = new DiskFileUpload();
086:
087: // Set upload parameters
088: upload.setSizeThreshold(this .multipartSizeThreshold);
089: upload.setSizeMax(this .multipartMaxSize);
090: if (this .multipartRepositoryPath != null) {
091: upload.setRepositoryPath(this .multipartRepositoryPath);
092: }
093:
094: // Parse the request
095: List items = upload.parseRequest(this);
096:
097: return items;
098: }
099:
100: }
|