Package org.gomba

Source Code of org.gomba.CustomHttpServletRequestWrapper

package org.gomba;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileUploadBase;
import org.apache.commons.fileupload.FileUploadException;

/**
* Adds multipart form handling capabilities to an HTTPServletRequest using
* Jakarta Commons FileUpload.
*
* @author Flavio Tordini
* @version $Id: CustomHttpServletRequestWrapper.java,v 1.1 2004/09/28 15:58:22 flaviotordini Exp $
*/
public class CustomHttpServletRequestWrapper extends HttpServletRequestWrapper {

    private final int multipartSizeThreshold;

    private final int multipartMaxSize;

    private final String multipartRepositoryPath;

    private List multipartItems;

    /**
     * Constructor.
     */
    public CustomHttpServletRequestWrapper(HttpServletRequest request,
            int multipartSizeThreshold, int multipartMaxSize,
            String multipartRepositoryPath) {
        super(request);

        this.multipartSizeThreshold = multipartSizeThreshold;
        this.multipartMaxSize = multipartMaxSize;
        this.multipartRepositoryPath = multipartRepositoryPath;
    }

    /**
     * Get a List of multipart items. Use the FileUpload API to retrieve values
     * from the items. <code>FileItem</code> has getter methods, so it's a
     * snap to use it in the JSP EL.
     *
     * @return A list of <code>org.apache.commons.fileupload.FileItem</code>
     * @throws FileUploadException
     */
    public List getMultipartItems() throws FileUploadException {
        // lazily parse a multipart request
        if (this.multipartItems == null) {
            this.multipartItems = parseMultipartRequest();
        }
        return this.multipartItems;
    }

    /**
     * @return <code>true</code> if the request is of type
     *               'multipart/form-data'.
     */
    public boolean isMultipart() {
        return FileUploadBase.isMultipartContent(this);
    }

    /**
     * Parse the multipart data.
     *
     * @return A list of <code>org.apache.commons.fileupload.FileItem</code>
     * @throws FileUploadException
     *                   when the request is not multipart or there is a problem
     *                   parsing the request.
     */
    private List parseMultipartRequest() throws FileUploadException {

        // Check that we have a file upload request
        boolean isMultipart = FileUploadBase.isMultipartContent(this);
        if (!isMultipart) {
            throw new FileUploadException("Not a multipart request: "
                    + this.getContentType());
        }

        // Create a new file upload handler
        DiskFileUpload upload = new DiskFileUpload();

        // Set upload parameters
        upload.setSizeThreshold(this.multipartSizeThreshold);
        upload.setSizeMax(this.multipartMaxSize);
        if (this.multipartRepositoryPath != null) {
            upload.setRepositoryPath(this.multipartRepositoryPath);
        }

        // Parse the request
        List items = upload.parseRequest(this);

        return items;
    }

}
TOP

Related Classes of org.gomba.CustomHttpServletRequestWrapper

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.