[prev in list] [next in list] [prev in thread] [next in thread] 

List:       jakarta-commons-dev
Subject:    cvs commit: jakarta-commons/fileupload/src/java/org/apache/commons/fileupload/servlet ServletFileUpl
From:       martinc () apache ! org
Date:       2004-10-31 5:21:43
Message-ID: 20041031052143.34475.qmail () minotaur ! apache ! org
[Download RAW message or body]

martinc     2004/10/30 22:21:43

  Modified:    fileupload build.xml project.xml
               fileupload/src/java/org/apache/commons/fileupload
                        DefaultFileItem.java DefaultFileItemFactory.java
                        DiskFileUpload.java FileUploadBase.java
  Added:       fileupload/src/java/org/apache/commons/fileupload
                        RequestContext.java
               fileupload/src/java/org/apache/commons/fileupload/disk
                        DiskFileItem.java DiskFileItemFactory.java
               fileupload/src/java/org/apache/commons/fileupload/portlet
                        PortletFileUpload.java PortletRequestContext.java
               fileupload/src/java/org/apache/commons/fileupload/servlet
                        ServletFileUpload.java ServletRequestContext.java
  Log:
  Substantial refactoring and additions:
  
  * The core package is now independent of servlet / portlet / other
    distinctions, as well as persistence schemes, other than deprecated
    classes and methods retained for backwards compatibility.
  
  * Servlet specific functionality has been moved to a new 'servlet' package.
    Existing users should migrate to this as soon as possible, since the
    servlet specific functionality in the generic package will be removed in
    the release after FileUpload 1.1.
  
  * Support for portlets (JSR 168) has been added, in a new 'portlet'
    package. This is not well tested at this point, and feedback would be
    very much appreciated. (This also resolves bug #23620.)
  
  * The disk-based file item implementation has been moved into a 'disk'
    package, and renamed from Default* to Disk* to reflect what it really is.
    The Default* classes have been retained in the top level package for
    backwards compatibility, but are now deprecated, and will be removed in
    the release after FileUpload 1.1.
  
  * The isMultipartRequest method is an unfortunate casualty of this
    refactoring. That method should really be moved to ServletFileUpload, but
    since the method is static, it can only exist in either FileUploadBase or
    ServletFileUpload. Backwards compatibility dictates the former for now,
    but the latter is the desired state, which implies some future breakage.
    Fair warning...
  
  Revision  Changes    Path
  1.11      +3 -1      jakarta-commons/fileupload/build.xml
  
  Index: build.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/fileupload/build.xml,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- build.xml	11 Oct 2004 03:50:19 -0000	1.10
  +++ build.xml	31 Oct 2004 05:21:43 -0000	1.11
  @@ -1,7 +1,7 @@
   <?xml version="1.0" encoding="UTF-8"?>
   
   <!--build.xml generated by maven from project.xml version 1.1-dev
  -  on date October 10 2004, time 2045-->
  +  on date October 30 2004, time 2208-->
   
   <project default="jar" name="commons-fileupload" basedir=".">
     <property name="defaulttargetdir" value="target">
  @@ -155,6 +155,8 @@
       <get dest="${libdir}/commons-io-1.0.jar" usetimestamp="true" \
ignoreerrors="true" src="http://www.ibiblio.org/maven/commons-io/jars/commons-io-1.0.jar">
  </get>
       <get dest="${libdir}/servletapi-2.3.jar" usetimestamp="true" \
ignoreerrors="true" src="http://www.ibiblio.org/maven/servletapi/jars/servletapi-2.3.jar">
  +    </get>
  +    <get dest="${libdir}/portlet-api-1.0.jar" usetimestamp="true" \
ignoreerrors="true" src="http://www.ibiblio.org/maven/portlet-api/jars/portlet-api-1.0.jar">
  </get>
       <get dest="${libdir}/junit-3.8.1.jar" usetimestamp="true" ignoreerrors="true" \
src="http://www.ibiblio.org/maven/junit/jars/junit-3.8.1.jar">  </get>
  
  
  
  1.30      +5 -0      jakarta-commons/fileupload/project.xml
  
  Index: project.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/fileupload/project.xml,v
  retrieving revision 1.29
  retrieving revision 1.30
  diff -u -r1.29 -r1.30
  --- project.xml	11 Oct 2004 03:18:45 -0000	1.29
  +++ project.xml	31 Oct 2004 05:21:43 -0000	1.30
  @@ -149,6 +149,11 @@
         <version>2.3</version>
       </dependency>
       <dependency>
  +      <groupId>portlet-api</groupId>
  +      <artifactId>portlet-api</artifactId>
  +      <version>1.0</version>
  +    </dependency>
  +    <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>3.8.1</version>
  
  
  
  1.29      +11 -524   \
jakarta-commons/fileupload/src/java/org/apache/commons/fileupload/DefaultFileItem.java
  
  Index: DefaultFileItem.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/fileupload/src/java/org/apache/commons/fileupload/DefaultFileItem.java,v
  retrieving revision 1.28
  retrieving revision 1.29
  diff -u -r1.28 -r1.29
  --- DefaultFileItem.java	29 Oct 2004 04:17:23 -0000	1.28
  +++ DefaultFileItem.java	31 Oct 2004 05:21:43 -0000	1.29
  @@ -15,19 +15,8 @@
    */
   package org.apache.commons.fileupload;
   
  -import java.io.BufferedInputStream;
  -import java.io.BufferedOutputStream;
  -import java.io.ByteArrayInputStream;
   import java.io.File;
  -import java.io.FileInputStream;
  -import java.io.FileOutputStream;
  -import java.io.IOException;
  -import java.io.InputStream;
  -import java.io.OutputStream;
  -import java.io.UnsupportedEncodingException;
  -import java.util.Map;
  -import org.apache.commons.io.FileCleaner;
  -import org.apache.commons.io.output.DeferredFileOutputStream;
  +import org.apache.commons.fileupload.disk.DiskFileItem;
   
   
   /**
  @@ -51,85 +40,11 @@
    * @author Sean C. Sullivan
    *
    * @version $Id$
  + *
  + * @deprecated Use <code>DiskFileItem</code> instead.
    */
   public class DefaultFileItem
  -    implements FileItem {
  -
  -    // ----------------------------------------------------- Manifest constants
  -
  -
  -    /**
  -     * Default content charset to be used when no explicit charset
  -     * parameter is provided by the sender. Media subtypes of the
  -     * "text" type are defined to have a default charset value of
  -     * "ISO-8859-1" when received via HTTP.
  -     */
  -    public static final String DEFAULT_CHARSET = "ISO-8859-1";
  -
  -
  -    /**
  -     * Size of buffer to use when writing an item to disk.
  -     */
  -    private static final int WRITE_BUFFER_SIZE = 2048;
  -
  -
  -    // ----------------------------------------------------------- Data members
  -
  -
  -    /**
  -     * Counter used in unique identifier generation.
  -     */
  -    private static int counter = 0;
  -
  -
  -    /**
  -     * The name of the form field as provided by the browser.
  -     */
  -    private String fieldName;
  -
  -
  -    /**
  -     * The content type passed by the browser, or <code>null</code> if
  -     * not defined.
  -     */
  -    private String contentType;
  -
  -
  -    /**
  -     * Whether or not this item is a simple form field.
  -     */
  -    private boolean isFormField;
  -
  -
  -    /**
  -     * The original filename in the user's filesystem.
  -     */
  -    private String fileName;
  -
  -
  -    /**
  -     * The threshold above which uploads will be stored on disk.
  -     */
  -    private int sizeThreshold;
  -
  -
  -    /**
  -     * The directory in which uploaded files will be stored, if stored on disk.
  -     */
  -    private File repository;
  -
  -
  -    /**
  -     * Cached contents of the file.
  -     */
  -    private byte[] cachedContent;
  -
  -
  -    /**
  -     * Output stream for this item.
  -     */
  -    private DeferredFileOutputStream dfos;
  -
  +    extends DiskFileItem {
   
       // ----------------------------------------------------------- Constructors
   
  @@ -150,443 +65,15 @@
        * @param repository    The data repository, which is the directory in
        *                      which files will be created, should the item size
        *                      exceed the threshold.
  -     */
  -    DefaultFileItem(String fieldName, String contentType, boolean isFormField,
  -                    String fileName, int sizeThreshold, File repository) {
  -        this.fieldName = fieldName;
  -        this.contentType = contentType;
  -        this.isFormField = isFormField;
  -        this.fileName = fileName;
  -        this.sizeThreshold = sizeThreshold;
  -        this.repository = repository;
  -    }
  -
  -
  -    // ------------------------------- Methods from javax.activation.DataSource
  -
  -
  -    /**
  -     * Returns an {@link java.io.InputStream InputStream} that can be
  -     * used to retrieve the contents of the file.
  -     *
  -     * @return An {@link java.io.InputStream InputStream} that can be
  -     *         used to retrieve the contents of the file.
        *
  -     * @exception IOException if an error occurs.
  +     * @deprecated Use <code>DiskFileItem</code> instead.
        */
  -    public InputStream getInputStream()
  -        throws IOException {
  -        if (!dfos.isInMemory()) {
  -            return new FileInputStream(dfos.getFile());
  -        }
  -
  -        if (cachedContent == null) {
  -            cachedContent = dfos.getData();
  -        }
  -        return new ByteArrayInputStream(cachedContent);
  -    }
  -
  -
  -    /**
  -     * Returns the content type passed by the agent or <code>null</code> if
  -     * not defined.
  -     *
  -     * @return The content type passed by the agent or <code>null</code> if
  -     *         not defined.
  -     */
  -    public String getContentType() {
  -        return contentType;
  -    }
  -
  -
  -    /**
  -     * Returns the content charset passed by the agent or <code>null</code> if
  -     * not defined.
  -     *
  -     * @return The content charset passed by the agent or <code>null</code> if
  -     *         not defined.
  -     */
  -    public String getCharSet() {
  -        ParameterParser parser = new ParameterParser();
  -        parser.setLowerCaseNames(true);
  -        // Parameter parser can handle null input
  -        Map params = parser.parse(getContentType(), ';');
  -        return (String) params.get("charset");
  -    }
  -
  -
  -    /**
  -     * Returns the original filename in the client's filesystem.
  -     *
  -     * @return The original filename in the client's filesystem.
  -     */
  -    public String getName() {
  -        return fileName;
  -    }
  -
  -
  -    // ------------------------------------------------------- FileItem methods
  -
  -
  -    /**
  -     * Provides a hint as to whether or not the file contents will be read
  -     * from memory.
  -     *
  -     * @return <code>true</code> if the file contents will be read
  -     *         from memory; <code>false</code> otherwise.
  -     */
  -    public boolean isInMemory() {
  -        return (dfos.isInMemory());
  -    }
  -
  -
  -    /**
  -     * Returns the size of the file.
  -     *
  -     * @return The size of the file, in bytes.
  -     */
  -    public long getSize() {
  -        if (cachedContent != null) {
  -            return cachedContent.length;
  -        } else if (dfos.isInMemory()) {
  -            return dfos.getData().length;
  -        } else {
  -            return dfos.getFile().length();
  -        }
  -    }
  -
  -
  -    /**
  -     * Returns the contents of the file as an array of bytes.  If the
  -     * contents of the file were not yet cached in memory, they will be
  -     * loaded from the disk storage and cached.
  -     *
  -     * @return The contents of the file as an array of bytes.
  -     */
  -    public byte[] get() {
  -        if (dfos.isInMemory()) {
  -            if (cachedContent == null) {
  -                cachedContent = dfos.getData();
  -            }
  -            return cachedContent;
  -        }
  -
  -        byte[] fileData = new byte[(int) getSize()];
  -        FileInputStream fis = null;
  -
  -        try {
  -            fis = new FileInputStream(dfos.getFile());
  -            fis.read(fileData);
  -        } catch (IOException e) {
  -            fileData = null;
  -        } finally {
  -            if (fis != null) {
  -                try {
  -                    fis.close();
  -                } catch (IOException e) {
  -                    // ignore
  -                }
  -            }
  -        }
  -
  -        return fileData;
  +    public DefaultFileItem(String fieldName, String contentType,
  +            boolean isFormField, String fileName, int sizeThreshold,
  +            File repository) {
  +        super(fieldName, contentType, isFormField, fileName, sizeThreshold,
  +                repository);
       }
   
  -
  -    /**
  -     * Returns the contents of the file as a String, using the specified
  -     * encoding.  This method uses {@link #get()} to retrieve the
  -     * contents of the file.
  -     *
  -     * @param charset The charset to use.
  -     *
  -     * @return The contents of the file, as a string.
  -     *
  -     * @exception UnsupportedEncodingException if the requested character
  -     *                                         encoding is not available.
  -     */
  -    public String getString(final String charset)
  -        throws UnsupportedEncodingException {
  -        return new String(get(), charset);
  -    }
  -
  -
  -    /**
  -     * Returns the contents of the file as a String, using the default
  -     * character encoding.  This method uses {@link #get()} to retrieve the
  -     * contents of the file.
  -     *
  -     * @return The contents of the file, as a string.
  -     *
  -     * @todo Consider making this method throw UnsupportedEncodingException.
  -     */
  -    public String getString() {
  -        byte[] rawdata = get();
  -        String charset = getCharSet();
  -        if (charset == null) {
  -            charset = DEFAULT_CHARSET;
  -        }
  -        try {
  -            return new String(rawdata, charset);
  -        } catch (UnsupportedEncodingException e) {
  -            return new String(rawdata);
  -        }
  -    }
  -
  -
  -    /**
  -     * A convenience method to write an uploaded item to disk. The client code
  -     * is not concerned with whether or not the item is stored in memory, or on
  -     * disk in a temporary location. They just want to write the uploaded item
  -     * to a file.
  -     * <p>
  -     * This implementation first attempts to rename the uploaded item to the
  -     * specified destination file, if the item was originally written to disk.
  -     * Otherwise, the data will be copied to the specified file.
  -     * <p>
  -     * This method is only guaranteed to work <em>once</em>, the first time it
  -     * is invoked for a particular item. This is because, in the event that the
  -     * method renames a temporary file, that file will no longer be available
  -     * to copy or rename again at a later time.
  -     *
  -     * @param file The <code>File</code> into which the uploaded item should
  -     *             be stored.
  -     *
  -     * @exception Exception if an error occurs.
  -     */
  -    public void write(File file) throws Exception {
  -        if (isInMemory()) {
  -            FileOutputStream fout = null;
  -            try {
  -                fout = new FileOutputStream(file);
  -                fout.write(get());
  -            } finally {
  -                if (fout != null) {
  -                    fout.close();
  -                }
  -            }
  -        } else {
  -            File outputFile = getStoreLocation();
  -            if (outputFile != null) {
  -                /*
  -                 * The uploaded file is being stored on disk
  -                 * in a temporary location so move it to the
  -                 * desired file.
  -                 */
  -                if (!outputFile.renameTo(file)) {
  -                    BufferedInputStream in = null;
  -                    BufferedOutputStream out = null;
  -                    try {
  -                        in = new BufferedInputStream(
  -                            new FileInputStream(outputFile));
  -                        out = new BufferedOutputStream(
  -                                new FileOutputStream(file));
  -                        byte[] bytes = new byte[WRITE_BUFFER_SIZE];
  -                        int s = 0;
  -                        while ((s = in.read(bytes)) != -1) {
  -                            out.write(bytes, 0, s);
  -                        }
  -                    } finally {
  -                        if (in != null) {
  -                            try {
  -                                in.close();
  -                            } catch (IOException e) {
  -                                // ignore
  -                            }
  -                        }
  -                        if (out != null) {
  -                            try {
  -                                out.close();
  -                            } catch (IOException e) {
  -                                // ignore
  -                            }
  -                        }
  -                    }
  -                }
  -            } else {
  -                /*
  -                 * For whatever reason we cannot write the
  -                 * file to disk.
  -                 */
  -                throw new FileUploadException(
  -                    "Cannot write uploaded file to disk!");
  -            }
  -        }
  -    }
  -
  -
  -    /**
  -     * Deletes the underlying storage for a file item, including deleting any
  -     * associated temporary disk file. Although this storage will be deleted
  -     * automatically when the <code>FileItem</code> instance is garbage
  -     * collected, this method can be used to ensure that this is done at an
  -     * earlier time, thus preserving system resources.
  -     */
  -    public void delete() {
  -        cachedContent = null;
  -        File outputFile = getStoreLocation();
  -        if (outputFile != null && outputFile.exists()) {
  -            outputFile.delete();
  -        }
  -    }
  -
  -
  -    /**
  -     * Returns the name of the field in the multipart form corresponding to
  -     * this file item.
  -     *
  -     * @return The name of the form field.
  -     *
  -     * @see #setFieldName(java.lang.String)
  -     *
  -     */
  -    public String getFieldName() {
  -        return fieldName;
  -    }
  -
  -
  -    /**
  -     * Sets the field name used to reference this file item.
  -     *
  -     * @param fieldName The name of the form field.
  -     *
  -     * @see #getFieldName()
  -     *
  -     */
  -    public void setFieldName(String fieldName) {
  -        this.fieldName = fieldName;
  -    }
  -
  -
  -    /**
  -     * Determines whether or not a <code>FileItem</code> instance represents
  -     * a simple form field.
  -     *
  -     * @return <code>true</code> if the instance represents a simple form
  -     *         field; <code>false</code> if it represents an uploaded file.
  -     *
  -     * @see #setFormField(boolean)
  -     *
  -     */
  -    public boolean isFormField() {
  -        return isFormField;
  -    }
  -
  -
  -    /**
  -     * Specifies whether or not a <code>FileItem</code> instance represents
  -     * a simple form field.
  -     *
  -     * @param state <code>true</code> if the instance represents a simple form
  -     *              field; <code>false</code> if it represents an uploaded file.
  -     *
  -     * @see #isFormField()
  -     *
  -     */
  -    public void setFormField(boolean state) {
  -        isFormField = state;
  -    }
  -
  -
  -    /**
  -     * Returns an {@link java.io.OutputStream OutputStream} that can
  -     * be used for storing the contents of the file.
  -     *
  -     * @return An {@link java.io.OutputStream OutputStream} that can be used
  -     *         for storing the contensts of the file.
  -     *
  -     * @exception IOException if an error occurs.
  -     */
  -    public OutputStream getOutputStream()
  -        throws IOException {
  -        if (dfos == null) {
  -            File outputFile = getTempFile();
  -            dfos = new DeferredFileOutputStream(sizeThreshold, outputFile);
  -        }
  -        return dfos;
  -    }
  -
  -
  -    // --------------------------------------------------------- Public methods
  -
  -
  -    /**
  -     * Returns the {@link java.io.File} object for the <code>FileItem</code>'s
  -     * data's temporary location on the disk. Note that for
  -     * <code>FileItem</code>s that have their data stored in memory,
  -     * this method will return <code>null</code>. When handling large
  -     * files, you can use {@link java.io.File#renameTo(java.io.File)} to
  -     * move the file to new location without copying the data, if the
  -     * source and destination locations reside within the same logical
  -     * volume.
  -     *
  -     * @return The data file, or <code>null</code> if the data is stored in
  -     *         memory.
  -     */
  -    public File getStoreLocation() {
  -        return dfos.getFile();
  -    }
  -
  -
  -    // ------------------------------------------------------ Protected methods
  -
  -
  -    /**
  -     * Removes the file contents from the temporary storage.
  -     */
  -    protected void finalize() {
  -        File outputFile = dfos.getFile();
  -
  -        if (outputFile != null && outputFile.exists()) {
  -            outputFile.delete();
  -        }
  -    }
  -
  -
  -    /**
  -     * Creates and returns a {@link java.io.File File} representing a uniquely
  -     * named temporary file in the configured repository path. The lifetime of
  -     * the file is tied to the lifetime of the <code>FileItem</code> instance;
  -     * the file will be deleted when the instance is garbage collected.
  -     *
  -     * @return The {@link java.io.File File} to be used for temporary storage.
  -     */
  -    protected File getTempFile() {
  -        File tempDir = repository;
  -        if (tempDir == null) {
  -            tempDir = new File(System.getProperty("java.io.tmpdir"));
  -        }
  -
  -        String fileName = "upload_" + getUniqueId() + ".tmp";
  -
  -        File f = new File(tempDir, fileName);
  -        FileCleaner.track(f, this);
  -        return f;
  -    }
  -
  -
  -    // -------------------------------------------------------- Private methods
  -
  -
  -    /**
  -     * Returns an identifier that is unique within the class loader used to
  -     * load this class, but does not have random-like apearance.
  -     *
  -     * @return A String with the non-random looking instance identifier.
  -     */
  -    private static String getUniqueId() {
  -        int current;
  -        synchronized (DefaultFileItem.class) {
  -            current = counter++;
  -        }
  -        String id = Integer.toString(current);
  -
  -        // If you manage to get more than 100 million of ids, you'll
  -        // start getting ids longer than 8 characters.
  -        if (current < 100000000) {
  -            id = ("00000000" + id).substring(id.length());
  -        }
  -        return id;
  -    }
   
   }
  
  
  
  1.6       +14 -86    \
jakarta-commons/fileupload/src/java/org/apache/commons/fileupload/DefaultFileItemFactory.java
  
  Index: DefaultFileItemFactory.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/fileupload/src/java/org/apache/commons/fileupload/DefaultFileItemFactory.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- DefaultFileItemFactory.java	29 Oct 2004 04:17:23 -0000	1.5
  +++ DefaultFileItemFactory.java	31 Oct 2004 05:21:43 -0000	1.6
  @@ -16,6 +16,7 @@
   package org.apache.commons.fileupload;
   
   import java.io.File;
  +import org.apache.commons.fileupload.disk.DiskFileItemFactory;
   
   /**
    * <p>The default {@link org.apache.commons.fileupload.FileItemFactory}
  @@ -38,32 +39,10 @@
    * @author <a href="mailto:martinc@apache.org">Martin Cooper</a>
    *
    * @version $Id$
  + *
  + * @deprecated Use <code>DiskFileItemFactory</code> instead.
    */
  -public class DefaultFileItemFactory implements FileItemFactory {
  -
  -    // ----------------------------------------------------- Manifest constants
  -
  -
  -    /**
  -     * The default threshold above which uploads will be stored on disk.
  -     */
  -    public static final int DEFAULT_SIZE_THRESHOLD = 10240;
  -
  -
  -    // ----------------------------------------------------- Instance Variables
  -
  -
  -    /**
  -     * The directory in which uploaded files will be stored, if stored on disk.
  -     */
  -    private File repository;
  -
  -
  -    /**
  -     * The threshold above which uploads will be stored on disk.
  -     */
  -    private int sizeThreshold = DEFAULT_SIZE_THRESHOLD;
  -
  +public class DefaultFileItemFactory extends DiskFileItemFactory {
   
       // ----------------------------------------------------------- Constructors
   
  @@ -71,8 +50,11 @@
       /**
        * Constructs an unconfigured instance of this class. The resulting factory
        * may be configured by calling the appropriate setter methods.
  +     *
  +     * @deprecated Use <code>DiskFileItemFactory</code> instead.
        */
       public DefaultFileItemFactory() {
  +        super();
       }
   
   
  @@ -85,67 +67,11 @@
        * @param repository    The data repository, which is the directory in
        *                      which files will be created, should the item size
        *                      exceed the threshold.
  -     */
  -    public DefaultFileItemFactory(int sizeThreshold, File repository) {
  -        this.sizeThreshold = sizeThreshold;
  -        this.repository = repository;
  -    }
  -
  -
  -    // ------------------------------------------------------------- Properties
  -
  -
  -    /**
  -     * Returns the directory used to temporarily store files that are larger
  -     * than the configured size threshold.
  -     *
  -     * @return The directory in which temporary files will be located.
        *
  -     * @see #setRepository(java.io.File)
  -     *
  -     */
  -    public File getRepository() {
  -        return repository;
  -    }
  -
  -
  -    /**
  -     * Sets the directory used to temporarily store files that are larger
  -     * than the configured size threshold.
  -     *
  -     * @param repository The directory in which temporary files will be located.
  -     *
  -     * @see #getRepository()
  -     *
  -     */
  -    public void setRepository(File repository) {
  -        this.repository = repository;
  -    }
  -
  -
  -    /**
  -     * Returns the size threshold beyond which files are written directly to
  -     * disk. The default value is 1024 bytes.
  -     *
  -     * @return The size threshold, in bytes.
  -     *
  -     * @see #setSizeThreshold(int)
  +     * @deprecated Use <code>DiskFileItemFactory</code> instead.
        */
  -    public int getSizeThreshold() {
  -        return sizeThreshold;
  -    }
  -
  -
  -    /**
  -     * Sets the size threshold beyond which files are written directly to disk.
  -     *
  -     * @param sizeThreshold The size threshold, in bytes.
  -     *
  -     * @see #getSizeThreshold()
  -     *
  -     */
  -    public void setSizeThreshold(int sizeThreshold) {
  -        this.sizeThreshold = sizeThreshold;
  +    public DefaultFileItemFactory(int sizeThreshold, File repository) {
  +        super(sizeThreshold, repository);
       }
   
   
  @@ -164,6 +90,8 @@
        *                    by the browser or other client.
        *
        * @return The newly created file item.
  +     *
  +     * @deprecated Use <code>DiskFileItemFactory</code> instead.
        */
       public FileItem createItem(
               String fieldName,
  @@ -172,7 +100,7 @@
               String fileName
               ) {
           return new DefaultFileItem(fieldName, contentType,
  -                isFormField, fileName, sizeThreshold, repository);
  +                isFormField, fileName, getSizeThreshold(), getRepository());
       }
   
   }
  
  
  
  1.7       +22 -1     \
jakarta-commons/fileupload/src/java/org/apache/commons/fileupload/DiskFileUpload.java \
  Index: DiskFileUpload.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/fileupload/src/java/org/apache/commons/fileupload/DiskFileUpload.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- DiskFileUpload.java	29 Oct 2004 04:17:23 -0000	1.6
  +++ DiskFileUpload.java	31 Oct 2004 05:21:43 -0000	1.7
  @@ -41,6 +41,9 @@
    * @author Sean C. Sullivan
    *
    * @version $Id$
  + *
  + * @deprecated Use <code>ServletFileUpload</code> together with
  + *             <code>DiskFileItemFactory</code> instead.
    */
   public class DiskFileUpload
       extends FileUploadBase {
  @@ -62,6 +65,8 @@
        * create <code>FileItem</code> instances.
        *
        * @see #DiskFileUpload(DefaultFileItemFactory fileItemFactory)
  +     *
  +     * @deprecated Use <code>FileUpload</code> instead.
        */
       public DiskFileUpload() {
           super();
  @@ -74,6 +79,8 @@
        * create <code>FileItem</code> instances.
        *
        * @see #DiskFileUpload()
  +     *
  +     * @deprecated Use <code>FileUpload</code> instead.
        */
       public DiskFileUpload(DefaultFileItemFactory fileItemFactory) {
           super();
  @@ -88,6 +95,8 @@
        * Returns the factory class used when creating file items.
        *
        * @return The factory class for new file items.
  +     *
  +     * @deprecated Use <code>FileUpload</code> instead.
        */
       public FileItemFactory getFileItemFactory() {
           return fileItemFactory;
  @@ -100,6 +109,8 @@
        * thereof, or else a <code>ClassCastException</code> will be thrown.
        *
        * @param factory The factory class for new file items.
  +     *
  +     * @deprecated Use <code>FileUpload</code> instead.
        */
       public void setFileItemFactory(FileItemFactory factory) {
           this.fileItemFactory = (DefaultFileItemFactory) factory;
  @@ -113,6 +124,8 @@
        * @return The size threshold, in bytes.
        *
        * @see #setSizeThreshold(int)
  +     *
  +     * @deprecated Use <code>DiskFileItemFactory</code> instead.
        */
       public int getSizeThreshold() {
           return fileItemFactory.getSizeThreshold();
  @@ -125,6 +138,8 @@
        * @param sizeThreshold The size threshold, in bytes.
        *
        * @see #getSizeThreshold()
  +     *
  +     * @deprecated Use <code>DiskFileItemFactory</code> instead.
        */
       public void setSizeThreshold(int sizeThreshold) {
           fileItemFactory.setSizeThreshold(sizeThreshold);
  @@ -138,6 +153,8 @@
        * @return The path to the temporary file location.
        *
        * @see #setRepositoryPath(String)
  +     *
  +     * @deprecated Use <code>DiskFileItemFactory</code> instead.
        */
       public String getRepositoryPath() {
           return fileItemFactory.getRepository().getPath();
  @@ -151,6 +168,8 @@
        * @param repositoryPath The path to the temporary file location.
        *
        * @see #getRepositoryPath()
  +     *
  +     * @deprecated Use <code>DiskFileItemFactory</code> instead.
        */
       public void setRepositoryPath(String repositoryPath) {
           fileItemFactory.setRepository(new File(repositoryPath));
  @@ -175,6 +194,8 @@
        *
        * @exception FileUploadException if there are problems reading/parsing
        *                                the request or storing files.
  +     *
  +     * @deprecated Use <code>ServletFileUpload</code> instead.
        */
       public List /* FileItem */ parseRequest(HttpServletRequest req,
                                               int sizeThreshold,
  
  
  
  1.13      +57 -9     \
jakarta-commons/fileupload/src/java/org/apache/commons/fileupload/FileUploadBase.java \
  Index: FileUploadBase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/fileupload/src/java/org/apache/commons/fileupload/FileUploadBase.java,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- FileUploadBase.java	31 Oct 2004 02:03:47 -0000	1.12
  +++ FileUploadBase.java	31 Oct 2004 05:21:43 -0000	1.13
  @@ -25,6 +25,8 @@
   import java.util.Map;
   import javax.servlet.http.HttpServletRequest;
   
  +import org.apache.commons.fileupload.servlet.ServletRequestContext;
  +
   /**
    * <p>High level API for processing file uploads.</p>
    *
  @@ -54,6 +56,32 @@
   
   
       /**
  +     * <p>Utility method that determines whether the request contains multipart
  +     * content.</p>
  +     *
  +     * <p><strong>NOTE:</strong>This method will be moved to the
  +     * <code>ServletFileUpload</code> class after the FileUpload 1.1 release.
  +     * Unfortunately, since this method is static, it is not possible to
  +     * provide its replacement until this method is removed.</p>
  +     *
  +     * @param ctx The request context to be evaluated. Must be non-null.
  +     *
  +     * @return <code>true</code> if the request is multipart;
  +     *         <code>false</code> otherwise.
  +     */
  +    public static final boolean isMultipartContent(RequestContext ctx) {
  +        String contentType = ctx.getContentType();
  +        if (contentType == null) {
  +            return false;
  +        }
  +        if (contentType.toLowerCase().startsWith(MULTIPART)) {
  +            return true;
  +        }
  +        return false;
  +    }
  +
  +
  +    /**
        * Utility method that determines whether the request contains multipart
        * content.
        *
  @@ -61,6 +89,8 @@
        *
        * @return <code>true</code> if the request is multipart;
        *         <code>false</code> otherwise.
  +     *
  +     * @deprecated Use the method on <code>ServletFileUpload</code> instead.
        */
       public static final boolean isMultipartContent(HttpServletRequest req) {
           if (!"post".equals(req.getMethod().toLowerCase())) {
  @@ -219,8 +249,7 @@
   
       /**
        * Processes an <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>
  -     * compliant <code>multipart/form-data</code> stream. If files are stored
  -     * on disk, the path is given by <code>getRepository()</code>.
  +     * compliant <code>multipart/form-data</code> stream.
        *
        * @param req The servlet request to be parsed.
        *
  @@ -229,15 +258,34 @@
        *
        * @exception FileUploadException if there are problems reading/parsing
        *                                the request or storing files.
  +     *
  +     * @deprecated Use the method in <code>ServletFileUpload</code> instead.
        */
       public List /* FileItem */ parseRequest(HttpServletRequest req)
  -        throws FileUploadException {
  -        if (null == req) {
  -            throw new NullPointerException("req parameter");
  +            throws FileUploadException {
  +        return parseRequest(new ServletRequestContext(req));
  +    }
  +
  +    /**
  +     * Processes an <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>
  +     * compliant <code>multipart/form-data</code> stream.
  +     *
  +     * @param ctx The context for the request to be parsed.
  +     *
  +     * @return A list of <code>FileItem</code> instances parsed from the
  +     *         request, in the order that they were transmitted.
  +     *
  +     * @exception FileUploadException if there are problems reading/parsing
  +     *                                the request or storing files.
  +     */
  +    public List /* FileItem */ parseRequest(RequestContext ctx)
  +            throws FileUploadException {
  +        if (ctx == null) {
  +            throw new NullPointerException("ctx parameter");
           }
   
           ArrayList items = new ArrayList();
  -        String contentType = req.getHeader(CONTENT_TYPE);
  +        String contentType = ctx.getContentType();
   
           if ((null == contentType)
               || (!contentType.toLowerCase().startsWith(MULTIPART))) {
  @@ -249,7 +297,7 @@
                   + " stream, content type header is "
                   + contentType);
           }
  -        int requestSize = req.getContentLength();
  +        int requestSize = ctx.getContentLength();
   
           if (requestSize == -1) {
               throw new UnknownSizeException(
  @@ -270,7 +318,7 @@
                           + "no multipart boundary was found");
               }
   
  -            InputStream input = req.getInputStream();
  +            InputStream input = ctx.getInputStream();
   
               MultipartStream multi = new MultipartStream(input, boundary);
               multi.setHeaderEncoding(headerEncoding);
  
  
  
  1.1                  \
jakarta-commons/fileupload/src/java/org/apache/commons/fileupload/RequestContext.java \
  Index: RequestContext.java
  ===================================================================
  /*
   * Copyright 2001-2004 The Apache Software Foundation
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.commons.fileupload;
  
  import java.io.InputStream;
  import java.io.IOException;
  
  /**
   * <p>Abstracts access to the request information needed for file uploads. This
   * interfsace should be implemented for each type of request that may be
   * handled by FileUpload, such as servlets and portlets.</p>
   *
   * @author <a href="mailto:martinc@apache.org">Martin Cooper</a>
   *
   * @since FileUpload 1.1
   *
   * @version $Id: RequestContext.java,v 1.1 2004/10/31 05:21:43 martinc Exp $
   */
  public interface RequestContext {
  
      /**
       * Retrieve the content type of the request.
       *
       * @return The content type of the request.
       */
      String getContentType();
  
      /**
       * Retrieve the content length of the request.
       *
       * @return The content length of the request.
       */
      int getContentLength();
  
      /**
       * Retrieve the input stream for the request.
       *
       * @return The input stream for the request.
       *
       * @throws IOException if a problem occurs.
       */
      InputStream getInputStream() throws IOException;
  }
  
  
  
  1.1                  \
jakarta-commons/fileupload/src/java/org/apache/commons/fileupload/disk/DiskFileItem.java
  
  Index: DiskFileItem.java
  ===================================================================
  /*
   * Copyright 2001-2004 The Apache Software Foundation
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.commons.fileupload.disk;
  
  import java.io.BufferedInputStream;
  import java.io.BufferedOutputStream;
  import java.io.ByteArrayInputStream;
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.OutputStream;
  import java.io.UnsupportedEncodingException;
  import java.util.Map;
  import org.apache.commons.io.FileCleaner;
  import org.apache.commons.io.output.DeferredFileOutputStream;
  
  import org.apache.commons.fileupload.FileItem;
  import org.apache.commons.fileupload.FileUploadException;
  import org.apache.commons.fileupload.ParameterParser;
  
  
  /**
   * <p> The default implementation of the
   * {@link org.apache.commons.fileupload.FileItem FileItem} interface.
   *
   * <p> After retrieving an instance of this class from a {@link
   * org.apache.commons.fileupload.DiskFileUpload DiskFileUpload} instance (see
   * {@link org.apache.commons.fileupload.DiskFileUpload
   * #parseRequest(javax.servlet.http.HttpServletRequest)}), you may
   * either request all contents of file at once using {@link #get()} or
   * request an {@link java.io.InputStream InputStream} with
   * {@link #getInputStream()} and process the file without attempting to load
   * it into memory, which may come handy with large files.
   *
   * @author <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a>
   * @author <a href="mailto:sean@informage.net">Sean Legassick</a>
   * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
   * @author <a href="mailto:jmcnally@apache.org">John McNally</a>
   * @author <a href="mailto:martinc@apache.org">Martin Cooper</a>
   * @author Sean C. Sullivan
   *
   * @since FileUpload 1.1
   *
   * @version $Id: DiskFileItem.java,v 1.1 2004/10/31 05:21:43 martinc Exp $
   */
  public class DiskFileItem
      implements FileItem {
  
      // ----------------------------------------------------- Manifest constants
  
  
      /**
       * Default content charset to be used when no explicit charset
       * parameter is provided by the sender. Media subtypes of the
       * "text" type are defined to have a default charset value of
       * "ISO-8859-1" when received via HTTP.
       */
      public static final String DEFAULT_CHARSET = "ISO-8859-1";
  
  
      /**
       * Size of buffer to use when writing an item to disk.
       */
      private static final int WRITE_BUFFER_SIZE = 2048;
  
  
      // ----------------------------------------------------------- Data members
  
  
      /**
       * Counter used in unique identifier generation.
       */
      private static int counter = 0;
  
  
      /**
       * The name of the form field as provided by the browser.
       */
      private String fieldName;
  
  
      /**
       * The content type passed by the browser, or <code>null</code> if
       * not defined.
       */
      private String contentType;
  
  
      /**
       * Whether or not this item is a simple form field.
       */
      private boolean isFormField;
  
  
      /**
       * The original filename in the user's filesystem.
       */
      private String fileName;
  
  
      /**
       * The threshold above which uploads will be stored on disk.
       */
      private int sizeThreshold;
  
  
      /**
       * The directory in which uploaded files will be stored, if stored on disk.
       */
      private File repository;
  
  
      /**
       * Cached contents of the file.
       */
      private byte[] cachedContent;
  
  
      /**
       * Output stream for this item.
       */
      private DeferredFileOutputStream dfos;
  
  
      // ----------------------------------------------------------- Constructors
  
  
      /**
       * Constructs a new <code>DiskFileItem</code> instance.
       *
       * @param fieldName     The name of the form field.
       * @param contentType   The content type passed by the browser or
       *                      <code>null</code> if not specified.
       * @param isFormField   Whether or not this item is a plain form field, as
       *                      opposed to a file upload.
       * @param fileName      The original filename in the user's filesystem, or
       *                      <code>null</code> if not specified.
       * @param sizeThreshold The threshold, in bytes, below which items will be
       *                      retained in memory and above which they will be
       *                      stored as a file.
       * @param repository    The data repository, which is the directory in
       *                      which files will be created, should the item size
       *                      exceed the threshold.
       */
      public DiskFileItem(String fieldName, String contentType,
              boolean isFormField, String fileName, int sizeThreshold,
              File repository) {
          this.fieldName = fieldName;
          this.contentType = contentType;
          this.isFormField = isFormField;
          this.fileName = fileName;
          this.sizeThreshold = sizeThreshold;
          this.repository = repository;
      }
  
  
      // ------------------------------- Methods from javax.activation.DataSource
  
  
      /**
       * Returns an {@link java.io.InputStream InputStream} that can be
       * used to retrieve the contents of the file.
       *
       * @return An {@link java.io.InputStream InputStream} that can be
       *         used to retrieve the contents of the file.
       *
       * @exception IOException if an error occurs.
       */
      public InputStream getInputStream()
          throws IOException {
          if (!dfos.isInMemory()) {
              return new FileInputStream(dfos.getFile());
          }
  
          if (cachedContent == null) {
              cachedContent = dfos.getData();
          }
          return new ByteArrayInputStream(cachedContent);
      }
  
  
      /**
       * Returns the content type passed by the agent or <code>null</code> if
       * not defined.
       *
       * @return The content type passed by the agent or <code>null</code> if
       *         not defined.
       */
      public String getContentType() {
          return contentType;
      }
  
  
      /**
       * Returns the content charset passed by the agent or <code>null</code> if
       * not defined.
       *
       * @return The content charset passed by the agent or <code>null</code> if
       *         not defined.
       */
      public String getCharSet() {
          ParameterParser parser = new ParameterParser();
          parser.setLowerCaseNames(true);
          // Parameter parser can handle null input
          Map params = parser.parse(getContentType(), ';');
          return (String) params.get("charset");
      }
  
  
      /**
       * Returns the original filename in the client's filesystem.
       *
       * @return The original filename in the client's filesystem.
       */
      public String getName() {
          return fileName;
      }
  
  
      // ------------------------------------------------------- FileItem methods
  
  
      /**
       * Provides a hint as to whether or not the file contents will be read
       * from memory.
       *
       * @return <code>true</code> if the file contents will be read
       *         from memory; <code>false</code> otherwise.
       */
      public boolean isInMemory() {
          return (dfos.isInMemory());
      }
  
  
      /**
       * Returns the size of the file.
       *
       * @return The size of the file, in bytes.
       */
      public long getSize() {
          if (cachedContent != null) {
              return cachedContent.length;
          } else if (dfos.isInMemory()) {
              return dfos.getData().length;
          } else {
              return dfos.getFile().length();
          }
      }
  
  
      /**
       * Returns the contents of the file as an array of bytes.  If the
       * contents of the file were not yet cached in memory, they will be
       * loaded from the disk storage and cached.
       *
       * @return The contents of the file as an array of bytes.
       */
      public byte[] get() {
          if (dfos.isInMemory()) {
              if (cachedContent == null) {
                  cachedContent = dfos.getData();
              }
              return cachedContent;
          }
  
          byte[] fileData = new byte[(int) getSize()];
          FileInputStream fis = null;
  
          try {
              fis = new FileInputStream(dfos.getFile());
              fis.read(fileData);
          } catch (IOException e) {
              fileData = null;
          } finally {
              if (fis != null) {
                  try {
                      fis.close();
                  } catch (IOException e) {
                      // ignore
                  }
              }
          }
  
          return fileData;
      }
  
  
      /**
       * Returns the contents of the file as a String, using the specified
       * encoding.  This method uses {@link #get()} to retrieve the
       * contents of the file.
       *
       * @param charset The charset to use.
       *
       * @return The contents of the file, as a string.
       *
       * @exception UnsupportedEncodingException if the requested character
       *                                         encoding is not available.
       */
      public String getString(final String charset)
          throws UnsupportedEncodingException {
          return new String(get(), charset);
      }
  
  
      /**
       * Returns the contents of the file as a String, using the default
       * character encoding.  This method uses {@link #get()} to retrieve the
       * contents of the file.
       *
       * @return The contents of the file, as a string.
       *
       * @todo Consider making this method throw UnsupportedEncodingException.
       */
      public String getString() {
          byte[] rawdata = get();
          String charset = getCharSet();
          if (charset == null) {
              charset = DEFAULT_CHARSET;
          }
          try {
              return new String(rawdata, charset);
          } catch (UnsupportedEncodingException e) {
              return new String(rawdata);
          }
      }
  
  
      /**
       * A convenience method to write an uploaded item to disk. The client code
       * is not concerned with whether or not the item is stored in memory, or on
       * disk in a temporary location. They just want to write the uploaded item
       * to a file.
       * <p>
       * This implementation first attempts to rename the uploaded item to the
       * specified destination file, if the item was originally written to disk.
       * Otherwise, the data will be copied to the specified file.
       * <p>
       * This method is only guaranteed to work <em>once</em>, the first time it
       * is invoked for a particular item. This is because, in the event that the
       * method renames a temporary file, that file will no longer be available
       * to copy or rename again at a later time.
       *
       * @param file The <code>File</code> into which the uploaded item should
       *             be stored.
       *
       * @exception Exception if an error occurs.
       */
      public void write(File file) throws Exception {
          if (isInMemory()) {
              FileOutputStream fout = null;
              try {
                  fout = new FileOutputStream(file);
                  fout.write(get());
              } finally {
                  if (fout != null) {
                      fout.close();
                  }
              }
          } else {
              File outputFile = getStoreLocation();
              if (outputFile != null) {
                  /*
                   * The uploaded file is being stored on disk
                   * in a temporary location so move it to the
                   * desired file.
                   */
                  if (!outputFile.renameTo(file)) {
                      BufferedInputStream in = null;
                      BufferedOutputStream out = null;
                      try {
                          in = new BufferedInputStream(
                              new FileInputStream(outputFile));
                          out = new BufferedOutputStream(
                                  new FileOutputStream(file));
                          byte[] bytes = new byte[WRITE_BUFFER_SIZE];
                          int s = 0;
                          while ((s = in.read(bytes)) != -1) {
                              out.write(bytes, 0, s);
                          }
                      } finally {
                          if (in != null) {
                              try {
                                  in.close();
                              } catch (IOException e) {
                                  // ignore
                              }
                          }
                          if (out != null) {
                              try {
                                  out.close();
                              } catch (IOException e) {
                                  // ignore
                              }
                          }
                      }
                  }
              } else {
                  /*
                   * For whatever reason we cannot write the
                   * file to disk.
                   */
                  throw new FileUploadException(
                      "Cannot write uploaded file to disk!");
              }
          }
      }
  
  
      /**
       * Deletes the underlying storage for a file item, including deleting any
       * associated temporary disk file. Although this storage will be deleted
       * automatically when the <code>FileItem</code> instance is garbage
       * collected, this method can be used to ensure that this is done at an
       * earlier time, thus preserving system resources.
       */
      public void delete() {
          cachedContent = null;
          File outputFile = getStoreLocation();
          if (outputFile != null && outputFile.exists()) {
              outputFile.delete();
          }
      }
  
  
      /**
       * Returns the name of the field in the multipart form corresponding to
       * this file item.
       *
       * @return The name of the form field.
       *
       * @see #setFieldName(java.lang.String)
       *
       */
      public String getFieldName() {
          return fieldName;
      }
  
  
      /**
       * Sets the field name used to reference this file item.
       *
       * @param fieldName The name of the form field.
       *
       * @see #getFieldName()
       *
       */
      public void setFieldName(String fieldName) {
          this.fieldName = fieldName;
      }
  
  
      /**
       * Determines whether or not a <code>FileItem</code> instance represents
       * a simple form field.
       *
       * @return <code>true</code> if the instance represents a simple form
       *         field; <code>false</code> if it represents an uploaded file.
       *
       * @see #setFormField(boolean)
       *
       */
      public boolean isFormField() {
          return isFormField;
      }
  
  
      /**
       * Specifies whether or not a <code>FileItem</code> instance represents
       * a simple form field.
       *
       * @param state <code>true</code> if the instance represents a simple form
       *              field; <code>false</code> if it represents an uploaded file.
       *
       * @see #isFormField()
       *
       */
      public void setFormField(boolean state) {
          isFormField = state;
      }
  
  
      /**
       * Returns an {@link java.io.OutputStream OutputStream} that can
       * be used for storing the contents of the file.
       *
       * @return An {@link java.io.OutputStream OutputStream} that can be used
       *         for storing the contensts of the file.
       *
       * @exception IOException if an error occurs.
       */
      public OutputStream getOutputStream()
          throws IOException {
          if (dfos == null) {
              File outputFile = getTempFile();
              dfos = new DeferredFileOutputStream(sizeThreshold, outputFile);
          }
          return dfos;
      }
  
  
      // --------------------------------------------------------- Public methods
  
  
      /**
       * Returns the {@link java.io.File} object for the <code>FileItem</code>'s
       * data's temporary location on the disk. Note that for
       * <code>FileItem</code>s that have their data stored in memory,
       * this method will return <code>null</code>. When handling large
       * files, you can use {@link java.io.File#renameTo(java.io.File)} to
       * move the file to new location without copying the data, if the
       * source and destination locations reside within the same logical
       * volume.
       *
       * @return The data file, or <code>null</code> if the data is stored in
       *         memory.
       */
      public File getStoreLocation() {
          return dfos.getFile();
      }
  
  
      // ------------------------------------------------------ Protected methods
  
  
      /**
       * Removes the file contents from the temporary storage.
       */
      protected void finalize() {
          File outputFile = dfos.getFile();
  
          if (outputFile != null && outputFile.exists()) {
              outputFile.delete();
          }
      }
  
  
      /**
       * Creates and returns a {@link java.io.File File} representing a uniquely
       * named temporary file in the configured repository path. The lifetime of
       * the file is tied to the lifetime of the <code>FileItem</code> instance;
       * the file will be deleted when the instance is garbage collected.
       *
       * @return The {@link java.io.File File} to be used for temporary storage.
       */
      protected File getTempFile() {
          File tempDir = repository;
          if (tempDir == null) {
              tempDir = new File(System.getProperty("java.io.tmpdir"));
          }
  
          String fileName = "upload_" + getUniqueId() + ".tmp";
  
          File f = new File(tempDir, fileName);
          FileCleaner.track(f, this);
          return f;
      }
  
  
      // -------------------------------------------------------- Private methods
  
  
      /**
       * Returns an identifier that is unique within the class loader used to
       * load this class, but does not have random-like apearance.
       *
       * @return A String with the non-random looking instance identifier.
       */
      private static String getUniqueId() {
          int current;
          synchronized (DiskFileItem.class) {
              current = counter++;
          }
          String id = Integer.toString(current);
  
          // If you manage to get more than 100 million of ids, you'll
          // start getting ids longer than 8 characters.
          if (current < 100000000) {
              id = ("00000000" + id).substring(id.length());
          }
          return id;
      }
  
  }
  
  
  
  1.1                  \
jakarta-commons/fileupload/src/java/org/apache/commons/fileupload/disk/DiskFileItemFactory.java
  
  Index: DiskFileItemFactory.java
  ===================================================================
  /*
   * Copyright 2001-2004 The Apache Software Foundation
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.commons.fileupload.disk;
  
  import java.io.File;
  
  import org.apache.commons.fileupload.FileItem;
  import org.apache.commons.fileupload.FileItemFactory;
  
  /**
   * <p>The default {@link org.apache.commons.fileupload.FileItemFactory}
   * implementation. This implementation creates
   * {@link org.apache.commons.fileupload.FileItem} instances which keep their
   * content either in memory, for smaller items, or in a temporary file on disk,
   * for larger items. The size threshold, above which content will be stored on
   * disk, is configurable, as is the directory in which temporary files will be
   * created.</p>
   *
   * <p>If not otherwise configured, the default configuration values are as
   * follows:
   * <ul>
   *   <li>Size threshold is 10KB.</li>
   *   <li>Repository is the system default temp directory, as returned by
   *       <code>System.getProperty("java.io.tmpdir")</code>.</li>
   * </ul>
   * </p>
   *
   * @author <a href="mailto:martinc@apache.org">Martin Cooper</a>
   *
   * @since FileUpload 1.1
   *
   * @version $Id: DiskFileItemFactory.java,v 1.1 2004/10/31 05:21:43 martinc Exp $
   */
  public class DiskFileItemFactory implements FileItemFactory {
  
      // ----------------------------------------------------- Manifest constants
  
  
      /**
       * The default threshold above which uploads will be stored on disk.
       */
      public static final int DEFAULT_SIZE_THRESHOLD = 10240;
  
  
      // ----------------------------------------------------- Instance Variables
  
  
      /**
       * The directory in which uploaded files will be stored, if stored on disk.
       */
      private File repository;
  
  
      /**
       * The threshold above which uploads will be stored on disk.
       */
      private int sizeThreshold = DEFAULT_SIZE_THRESHOLD;
  
  
      // ----------------------------------------------------------- Constructors
  
  
      /**
       * Constructs an unconfigured instance of this class. The resulting factory
       * may be configured by calling the appropriate setter methods.
       */
      public DiskFileItemFactory() {
      }
  
  
      /**
       * Constructs a preconfigured instance of this class.
       *
       * @param sizeThreshold The threshold, in bytes, below which items will be
       *                      retained in memory and above which they will be
       *                      stored as a file.
       * @param repository    The data repository, which is the directory in
       *                      which files will be created, should the item size
       *                      exceed the threshold.
       */
      public DiskFileItemFactory(int sizeThreshold, File repository) {
          this.sizeThreshold = sizeThreshold;
          this.repository = repository;
      }
  
  
      // ------------------------------------------------------------- Properties
  
  
      /**
       * Returns the directory used to temporarily store files that are larger
       * than the configured size threshold.
       *
       * @return The directory in which temporary files will be located.
       *
       * @see #setRepository(java.io.File)
       *
       */
      public File getRepository() {
          return repository;
      }
  
  
      /**
       * Sets the directory used to temporarily store files that are larger
       * than the configured size threshold.
       *
       * @param repository The directory in which temporary files will be located.
       *
       * @see #getRepository()
       *
       */
      public void setRepository(File repository) {
          this.repository = repository;
      }
  
  
      /**
       * Returns the size threshold beyond which files are written directly to
       * disk. The default value is 1024 bytes.
       *
       * @return The size threshold, in bytes.
       *
       * @see #setSizeThreshold(int)
       */
      public int getSizeThreshold() {
          return sizeThreshold;
      }
  
  
      /**
       * Sets the size threshold beyond which files are written directly to disk.
       *
       * @param sizeThreshold The size threshold, in bytes.
       *
       * @see #getSizeThreshold()
       *
       */
      public void setSizeThreshold(int sizeThreshold) {
          this.sizeThreshold = sizeThreshold;
      }
  
  
      // --------------------------------------------------------- Public Methods
  
      /**
       * Create a new {@link org.apache.commons.fileupload.disk.DiskFileItem}
       * instance from the supplied parameters and the local factory
       * configuration.
       *
       * @param fieldName   The name of the form field.
       * @param contentType The content type of the form field.
       * @param isFormField <code>true</code> if this is a plain form field;
       *                    <code>false</code> otherwise.
       * @param fileName    The name of the uploaded file, if any, as supplied
       *                    by the browser or other client.
       *
       * @return The newly created file item.
       */
      public FileItem createItem(
              String fieldName,
              String contentType,
              boolean isFormField,
              String fileName
              ) {
          return new DiskFileItem(fieldName, contentType,
                  isFormField, fileName, sizeThreshold, repository);
      }
  
  }
  
  
  
  1.1                  \
jakarta-commons/fileupload/src/java/org/apache/commons/fileupload/portlet/PortletFileUpload.java
  
  Index: PortletFileUpload.java
  ===================================================================
  /*
   * Copyright 2001-2004 The Apache Software Foundation
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.commons.fileupload.portlet;
  
  import java.util.List;
  import javax.portlet.ActionRequest;
  import org.apache.commons.fileupload.FileItemFactory;
  import org.apache.commons.fileupload.FileUpload;
  import org.apache.commons.fileupload.FileUploadBase;
  import org.apache.commons.fileupload.FileUploadException;
  
  /**
   * <p>High level API for processing file uploads.</p>
   *
   * <p>This class handles multiple files per single HTML widget, sent using
   * <code>multipart/mixed</code> encoding type, as specified by
   * <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>.  Use {@link
   * #parseRequest(HttpServletRequest)} to acquire a list of {@link
   * org.apache.commons.fileupload.FileItem}s associated with a given HTML
   * widget.</p>
   *
   * <p>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.</p>
   *
   * @author <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a>
   * @author <a href="mailto:dlr@collab.net">Daniel Rall</a>
   * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
   * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
   * @author <a href="mailto:martinc@apache.org">Martin Cooper</a>
   * @author Sean C. Sullivan
   *
   * @version $Id: PortletFileUpload.java,v 1.1 2004/10/31 05:21:43 martinc Exp $
   */
  public class PortletFileUpload extends FileUpload {
  
      // ---------------------------------------------------------- Class methods
  
  
      /**
       * Utility method that determines whether the request contains multipart
       * content.
       *
       * @param request The portlet request to be evaluated. Must be non-null.
       *
       * @return <code>true</code> if the request is multipart;
       *         <code>false</code> otherwise.
       */
      public static final boolean isMultipartContent(ActionRequest request) {
          return FileUploadBase.isMultipartContent(
                  new PortletRequestContext(request));
      }
  
  
      // ----------------------------------------------------------- Constructors
  
  
      /**
       * Constructs an uninitialised instance of this class. A factory must be
       * configured, using <code>setFileItemFactory()</code>, before attempting
       * to parse requests.
       *
       * @see #FileUpload(FileItemFactory)
       */
      public PortletFileUpload() {
          super();
      }
  
  
      /**
       * Constructs an instance of this class which uses the supplied factory to
       * create <code>FileItem</code> instances.
       *
       * @see #FileUpload()
       */
      public PortletFileUpload(FileItemFactory fileItemFactory) {
          super(fileItemFactory);
      }
  
  
      // --------------------------------------------------------- Public methods
  
  
      public List /* FileItem */ parseRequest(ActionRequest request)
              throws FileUploadException {
          return parseRequest(new PortletRequestContext(request));
      }
  }
  
  
  
  1.1                  \
jakarta-commons/fileupload/src/java/org/apache/commons/fileupload/portlet/PortletRequestContext.java
  
  Index: PortletRequestContext.java
  ===================================================================
  /*
   * Copyright 2001-2004 The Apache Software Foundation
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.commons.fileupload.portlet;
  
  import java.io.InputStream;
  import java.io.IOException;
  import javax.portlet.ActionRequest;
  import org.apache.commons.fileupload.RequestContext;
  
  /**
   * <p>Provides access to the request information needed for a request made to
   * a portlet.</p>
   *
   * @author <a href="mailto:martinc@apache.org">Martin Cooper</a>
   *
   * @since FileUpload 1.1
   *
   * @version $Id: PortletRequestContext.java,v 1.1 2004/10/31 05:21:43 martinc Exp $
   */
  public class PortletRequestContext implements RequestContext {
  
      // ----------------------------------------------------- Instance Variables
  
      /**
       * The request for which the context is being provided.
       */
      private ActionRequest request;
  
  
      // ----------------------------------------------------------- Constructors
  
      /**
       * Construct a context for this request.
       */
      public PortletRequestContext(ActionRequest request) {
          this.request = request;
      }
  
  
      // --------------------------------------------------------- Public Methods
  
      /**
       * Retrieve the content type of the request.
       *
       * @return The content type of the request.
       */
      public String getContentType() {
          return request.getContentType();
      }
  
      /**
       * Retrieve the content length of the request.
       *
       * @return The content length of the request.
       */
      public int getContentLength() {
          return request.getContentLength();
      }
  
      /**
       * Retrieve the input stream for the request.
       *
       * @return The input stream for the request.
       *
       * @throws IOException if a problem occurs.
       */
      public InputStream getInputStream() throws IOException {
          return request.getPortletInputStream();
      }
  }
  
  
  
  1.1                  \
jakarta-commons/fileupload/src/java/org/apache/commons/fileupload/servlet/ServletFileUpload.java
  
  Index: ServletFileUpload.java
  ===================================================================
  /*
   * Copyright 2001-2004 The Apache Software Foundation
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.commons.fileupload.servlet;
  
  import java.util.List;
  import javax.servlet.http.HttpServletRequest;
  import org.apache.commons.fileupload.FileItemFactory;
  import org.apache.commons.fileupload.FileUpload;
  import org.apache.commons.fileupload.FileUploadException;
  
  /**
   * <p>High level API for processing file uploads.</p>
   *
   * <p>This class handles multiple files per single HTML widget, sent using
   * <code>multipart/mixed</code> encoding type, as specified by
   * <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>.  Use {@link
   * #parseRequest(HttpServletRequest)} to acquire a list of {@link
   * org.apache.commons.fileupload.FileItem}s associated with a given HTML
   * widget.</p>
   *
   * <p>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.</p>
   *
   * @author <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a>
   * @author <a href="mailto:dlr@collab.net">Daniel Rall</a>
   * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
   * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
   * @author <a href="mailto:martinc@apache.org">Martin Cooper</a>
   * @author Sean C. Sullivan
   *
   * @version $Id: ServletFileUpload.java,v 1.1 2004/10/31 05:21:43 martinc Exp $
   */
  public class ServletFileUpload extends FileUpload {
  
      // ---------------------------------------------------------- Class methods
  
  
      /**
       * Utility method that determines whether the request contains multipart
       * content.
       *
       * @param req The servlet request to be evaluated. Must be non-null.
       *
       * @return <code>true</code> if the request is multipart;
       *         <code>false</code> otherwise.
       */
      //NOTE: This method cannot be enabled until the one in FileUploadBase is
      //      removed, since it is not possible to override a static method.
      //public static final boolean isMultipartContent(
      //        HttpServletRequest request) {
      //    if (!"post".equals(request.getMethod().toLowerCase())) {
      //        return false;
      //    }
      //    return FileUploadBase.isMultipartContent(
      //            new ServletRequestContext(request));
      //}
  
  
      // ----------------------------------------------------------- Constructors
  
  
      /**
       * Constructs an uninitialised instance of this class. A factory must be
       * configured, using <code>setFileItemFactory()</code>, before attempting
       * to parse requests.
       *
       * @see #FileUpload(FileItemFactory)
       */
      public ServletFileUpload() {
          super();
      }
  
  
      /**
       * Constructs an instance of this class which uses the supplied factory to
       * create <code>FileItem</code> instances.
       *
       * @see #FileUpload()
       */
      public ServletFileUpload(FileItemFactory fileItemFactory) {
          super(fileItemFactory);
      }
  
  
      // --------------------------------------------------------- Public methods
  
  
      /**
       * Processes an <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>
       * compliant <code>multipart/form-data</code> stream.
       *
       * @param request The servlet request to be parsed.
       *
       * @return A list of <code>FileItem</code> instances parsed from the
       *         request, in the order that they were transmitted.
       *
       * @exception FileUploadException if there are problems reading/parsing
       *                                the request or storing files.
       */
      public List /* FileItem */ parseRequest(HttpServletRequest request)
              throws FileUploadException {
          return parseRequest(new ServletRequestContext(request));
      }
  }
  
  
  
  1.1                  \
jakarta-commons/fileupload/src/java/org/apache/commons/fileupload/servlet/ServletRequestContext.java
  
  Index: ServletRequestContext.java
  ===================================================================
  /*
   * Copyright 2001-2004 The Apache Software Foundation
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.commons.fileupload.servlet;
  
  import java.io.InputStream;
  import java.io.IOException;
  import javax.servlet.http.HttpServletRequest;
  import org.apache.commons.fileupload.RequestContext;
  
  /**
   * <p>Provides access to the request information needed for a request made to
   * an HTTP servlet.</p>
   *
   * @author <a href="mailto:martinc@apache.org">Martin Cooper</a>
   *
   * @since FileUpload 1.1
   *
   * @version $Id: ServletRequestContext.java,v 1.1 2004/10/31 05:21:43 martinc Exp $
   */
  public class ServletRequestContext implements RequestContext {
  
      // ----------------------------------------------------- Manifest constants
  
      /**
       * HTTP content type header name.
       */
      public static final String CONTENT_TYPE = "Content-type";
  
  
      // ----------------------------------------------------- Instance Variables
  
      /**
       * The request for which the context is being provided.
       */
      private HttpServletRequest request;
  
  
      // ----------------------------------------------------------- Constructors
  
      /**
       * Construct a context for this request.
       *
       * @param request The request to which this context applies.
       */
      public ServletRequestContext(HttpServletRequest request) {
          this.request = request;
      }
  
  
      // --------------------------------------------------------- Public Methods
  
      /**
       * Retrieve the content type of the request.
       *
       * @return The content type of the request.
       */
      public String getContentType() {
          return request.getHeader(CONTENT_TYPE);
      }
  
      /**
       * Retrieve the content length of the request.
       *
       * @return The content length of the request.
       */
      public int getContentLength() {
          return request.getContentLength();
      }
  
      /**
       * Retrieve the input stream for the request.
       *
       * @return The input stream for the request.
       *
       * @throws IOException if a problem occurs.
       */
      public InputStream getInputStream() throws IOException {
          return request.getInputStream();
      }
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic