    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        public static FileItemHeaders newFileItemHeaders() {
    042            return new FileItemHeadersImpl();
    043        }
    044
    045        /**
    046         * Field name.
    047         */
    048        private String fieldName;
    049
    050        /**
    051         * Content type.
    052         */
    053        private String contentType;
    054
    055        /**
    056         * Is this a form field.
    057         */
    058        private boolean isFormField;
    059
    060        /**
    061         * File name.
    062         */
    063        private String fileName;
    064
    065        /**
    066         * File item headers.
    067         */
    068        private FileItemHeaders fileItemHeaders = newFileItemHeaders();
    069
    070        /**
    071         * The instance of {@link FileCleaningTracker}, which is responsible for deleting temporary files.
    072         * <p>
    073         * May be null, if tracking files is not required.
    074         * </p>
    075         */
    076        private FileCleaningTracker fileCleaningTracker;
    077
    078        public AbstractFileItemBuilder() {
    079            setBufferSize(DiskFileItemFactory.DEFAULT_THRESHOLD);
    080            setPath(PathUtils.getTempDirectory());
    081        }
    082
    083        public String getContentType() {
    084            return contentType;
    085        }
    086
    087        public String getFieldName() {
    088            return fieldName;
    089        }
    090
    091        public FileCleaningTracker getFileCleaningTracker() {
    092            return fileCleaningTracker;
    093        }
    094
    095        public FileItemHeaders getFileItemHeaders() {
    096            return fileItemHeaders;
    097        }
    098
    099        public String getFileName() {
    100            return fileName;
    101        }
    102
    103        public boolean isFormField() {
    104            return isFormField;
    105        }
    106
    107        public B setContentType(final String contentType) {
    108            this.contentType = contentType;
    109            return asThis();
    110        }
    111
    112        public B setFieldName(final String fieldName) {
    113            this.fieldName = fieldName;
    114            return asThis();
    115        }
    116
    117        public B setFileCleaningTracker(final FileCleaningTracker fileCleaningTracker) {
    118            this.fileCleaningTracker = fileCleaningTracker;
    119            return asThis();
    120        }
    121
    122        public B setFileItemHeaders(final FileItemHeaders fileItemHeaders) {
    123            this.fileItemHeaders = fileItemHeaders != null ? fileItemHeaders : newFileItemHeaders();
    124            return asThis();
    125        }
    126
    127        public B setFileName(final String fileName) {
    128            this.fileName = fileName;
    129            return asThis();
    130        }
    131
    132        public B setFormField(final boolean isFormField) {
    133            this.isFormField = isFormField;
    134            return asThis();
    135        }
    136
    137    }
    138
    139    /**
    140     * Creates a new AbstractFileItemBuilder.
    141     *
    142     * @param <B> The type of AbstractFileItemBuilder.
    143     * @return a new AbstractFileItemBuilder.
    144     */
    145    <B extends AbstractFileItemBuilder<I, B>> AbstractFileItemBuilder<I, B> fileItemBuilder();
    146
    147}