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.fileupload;
018
019import java.io.File;
020import java.util.List;
021import javax.servlet.http.HttpServletRequest;
022
023/**
024 * <p>High level API for processing file uploads.</p>
025 *
026 * <p>This class handles multiple files per single HTML widget, sent using
027 * <code>multipart/mixed</code> encoding type, as specified by
028 * <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>.  Use {@link
029 * #parseRequest(HttpServletRequest)} to acquire a list of {@link
030 * org.apache.commons.fileupload.FileItem}s associated with a given HTML
031 * widget.</p>
032 *
033 * <p>Individual parts will be stored in temporary disk storage or in memory,
034 * depending on their size, and will be available as {@link
035 * org.apache.commons.fileupload.FileItem}s.</p>
036 *
037 * @deprecated 1.1 Use <code>ServletFileUpload</code> together with
038 *             <code>DiskFileItemFactory</code> instead.
039 */
040@Deprecated
041public class DiskFileUpload
042    extends FileUploadBase {
043
044    // ----------------------------------------------------------- Data members
045
046    /**
047     * The factory to use to create new form items.
048     */
049    private DefaultFileItemFactory fileItemFactory;
050
051    // ----------------------------------------------------------- Constructors
052
053    /**
054     * Constructs an instance of this class which uses the default factory to
055     * create <code>FileItem</code> instances.
056     *
057     * @see #DiskFileUpload(DefaultFileItemFactory fileItemFactory)
058     *
059     * @deprecated 1.1 Use <code>FileUpload</code> instead.
060     */
061    @Deprecated
062    public DiskFileUpload() {
063        super();
064        this.fileItemFactory = new DefaultFileItemFactory();
065    }
066
067    /**
068     * Constructs an instance of this class which uses the supplied factory to
069     * create <code>FileItem</code> instances.
070     *
071     * @see #DiskFileUpload()
072     * @param fileItemFactory The file item factory to use.
073     *
074     * @deprecated 1.1 Use <code>FileUpload</code> instead.
075     */
076    @Deprecated
077    public DiskFileUpload(DefaultFileItemFactory fileItemFactory) {
078        super();
079        this.fileItemFactory = fileItemFactory;
080    }
081
082    // ----------------------------------------------------- Property accessors
083
084    /**
085     * Returns the factory class used when creating file items.
086     *
087     * @return The factory class for new file items.
088     *
089     * @deprecated 1.1 Use <code>FileUpload</code> instead.
090     */
091    @Override
092    @Deprecated
093    public FileItemFactory getFileItemFactory() {
094        return fileItemFactory;
095    }
096
097    /**
098     * Sets the factory class to use when creating file items. The factory must
099     * be an instance of <code>DefaultFileItemFactory</code> or a subclass
100     * thereof, or else a <code>ClassCastException</code> will be thrown.
101     *
102     * @param factory The factory class for new file items.
103     *
104     * @deprecated 1.1 Use <code>FileUpload</code> instead.
105     */
106    @Override
107    @Deprecated
108    public void setFileItemFactory(FileItemFactory factory) {
109        this.fileItemFactory = (DefaultFileItemFactory) factory;
110    }
111
112    /**
113     * Returns the size threshold beyond which files are written directly to
114     * disk.
115     *
116     * @return The size threshold, in bytes.
117     *
118     * @see #setSizeThreshold(int)
119     *
120     * @deprecated 1.1 Use <code>DiskFileItemFactory</code> instead.
121     */
122    @Deprecated
123    public int getSizeThreshold() {
124        return fileItemFactory.getSizeThreshold();
125    }
126
127    /**
128     * Sets the size threshold beyond which files are written directly to disk.
129     *
130     * @param sizeThreshold The size threshold, in bytes.
131     *
132     * @see #getSizeThreshold()
133     *
134     * @deprecated 1.1 Use <code>DiskFileItemFactory</code> instead.
135     */
136    @Deprecated
137    public void setSizeThreshold(int sizeThreshold) {
138        fileItemFactory.setSizeThreshold(sizeThreshold);
139    }
140
141    /**
142     * Returns the location used to temporarily store files that are larger
143     * than the configured size threshold.
144     *
145     * @return The path to the temporary file location.
146     *
147     * @see #setRepositoryPath(String)
148     *
149     * @deprecated 1.1 Use <code>DiskFileItemFactory</code> instead.
150     */
151    @Deprecated
152    public String getRepositoryPath() {
153        return fileItemFactory.getRepository().getPath();
154    }
155
156    /**
157     * Sets the location used to temporarily store files that are larger
158     * than the configured size threshold.
159     *
160     * @param repositoryPath The path to the temporary file location.
161     *
162     * @see #getRepositoryPath()
163     *
164     * @deprecated 1.1 Use <code>DiskFileItemFactory</code> instead.
165     */
166    @Deprecated
167    public void setRepositoryPath(String repositoryPath) {
168        fileItemFactory.setRepository(new File(repositoryPath));
169    }
170
171    // --------------------------------------------------------- Public methods
172
173    /**
174     * Processes an <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>
175     * compliant <code>multipart/form-data</code> stream. If files are stored
176     * on disk, the path is given by <code>getRepository()</code>.
177     *
178     * @param req           The servlet request to be parsed. Must be non-null.
179     * @param sizeThreshold The max size in bytes to be stored in memory.
180     * @param sizeMax       The maximum allowed upload size, in bytes.
181     * @param path          The location where the files should be stored.
182     *
183     * @return A list of <code>FileItem</code> instances parsed from the
184     *         request, in the order that they were transmitted.
185     *
186     * @throws FileUploadException if there are problems reading/parsing
187     *                             the request or storing files.
188     *
189     * @deprecated 1.1 Use <code>ServletFileUpload</code> instead.
190     */
191    @Deprecated
192    public List<FileItem> parseRequest(HttpServletRequest req,
193                                            int sizeThreshold,
194                                            long sizeMax, String path)
195        throws FileUploadException {
196        setSizeThreshold(sizeThreshold);
197        setSizeMax(sizeMax);
198        setRepositoryPath(path);
199        return parseRequest(req);
200    }
201
202}