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.fileupload;
018
019import java.io.IOException;
020import java.io.InputStream;
021
022/**
023 * <p> This interface provides access to a file or form item that was
024 * received within a {@code multipart/form-data} POST request.
025 * The items contents are retrieved by calling {@link #openStream()}.</p>
026 * <p>Instances of this class are created by accessing the
027 * iterator, returned by
028 * {@link FileUploadBase#getItemIterator(RequestContext)}.</p>
029 * <p><em>Note</em>: There is an interaction between the iterator and
030 * its associated instances of {@link FileItemStream}: By invoking
031 * {@link java.util.Iterator#hasNext()} on the iterator, you discard all data,
032 * which hasn't been read so far from the previous data.</p>
033 */
034public interface FileItemStream extends FileItemHeadersSupport {
035
036    /**
037     * This exception is thrown, if an attempt is made to read
038     * data from the {@link InputStream}, which has been returned
039     * by {@link FileItemStream#openStream()}, after
040     * {@link java.util.Iterator#hasNext()} has been invoked on the
041     * iterator, which created the {@link FileItemStream}.
042     */
043    class ItemSkippedException extends IOException {
044
045        /**
046         * The exceptions serial version UID, which is being used
047         * when serializing an exception instance.
048         */
049        private static final long serialVersionUID = -7280778431581963740L;
050
051        /**
052         * Constructs a new instance.
053         */
054        public ItemSkippedException() {
055            // empty
056        }
057
058    }
059
060    /**
061     * Returns the content type passed by the browser or {@code null} if
062     * not defined.
063     *
064     * @return The content type passed by the browser or {@code null} if
065     *         not defined.
066     */
067    String getContentType();
068
069    /**
070     * Returns the name of the field in the multipart form corresponding to
071     * this file item.
072     *
073     * @return The name of the form field.
074     */
075    String getFieldName();
076
077    /**
078     * Returns the original file name in the client's file system, as provided by
079     * the browser (or other client software). In most cases, this will be the
080     * base file name, without path information. However, some clients, such as
081     * the Opera browser, do include path information.
082     *
083     * @return The original file name in the client's file system.
084     */
085    String getName();
086
087    /**
088     * Determines whether or not a {@code FileItem} instance represents
089     * a simple form field.
090     *
091     * @return {@code true} if the instance represents a simple form
092     *         field; {@code false} if it represents an uploaded file.
093     */
094    boolean isFormField();
095
096    /**
097     * Creates an {@link InputStream}, which allows to read the
098     * items contents.
099     *
100     * @return The input stream, from which the items data may
101     *   be read.
102     * @throws IllegalStateException The method was already invoked on
103     * this item. It is not possible to recreate the data stream.
104     * @throws IOException An I/O error occurred.
105     * @see ItemSkippedException
106     */
107    InputStream openStream() throws IOException;
108
109}