1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
51
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
61
62
63
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
75
76
77 @Override
78 public Enumeration getParameterNames( )
79 {
80 return Collections.enumeration( _stringParameters.keySet( ) );
81 }
82
83
84
85
86
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
98
99
100
101 @Override
102 public String[] getParameterValues( String strName )
103 {
104 return _stringParameters.get( strName );
105 }
106
107
108
109
110
111 @Override
112 public Map getParameterMap( )
113 {
114 return _stringParameters;
115 }
116
117
118
119
120
121 public Enumeration<String> getFileNames( )
122 {
123 return Collections.enumeration( _multipartFiles.keySet( ) );
124 }
125
126
127
128
129
130
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
145
146
147 public Map<String, List<FileItem>> getFileListMap( )
148 {
149 return _multipartFiles;
150 }
151
152
153
154
155
156
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
167
168
169
170 public List<FileItem> getFileList( String strName )
171 {
172 return _multipartFiles.get( strName );
173 }
174
175
176
177
178
179
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 }