FileItemFactory.java

  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. import org.apache.commons.io.FileCleaningTracker;
  19. import org.apache.commons.io.build.AbstractStreamBuilder;
  20. import org.apache.commons.io.file.PathUtils;

  21. /**
  22.  * Creates {@link FileItem} instances.
  23.  * <p>
  24.  * Factories can provide their own custom configuration, over and above that provided by the default file upload implementation.
  25.  * </p>
  26.  *
  27.  * @param <I> The {@link FileItem} type this factory creates.
  28.  */
  29. public interface FileItemFactory<I extends FileItem<I>> {

  30.     /**
  31.      * Abstracts building for subclasses.
  32.      *
  33.      * @param <I> the type of {@link FileItem} to build.
  34.      * @param <B> the type of builder subclass.
  35.      */
  36.     abstract class AbstractFileItemBuilder<I extends FileItem<I>, B extends AbstractFileItemBuilder<I, B>> extends AbstractStreamBuilder<I, B> {

  37.         /**
  38.          * Create a new FileItemHeaders implementation.
  39.          *
  40.          * @return a new FileItemHeaders implementation.
  41.          */
  42.         public static FileItemHeaders newFileItemHeaders() {
  43.             return new FileItemHeadersImpl();
  44.         }

  45.         /**
  46.          * Field name.
  47.          */
  48.         private String fieldName;

  49.         /**
  50.          * Content type.
  51.          */
  52.         private String contentType;

  53.         /**
  54.          * Is this a form field.
  55.          */
  56.         private boolean isFormField;

  57.         /**
  58.          * File name.
  59.          */
  60.         private String fileName;

  61.         /**
  62.          * File item headers.
  63.          */
  64.         private FileItemHeaders fileItemHeaders = newFileItemHeaders();

  65.         /**
  66.          * The instance of {@link FileCleaningTracker}, which is responsible for deleting temporary files.
  67.          * <p>
  68.          * May be null, if tracking files is not required.
  69.          * </p>
  70.          */
  71.         private FileCleaningTracker fileCleaningTracker;

  72.         /**
  73.          * Constructs a new instance.
  74.          */
  75.         public AbstractFileItemBuilder() {
  76.             setBufferSize(DiskFileItemFactory.DEFAULT_THRESHOLD);
  77.             setPath(PathUtils.getTempDirectory());
  78.         }

  79.         /**
  80.          * Gets the content type.
  81.          *
  82.          * @return the content type.
  83.          */
  84.         public String getContentType() {
  85.             return contentType;
  86.         }

  87.         /**
  88.          * Gets the field name.
  89.          *
  90.          * @return the field name.
  91.          */
  92.         public String getFieldName() {
  93.             return fieldName;
  94.         }

  95.         /**
  96.          * Gets the file cleaning tracker.
  97.          *
  98.          * @return the file cleaning tracker.
  99.          */
  100.         public FileCleaningTracker getFileCleaningTracker() {
  101.             return fileCleaningTracker;
  102.         }

  103.         /**
  104.          * Gets the field item headers.
  105.          *
  106.          * @return the field item headers.
  107.          */
  108.         public FileItemHeaders getFileItemHeaders() {
  109.             return fileItemHeaders;
  110.         }

  111.         /**
  112.          * Gets the file name.
  113.          *
  114.          * @return the file name.
  115.          */
  116.         public String getFileName() {
  117.             return fileName;
  118.         }

  119.         /**
  120.          * Tests whether this is a form field.
  121.          *
  122.          * @return whether this is a form field.
  123.          */
  124.         public boolean isFormField() {
  125.             return isFormField;
  126.         }

  127.         /**
  128.          * Sets the content type.
  129.          *
  130.          * @param contentType the content type.
  131.          * @return {@code this} instance.
  132.          */
  133.         public B setContentType(final String contentType) {
  134.             this.contentType = contentType;
  135.             return asThis();
  136.         }

  137.         /**
  138.          * Sets the field name.
  139.          *
  140.          * @param fieldName the field name.
  141.          * @return {@code this} instance.
  142.          */
  143.         public B setFieldName(final String fieldName) {
  144.             this.fieldName = fieldName;
  145.             return asThis();
  146.         }

  147.         /**
  148.          * Sets the file cleaning tracker.
  149.          *
  150.          * @param fileCleaningTracker the file cleaning tracker.
  151.          * @return {@code this} instance.
  152.          */
  153.         public B setFileCleaningTracker(final FileCleaningTracker fileCleaningTracker) {
  154.             this.fileCleaningTracker = fileCleaningTracker;
  155.             return asThis();
  156.         }

  157.         /**
  158.          * Sets the file item headers.
  159.          *
  160.          * @param fileItemHeaders the item headers.
  161.          * @return {@code this} instance.
  162.          */
  163.         public B setFileItemHeaders(final FileItemHeaders fileItemHeaders) {
  164.             this.fileItemHeaders = fileItemHeaders != null ? fileItemHeaders : newFileItemHeaders();
  165.             return asThis();
  166.         }

  167.         /**
  168.          * Sets the file name.
  169.          *
  170.          * @param fileName the file name.
  171.          * @return {@code this} instance.
  172.          */
  173.         public B setFileName(final String fileName) {
  174.             this.fileName = fileName;
  175.             return asThis();
  176.         }

  177.         /**
  178.          * Sets whether this is a form field.
  179.          *
  180.          * @param isFormField whether this is a form field.
  181.          * @return {@code this} instance.
  182.          */
  183.         public B setFormField(final boolean isFormField) {
  184.             this.isFormField = isFormField;
  185.             return asThis();
  186.         }

  187.     }

  188.     /**
  189.      * Creates a new AbstractFileItemBuilder.
  190.      *
  191.      * @param <B> The type of AbstractFileItemBuilder.
  192.      * @return a new AbstractFileItemBuilder.
  193.      */
  194.     <B extends AbstractFileItemBuilder<I, B>> AbstractFileItemBuilder<I, B> fileItemBuilder();

  195. }