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.util.http;
35
36 import fr.paris.lutece.portal.service.html.EncodingService;
37 import fr.paris.lutece.portal.web.upload.MultipartHttpServletRequest;
38 import fr.paris.lutece.portal.web.upload.NormalizeFileItem;
39
40 import org.apache.commons.fileupload.FileItem;
41 import org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException;
42 import org.apache.commons.fileupload.FileUploadException;
43 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
44 import org.apache.commons.fileupload.servlet.ServletFileUpload;
45 import org.apache.commons.lang3.StringUtils;
46
47 import java.io.UnsupportedEncodingException;
48
49 import java.util.ArrayList;
50 import java.util.HashMap;
51 import java.util.List;
52 import java.util.Map;
53 import java.util.Optional;
54
55 import javax.servlet.http.HttpServletRequest;
56
57
58
59
60
61
62 public final class MultipartUtil
63 {
64
65
66
67 private MultipartUtil( )
68 {
69 }
70
71
72
73
74
75
76
77
78 public static boolean isMultipart( HttpServletRequest request )
79 {
80 return ServletFileUpload.isMultipartContent( request );
81 }
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100 public static MultipartHttpServletRequest convert( int nSizeThreshold, long nRequestSizeMax, boolean bActivateNormalizeFileName,
101 HttpServletRequest request ) throws FileUploadException
102 {
103 if ( !isMultipart( request ) )
104 {
105 return null;
106 }
107
108
109 DiskFileItemFactory factory = new DiskFileItemFactory( );
110
111
112 factory.setSizeThreshold( nSizeThreshold );
113
114
115 ServletFileUpload upload = new ServletFileUpload( factory );
116
117
118 upload.setSizeMax( nRequestSizeMax );
119
120
121 String strEncoding = Optional.ofNullable( request.getCharacterEncoding( ) ).orElse( EncodingService.getEncoding( ) );
122
123 Map<String, List<FileItem>> mapFiles = new HashMap<>( );
124 Map<String, String [ ]> mapParameters = new HashMap<>( );
125
126 List<FileItem> listItems = upload.parseRequest( request );
127
128
129 for ( FileItem item : listItems )
130 {
131 processItem( item, strEncoding, bActivateNormalizeFileName, mapFiles, mapParameters );
132 }
133
134 return new MultipartHttpServletRequest( request, mapFiles, mapParameters );
135 }
136
137 private static void processItem( FileItem item, String strEncoding, boolean bActivateNormalizeFileName, Map<String, List<FileItem>> mapFiles,
138 Map<String, String [ ]> mapParameters )
139 {
140 if ( item.isFormField( ) )
141 {
142 String strValue = StringUtils.EMPTY;
143
144 if ( item.getSize( ) > 0 )
145 {
146 try
147 {
148 strValue = item.getString( strEncoding );
149 }
150 catch( UnsupportedEncodingException ex )
151 {
152
153 strValue = item.getString( );
154 }
155 }
156
157
158 String [ ] curParam = mapParameters.get( item.getFieldName( ) );
159
160 if ( curParam == null )
161 {
162
163 mapParameters.put( item.getFieldName( ), new String [ ] {
164 strValue
165 } );
166 }
167 else
168 {
169
170 String [ ] newArray = new String [ curParam.length + 1];
171 System.arraycopy( curParam, 0, newArray, 0, curParam.length );
172 newArray [curParam.length] = strValue;
173 mapParameters.put( item.getFieldName( ), newArray );
174 }
175 }
176 else
177 {
178
179
180
181 FileItem fileItem = bActivateNormalizeFileName ? new NormalizeFileItem( item ) : item;
182 List<FileItem> listFileItem = mapFiles.get( fileItem.getFieldName( ) );
183
184 if ( listFileItem != null )
185 {
186 listFileItem.add( fileItem );
187 }
188 else
189 {
190 listFileItem = new ArrayList<>( 1 );
191 listFileItem.add( fileItem );
192 mapFiles.put( fileItem.getFieldName( ), listFileItem );
193 }
194 }
195 }
196 }