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 net.fckeditor.localization.LocalizedMessages;
24 import net.fckeditor.requestcycle.ThreadLocalData;
25 import net.fckeditor.tool.Utils;
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 public class UploadResponse {
51
52
53 protected Object[] parameters;
54
55
56 public static final int EN_OK = 0;
57
58
59 public static final int EN_CUSTOM_ERROR = 1;
60
61
62 public static final int EN_CUSTOM_WARNING = 101;
63
64
65 public static final int EN_FILE_RENAMED_WARNING = 201;
66
67
68 public static final int EN_INVALID_FILE_TYPE_ERROR = 202;
69
70
71 public static final int EN_SECURITY_ERROR = 203;
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91 public UploadResponse(Object... arguments) {
92 if (arguments.length < 1 || arguments.length > 4)
93 throw new IllegalArgumentException(
94 "The amount of arguments has to be between 1 and 4");
95
96 parameters = new Object[arguments.length];
97
98 if (!(arguments[0] instanceof Integer))
99 throw new IllegalArgumentException(
100 "The first argument has to be an error number (int)");
101
102 System.arraycopy(arguments, 0, parameters, 0, arguments.length);
103 }
104
105
106
107
108
109
110
111
112
113
114 public void setCustomMessage(final String customMassage) {
115 if (Utils.isNotEmpty(customMassage)) {
116 if (parameters.length == 1) {
117 Object errorNumber = parameters[0];
118 parameters = new Object[4];
119 parameters[0] = errorNumber;
120 parameters[1] = null;
121 parameters[2] = null;
122 }
123 parameters[3] = customMassage;
124 }
125 }
126
127
128 public static UploadResponse getOK(String fileUrl) {
129 return new UploadResponse(EN_OK, fileUrl);
130 }
131
132
133 public static UploadResponse getFileRenamedWarning(String fileUrl,
134 String newFileName) {
135 LocalizedMessages lm = LocalizedMessages.getInstance(ThreadLocalData
136 .getRequest());
137 return new UploadResponse(EN_FILE_RENAMED_WARNING, fileUrl,
138 newFileName, lm.getFileRenamedWarning(newFileName));
139 }
140
141
142 public static UploadResponse getInvalidFileTypeError() {
143 LocalizedMessages lm = LocalizedMessages.getInstance(ThreadLocalData
144 .getRequest());
145 return new UploadResponse(EN_INVALID_FILE_TYPE_ERROR, lm
146 .getInvalidFileTypeSpecified());
147 }
148
149
150 public static UploadResponse getInvalidCommandError() {
151 LocalizedMessages lm = LocalizedMessages.getInstance(ThreadLocalData
152 .getRequest());
153 return new UploadResponse(EN_CUSTOM_ERROR, null, null, lm
154 .getInvalidCommandSpecified());
155 }
156
157
158 public static UploadResponse getInvalidResourceTypeError() {
159 LocalizedMessages lm = LocalizedMessages.getInstance(ThreadLocalData
160 .getRequest());
161 return new UploadResponse(EN_CUSTOM_ERROR, null, null, lm
162 .getInvalidResouceTypeSpecified());
163 }
164
165
166 public static UploadResponse getInvalidCurrentFolderError() {
167 LocalizedMessages lm = LocalizedMessages.getInstance(ThreadLocalData
168 .getRequest());
169 return new UploadResponse(EN_CUSTOM_ERROR, null, null, lm
170 .getInvalidCurrentFolderSpecified());
171 }
172
173
174 public static UploadResponse getFileUploadDisabledError() {
175 LocalizedMessages lm = LocalizedMessages.getInstance(ThreadLocalData
176 .getRequest());
177 return new UploadResponse(EN_SECURITY_ERROR, null, null, lm
178 .getFileUploadDisabled());
179 }
180
181
182 public static UploadResponse getFileUploadWriteError() {
183 LocalizedMessages lm = LocalizedMessages.getInstance(ThreadLocalData
184 .getRequest());
185 return new UploadResponse(EN_CUSTOM_ERROR, null, null, lm
186 .getFileUploadWriteError());
187 }
188
189
190
191
192
193
194 @Override
195 public String toString() {
196 StringBuffer sb = new StringBuffer(400);
197 sb.append("<script type=\"text/javascript\">\n");
198
199
200
201 sb.append("(function(){var d=document.domain;while (true){try{var A=window.parent.document.domain;break;}catch(e) {};d=d.replace(/.*?(?:\\.|$)/,'');if (d.length==0) break;try{document.domain=d;}catch (e){break;}}})();\n");
202 sb.append("window.parent.OnUploadCompleted(");
203
204 for (Object parameter : parameters) {
205 if (parameter instanceof Integer) {
206 sb.append(parameter);
207 } else {
208 sb.append("'");
209 if (parameter != null)
210 sb.append(parameter);
211 sb.append("'");
212 }
213 sb.append(",");
214 }
215
216 sb.deleteCharAt(sb.length() - 1);
217 sb.append(");\n");
218 sb.append("</script>");
219
220 return sb.toString();
221 }
222 }