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