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    *      https://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="https://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       * Tests whether the request contains multipart content.
49       *
50       * @param request The servlet request to be evaluated. Must be non-null.
51       * @return {@code true} if the request is multipart; {@code false} otherwise.
52       */
53      public static final boolean isMultipartContent(final HttpServletRequest request) {
54          return isMultipartRequestMethod(request.getMethod()) && isMultipartContent(new JakartaServletRequestContext(request));
55      }
56  
57      /**
58       * Constructs an uninitialized instance of this class. A factory must be configured, using {@code setFileItemFactory()}, before attempting to parse
59       * requests.
60       *
61       * @see AbstractFileUpload#AbstractFileUpload()
62       */
63      public JakartaServletFileUpload() {
64      }
65  
66      /**
67       * Constructs an instance of this class which uses the supplied factory to create {@code FileItem} instances.
68       *
69       * @see AbstractFileUpload#AbstractFileUpload()
70       * @param fileItemFactory The factory to use for creating file items.
71       */
72      public JakartaServletFileUpload(final F fileItemFactory) {
73          setFileItemFactory(fileItemFactory);
74      }
75  
76      /**
77       * Gets an <a href="https://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a> compliant {@code multipart/form-data} file item iterator.
78       *
79       * @param request The servlet request to be parsed.
80       * @return An iterator to instances of {@code FileItemInput} parsed from the request, in the order that they were transmitted.
81       * @throws FileUploadException if there are problems reading/parsing the request or storing files.
82       * @throws IOException         An I/O error occurred. This may be a network error while communicating with the client or a problem while storing the
83       *                             uploaded content.
84       */
85      @Override
86      public FileItemInputIterator getItemIterator(final HttpServletRequest request) throws FileUploadException, IOException {
87          return super.getItemIterator(new JakartaServletRequestContext(request));
88      }
89  
90      /**
91       * Parses an <a href="https://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a> compliant {@code multipart/form-data} stream.
92       *
93       * @param request The servlet request to be parsed.
94       * @return A map of {@code FileItem} instances parsed from the request.
95       * @throws FileUploadException if there are problems reading/parsing the request or storing files.
96       */
97      @Override
98      public Map<String, List<I>> parseParameterMap(final HttpServletRequest request) throws FileUploadException {
99          return parseParameterMap(new JakartaServletRequestContext(request));
100     }
101 
102     /**
103      * Parses an <a href="https://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a> compliant {@code multipart/form-data} stream.
104      *
105      * @param request The servlet request to be parsed.
106      * @return A list of {@code FileItem} instances parsed from the request, in the order that they were transmitted.
107      * @throws FileUploadException if there are problems reading/parsing the request or storing files.
108      */
109     @Override
110     public List<I> parseRequest(final HttpServletRequest request) throws FileUploadException {
111         return parseRequest(new JakartaServletRequestContext(request));
112     }
113 
114 }