Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
UploadResponse |
|
| 0.0;0 |
1 | /* | |
2 | * FCKeditor - The text editor for Internet - http://www.fckeditor.net | |
3 | * Copyright (C) 2003-2008 Frederico Caldeira Knabben | |
4 | * | |
5 | * == BEGIN LICENSE == | |
6 | * | |
7 | * Licensed under the terms of any of the following licenses at your | |
8 | * choice: | |
9 | * | |
10 | * - GNU General Public License Version 2 or later (the "GPL") | |
11 | * http://www.gnu.org/licenses/gpl.html | |
12 | * | |
13 | * - GNU Lesser General Public License Version 2.1 or later (the "LGPL") | |
14 | * http://www.gnu.org/licenses/lgpl.html | |
15 | * | |
16 | * - Mozilla Public License Version 1.1 or later (the "MPL") | |
17 | * http://www.mozilla.org/MPL/MPL-1.1.html | |
18 | * | |
19 | * == END LICENSE == | |
20 | */ | |
21 | package net.fckeditor.response; | |
22 | ||
23 | import net.fckeditor.connector.Messages; | |
24 | import net.fckeditor.tool.Utils; | |
25 | ||
26 | /** | |
27 | * Simply abstracts from the javascript callback to a java class. | |
28 | * | |
29 | * <p> | |
30 | * The usage is quite easy but can be tricky since varargs are used in the class | |
31 | * constructor.<br/> The requestor expects a JS method callback with variable | |
32 | * arguments size. | |
33 | * </p> | |
34 | * <p> | |
35 | * e.g. | |
36 | * <code>window.parent.OnUploadCompleted(101,'some/url/file.img','file.img','no error');</code> | |
37 | * </p> | |
38 | * <p> | |
39 | * The UploadResponse constructor behaves the same way by simply calling it | |
40 | * with:<br/> | |
41 | * <code>UploadResponse ur = new UploadResonse(EN_SOME_ERROR,"/some/url/file.img","file.img","no error"):</code> | |
42 | * </p> | |
43 | * | |
44 | * @since 2.4 | |
45 | * @version $Id: UploadResponse.java 1966 2008-05-08 10:43:36Z th-schwarz $ | |
46 | */ | |
47 | public class UploadResponse { | |
48 | ||
49 | private Object[] parameters; | |
50 | ||
51 | // TODO Rename 'En_'-constants to something more intuitive like 'CODE_' | |
52 | /** Error number OK */ | |
53 | public static final int EN_OK = 0; | |
54 | ||
55 | /** Error number ERROR */ | |
56 | public static final int EN_ERROR = 1; | |
57 | ||
58 | /** Error number RENAMED */ | |
59 | public static final int EN_RENAMED = 201; | |
60 | ||
61 | /** Error number INVALID EXTENSION */ | |
62 | public static final int EN_INVALID_EXTENSION = 202; | |
63 | ||
64 | /** Error number SECURITY ERROR */ | |
65 | public static final int EN_SECURITY_ERROR = 203; | |
66 | ||
67 | /** UploadResponse INVALID CURRENT FOLDER */ | |
68 | 1 | public static final UploadResponse UR_INVALID_CURRENT_FOLDER = new UploadResponse( |
69 | UploadResponse.EN_ERROR, null, null, | |
70 | Messages.INVALID_CURRENT_FOLDER); | |
71 | ||
72 | /** | |
73 | * Constructs the response with variable amount of parameters. | |
74 | * <p> | |
75 | * Put the desired parameters in the constructor. You may omit them from | |
76 | * right to left but you have to remain the order.<br/> e.g. | |
77 | * <code>UploadResponse(EN_OK,"/some/url/to/pic.jpg","pic")</code> or | |
78 | * <code>UploadResponse(EN_OK)</code> but <b>not</b> | |
79 | * <code>UploadResponse(EN_OK,"some error message")</code> | |
80 | * </p> | |
81 | * <p> | |
82 | * Use, if possible, the pre-defined error numbers or upload responses. | |
83 | * </p> | |
84 | * <p> | |
85 | * If you need to set error number and message only, use constructor with | |
86 | * one parameter and call {@link UploadResponse#setCustomMessage(String)}. | |
87 | * | |
88 | * @param arguments | |
89 | * possible argument order: | |
90 | * <code>int errorNumber, String fileUrl, String filename, String customMessage</code> | |
91 | * @throws IllegalArgumentException | |
92 | * if amount of arguments is less than 1 and above 4 | |
93 | * @throws IllegalArgumentException | |
94 | * if the first argument is not an error number (int) | |
95 | */ | |
96 | ||
97 | 9 | public UploadResponse(Object... arguments) throws IllegalArgumentException { |
98 | 9 | if (arguments.length < 1 || arguments.length > 4) |
99 | 2 | throw new IllegalArgumentException( |
100 | "The amount of arguments has to be between 1 and 4"); | |
101 | ||
102 | 7 | parameters = new Object[arguments.length]; |
103 | ||
104 | 7 | if (!(arguments[0] instanceof Integer)) |
105 | 1 | throw new IllegalArgumentException( |
106 | "The first argument has to be an error number (int)"); | |
107 | ||
108 | 6 | System.arraycopy(arguments, 0, parameters, 0, arguments.length); |
109 | 6 | } |
110 | ||
111 | /** | |
112 | * Sets the message in the UploadResponse. | |
113 | * | |
114 | * Methods automatically determines how many arguments are set and puts the | |
115 | * message at the end. | |
116 | * | |
117 | * @param customMassage | |
118 | * the message you want to pass to the user | |
119 | */ | |
120 | public void setCustomMessage(final String customMassage) { | |
121 | 1 | if (Utils.isNotEmpty(customMassage)) { |
122 | 1 | if (parameters.length == 1) { |
123 | 1 | Object errorNumber = parameters[0]; |
124 | 1 | parameters = new Object[4]; |
125 | 1 | parameters[0] = errorNumber; |
126 | 1 | parameters[1] = null; |
127 | 1 | parameters[2] = null; |
128 | } | |
129 | 1 | parameters[3] = customMassage; |
130 | } | |
131 | 1 | } |
132 | ||
133 | /** | |
134 | * Assembles the JavaScript method for the user callback | |
135 | */ | |
136 | @Override | |
137 | public String toString() { | |
138 | 5 | StringBuffer sb = new StringBuffer(150); |
139 | 5 | sb.append("<script type=\"text/javascript\">\n"); |
140 | // Minified version of the document.domain automatic fix script. | |
141 | // The original script can be found at _dev/domain_fix_template.js | |
142 | 5 | sb.append("(function(){var d=document.domain;while (true){try{var A=window.top.opener.document.domain;break;}catch(e) {};d=d.replace(/.*?(?:\\.|$)/,'');if (d.length==0) break;try{document.domain=d;}catch (e){break;}}})();\n"); |
143 | 5 | sb.append("window.parent.OnUploadCompleted("); |
144 | ||
145 | 19 | for (Object parameter : parameters) { |
146 | 14 | if (parameter instanceof Integer) { |
147 | 5 | sb.append(parameter); |
148 | } else { | |
149 | 9 | sb.append("'"); |
150 | 9 | if (parameter != null) |
151 | 4 | sb.append(parameter); |
152 | 9 | sb.append("'"); |
153 | } | |
154 | 14 | sb.append(","); |
155 | } | |
156 | ||
157 | 5 | sb.deleteCharAt(sb.length() - 1); |
158 | 5 | sb.append(");\n"); |
159 | 5 | sb.append("</script>"); |
160 | ||
161 | 5 | return sb.toString(); |
162 | } | |
163 | } |