001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * https://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.apache.commons.fileupload2.portlet; 019 020import java.io.IOException; 021import java.util.List; 022import java.util.Map; 023 024import javax.portlet.ActionRequest; 025 026import org.apache.commons.fileupload2.core.AbstractFileUpload; 027import org.apache.commons.fileupload2.core.FileItem; 028import org.apache.commons.fileupload2.core.FileItemFactory; 029import org.apache.commons.fileupload2.core.FileItemInputIterator; 030import org.apache.commons.fileupload2.core.FileUploadException; 031import org.apache.commons.fileupload2.javax.JavaxServletFileUpload; 032 033/** 034 * High level API for processing file uploads. 035 * <p> 036 * This class handles multiple files per single HTML widget, sent using {@code multipart/mixed} encoding type, as specified by 037 * <a href="https://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>. Use {@link JavaxServletFileUpload#parseRequest(javax.servlet.http.HttpServletRequest)} to 038 * acquire a list of {@link FileItem}s associated with a given HTML widget. 039 * </p> 040 * <p> 041 * 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. 042 * </p> 043 * 044 * @param <I> The FileItem type. 045 * @param <F> the FileItemFactory type. 046 */ 047public class JavaxPortletFileUpload<I extends FileItem<I>, F extends FileItemFactory<I>> extends AbstractFileUpload<ActionRequest, I, F> { 048 049 /** 050 * Tests whether the request contains multipart content. 051 * 052 * @param request The portlet request to be evaluated. Must be non-null. 053 * @return {@code true} if the request is multipart; {@code false} otherwise. 054 */ 055 public static final boolean isMultipartContent(final ActionRequest request) { 056 return AbstractFileUpload.isMultipartContent(new JavaxPortletRequestContext(request)); 057 } 058 059 /** 060 * Constructs an uninitialized instance of this class. A factory must be configured, using {@code setFileItemFactory()}, before attempting to parse 061 * requests. 062 * 063 * @see AbstractFileUpload#AbstractFileUpload() 064 */ 065 public JavaxPortletFileUpload() { 066 } 067 068 /** 069 * Constructs an instance of this class which uses the supplied factory to create {@code FileItem} instances. 070 * 071 * @see AbstractFileUpload#AbstractFileUpload() 072 * @param fileItemFactory The factory to use for creating file items. 073 */ 074 public JavaxPortletFileUpload(final F fileItemFactory) { 075 setFileItemFactory(fileItemFactory); 076 } 077 078 /** 079 * Gets an <a href="https://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a> compliant {@code multipart/form-data} file item iterator. 080 * 081 * @param request The portlet request to be parsed. 082 * @return An iterator to instances of {@code FileItemInput} parsed from the request, in the order that they were transmitted. 083 * @throws FileUploadException if there are problems reading/parsing the request or storing files. 084 * @throws IOException An I/O error occurred. This may be a network error while communicating with the client or a problem while storing the 085 * uploaded content. 086 */ 087 @Override 088 public FileItemInputIterator getItemIterator(final ActionRequest request) throws FileUploadException, IOException { 089 return super.getItemIterator(new JavaxPortletRequestContext(request)); 090 } 091 092 /** 093 * Parses an <a href="https://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a> compliant {@code multipart/form-data} stream. 094 * 095 * @param request The portlet request to be parsed. 096 * @return A map of {@code FileItem} instances parsed from the request. 097 * @throws FileUploadException if there are problems reading/parsing the request or storing files. 098 */ 099 @Override 100 public Map<String, List<I>> parseParameterMap(final ActionRequest request) throws FileUploadException { 101 return parseParameterMap(new JavaxPortletRequestContext(request)); 102 } 103 104 /** 105 * Parses an <a href="https://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a> compliant {@code multipart/form-data} stream. 106 * 107 * @param request The portlet request to be parsed. 108 * @return A list of {@code FileItem} instances parsed from the request, in the order that they were transmitted. 109 * @throws FileUploadException if there are problems reading/parsing the request or storing files. 110 */ 111 @Override 112 public List<I> parseRequest(final ActionRequest request) throws FileUploadException { 113 return parseRequest(new JavaxPortletRequestContext(request)); 114 } 115}