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 object. 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(SC_SOME_ERROR,"/some/url/file.img","file.img","no error"):</code> 42 * </p> 43 * 44 * @version $Id: UploadResponse.java 2151 2008-07-02 22:03:15Z mosipov $ 45 */ 46 public class UploadResponse { 47 48 private Object[] parameters; 49 50 // Naming scheme according static status codes in javax.servlet.http.HttpServletResponse 51 /** Error number OK */ 52 public static final int SC_OK = 0; 53 54 /** Error number ERROR */ 55 public static final int SC_ERROR = 1; 56 57 /** Error number RENAMED */ 58 public static final int SC_RENAMED = 201; 59 60 /** Error number INVALID EXTENSION */ 61 public static final int SC_INVALID_EXTENSION = 202; 62 63 /** Error number SECURITY ERROR */ 64 public static final int SC_SECURITY_ERROR = 203; 65 66 /** UploadResponse INVALID CURRENT FOLDER */ 67 public static final UploadResponse UR_INVALID_CURRENT_FOLDER = new UploadResponse( 68 UploadResponse.SC_ERROR, null, null, 69 Messages.INVALID_CURRENT_FOLDER); 70 71 /** 72 * Constructs the response with variable amount of parameters. 73 * <p> 74 * Put the desired parameters in the constructor. You may omit them from 75 * right to left but you have to remain the order.<br/> e.g. 76 * <code>UploadResponse(SC_OK,"/some/url/to/pic.jpg","pic")</code> or 77 * <code>UploadResponse(SC_OK)</code> but <b>not</b> 78 * <code>UploadResponse(SC_OK,"some error message")</code> 79 * </p> 80 * <p> 81 * Use, if possible, the pre-defined error numbers or upload responses. 82 * </p> 83 * <p> 84 * If you need to set error number and message only, use constructor with 85 * one parameter and call {@link UploadResponse#setCustomMessage(String)}. 86 * 87 * @param arguments 88 * possible argument order: 89 * <code>int errorNumber, String fileUrl, String filename, String customMessage</code> 90 * @throws IllegalArgumentException 91 * if amount of arguments is less than 1 and above 4 92 * @throws IllegalArgumentException 93 * if the first argument is not an error number (int) 94 */ 95 public UploadResponse(Object... arguments) throws IllegalArgumentException { 96 if (arguments.length < 1 || arguments.length > 4) 97 throw new IllegalArgumentException( 98 "The amount of arguments has to be between 1 and 4"); 99 100 parameters = new Object[arguments.length]; 101 102 if (!(arguments[0] instanceof Integer)) 103 throw new IllegalArgumentException( 104 "The first argument has to be an error number (int)"); 105 106 System.arraycopy(arguments, 0, parameters, 0, arguments.length); 107 } 108 109 /** 110 * Sets the message in the <code>UploadResponse</code>. 111 * 112 * Methods automatically determines how many arguments are set and puts the 113 * message at the end. 114 * 115 * @param customMassage 116 * the message you want to pass to the user 117 */ 118 public void setCustomMessage(final String customMassage) { 119 if (Utils.isNotEmpty(customMassage)) { 120 if (parameters.length == 1) { 121 Object errorNumber = parameters[0]; 122 parameters = new Object[4]; 123 parameters[0] = errorNumber; 124 parameters[1] = null; 125 parameters[2] = null; 126 } 127 parameters[3] = customMassage; 128 } 129 } 130 131 /** 132 * Assembles the JavaScript method for the user callback 133 */ 134 @Override 135 public String toString() { 136 StringBuffer sb = new StringBuffer(400); 137 sb.append("<script type=\"text/javascript\">\n"); 138 // Minified version of the document.domain automatic fix script. 139 // The original script can be found at [fckeditor dir]/_dev/domain_fix_template.js 140 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"); 141 sb.append("window.parent.OnUploadCompleted("); 142 143 for (Object parameter : parameters) { 144 if (parameter instanceof Integer) { 145 sb.append(parameter); 146 } else { 147 sb.append("'"); 148 if (parameter != null) 149 sb.append(parameter); 150 sb.append("'"); 151 } 152 sb.append(","); 153 } 154 155 sb.deleteCharAt(sb.length() - 1); 156 sb.append(");\n"); 157 sb.append("</script>"); 158 159 return sb.toString(); 160 } 161 }