001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.tomcat.util.http.fileupload;
019:
020: import java.io.File;
021:
022: /**
023: * <p>The default {@link org.apache.tomcat.util.http.fileupload.FileItemFactory}
024: * implementation. This implementation creates
025: * {@link org.apache.tomcat.util.http.fileupload.FileItem} instances which keep their
026: * content either in memory, for smaller items, or in a temporary file on disk,
027: * for larger items. The size threshold, above which content will be stored on
028: * disk, is configurable, as is the directory in which temporary files will be
029: * created.</p>
030: *
031: * <p>If not otherwise configured, the default configuration values are as
032: * follows:
033: * <ul>
034: * <li>Size threshold is 10KB.</li>
035: * <li>Repository is the system default temp directory, as returned by
036: * <code>System.getProperty("java.io.tmpdir")</code>.</li>
037: * </ul>
038: * </p>
039: *
040: * @author <a href="mailto:martinc@apache.org">Martin Cooper</a>
041: *
042: * @version $Id: DefaultFileItemFactory.java 467222 2006-10-24 03:17:11Z markt $
043: */
044: public class DefaultFileItemFactory implements FileItemFactory {
045:
046: // ----------------------------------------------------- Manifest constants
047:
048: /**
049: * The default threshold above which uploads will be stored on disk.
050: */
051: public static final int DEFAULT_SIZE_THRESHOLD = 10240;
052:
053: // ----------------------------------------------------- Instance Variables
054:
055: /**
056: * The directory in which uploaded files will be stored, if stored on disk.
057: */
058: private File repository;
059:
060: /**
061: * The threshold above which uploads will be stored on disk.
062: */
063: private int sizeThreshold = DEFAULT_SIZE_THRESHOLD;
064:
065: // ----------------------------------------------------------- Constructors
066:
067: /**
068: * Constructs an unconfigured instance of this class. The resulting factory
069: * may be configured by calling the appropriate setter methods.
070: */
071: public DefaultFileItemFactory() {
072: }
073:
074: /**
075: * Constructs a preconfigured instance of this class.
076: *
077: * @param sizeThreshold The threshold, in bytes, below which items will be
078: * retained in memory and above which they will be
079: * stored as a file.
080: * @param repository The data repository, which is the directory in
081: * which files will be created, should the item size
082: * exceed the threshold.
083: */
084: public DefaultFileItemFactory(int sizeThreshold, File repository) {
085: this .sizeThreshold = sizeThreshold;
086: this .repository = repository;
087: }
088:
089: // ------------------------------------------------------------- Properties
090:
091: /**
092: * Returns the directory used to temporarily store files that are larger
093: * than the configured size threshold.
094: *
095: * @return The directory in which temporary files will be located.
096: *
097: * @see #setRepository(java.io.File)
098: *
099: */
100: public File getRepository() {
101: return repository;
102: }
103:
104: /**
105: * Sets the directory used to temporarily store files that are larger
106: * than the configured size threshold.
107: *
108: * @param repository The directory in which temporary files will be located.
109: *
110: * @see #getRepository()
111: *
112: */
113: public void setRepository(File repository) {
114: this .repository = repository;
115: }
116:
117: /**
118: * Returns the size threshold beyond which files are written directly to
119: * disk. The default value is 1024 bytes.
120: *
121: * @return The size threshold, in bytes.
122: *
123: * @see #setSizeThreshold(int)
124: */
125: public int getSizeThreshold() {
126: return sizeThreshold;
127: }
128:
129: /**
130: * Sets the size threshold beyond which files are written directly to disk.
131: *
132: * @param sizeThreshold The size threshold, in bytes.
133: *
134: * @see #getSizeThreshold()
135: *
136: */
137: public void setSizeThreshold(int sizeThreshold) {
138: this .sizeThreshold = sizeThreshold;
139: }
140:
141: // --------------------------------------------------------- Public Methods
142:
143: /**
144: * Create a new {@link org.apache.tomcat.util.http.fileupload.DefaultFileItem}
145: * instance from the supplied parameters and the local factory
146: * configuration.
147: *
148: * @param fieldName The name of the form field.
149: * @param contentType The content type of the form field.
150: * @param isFormField <code>true</code> if this is a plain form field;
151: * <code>false</code> otherwise.
152: * @param fileName The name of the uploaded file, if any, as supplied
153: * by the browser or other client.
154: *
155: * @return The newly created file item.
156: */
157: public FileItem createItem(String fieldName, String contentType,
158: boolean isFormField, String fileName) {
159: return new DefaultFileItem(fieldName, contentType, isFormField,
160: fileName, sizeThreshold, repository);
161: }
162:
163: }
|