1 /*
2 * Copyright (c) 2002-2014, Mairie de Paris
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice
10 * and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright notice
13 * and the following disclaimer in the documentation and/or other materials
14 * provided with the distribution.
15 *
16 * 3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 *
32 * License 1.0
33 */
34 package fr.paris.lutece.portal.web.upload;
35
36 import org.apache.commons.fileupload.FileItem;
37
38 import java.util.Collections;
39 import java.util.Enumeration;
40 import java.util.HashMap;
41 import java.util.List;
42 import java.util.Map;
43 import java.util.Map.Entry;
44
45 import javax.servlet.http.HttpServletRequest;
46 import javax.servlet.http.HttpServletRequestWrapper;
47
48
49 /**
50 * This class provides a Wrapper of an HTTP request that handle multipart
51 * content
52 */
53 public class MultipartHttpServletRequest extends HttpServletRequestWrapper
54 {
55 private final Map<String, List<FileItem>> _multipartFiles;
56 private final Map<String, String[]> _stringParameters;
57 private Map<String, FileItem> _multipartSingleFiles;
58
59 /**
60 * Constructor
61 * @param request The HTTP request
62 * @param multipartFiles Files
63 * @param parameters Request parameters
64 */
65 public MultipartHttpServletRequest( HttpServletRequest request, Map<String, List<FileItem>> multipartFiles,
66 Map<String, String[]> parameters )
67 {
68 super( request );
69 _multipartFiles = Collections.unmodifiableMap( multipartFiles );
70 _stringParameters = Collections.unmodifiableMap( parameters );
71 }
72
73 /**
74 * Gets parameters names
75 * @return An enumeration of parameters names
76 */
77 @Override
78 public Enumeration getParameterNames( )
79 {
80 return Collections.enumeration( _stringParameters.keySet( ) );
81 }
82
83 /**
84 * Gets a parameter value
85 * @param strName The parameter name
86 * @return The value
87 */
88 @Override
89 public String getParameter( String strName )
90 {
91 String[] values = getParameterValues( strName );
92
93 return ( ( ( values != null ) && ( values.length > 0 ) ) ? values[0] : null );
94 }
95
96 /**
97 * Gets parameter values
98 * @param strName The parameter name
99 * @return An array of values
100 */
101 @Override
102 public String[] getParameterValues( String strName )
103 {
104 return _stringParameters.get( strName );
105 }
106
107 /**
108 * Gets the parameter map
109 * @return A map containing all request parameters
110 */
111 @Override
112 public Map getParameterMap( )
113 {
114 return _stringParameters;
115 }
116
117 /**
118 * Gets the list of filenames attached to the request
119 * @return The list as an enumeration
120 */
121 public Enumeration<String> getFileNames( )
122 {
123 return Collections.enumeration( _multipartFiles.keySet( ) );
124 }
125
126 /**
127 * Gets a map of files attached to the request. Only one file is returned
128 * for each name of the form.
129 * @return The map
130 * @deprecated use {@link #getFileListMap()} instead to get every files
131 */
132 @Deprecated
133 public Map<String, FileItem> getFileMap( )
134 {
135 if ( _multipartSingleFiles == null )
136 {
137 _multipartSingleFiles = Collections.unmodifiableMap( convertFileMap( _multipartFiles ) );
138 }
139
140 return _multipartSingleFiles;
141 }
142
143 /**
144 * Gets a map of all files attached to the request
145 * @return The map
146 */
147 public Map<String, List<FileItem>> getFileListMap( )
148 {
149 return _multipartFiles;
150 }
151
152 /**
153 * Gets a file. If several files are available for a given name, then only
154 * the first one is returned
155 * @param strName The file name
156 * @return The file as a FileItem
157 */
158 public FileItem getFile( String strName )
159 {
160 List<FileItem> listFileItem = _multipartFiles.get( strName );
161
162 return ( ( listFileItem != null ) && ( listFileItem.size( ) > 0 ) ) ? listFileItem.get( 0 ) : null;
163 }
164
165 /**
166 * Gets a file
167 * @param strName The file name
168 * @return The file as a FileItem
169 */
170 public List<FileItem> getFileList( String strName )
171 {
172 return _multipartFiles.get( strName );
173 }
174
175 /**
176 * Convert a map of file list with their name into a map of single files
177 * with their names
178 * @param multipartFiles The map to convert
179 * @return The converted map
180 */
181 private Map<String, FileItem> convertFileMap( Map<String, List<FileItem>> multipartFiles )
182 {
183 Map<String, FileItem> mapFiles = new HashMap<String, FileItem>( multipartFiles.size( ) );
184
185 for ( Entry<String, List<FileItem>> entry : multipartFiles.entrySet( ) )
186 {
187 mapFiles.put( entry.getKey( ),
188 ( ( entry.getValue( ) == null ) || ( entry.getValue( ).size( ) == 0 ) ) ? null
189 : entry.getValue( ).get( 0 ) );
190 }
191
192 return mapFiles;
193 }
194 }