001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.fileupload2.core;
018
019import org.apache.commons.io.FileCleaningTracker;
020import org.apache.commons.io.build.AbstractStreamBuilder;
021import org.apache.commons.io.file.PathUtils;
022
023/**
024 * Creates {@link FileItem} instances.
025 * <p>
026 * Factories can provide their own custom configuration, over and above that provided by the default file upload implementation.
027 * </p>
028 *
029 * @param <I> The {@link FileItem} type this factory creates.
030 */
031public interface FileItemFactory<I extends FileItem<I>> {
032
033    /**
034     * Abstracts building for subclasses.
035     *
036     * @param <I> the type of {@link FileItem} to build.
037     * @param <B> the type of builder subclass.
038     */
039    abstract class AbstractFileItemBuilder<I extends FileItem<I>, B extends AbstractFileItemBuilder<I, B>> extends AbstractStreamBuilder<I, B> {
040
041        /**
042         * Create a new FileItemHeaders implementation.
043         *
044         * @return a new FileItemHeaders implementation.
045         */
046        public static FileItemHeaders newFileItemHeaders() {
047            return new FileItemHeadersImpl();
048        }
049
050        /**
051         * Field name.
052         */
053        private String fieldName;
054
055        /**
056         * Content type.
057         */
058        private String contentType;
059
060        /**
061         * Is this a form field.
062         */
063        private boolean isFormField;
064
065        /**
066         * File name.
067         */
068        private String fileName;
069
070        /**
071         * File item headers.
072         */
073        private FileItemHeaders fileItemHeaders = newFileItemHeaders();
074
075        /**
076         * The instance of {@link FileCleaningTracker}, which is responsible for deleting temporary files.
077         * <p>
078         * May be null, if tracking files is not required.
079         * </p>
080         */
081        private FileCleaningTracker fileCleaningTracker;
082
083        /**
084         * Constructs a new instance.
085         */
086        public AbstractFileItemBuilder() {
087            setBufferSize(DiskFileItemFactory.DEFAULT_THRESHOLD);
088            setPath(PathUtils.getTempDirectory());
089        }
090
091        /**
092         * Gets the content type.
093         *
094         * @return the content type.
095         */
096        public String getContentType() {
097            return contentType;
098        }
099
100        /**
101         * Gets the field name.
102         *
103         * @return the field name.
104         */
105        public String getFieldName() {
106            return fieldName;
107        }
108
109        /**
110         * Gets the file cleaning tracker.
111         *
112         * @return the file cleaning tracker.
113         */
114        public FileCleaningTracker getFileCleaningTracker() {
115            return fileCleaningTracker;
116        }
117
118        /**
119         * Gets the field item headers.
120         *
121         * @return the field item headers.
122         */
123        public FileItemHeaders getFileItemHeaders() {
124            return fileItemHeaders;
125        }
126
127        /**
128         * Gets the file name.
129         *
130         * @return the file name.
131         */
132        public String getFileName() {
133            return fileName;
134        }
135
136        /**
137         * Tests whether this is a form field.
138         *
139         * @return whether this is a form field.
140         */
141        public boolean isFormField() {
142            return isFormField;
143        }
144
145        /**
146         * Sets the content type.
147         *
148         * @param contentType the content type.
149         * @return {@code this} instance.
150         */
151        public B setContentType(final String contentType) {
152            this.contentType = contentType;
153            return asThis();
154        }
155
156        /**
157         * Sets the field name.
158         *
159         * @param fieldName the field name.
160         * @return {@code this} instance.
161         */
162        public B setFieldName(final String fieldName) {
163            this.fieldName = fieldName;
164            return asThis();
165        }
166
167        /**
168         * Sets the file cleaning tracker.
169         *
170         * @param fileCleaningTracker the file cleaning tracker.
171         * @return {@code this} instance.
172         */
173        public B setFileCleaningTracker(final FileCleaningTracker fileCleaningTracker) {
174            this.fileCleaningTracker = fileCleaningTracker;
175            return asThis();
176        }
177
178        /**
179         * Sets the file item headers.
180         *
181         * @param fileItemHeaders the item headers.
182         * @return {@code this} instance.
183         */
184        public B setFileItemHeaders(final FileItemHeaders fileItemHeaders) {
185            this.fileItemHeaders = fileItemHeaders != null ? fileItemHeaders : newFileItemHeaders();
186            return asThis();
187        }
188
189        /**
190         * Sets the file name.
191         *
192         * @param fileName the file name.
193         * @return {@code this} instance.
194         */
195        public B setFileName(final String fileName) {
196            this.fileName = fileName;
197            return asThis();
198        }
199
200        /**
201         * Sets whether this is a form field.
202         *
203         * @param isFormField whether this is a form field.
204         * @return {@code this} instance.
205         */
206        public B setFormField(final boolean isFormField) {
207            this.isFormField = isFormField;
208            return asThis();
209        }
210
211    }
212
213    /**
214     * Creates a new AbstractFileItemBuilder.
215     *
216     * @param <B> The type of AbstractFileItemBuilder.
217     * @return a new AbstractFileItemBuilder.
218     */
219    <B extends AbstractFileItemBuilder<I, B>> AbstractFileItemBuilder<I, B> fileItemBuilder();
220
221}