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