View Javadoc

1   /*
2    * $Header: /cvsroot/director168/director_standard_portlets/src/org/apache/commons/fileupload/PortletDiskFileUpload.java,v 1.1 2003/10/01 22:21:43 jsackett Exp $
3    * $Revision: 1.1 $
4    * $Date: 2003/10/01 22:21:43 $
5    *
6    * ====================================================================
7    *
8    * The Apache Software License, Version 1.1
9    *
10   * Copyright (c) 2001-2003 The Apache Software Foundation.  All rights
11   * reserved.
12   *
13   * Redistribution and use in source and binary forms, with or without
14   * modification, are permitted provided that the following conditions
15   * are met:
16   *
17   * 1. Redistributions of source code must retain the above copyright
18   *    notice, this list of conditions and the following disclaimer.
19   *
20   * 2. Redistributions in binary form must reproduce the above copyright
21   *    notice, this list of conditions and the following disclaimer in
22   *    the documentation and/or other materials provided with the
23   *    distribution.
24   *
25   * 3. The end-user documentation included with the redistribution, if
26   *    any, must include the following acknowlegement:
27   *       "This product includes software developed by the
28   *        Apache Software Foundation (http://www.apache.org/)."
29   *    Alternately, this acknowlegement may appear in the software itself,
30   *    if and wherever such third-party acknowlegements normally appear.
31   *
32   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
33   *    Foundation" must not be used to endorse or promote products derived
34   *    from this software without prior written permission. For written
35   *    permission, please contact apache@apache.org.
36   *
37   * 5. Products derived from this software may not be called "Apache"
38   *    nor may "Apache" appear in their names without prior written
39   *    permission of the Apache Group.
40   *
41   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
42   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
45   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
48   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
51   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52   * SUCH DAMAGE.
53   * ====================================================================
54   *
55   * This software consists of voluntary contributions made by many
56   * individuals on behalf of the Apache Software Foundation.  For more
57   * information on the Apache Software Foundation, please see
58   * <http://www.apache.org/>.
59   *
60   */
61  
62  
63  package org.apache.commons.fileupload;
64  
65  
66  import java.io.File;
67  import java.util.List;
68  
69  import javax.portlet.ActionRequest;
70  
71  
72  /***
73   * <p>High level API for processing file uploads.</p>
74   *
75   * <p>This class handles multiple files per single HTML widget, sent using
76   * <code>multipart/mixed</code> encoding type, as specified by
77   * <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>.  Use {@link
78   * #parseRequest(HttpServletRequest)} to acquire a list of {@link
79   * org.apache.commons.fileupload.FileItem}s associated with a given HTML
80   * widget.</p>
81   *
82   * <p>Individual parts will be stored in temporary disk storage or in memory,
83   * depending on their size, and will be available as {@link
84   * org.apache.commons.fileupload.FileItem}s.</p>
85   *
86   * @author <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a>
87   * @author <a href="mailto:dlr@collab.net">Daniel Rall</a>
88   * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
89   * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
90   * @author <a href="mailto:martinc@apache.org">Martin Cooper</a>
91   * @author Sean C. Sullivan
92   *
93   * @version $Id: PortletDiskFileUpload.java,v 1.1 2003/10/01 22:21:43 jsackett Exp $
94   */
95  public class PortletDiskFileUpload
96      extends PortletFileUploadBase
97   {
98  
99      // ----------------------------------------------------------- Data members
100 
101 
102     /***
103      * The factory to use to create new form items.
104      */
105     private DefaultFileItemFactory fileItemFactory;
106 
107 
108     // ----------------------------------------------------------- Constructors
109 
110 
111     /***
112      * Constructs an instance of this class which uses the default factory to
113      * create <code>FileItem</code> instances.
114      *
115      * @see #DiskFileUpload(DefaultFileItemFactory fileItemFactory)
116      */
117     public PortletDiskFileUpload()
118     {
119         super();
120         this.fileItemFactory = new DefaultFileItemFactory();
121     }
122 
123 
124     /***
125      * Constructs an instance of this class which uses the supplied factory to
126      * create <code>FileItem</code> instances.
127      *
128      * @see #DiskFileUpload()
129      */
130     public PortletDiskFileUpload(DefaultFileItemFactory fileItemFactory)
131     {
132         super();
133         this.fileItemFactory = fileItemFactory;
134     }
135 
136 
137     // ----------------------------------------------------- Property accessors
138 
139 
140     /***
141      * Returns the factory class used when creating file items.
142      *
143      * @return The factory class for new file items.
144      */
145     public FileItemFactory getFileItemFactory()
146     {
147         return fileItemFactory;
148     }
149 
150 
151     /***
152      * Sets the factory class to use when creating file items. The factory must
153      * be an instance of <code>DefaultFileItemFactory</code> or a subclass
154      * thereof, or else a <code>ClassCastException</code> will be thrown.
155      *
156      * @param factory The factory class for new file items.
157      */
158     public void setFileItemFactory(FileItemFactory factory)
159     {
160         this.fileItemFactory = (DefaultFileItemFactory) factory;
161     }
162 
163 
164     /***
165      * Returns the size threshold beyond which files are written directly to
166      * disk.
167      *
168      * @return The size threshold, in bytes.
169      *
170      * @see #setSizeThreshold(int)
171      */
172     public int getSizeThreshold()
173     {
174         return fileItemFactory.getSizeThreshold();
175     }
176 
177 
178     /***
179      * Sets the size threshold beyond which files are written directly to disk.
180      *
181      * @param sizeThreshold The size threshold, in bytes.
182      *
183      * @see #getSizeThreshold()
184      */
185     public void setSizeThreshold(int sizeThreshold)
186     {
187         fileItemFactory.setSizeThreshold(sizeThreshold);
188     }
189 
190 
191     /***
192      * Returns the location used to temporarily store files that are larger
193      * than the configured size threshold.
194      *
195      * @return The path to the temporary file location.
196      *
197      * @see #setRepositoryPath(String)
198      */
199     public String getRepositoryPath()
200     {
201         return fileItemFactory.getRepository().getPath();
202     }
203 
204 
205     /***
206      * Sets the location used to temporarily store files that are larger
207      * than the configured size threshold.
208      *
209      * @param repositoryPath The path to the temporary file location.
210      *
211      * @see #getRepositoryPath()
212      */
213     public void setRepositoryPath(String repositoryPath)
214     {
215         fileItemFactory.setRepository(new File(repositoryPath));
216     }
217 
218 
219     // --------------------------------------------------------- Public methods
220 
221 
222     /***
223      * Processes an <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>
224      * compliant <code>multipart/form-data</code> stream. If files are stored
225      * on disk, the path is given by <code>getRepository()</code>.
226      *
227      * @param req           The servlet request to be parsed. Must be non-null.
228      * @param sizeThreshold The max size in bytes to be stored in memory.
229      * @param sizeMax       The maximum allowed upload size, in bytes.
230      * @param path          The location where the files should be stored.
231      *
232      * @return A list of <code>FileItem</code> instances parsed from the
233      *         request, in the order that they were transmitted.
234      *
235      * @exception FileUploadException if there are problems reading/parsing
236      *                                the request or storing files.
237      */
238     public List /* FileItem */ parseRequest(ActionRequest req,
239                                             int sizeThreshold,
240                                             long sizeMax, String path)
241         throws FileUploadException
242     {
243         setSizeThreshold(sizeThreshold);
244         setSizeMax(sizeMax);
245         setRepositoryPath(path);
246         return parseRequest(req);
247     }
248 
249 }