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 |
3
| public IUploadFile getFileUpload(String parameterName) {
|
50 |
3
| return (IUploadFile) _uploadParts.get(parameterName);
|
51 |
| } |
52 |
| |
53 |
5
| public void cleanup() {
|
54 |
5
| Iterator i = _uploadParts.values().iterator();
|
55 |
| |
56 |
5
| while (i.hasNext()) {
|
57 |
4
| UploadPart part = (UploadPart) i.next();
|
58 |
| |
59 |
4
| part.cleanup();
|
60 |
| } |
61 |
| } |
62 |
| |
63 |
5
| protected Map buildParameterMap() {
|
64 |
5
| Map result = new HashMap();
|
65 |
| |
66 |
5
| Iterator i = _valueParts.entrySet().iterator();
|
67 |
5
| while (i.hasNext()) {
|
68 |
22
| Map.Entry e = (Map.Entry) i.next();
|
69 |
| |
70 |
22
| String name = (String) e.getKey();
|
71 |
22
| ValuePart part = (ValuePart) e.getValue();
|
72 |
| |
73 |
22
| result.put(name, part.getValues());
|
74 |
| } |
75 |
| |
76 |
5
| return result;
|
77 |
| } |
78 |
| |
79 |
5
| protected void processFileItems(List parts) {
|
80 |
5
| if (parts == null)
|
81 |
0
| return;
|
82 |
| |
83 |
5
| Iterator i = parts.iterator();
|
84 |
| |
85 |
5
| while (i.hasNext()) {
|
86 |
27
| FileItem item = (FileItem) i.next();
|
87 |
| |
88 |
27
| processFileItem(item);
|
89 |
| } |
90 |
| } |
91 |
| |
92 |
27
| private void processFileItem(FileItem item) {
|
93 |
27
| if (item.isFormField()) {
|
94 |
23
| processFormFieldItem(item);
|
95 |
23
| return;
|
96 |
| } |
97 |
| |
98 |
4
| processUploadFileItem(item);
|
99 |
| } |
100 |
| |
101 |
4
| private void processUploadFileItem(FileItem item) {
|
102 |
4
| String name = item.getFieldName();
|
103 |
| |
104 |
4
| UploadPart part = new UploadPart(item);
|
105 |
| |
106 |
4
| _uploadParts.put(name, part);
|
107 |
| } |
108 |
| |
109 |
23
| void processFormFieldItem(FileItem item) {
|
110 |
23
| String name = item.getFieldName();
|
111 |
| |
112 |
23
| String value = extractFileItemValue(item);
|
113 |
| |
114 |
23
| ValuePart part = (ValuePart) _valueParts.get(name);
|
115 |
| |
116 |
23
| if (part == null)
|
117 |
22
| _valueParts.put(name, new ValuePart(value));
|
118 |
| else |
119 |
1
| part.add(value);
|
120 |
| } |
121 |
| |
122 |
23
| private String extractFileItemValue(FileItem item) {
|
123 |
23
| try {
|
124 |
23
| 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 |
| } |