FileItemInput.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 java.io.IOException;
  19. import java.io.InputStream;

  20. /**
  21.  * Provides access to a file or form item that was received within a {@code multipart/form-data} POST request.
  22.  * <p>
  23.  * The items contents are retrieved by calling {@link #getInputStream()}.
  24.  * </p>
  25.  * <p>
  26.  * Instances of this class are created by accessing the iterator, returned by {@link AbstractFileUpload#getItemIterator(RequestContext)}.
  27.  * </p>
  28.  * <p>
  29.  * <em>Note</em>: There is an interaction between the iterator and its associated instances of {@link FileItemInput}: By invoking
  30.  * {@link java.util.Iterator#hasNext()} on the iterator, you discard all data, which hasn't been read so far from the previous data.
  31.  * </p>
  32.  */
  33. public interface FileItemInput extends FileItemHeadersProvider<FileItemInput> {

  34.     /**
  35.      * This exception is thrown, if an attempt is made to read data from the {@link InputStream}, which has been returned by
  36.      * {@link FileItemInput#getInputStream()}, after {@link java.util.Iterator#hasNext()} has been invoked on the iterator, which created the
  37.      * {@link FileItemInput}.
  38.      */
  39.     class ItemSkippedException extends FileUploadException {

  40.         /**
  41.          * The exceptions serial version UID, which is being used when serializing an exception instance.
  42.          */
  43.         private static final long serialVersionUID = 2;

  44.         /**
  45.          * Constructs an instance with a given detail message.
  46.          *
  47.          * @param message The detail message (which is saved for later retrieval by the {@link #getMessage()} method)
  48.          */
  49.         ItemSkippedException(final String message) {
  50.             super(message);
  51.         }

  52.     }

  53.     /**
  54.      * Gets the content type passed by the browser or {@code null} if not defined.
  55.      *
  56.      * @return The content type passed by the browser or {@code null} if not defined.
  57.      */
  58.     String getContentType();

  59.     /**
  60.      * Gets the name of the field in the multipart form corresponding to this file item.
  61.      *
  62.      * @return The name of the form field.
  63.      */
  64.     String getFieldName();

  65.     /**
  66.      * Opens an {@link InputStream}, which allows to read the items contents.
  67.      *
  68.      * @return The input stream, from which the items data may be read.
  69.      * @throws IllegalStateException The method was already invoked on this item. It is not possible to recreate the data stream.
  70.      * @throws IOException           An I/O error occurred.
  71.      * @see ItemSkippedException
  72.      */
  73.     InputStream getInputStream() throws IOException;

  74.     /**
  75.      * Gets the original file name in the client's file system, as provided by the browser (or other client software). In most cases, this will be the base file
  76.      * name, without path information. However, some clients, such as the Opera browser, do include path information.
  77.      *
  78.      * @return The original file name in the client's file system.
  79.      */
  80.     String getName();

  81.     /**
  82.      * Tests whether or not a {@code FileItem} instance represents a simple form field.
  83.      *
  84.      * @return {@code true} if the instance represents a simple form field; {@code false} if it represents an uploaded file.
  85.      */
  86.     boolean isFormField();

  87. }