1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package net.fckeditor.response;
22
23 import java.io.StringWriter;
24 import java.util.List;
25 import java.util.Map;
26
27 import javax.xml.parsers.DocumentBuilder;
28 import javax.xml.parsers.DocumentBuilderFactory;
29 import javax.xml.parsers.ParserConfigurationException;
30 import javax.xml.transform.Transformer;
31 import javax.xml.transform.TransformerException;
32 import javax.xml.transform.TransformerFactory;
33 import javax.xml.transform.dom.DOMSource;
34 import javax.xml.transform.stream.StreamResult;
35
36 import net.fckeditor.connector.Connector;
37 import net.fckeditor.handlers.Command;
38 import net.fckeditor.handlers.ResourceType;
39 import net.fckeditor.localization.LocalizedMessages;
40 import net.fckeditor.requestcycle.ThreadLocalData;
41 import net.fckeditor.tool.Utils;
42
43 import org.w3c.dom.Document;
44 import org.w3c.dom.Element;
45
46
47
48
49
50
51
52
53 public class GetResponse {
54
55
56 protected Document document;
57
58 protected Element errorElement;
59
60 protected Element foldersElement;
61
62 protected Element filesElement;
63
64
65 public static final int EN_OK = 0;
66
67
68 public static final int EN_CUSTOM_ERROR = 1;
69
70
71 public static final int EN_FOLDER_ALREADY_EXISTS_ERROR = 101;
72
73
74 public static final int EN_INVALID_NEW_FOLDER_NAME_ERROR = 102;
75
76
77 public static final int EN_CREATE_FOLDER_SECURITY_ERROR = 103;
78
79
80 public static final int EN_UKNOWN_CREATE_FOLDER_ERROR = 110;
81
82
83
84
85
86
87
88
89
90
91
92 public GetResponse(int number, String message) {
93 try {
94 DocumentBuilderFactory factory = DocumentBuilderFactory
95 .newInstance();
96 DocumentBuilder builder = factory.newDocumentBuilder();
97 document = builder.newDocument();
98 } catch (ParserConfigurationException e) {
99 throw new RuntimeException(e);
100 }
101
102 Element root = document.createElement("Connector");
103 document.appendChild(root);
104 setError(number, message);
105 }
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122 public GetResponse(Command command, ResourceType type,
123 String currentFolder, String constructedUrl) {
124
125 try {
126 DocumentBuilderFactory factory = DocumentBuilderFactory
127 .newInstance();
128 DocumentBuilder builder = factory.newDocumentBuilder();
129 document = builder.newDocument();
130 } catch (ParserConfigurationException e) {
131 throw new RuntimeException(e);
132 }
133
134 Element root = document.createElement("Connector");
135 document.appendChild(root);
136 root.setAttribute("command", command.getName());
137 root.setAttribute("resourceType", type.getName());
138
139 Element currentFolderElement = document.createElement("CurrentFolder");
140 currentFolderElement.setAttribute("path", currentFolder);
141
142 currentFolderElement.setAttribute("url", constructedUrl);
143 root.appendChild(currentFolderElement);
144
145 }
146
147
148
149
150
151
152
153
154
155 public GetResponse(int number) {
156 this(number, null);
157 }
158
159
160
161
162
163
164
165
166
167 public void setError(int number, String message) {
168
169 if (errorElement == null) {
170 errorElement = document.createElement("Error");
171 document.getDocumentElement().appendChild(errorElement);
172 }
173
174 errorElement.setAttribute("number", String.valueOf(number));
175 if (Utils.isNotEmpty(message))
176 errorElement.setAttribute("text", message);
177
178 }
179
180
181
182
183
184
185
186 public void setError(int number) {
187 setError(number, null);
188 }
189
190
191
192
193
194
195
196
197 public void setFolders(final List<String> folders) {
198 if (foldersElement != null) {
199 Element parent = (Element) foldersElement.getParentNode();
200 parent.removeChild(foldersElement);
201 }
202
203 foldersElement = document.createElement("Folders");
204 document.getDocumentElement().appendChild(foldersElement);
205
206 for (String folder : folders) {
207 Element folderElement = document.createElement("Folder");
208 folderElement.setAttribute("name", folder);
209 foldersElement.appendChild(folderElement);
210 }
211 }
212
213
214
215
216
217
218
219
220 public void setFiles(final List<Map<String, Object>> files) {
221 if (filesElement != null) {
222 Element parent = (Element) filesElement.getParentNode();
223 parent.removeChild(filesElement);
224 }
225
226 filesElement = document.createElement("Files");
227 document.getDocumentElement().appendChild(filesElement);
228
229 long length = 1L;
230 long tempLength;
231
232 for (Map<String, Object> file : files) {
233 Element fileElement = document.createElement("File");
234 fileElement.setAttribute("name", (String) file
235 .get(Connector.KEY_NAME));
236 tempLength = ((Number) file.get(Connector.KEY_SIZE)).longValue();
237 if (tempLength > 1024L)
238 length = tempLength / 1024L;
239 fileElement.setAttribute("size", String.valueOf(length));
240 filesElement.appendChild(fileElement);
241 }
242 }
243
244
245
246
247
248
249
250
251
252 @Override
253 public String toString() {
254 document.getDocumentElement().normalize();
255 TransformerFactory factory = TransformerFactory.newInstance();
256
257 StringWriter sw = new StringWriter();
258
259 try {
260 Transformer transformer = factory.newTransformer();
261
262 DOMSource source = new DOMSource(document);
263 StreamResult result = new StreamResult(sw);
264
265 transformer.transform(source, result);
266 } catch (TransformerException e) {
267 throw new RuntimeException(e);
268 }
269
270 return sw.toString();
271 }
272
273
274 public static GetResponse getOK() {
275 return new GetResponse(EN_OK);
276 }
277
278
279 public static GetResponse getInvalidCommandError() {
280 LocalizedMessages lm = LocalizedMessages.getInstance(ThreadLocalData
281 .getRequest());
282 return new GetResponse(EN_CUSTOM_ERROR, lm.getInvalidCommandSpecified());
283 }
284
285
286 public static GetResponse getInvalidResourceTypeError() {
287 LocalizedMessages lm = LocalizedMessages.getInstance(ThreadLocalData
288 .getRequest());
289 return new GetResponse(EN_CUSTOM_ERROR, lm
290 .getInvalidResouceTypeSpecified());
291 }
292
293
294 public static GetResponse getInvalidCurrentFolderError() {
295 LocalizedMessages lm = LocalizedMessages.getInstance(ThreadLocalData
296 .getRequest());
297 return new GetResponse(EN_CUSTOM_ERROR, lm
298 .getInvalidCurrentFolderSpecified());
299 }
300
301
302 public static GetResponse getGetResourcesDisabledError() {
303 LocalizedMessages lm = LocalizedMessages.getInstance(ThreadLocalData
304 .getRequest());
305 return new GetResponse(EN_CUSTOM_ERROR, lm.getGetResourcesDisabled());
306 }
307
308
309 public static GetResponse getGetResourcesReadError() {
310 LocalizedMessages lm = LocalizedMessages.getInstance(ThreadLocalData
311 .getRequest());
312 return new GetResponse(EN_CUSTOM_ERROR, lm.getGetResourcesReadError());
313 }
314
315
316 public static GetResponse getCreateFolderDisabledError() {
317 LocalizedMessages lm = LocalizedMessages.getInstance(ThreadLocalData
318 .getRequest());
319 return new GetResponse(EN_CUSTOM_ERROR, lm.getCreateFolderDisabled());
320 }
321
322
323 public static GetResponse getInvalidNewFolderNameError() {
324 LocalizedMessages lm = LocalizedMessages.getInstance(ThreadLocalData
325 .getRequest());
326 return new GetResponse(EN_INVALID_NEW_FOLDER_NAME_ERROR, lm
327 .getInvalidNewFolderNameSpecified());
328 }
329
330
331 public static GetResponse getFolderAlreadyExistsError() {
332 LocalizedMessages lm = LocalizedMessages.getInstance(ThreadLocalData
333 .getRequest());
334 return new GetResponse(EN_FOLDER_ALREADY_EXISTS_ERROR, lm
335 .getFolderAlreadyExistsError());
336 }
337
338
339 public static GetResponse getCreateFolderWriteError() {
340 LocalizedMessages lm = LocalizedMessages.getInstance(ThreadLocalData
341 .getRequest());
342 return new GetResponse(EN_UKNOWN_CREATE_FOLDER_ERROR, lm
343 .getCreateFolderWriteError());
344 }
345 }