1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.cms.beans.runtime;
35
36 import org.apache.commons.lang.StringUtils;
37
38 import java.io.File;
39 import java.util.Enumeration;
40 import java.util.Hashtable;
41 import java.util.Map;
42
43
44
45
46
47
48
49 public class MultipartForm {
50
51
52
53
54 public static final String REQUEST_ATTRIBUTE_NAME = "multipartform";
55
56 private final Map<String, String[]> parameters;
57
58 private final Map<String, Document> documents;
59
60 private final Map parameterList;
61
62 public MultipartForm() {
63 this.parameters = new Hashtable<String, String[]>();
64 this.documents = new Hashtable<String, Document>();
65 this.parameterList = new Hashtable();
66 }
67
68
69
70
71 public void addParameter(String name, Object value) {
72 if (value instanceof String[]) {
73 this.parameters.put(name, (String[]) value);
74 } else {
75 this.parameters.put(name, new String[]{(String) value });
76 }
77 }
78
79
80
81
82 public void removeParameter(String name) {
83 this.parameters.remove(name);
84 }
85
86
87
88
89 public Map<String, String[]> getParameters() {
90 return this.parameters;
91 }
92
93
94
95
96 public String getParameter(String name) {
97 try {
98 String[] params = this.parameters.get(name);
99 if (params != null && params.length > 0) {
100 return params[0];
101 }
102 return null;
103 } catch (Exception e) {
104 return null;
105 }
106 }
107
108
109
110
111 public String[] getParameterValues(String name) {
112 try {
113 return ((String[]) this.parameterList.get(name));
114 } catch (Exception e) {
115 return null;
116 }
117 }
118
119
120
121
122 public void addparameterValues(String name, String[] values) {
123 this.parameterList.put(name, values);
124 }
125
126 public void addDocument(String atomName, String fileName, String type, File file) {
127 if (StringUtils.isEmpty(fileName)) {
128 return;
129 }
130 Document document = new Document();
131 document.setAtomName(atomName);
132 document.setType(type);
133 document.setFile(file);
134 if (!StringUtils.contains(fileName, ".")) {
135 document.setExtension(StringUtils.EMPTY);
136 document.setFileName(fileName);
137 } else {
138 document.setExtension(StringUtils.substringAfterLast(fileName, "."));
139 document.setFileName(StringUtils.substringBeforeLast(fileName, "."));
140 }
141 this.documents.put(atomName, document);
142 }
143
144 public Document getDocument(String name) {
145 return this.documents.get(name);
146 }
147
148 public Map<String, Document> getDocuments() {
149 return this.documents;
150 }
151
152
153
154
155 public Enumeration<String> getParameterNames() {
156 return ((Hashtable<String, String[]>) this.parameters).keys();
157 }
158 }