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