View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  package org.apache.commons.fileupload2.core;
    18
    19  import org.apache.commons.io.FileCleaningTracker;
    20  import org.apache.commons.io.build.AbstractStreamBuilder;
    21  import org.apache.commons.io.file.PathUtils;
    22
    23  /**
    24   * Creates {@link FileItem} instances.
    25   * <p>
    26   * Factories can provide their own custom configuration, over and above that provided by the default file upload implementation.
    27   * </p>
    28   *
    29   * @param <I> The {@link FileItem} type this factory creates.
    30   */
    31  public interface FileItemFactory<I extends FileItem<I>> {
    32
    33      /**
    34       * Abstracts building for subclasses.
    35       *
    36       * @param <I> the type of {@link FileItem} to build.
    37       * @param <B> the type of builder subclass.
    38       */
    39      abstract class AbstractFileItemBuilder<I extends FileItem<I>, B extends AbstractFileItemBuilder<I, B>> extends AbstractStreamBuilder<I, B> {
    40
    41          public static FileItemHeaders newFileItemHeaders() {
    42              return new FileItemHeadersImpl();
    43          }
    44
    45          /**
    46           * Field name.
    47           */
    48          private String fieldName;
    49
    50          /**
    51           * Content type.
    52           */
    53          private String contentType;
    54
    55          /**
    56           * Is this a form field.
    57           */
    58          private boolean isFormField;
    59
    60          /**
    61           * File name.
    62           */
    63          private String fileName;
    64
    65          /**
    66           * File item headers.
    67           */
    68          private FileItemHeaders fileItemHeaders = newFileItemHeaders();
    69
    70          /**
    71           * The instance of {@link FileCleaningTracker}, which is responsible for deleting temporary files.
    72           * <p>
    73           * May be null, if tracking files is not required.
    74           * </p>
    75           */
    76          private FileCleaningTracker fileCleaningTracker;
    77
    78          public AbstractFileItemBuilder() {
    79              setBufferSize(DiskFileItemFactory.DEFAULT_THRESHOLD);
    80              setPath(PathUtils.getTempDirectory());
    81          }
    82
    83          public String getContentType() {
    84              return contentType;
    85          }
    86
    87          public String getFieldName() {
    88              return fieldName;
    89          }
    90
    91          public FileCleaningTracker getFileCleaningTracker() {
    92              return fileCleaningTracker;
    93          }
    94
    95          public FileItemHeaders getFileItemHeaders() {
    96              return fileItemHeaders;
    97          }
    98
    99          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 }