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.fileupload.portlet;
18  
19  import java.io.IOException;
20  import java.util.List;
21  
22  import javax.portlet.ActionRequest;
23  
24  import org.apache.commons.fileupload.FileItemFactory;
25  import org.apache.commons.fileupload.FileItemIterator;
26  import org.apache.commons.fileupload.FileUpload;
27  import org.apache.commons.fileupload.FileUploadBase;
28  import org.apache.commons.fileupload.FileUploadException;
29  
30  /**
31   * <p>High level API for processing file uploads.</p>
32   *
33   * <p>This class handles multiple files per single HTML widget, sent using
34   * <code>multipart/mixed</code> encoding type, as specified by
35   * <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>.  Use {@link
36   * #parseRequest(javax.servlet.http.HttpServletRequest)} to acquire a list
37   * of {@link org.apache.commons.fileupload.FileItem FileItems} associated
38   * with a given HTML widget.</p>
39   *
40   * <p>How the data for individual parts is stored is determined by the factory
41   * used to create them; a given part may be in memory, on disk, or somewhere
42   * else.</p>
43   *
44   * @author <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a>
45   * @author <a href="mailto:dlr@collab.net">Daniel Rall</a>
46   * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
47   * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
48   * @author <a href="mailto:martinc@apache.org">Martin Cooper</a>
49   * @author Sean C. Sullivan
50   *
51   * @since FileUpload 1.1
52   *
53   * @version $Id: PortletFileUpload.java 479484 2006-11-27 01:06:53Z jochen $
54   */
55  public class PortletFileUpload extends FileUpload {
56  
57      // ---------------------------------------------------------- Class methods
58  
59  
60      /**
61       * Utility method that determines whether the request contains multipart
62       * content.
63       *
64       * @param request The portlet request to be evaluated. Must be non-null.
65       *
66       * @return <code>true</code> if the request is multipart;
67       *         <code>false</code> otherwise.
68       */
69      public static final boolean isMultipartContent(ActionRequest request) {
70          return FileUploadBase.isMultipartContent(
71                  new PortletRequestContext(request));
72      }
73  
74  
75      // ----------------------------------------------------------- Constructors
76  
77  
78      /**
79       * Constructs an uninitialised instance of this class. A factory must be
80       * configured, using <code>setFileItemFactory()</code>, before attempting
81       * to parse requests.
82       *
83       * @see FileUpload#FileUpload(FileItemFactory)
84       */
85      public PortletFileUpload() {
86          super();
87      }
88  
89  
90      /**
91       * Constructs an instance of this class which uses the supplied factory to
92       * create <code>FileItem</code> instances.
93       *
94       * @see FileUpload#FileUpload()
95       * @param fileItemFactory The factory to use for creating file items.
96       */
97      public PortletFileUpload(FileItemFactory fileItemFactory) {
98          super(fileItemFactory);
99      }
100 
101 
102     // --------------------------------------------------------- Public methods
103 
104 
105     /**
106      * Processes an <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>
107      * compliant <code>multipart/form-data</code> stream.
108      *
109      * @param request The portlet request to be parsed.
110      *
111      * @return A list of <code>FileItem</code> instances parsed from the
112      *         request, in the order that they were transmitted.
113      *
114      * @throws FileUploadException if there are problems reading/parsing
115      *                             the request or storing files.
116      */
117     public List /* FileItem */ parseRequest(ActionRequest request)
118             throws FileUploadException {
119         return parseRequest(new PortletRequestContext(request));
120     }
121 
122     /**
123      * Processes an <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>
124      * compliant <code>multipart/form-data</code> stream.
125      *
126      * @param request The portlet request to be parsed.
127      *
128      * @return An iterator to instances of <code>FileItemStream</code>
129      *         parsed from the request, in the order that they were
130      *         transmitted.
131      *
132      * @throws FileUploadException if there are problems reading/parsing
133      *                             the request or storing files.
134      * @throws IOException An I/O error occurred. This may be a network
135      *   error while communicating with the client or a problem while
136      *   storing the uploaded content.
137      */
138     public FileItemIterator getItemIterator(ActionRequest request)
139             throws FileUploadException, IOException {
140         return super.getItemIterator(new PortletRequestContext(request));
141     }
142 }