Skip to content

Commit c5453c6

Browse files
authored
Merge pull request #119 from cip4/list1
case insensitive file items
2 parents 7727575 + 84c6670 commit c5453c6

File tree

3 files changed

+221
-85
lines changed

3 files changed

+221
-85
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ dependencies {
134134
implementation 'org.apache.logging.log4j:log4j-jcl:2.24.3'
135135

136136
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.11.4'
137-
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.11.2'
137+
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.11.4'
138138
testImplementation 'org.springframework:spring-test:5.3.39'
139139
testImplementation 'org.springframework:spring-web:5.3.39'
140140
testImplementation 'org.mockito:mockito-core:5.15.2'

src/main/java/org/cip4/jdfutility/FileItemList.java

Lines changed: 73 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* The CIP4 Software License, Version 1.0
44
*
55
*
6-
* Copyright (c) 2001-2013 The International Cooperation for the Integration of
6+
* Copyright (c) 2001-2025 The International Cooperation for the Integration of
77
* Processes in Prepress, Press and Postpress (CIP4). All rights
88
* reserved.
99
*
@@ -72,10 +72,10 @@
7272

7373
import java.io.IOException;
7474
import java.io.InputStream;
75+
import java.util.ArrayList;
7576
import java.util.List;
7677
import java.util.Map;
7778
import java.util.Set;
78-
import java.util.Vector;
7979

8080
import javax.servlet.ServletException;
8181
import javax.servlet.http.HttpServletRequest;
@@ -86,6 +86,7 @@
8686
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
8787
import org.apache.commons.fileupload.servlet.ServletFileUpload;
8888
import org.cip4.jdflib.datatypes.JDFAttributeMap;
89+
import org.cip4.jdflib.util.ContainerUtil;
8990
import org.cip4.jdflib.util.StreamUtil;
9091
import org.cip4.jdflib.util.StringUtil;
9192
import org.cip4.jdflib.util.UrlUtil;
@@ -98,22 +99,22 @@ public class FileItemList
9899
{
99100

100101
final private List<FileItem> fileItems;
101-
private JDFAttributeMap mapCache;
102-
private JDFAttributeMap reqParameters;
102+
private final JDFAttributeMap mapCache;
103+
private final JDFAttributeMap reqParameters;
103104

104105
/**
105-
*
106+
*
106107
* @param request
107108
* @param filesize
108109
* @return
109110
*/
110-
public static FileItemList getFileItemList(HttpServletRequest request, long filesize)
111+
public static FileItemList getFileItemList(final HttpServletRequest request, final long filesize)
111112
{
112113
try
113114
{
114115
return new FileItemList(request, filesize);
115116
}
116-
catch (ServletException e)
117+
catch (final ServletException e)
117118
{
118119
return null;
119120
}
@@ -125,30 +126,25 @@ public static FileItemList getFileItemList(HttpServletRequest request, long file
125126
* @param filesize
126127
* @throws ServletException
127128
*/
128-
@SuppressWarnings("unchecked")
129-
public FileItemList(HttpServletRequest request, long filesize) throws ServletException
129+
public FileItemList(final HttpServletRequest request, final long filesize) throws ServletException
130130
{
131-
mapCache = null;
131+
mapCache = new JDFAttributeMap();
132+
reqParameters = new JDFAttributeMap();
133+
fileItems = new ArrayList<FileItem>();
132134
// Create a factory for disk-based file items
133135
final FileItemFactory factory = new DiskFileItemFactory();
134136

135137
// Create a new file upload handler
136138
final ServletFileUpload upload = new ServletFileUpload(factory);
137-
if (request == null)
139+
if (request != null)
138140
{
139-
fileItems = null;
140-
reqParameters = null;
141-
}
142-
else
143-
{
144-
reqParameters = new JDFAttributeMap();
145-
Map<String, String[]> parameterMap = request.getParameterMap();
141+
final Map<String, String[]> parameterMap = request.getParameterMap();
146142
if (parameterMap != null)
147143
{
148-
Set<String> keySet = parameterMap.keySet();
149-
for (String key : keySet)
144+
final Set<String> keySet = parameterMap.keySet();
145+
for (final String key : keySet)
150146
{
151-
String[] vals = parameterMap.get(key);
147+
final String[] vals = parameterMap.get(key);
152148
if (vals != null && vals.length > 0)
153149
{
154150
reqParameters.put(key, vals[0]);
@@ -161,43 +157,36 @@ public FileItemList(HttpServletRequest request, long filesize) throws ServletExc
161157
{
162158
if (filesize > 0)
163159
upload.setSizeMax(filesize);
164-
fileItems = upload.parseRequest(request);
160+
ContainerUtil.addAll(fileItems, upload.parseRequest(request));
165161
}
166162
catch (final FileUploadException fue)
167163
{
168164
throw new ServletException("Could not parse multipart request.", fue);
169165
}
170166
}
171-
else
172-
{
173-
fileItems = null;
174-
}
175167
}
176168
}
177169

178170
/**
179171
*
180-
*
172+
*
181173
* @return
182-
*
174+
*
183175
*/
184176
public JDFAttributeMap getFieldsFromForm()
185177
{
186-
if (mapCache == null)
178+
if (ContainerUtil.isEmpty(mapCache))
187179
{
188-
mapCache = new JDFAttributeMap();
189-
List<FileItem> fileList = getFileList(false, true);
190-
if (fileList != null)
180+
final List<FileItem> fileList = getFileList(false, true);
181+
for (final FileItem fi : fileList)
191182
{
192-
for (FileItem fi : fileList)
183+
final String itemString = StringUtil.getNonEmpty(fi.getString());
184+
if (itemString != null)
193185
{
194-
String itemString = StringUtil.getNonEmpty(fi.getString());
195-
if (itemString != null)
196-
{
197-
mapCache.put(fi.getFieldName(), itemString);
198-
}
186+
mapCache.put(fi.getFieldName(), itemString);
199187
}
200188
}
189+
201190
mapCache.putAll(reqParameters);
202191
}
203192
return mapCache;
@@ -206,55 +195,58 @@ public JDFAttributeMap getFieldsFromForm()
206195
/**
207196
*
208197
* get a form value
198+
*
209199
* @param key
210200
* @return
211201
*/
212-
public String getField(String key)
202+
public String getField(final String key)
213203
{
214204
getFieldsFromForm();
215-
return mapCache.get(key);
205+
return mapCache.getIgnoreCase(key);
216206
}
217207

218208
/**
219209
*
220210
* get a form value
211+
*
221212
* @param key
222213
* @param def
223214
* @return
224215
*/
225-
public int getIntField(String key, int def)
216+
public int getIntField(final String key, final int def)
226217
{
227218
getFieldsFromForm();
228-
return mapCache.getInt(key, def);
219+
return StringUtil.parseInt(mapCache.getIgnoreCase(key), def);
229220
}
230221

231222
/**
232223
*
233224
* get a form value
225+
*
234226
* @param key
235227
* @param def
236228
* @return
237229
*/
238-
public boolean getBoolField(String key, boolean def)
230+
public boolean getBoolField(final String key, final boolean def)
239231
{
240232
getFieldsFromForm();
241-
return mapCache.getBool(key, def);
233+
return StringUtil.parseBoolean(mapCache.getIgnoreCase(key), def);
242234
}
243235

244236
/**
245-
* @param bFile if true return files
237+
* @param bFile if true return files
246238
* @param bForm if true return form fields
247239
* @return
248-
*
240+
*
249241
*/
250-
public List<FileItem> getFileList(boolean bFile, boolean bForm)
242+
public List<FileItem> getFileList(final boolean bFile, final boolean bForm)
251243
{
252-
List<FileItem> retList = new Vector<FileItem>();
253-
if ((bFile || bForm) && fileItems != null)
244+
final List<FileItem> retList = new ArrayList<FileItem>();
245+
if (bFile || bForm)
254246
{
255-
for (FileItem f : fileItems)
247+
for (final FileItem f : fileItems)
256248
{
257-
boolean formField = f.isFormField();
249+
final boolean formField = f.isFormField();
258250
if (formField && bForm || !formField && bFile)
259251
{
260252
retList.add(f);
@@ -267,47 +259,42 @@ public List<FileItem> getFileList(boolean bFile, boolean bForm)
267259
/**
268260
*
269261
* get the iTh file
262+
*
270263
* @param i may be<0 to count from end
271264
* @return
272265
*/
273266
public FileItem getFile(int i)
274267
{
275-
List<FileItem> fileList = getFileList(true, false);
276-
if (fileList != null)
268+
final List<FileItem> fileList = getFileList(true, false);
269+
if (i < 0)
277270
{
278-
if (i < 0)
279-
{
280-
i += fileList.size();
281-
}
282-
if (i < 0 || i >= fileList.size())
283-
{
284-
return null;
285-
}
286-
else
287-
{
288-
return fileList.get(i);
289-
}
271+
i += fileList.size();
272+
}
273+
if (i < 0 || i >= fileList.size())
274+
{
275+
return null;
276+
}
277+
else
278+
{
279+
return fileList.get(i);
290280
}
291-
return null;
292281
}
293282

294283
/**
295284
*
296285
* get the file attached to formName
297-
* @param formName
286+
*
287+
* @param formName
298288
* @return
299289
*/
300-
public FileItem getFile(String formName)
290+
public FileItem getFile(final String formName)
301291
{
302-
List<FileItem> fileList = getFileList(true, false);
303-
if (fileList != null)
292+
final List<FileItem> fileList = getFileList(true, false);
293+
for (final FileItem f : fileList)
304294
{
305-
for (FileItem f : fileList)
295+
if (f.getFieldName().equalsIgnoreCase(formName))
306296
{
307-
if (f.getFieldName().equals(formName))
308-
{
309-
return f;
310-
}
297+
return f;
311298
}
312299
}
313300
return null;
@@ -316,34 +303,36 @@ public FileItem getFile(String formName)
316303
/**
317304
*
318305
* get the input stream for formName
319-
* @param formName
306+
*
307+
* @param formName
320308
* @return
321309
*/
322-
public InputStream getFileInputStream(String formName)
310+
public InputStream getFileInputStream(final String formName)
323311
{
324-
FileItem fi = getFile(formName);
312+
final FileItem fi = getFile(formName);
325313
return getInputStream(fi);
326314
}
327315

328316
/**
329317
*
330318
* get the input stream for formName
319+
*
331320
* @param i may be<0 to count from end
332321
* @return
333322
*/
334-
public InputStream getFileInputStream(int i)
323+
public InputStream getFileInputStream(final int i)
335324
{
336-
FileItem fi = getFile(i);
325+
final FileItem fi = getFile(i);
337326
return getInputStream(fi);
338327
}
339328

340329
/**
341330
*
342-
*
331+
*
343332
* @param fi
344333
* @return
345334
*/
346-
public static InputStream getInputStream(FileItem fi)
335+
public static InputStream getInputStream(final FileItem fi)
347336
{
348337
if (fi != null)
349338
{
@@ -352,7 +341,7 @@ public static InputStream getInputStream(FileItem fi)
352341
{
353342
inputStream = fi.getInputStream();
354343
}
355-
catch (IOException e)
344+
catch (final IOException e)
356345
{
357346
inputStream = null;
358347
}
@@ -367,7 +356,7 @@ public static InputStream getInputStream(FileItem fi)
367356
@Override
368357
public String toString()
369358
{
370-
return "FileItemList [mapCache=" + mapCache + ", fileItems=" + fileItems + "]";
359+
return "FileItemList [mapCache=" + mapCache.getKeyList().getString() + ", fileItems=" + fileItems + "]";
371360
}
372361

373362
}

0 commit comments

Comments
 (0)