|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| package org.apache.tapestry.multipart; |
|
15 |
| |
|
16 |
| import java.io.UnsupportedEncodingException; |
|
17 |
| import java.util.HashMap; |
|
18 |
| import java.util.Iterator; |
|
19 |
| import java.util.List; |
|
20 |
| import java.util.Map; |
|
21 |
| |
|
22 |
| import org.apache.commons.fileupload.FileItem; |
|
23 |
| import org.apache.hivemind.ApplicationRuntimeException; |
|
24 |
| import org.apache.tapestry.request.IUploadFile; |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| public abstract class AbstractMultipartDecoder { |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| protected Map _uploadParts = new HashMap(); |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| private Map _valueParts = new HashMap(); |
|
42 |
| |
|
43 |
| protected int _thresholdSize = 1024; |
|
44 |
| |
|
45 |
| protected String _repositoryPath = System.getProperty("java.io.tmpdir"); |
|
46 |
| |
|
47 |
| protected String _encoding; |
|
48 |
| |
|
49 |
9
| public IUploadFile getFileUpload(String parameterName) {
|
|
50 |
9
| return (IUploadFile) _uploadParts.get(parameterName);
|
|
51 |
| } |
|
52 |
| |
|
53 |
15
| public void cleanup() {
|
|
54 |
15
| Iterator i = _uploadParts.values().iterator();
|
|
55 |
| |
|
56 |
15
| while (i.hasNext()) {
|
|
57 |
12
| UploadPart part = (UploadPart) i.next();
|
|
58 |
| |
|
59 |
12
| part.cleanup();
|
|
60 |
| } |
|
61 |
| } |
|
62 |
| |
|
63 |
15
| protected Map buildParameterMap() {
|
|
64 |
15
| Map result = new HashMap();
|
|
65 |
| |
|
66 |
15
| Iterator i = _valueParts.entrySet().iterator();
|
|
67 |
15
| while (i.hasNext()) {
|
|
68 |
66
| Map.Entry e = (Map.Entry) i.next();
|
|
69 |
| |
|
70 |
66
| String name = (String) e.getKey();
|
|
71 |
66
| ValuePart part = (ValuePart) e.getValue();
|
|
72 |
| |
|
73 |
66
| result.put(name, part.getValues());
|
|
74 |
| } |
|
75 |
| |
|
76 |
15
| return result;
|
|
77 |
| } |
|
78 |
| |
|
79 |
15
| protected void processFileItems(List parts) {
|
|
80 |
15
| if (parts == null)
|
|
81 |
0
| return;
|
|
82 |
| |
|
83 |
15
| Iterator i = parts.iterator();
|
|
84 |
| |
|
85 |
15
| while (i.hasNext()) {
|
|
86 |
81
| FileItem item = (FileItem) i.next();
|
|
87 |
| |
|
88 |
81
| processFileItem(item);
|
|
89 |
| } |
|
90 |
| } |
|
91 |
| |
|
92 |
81
| private void processFileItem(FileItem item) {
|
|
93 |
81
| if (item.isFormField()) {
|
|
94 |
69
| processFormFieldItem(item);
|
|
95 |
69
| return;
|
|
96 |
| } |
|
97 |
| |
|
98 |
12
| processUploadFileItem(item);
|
|
99 |
| } |
|
100 |
| |
|
101 |
12
| private void processUploadFileItem(FileItem item) {
|
|
102 |
12
| String name = item.getFieldName();
|
|
103 |
| |
|
104 |
12
| UploadPart part = new UploadPart(item);
|
|
105 |
| |
|
106 |
12
| _uploadParts.put(name, part);
|
|
107 |
| } |
|
108 |
| |
|
109 |
69
| void processFormFieldItem(FileItem item) {
|
|
110 |
69
| String name = item.getFieldName();
|
|
111 |
| |
|
112 |
69
| String value = extractFileItemValue(item);
|
|
113 |
| |
|
114 |
69
| ValuePart part = (ValuePart) _valueParts.get(name);
|
|
115 |
| |
|
116 |
69
| if (part == null)
|
|
117 |
66
| _valueParts.put(name, new ValuePart(value));
|
|
118 |
| else |
|
119 |
3
| part.add(value);
|
|
120 |
| } |
|
121 |
| |
|
122 |
69
| private String extractFileItemValue(FileItem item) {
|
|
123 |
69
| try {
|
|
124 |
69
| return (_encoding == null) ? item.getString() : item
|
|
125 |
| .getString(_encoding); |
|
126 |
| } catch (UnsupportedEncodingException ex) { |
|
127 |
0
| throw new ApplicationRuntimeException(MultipartMessages
|
|
128 |
| .unsupportedEncoding(_encoding, ex), ex); |
|
129 |
| } |
|
130 |
| } |
|
131 |
| } |