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     */
017    package org.apache.commons.fileupload.servlet;
018    
019    import java.io.IOException;
020    import java.util.List;
021    
022    import javax.servlet.http.HttpServletRequest;
023    
024    import org.apache.commons.fileupload.FileItemFactory;
025    import org.apache.commons.fileupload.FileItemIterator;
026    import org.apache.commons.fileupload.FileUpload;
027    import org.apache.commons.fileupload.FileUploadException;
028    
029    /**
030     * <p>High level API for processing file uploads.</p>
031     *
032     * <p>This class handles multiple files per single HTML widget, sent using
033     * <code>multipart/mixed</code> encoding type, as specified by
034     * <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>.  Use {@link
035     * #parseRequest(HttpServletRequest)} to acquire a list of {@link
036     * org.apache.commons.fileupload.FileItem}s associated with a given HTML
037     * widget.</p>
038     *
039     * <p>How the data for individual parts is stored is determined by the factory
040     * used to create them; a given part may be in memory, on disk, or somewhere
041     * else.</p>
042     *
043     * @author <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a>
044     * @author <a href="mailto:dlr@collab.net">Daniel Rall</a>
045     * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
046     * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
047     * @author <a href="mailto:martinc@apache.org">Martin Cooper</a>
048     * @author Sean C. Sullivan
049     *
050     * @version $Id: ServletFileUpload.java 479484 2006-11-27 01:06:53Z jochen $
051     */
052    public class ServletFileUpload extends FileUpload {
053    
054        // ---------------------------------------------------------- Class methods
055    
056    
057        /**
058         * Utility method that determines whether the request contains multipart
059         * content.
060         *
061         * @param request The servlet request to be evaluated. Must be non-null.
062         *
063         * @return <code>true</code> if the request is multipart;
064         *         <code>false</code> otherwise.
065         */
066        public static final boolean isMultipartContent(
067                HttpServletRequest request) {
068            if (!"post".equals(request.getMethod().toLowerCase())) {
069                return false;
070            }
071            String contentType = request.getContentType();
072            if (contentType == null) {
073                return false;
074            }
075            if (contentType.toLowerCase().startsWith(MULTIPART)) {
076                return true;
077            }
078            return false;
079        }
080    
081    
082        // ----------------------------------------------------------- Constructors
083    
084    
085        /**
086         * Constructs an uninitialised instance of this class. A factory must be
087         * configured, using <code>setFileItemFactory()</code>, before attempting
088         * to parse requests.
089         *
090         * @see FileUpload#FileUpload(FileItemFactory)
091         */
092        public ServletFileUpload() {
093            super();
094        }
095    
096    
097        /**
098         * Constructs an instance of this class which uses the supplied factory to
099         * create <code>FileItem</code> instances.
100         *
101         * @see FileUpload#FileUpload()
102         * @param fileItemFactory The factory to use for creating file items.
103         */
104        public ServletFileUpload(FileItemFactory fileItemFactory) {
105            super(fileItemFactory);
106        }
107    
108    
109        // --------------------------------------------------------- Public methods
110    
111    
112        /**
113         * Processes an <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>
114         * compliant <code>multipart/form-data</code> stream.
115         *
116         * @param request The servlet request to be parsed.
117         *
118         * @return A list of <code>FileItem</code> instances parsed from the
119         *         request, in the order that they were transmitted.
120         *
121         * @throws FileUploadException if there are problems reading/parsing
122         *                             the request or storing files.
123         */
124        public List /* FileItem */ parseRequest(HttpServletRequest request)
125        throws FileUploadException {
126            return parseRequest(new ServletRequestContext(request));
127        }
128    
129    
130        /**
131         * Processes an <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>
132         * compliant <code>multipart/form-data</code> stream.
133         *
134         * @param request The servlet request to be parsed.
135         *
136         * @return An iterator to instances of <code>FileItemStream</code>
137         *         parsed from the request, in the order that they were
138         *         transmitted.
139         *
140         * @throws FileUploadException if there are problems reading/parsing
141         *                             the request or storing files.
142         * @throws IOException An I/O error occurred. This may be a network
143         *   error while communicating with the client or a problem while
144         *   storing the uploaded content.
145         */
146        public FileItemIterator getItemIterator(HttpServletRequest request)
147        throws FileUploadException, IOException {
148            return super.getItemIterator(new ServletRequestContext(request));
149        }
150    }