JavaxServletFileUpload.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.javax;

  18. import java.io.IOException;
  19. import java.util.List;
  20. import java.util.Map;

  21. import javax.servlet.http.HttpServletRequest;

  22. import org.apache.commons.fileupload2.core.AbstractFileUpload;
  23. import org.apache.commons.fileupload2.core.FileItem;
  24. import org.apache.commons.fileupload2.core.FileItemFactory;
  25. import org.apache.commons.fileupload2.core.FileItemInputIterator;
  26. import org.apache.commons.fileupload2.core.FileUploadException;

  27. /**
  28.  * High level API for processing file uploads.
  29.  * <p>
  30.  * This class handles multiple files per single HTML widget, sent using {@code multipart/mixed} encoding type, as specified by
  31.  * <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>. Use {@link #parseRequest(HttpServletRequest)} to acquire a list of {@link FileItem}s associated
  32.  * with a given HTML widget.
  33.  * </p>
  34.  * <p>
  35.  * How the data for individual parts is stored is determined by the factory used to create them; a given part may be in memory, on disk, or somewhere else.
  36.  * </p>
  37.  *
  38.  * @param <I> The FileItem type.
  39.  * @param <F> the FileItemFactory type.
  40.  */
  41. public class JavaxServletFileUpload<I extends FileItem<I>, F extends FileItemFactory<I>> extends AbstractFileUpload<HttpServletRequest, I, F> {

  42.     /**
  43.      * Constant for HTTP POST method.
  44.      */
  45.     private static final String POST_METHOD = "POST";

  46.     /**
  47.      * Tests whether the request contains multipart content.
  48.      *
  49.      * @param request The servlet request to be evaluated. Must be non-null.
  50.      * @return {@code true} if the request is multipart; {@code false} otherwise.
  51.      */
  52.     public static final boolean isMultipartContent(final HttpServletRequest request) {
  53.         return POST_METHOD.equalsIgnoreCase(request.getMethod()) && AbstractFileUpload.isMultipartContent(new JavaxServletRequestContext(request));
  54.     }

  55.     /**
  56.      * Constructs an uninitialized instance of this class. A factory must be configured, using {@code setFileItemFactory()}, before attempting to parse
  57.      * requests.
  58.      *
  59.      * @see AbstractFileUpload#AbstractFileUpload()
  60.      */
  61.     public JavaxServletFileUpload() {
  62.     }

  63.     /**
  64.      * Constructs an instance of this class which uses the supplied factory to create {@code FileItem} instances.
  65.      *
  66.      * @see AbstractFileUpload#AbstractFileUpload()
  67.      * @param fileItemFactory The factory to use for creating file items.
  68.      */
  69.     public JavaxServletFileUpload(final F fileItemFactory) {
  70.         setFileItemFactory(fileItemFactory);
  71.     }

  72.     /**
  73.      * Gets an <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a> compliant {@code multipart/form-data} file item iterator.
  74.      *
  75.      * @param request The servlet request to be parsed.
  76.      * @return An iterator to instances of {@code FileItemInput} parsed from the request, in the order that they were transmitted.
  77.      * @throws FileUploadException if there are problems reading/parsing the request or storing files.
  78.      * @throws IOException         An I/O error occurred. This may be a network error while communicating with the client or a problem while storing the
  79.      *                             uploaded content.
  80.      */
  81.     @Override
  82.     public FileItemInputIterator getItemIterator(final HttpServletRequest request) throws FileUploadException, IOException {
  83.         return super.getItemIterator(new JavaxServletRequestContext(request));
  84.     }

  85.     /**
  86.      * Parses an <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a> compliant {@code multipart/form-data} stream.
  87.      *
  88.      * @param request The servlet request to be parsed.
  89.      * @return A map of {@code FileItem} instances parsed from the request.
  90.      * @throws FileUploadException if there are problems reading/parsing the request or storing files.
  91.      */
  92.     @Override
  93.     public Map<String, List<I>> parseParameterMap(final HttpServletRequest request) throws FileUploadException {
  94.         return parseParameterMap(new JavaxServletRequestContext(request));
  95.     }

  96.     /**
  97.      * Parses an <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a> compliant {@code multipart/form-data} stream.
  98.      *
  99.      * @param request The servlet request to be parsed.
  100.      * @return A list of {@code FileItem} instances parsed from the request, in the order that they were transmitted.
  101.      * @throws FileUploadException if there are problems reading/parsing the request or storing files.
  102.      */
  103.     @Override
  104.     public List<I> parseRequest(final HttpServletRequest request) throws FileUploadException {
  105.         return parseRequest(new JavaxServletRequestContext(request));
  106.     }

  107. }