001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.fileupload2.portlet;
018
019import java.io.IOException;
020import java.util.List;
021import java.util.Map;
022
023import javax.portlet.ActionRequest;
024
025import org.apache.commons.fileupload2.core.AbstractFileUpload;
026import org.apache.commons.fileupload2.core.FileItem;
027import org.apache.commons.fileupload2.core.FileItemFactory;
028import org.apache.commons.fileupload2.core.FileItemInputIterator;
029import org.apache.commons.fileupload2.core.FileUploadException;
030import org.apache.commons.fileupload2.javax.JavaxServletFileUpload;
031
032/**
033 * High level API for processing file uploads.
034 * <p>
035 * This class handles multiple files per single HTML widget, sent using {@code multipart/mixed} encoding type, as specified by
036 * <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>. Use {@link JavaxServletFileUpload#parseRequest(javax.servlet.http.HttpServletRequest)} to acquire
037 * a list of {@link FileItem}s associated with a given HTML widget.
038 * </p>
039 * <p>
040 * How the data for individual parts is stored is determined by the factory used to create them; a given part may be in memory, on disk, or somewhere else.
041 * </p>
042 *
043 * @param <I> The FileItem type.
044 * @param <F> the FileItemFactory type.
045 */
046public class JavaxPortletFileUpload<I extends FileItem<I>, F extends FileItemFactory<I>> extends AbstractFileUpload<ActionRequest, I, F> {
047
048    /**
049     * Tests whether the request contains multipart content.
050     *
051     * @param request The portlet request to be evaluated. Must be non-null.
052     * @return {@code true} if the request is multipart; {@code false} otherwise.
053     */
054    public static final boolean isMultipartContent(final ActionRequest request) {
055        return AbstractFileUpload.isMultipartContent(new JavaxPortletRequestContext(request));
056    }
057
058    /**
059     * Constructs an uninitialized instance of this class. A factory must be configured, using {@code setFileItemFactory()}, before attempting to parse
060     * requests.
061     *
062     * @see AbstractFileUpload#AbstractFileUpload()
063     */
064    public JavaxPortletFileUpload() {
065    }
066
067    /**
068     * Constructs an instance of this class which uses the supplied factory to create {@code FileItem} instances.
069     *
070     * @see AbstractFileUpload#AbstractFileUpload()
071     * @param fileItemFactory The factory to use for creating file items.
072     */
073    public JavaxPortletFileUpload(final F fileItemFactory) {
074        setFileItemFactory(fileItemFactory);
075    }
076
077    /**
078     * Gets an <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a> compliant {@code multipart/form-data} file item iterator.
079     *
080     * @param request The portlet request to be parsed.
081     * @return An iterator to instances of {@code FileItemInput} parsed from the request, in the order that they were transmitted.
082     * @throws FileUploadException if there are problems reading/parsing the request or storing files.
083     * @throws IOException         An I/O error occurred. This may be a network error while communicating with the client or a problem while storing the
084     *                             uploaded content.
085     */
086    @Override
087    public FileItemInputIterator getItemIterator(final ActionRequest request) throws FileUploadException, IOException {
088        return super.getItemIterator(new JavaxPortletRequestContext(request));
089    }
090
091    /**
092     * Parses an <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a> compliant {@code multipart/form-data} stream.
093     *
094     * @param request The portlet request to be parsed.
095     * @return A map of {@code FileItem} instances parsed from the request.
096     * @throws FileUploadException if there are problems reading/parsing the request or storing files.
097     */
098    @Override
099    public Map<String, List<I>> parseParameterMap(final ActionRequest request) throws FileUploadException {
100        return parseParameterMap(new JavaxPortletRequestContext(request));
101    }
102
103    /**
104     * Parses an <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a> compliant {@code multipart/form-data} stream.
105     *
106     * @param request The portlet request to be parsed.
107     * @return A list of {@code FileItem} instances parsed from the request, in the order that they were transmitted.
108     * @throws FileUploadException if there are problems reading/parsing the request or storing files.
109     */
110    @Override
111    public List<I> parseRequest(final ActionRequest request) throws FileUploadException {
112        return parseRequest(new JavaxPortletRequestContext(request));
113    }
114
115}