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.jakarta.servlet6;
18  
19  import java.io.IOException;
20  import java.util.List;
21  import java.util.Map;
22  
23  import org.apache.commons.fileupload2.core.AbstractFileUpload;
24  import org.apache.commons.fileupload2.core.FileItem;
25  import org.apache.commons.fileupload2.core.FileItemFactory;
26  import org.apache.commons.fileupload2.core.FileItemInputIterator;
27  import org.apache.commons.fileupload2.core.FileUploadException;
28  
29  import jakarta.servlet.http.HttpServletRequest;
30  
31  /**
32   * High level API for processing file uploads.
33   * <p>
34   * This class handles multiple files per single HTML widget, sent using {@code multipart/mixed} encoding type, as specified by
35   * <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
36   * with a given HTML widget.
37   * </p>
38   * <p>
39   * 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.
40   * </p>
41   *
42   * @param <I> The FileItem type.
43   * @param <F> the FileItemFactory type.
44   */
45  public class JakartaServletFileUpload<I extends FileItem<I>, F extends FileItemFactory<I>> extends AbstractFileUpload<HttpServletRequest, I, F> {
46  
47      /**
48       * Constant for HTTP POST method.
49       */
50      private static final String POST_METHOD = "POST";
51  
52      /**
53       * Tests whether the request contains multipart content.
54       *
55       * @param request The servlet request to be evaluated. Must be non-null.
56       *
57       * @return {@code true} if the request is multipart; {@code false} otherwise.
58       */
59      public static final boolean isMultipartContent(final HttpServletRequest request) {
60          return POST_METHOD.equalsIgnoreCase(request.getMethod()) && AbstractFileUpload.isMultipartContent(new JakartaServletRequestContext(request));
61      }
62  
63      /**
64       * Constructs an uninitialized instance of this class. A factory must be configured, using {@code setFileItemFactory()}, before attempting to parse
65       * requests.
66       *
67       * @see AbstractFileUpload#AbstractFileUpload()
68       */
69      public JakartaServletFileUpload() {
70      }
71  
72      /**
73       * Constructs an instance of this class which uses the supplied factory to create {@code FileItem} instances.
74       *
75       * @see AbstractFileUpload#AbstractFileUpload()
76       * @param fileItemFactory The factory to use for creating file items.
77       */
78      public JakartaServletFileUpload(final F fileItemFactory) {
79          setFileItemFactory(fileItemFactory);
80      }
81  
82      /**
83       * Gets an <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a> compliant {@code multipart/form-data} file item iterator.
84       *
85       * @param request The servlet request to be parsed.
86       * @return An iterator to instances of {@code FileItemInput} parsed from the request, in the order that they were transmitted.
87       * @throws FileUploadException if there are problems reading/parsing the request or storing files.
88       * @throws IOException         An I/O error occurred. This may be a network error while communicating with the client or a problem while storing the
89       *                             uploaded content.
90       */
91      @Override
92      public FileItemInputIterator getItemIterator(final HttpServletRequest request) throws FileUploadException, IOException {
93          return super.getItemIterator(new JakartaServletRequestContext(request));
94      }
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 map of {@code FileItem} instances parsed from the request.
101      * @throws FileUploadException if there are problems reading/parsing the request or storing files.
102      */
103     @Override
104     public Map<String, List<I>> parseParameterMap(final HttpServletRequest request) throws FileUploadException {
105         return parseParameterMap(new JakartaServletRequestContext(request));
106     }
107 
108     /**
109      * Parses an <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a> compliant {@code multipart/form-data} stream.
110      *
111      * @param request The servlet request to be parsed.
112      * @return A list of {@code FileItem} instances parsed from the request, in the order that they were transmitted.
113      * @throws FileUploadException if there are problems reading/parsing the request or storing files.
114      */
115     @Override
116     public List<I> parseRequest(final HttpServletRequest request) throws FileUploadException {
117         return parseRequest(new JakartaServletRequestContext(request));
118     }
119 
120 }