| 1 | |
package org.webslinger.servlet; |
| 2 | |
|
| 3 | |
import java.io.IOException; |
| 4 | |
import java.util.ArrayList; |
| 5 | |
import java.util.Enumeration; |
| 6 | |
import java.util.HashMap; |
| 7 | |
import java.util.List; |
| 8 | |
import java.util.Map; |
| 9 | |
import javax.servlet.http.HttpServletRequest; |
| 10 | |
|
| 11 | |
import org.apache.commons.fileupload.FileItem; |
| 12 | |
import org.apache.commons.fileupload.FileItemFactory; |
| 13 | |
import org.apache.commons.fileupload.FileUploadException; |
| 14 | |
import org.apache.commons.fileupload.disk.DiskFileItemFactory; |
| 15 | |
import org.apache.commons.fileupload.servlet.ServletFileUpload; |
| 16 | |
|
| 17 | |
import org.webslinger.io.Charsets; |
| 18 | |
import org.webslinger.io.IOUtil; |
| 19 | |
|
| 20 | |
public final class MultipartFormUtil { |
| 21 | 1 | private static final FileItemFactory fileItemFactory = new DiskFileItemFactory(); |
| 22 | |
|
| 23 | 0 | private MultipartFormUtil() { |
| 24 | 0 | } |
| 25 | |
|
| 26 | |
public static FileItem createStringItem(String name, boolean isFormField, String value) throws IOException { |
| 27 | 9 | FileItem item = createFileItem(name, "UTF-8", isFormField, null); |
| 28 | 9 | IOUtil.writeString(item.getOutputStream(), Charsets.UTF8, value); |
| 29 | 9 | return item; |
| 30 | |
} |
| 31 | |
|
| 32 | |
public static FileItem createFileItem(String name, String contentType, boolean isFormField, String fileName) { |
| 33 | 26 | return fileItemFactory.createItem(name, contentType, isFormField, fileName); |
| 34 | |
} |
| 35 | |
|
| 36 | |
public static FileItem copyFileItem(FileItem src, String name) throws IOException { |
| 37 | 0 | FileItem dest = fileItemFactory.createItem(name, src.getContentType(), src.isFormField(), src.getName()); |
| 38 | 0 | IOUtil.copy(src.getInputStream(), true, dest.getOutputStream(), true); |
| 39 | 0 | return dest; |
| 40 | |
} |
| 41 | |
|
| 42 | |
public static Map<String, FileItem> filterOutFiles(Map<String, List<FileItem>> items) throws IOException { |
| 43 | 0 | HashMap<String, FileItem> map = new HashMap<String, FileItem>(items.size()); |
| 44 | 0 | for (Map.Entry<String, List<FileItem>> entry: items.entrySet()) { |
| 45 | 0 | List<FileItem> values = entry.getValue(); |
| 46 | 0 | if (values.get(0).getName() == null) continue; |
| 47 | 0 | map.put(entry.getKey(), values.get(0)); |
| 48 | 0 | } |
| 49 | 0 | return map; |
| 50 | |
} |
| 51 | |
|
| 52 | |
public static Map<String, String> filterOutStrings(Map<String, List<FileItem>> items) throws IOException { |
| 53 | 3 | HashMap<String, String> map = new HashMap<String, String>(items.size()); |
| 54 | 3 | for (Map.Entry<String, List<FileItem>> entry: items.entrySet()) { |
| 55 | 0 | List<FileItem> values = entry.getValue(); |
| 56 | 0 | if (values.get(0).getName() != null) continue; |
| 57 | 0 | map.put(entry.getKey(), values.get(0).getString("UTF-8")); |
| 58 | 0 | } |
| 59 | 3 | return map; |
| 60 | |
} |
| 61 | |
|
| 62 | |
public static Map<String, List<FileItem>> parseRequestAsMap(HttpServletRequest request) throws IOException { |
| 63 | 3 | HashMap<String, List<FileItem>> map = (HashMap<String, List<FileItem>>) request.getAttribute(MultipartFormUtil.class.getName() + ".allParametersMap"); |
| 64 | 3 | if (map != null) return map; |
| 65 | 3 | List<FileItem> items = parseRequestAsList(request); |
| 66 | 3 | map = new HashMap<String, List<FileItem>>(); |
| 67 | 3 | for (FileItem item: items) { |
| 68 | 0 | List<FileItem> values = map.get(item.getFieldName()); |
| 69 | 0 | if (values == null) { |
| 70 | 0 | values = new ArrayList<FileItem>(); |
| 71 | 0 | map.put(item.getFieldName(), values); |
| 72 | |
} |
| 73 | 0 | values.add(item); |
| 74 | 0 | } |
| 75 | 3 | request.setAttribute(MultipartFormUtil.class.getName() + ".allParametersMap", map); |
| 76 | 3 | return map; |
| 77 | |
} |
| 78 | |
|
| 79 | |
public static List<FileItem> parseRequestAsList(HttpServletRequest request) throws IOException { |
| 80 | 26 | ArrayList<FileItem> items = (ArrayList<FileItem>) request.getAttribute(MultipartFormUtil.class.getName() + ".allParametersList"); |
| 81 | 26 | if (items != null) return items; |
| 82 | 26 | items = new ArrayList<FileItem>(); |
| 83 | 26 | Enumeration en = request.getParameterNames(); |
| 84 | 35 | while (en.hasMoreElements()) { |
| 85 | 9 | String name = (String) en.nextElement(); |
| 86 | 18 | for (String value: request.getParameterValues(name)) { |
| 87 | 9 | items.add(createStringItem(name, true, value)); |
| 88 | |
} |
| 89 | 9 | } |
| 90 | 26 | if (ServletFileUpload.isMultipartContent(request)) { |
| 91 | 13 | ServletFileUpload fileUpload = new ServletFileUpload(fileItemFactory); |
| 92 | |
try { |
| 93 | 13 | items.addAll(fileUpload.parseRequest(request)); |
| 94 | 0 | } catch (FileUploadException e) { |
| 95 | 0 | throw (IOException) new IOException("Couldn't parse request").initCause(e); |
| 96 | 13 | } |
| 97 | |
} |
| 98 | 26 | request.setAttribute(MultipartFormUtil.class.getName() + ".allParametersList", items); |
| 99 | 26 | return items; |
| 100 | |
} |
| 101 | |
|
| 102 | |
public static void disposeOf(Map<String, List<FileItem>> items) throws IOException { |
| 103 | 0 | for (List<FileItem> values: items.values()) { |
| 104 | 0 | disposeOf(values); |
| 105 | |
} |
| 106 | 0 | } |
| 107 | |
|
| 108 | |
public static void disposeOf(List<FileItem> items) throws IOException { |
| 109 | 0 | for (FileItem item: items) { |
| 110 | 0 | item.delete(); |
| 111 | |
} |
| 112 | 0 | } |
| 113 | |
} |