/* * FCKeditor - The text editor for Internet - http://www.fckeditor.net * Copyright (C) 2004-2010 Frederico Caldeira Knabben * * == BEGIN LICENSE == * * Licensed under the terms of any of the following licenses at your * choice: * * - GNU General Public License Version 2 or later (the "GPL") * http://www.gnu.org/licenses/gpl.html * * - GNU Lesser General Public License Version 2.1 or later (the "LGPL") * http://www.gnu.org/licenses/lgpl.html * * - Mozilla Public License Version 1.1 or later (the "MPL") * http://www.mozilla.org/MPL/MPL-1.1.html * * == END LICENSE == */ package net.fckeditor.response; import net.fckeditor.localization.LocalizedMessages; import net.fckeditor.requestcycle.ThreadLocalData; import net.fckeditor.tool.Utils; /** * Represents the HTML/JS response for the File Browser POST * requests. *

* This class utilizes varags to reflect the JavaScript callback function in an * optimal way. However, varargs can be tricky. You can always omit passed * parameters from right to left but any violation may result in an exception or * a failed callback. *

*

* For example: window.parent.OnUploadCompleted(101,'some/url/file.img','file.img','no error') can be mapped with * * UploadResponse ur = new UploadResponse(SC_SOME_ERROR,"/some/url/file.img","file.img","no error") * . *

*

* But window.parent.OnUploadCompleted(101,'some/url/file.img','no error') is an illegal callback and * will fail. *

* * */ public class UploadResponse { /** JavaScript callback parameters */ protected Object[] parameters; /** Error number OK */ public static final int EN_OK = 0; /** Error number CUSTOM ERROR */ public static final int EN_CUSTOM_ERROR = 1; /** Error number CUSTOM WARNING */ public static final int EN_CUSTOM_WARNING = 101; /** Error number FILE RENAMED WARNING */ public static final int EN_FILE_RENAMED_WARNING = 201; /** Error number INVALID FILE TYPE */ public static final int EN_INVALID_FILE_TYPE_ERROR = 202; /** Error number SECURITY ERROR */ public static final int EN_SECURITY_ERROR = 203; /** * Constructs a response with a varying amount of arguments. *

* Use the predefined error numbers or upload responses, if possible. If you * need to set error number and message only, use this constructor with the * first argument only and call * {@link UploadResponse#setCustomMessage(String)}. *

* * @param arguments * possible argument order: * int errorNumber, String fileUrl, String filename, String customMessage * . Omit from right to left. * @throws IllegalArgumentException * if amount of arguments is less than 1 and above 4 * @throws IllegalArgumentException * if the first argument is not an error number (int) */ public UploadResponse(Object... arguments) { if (arguments.length < 1 || arguments.length > 4) throw new IllegalArgumentException( "The amount of arguments has to be between 1 and 4"); parameters = new Object[arguments.length]; if (!(arguments[0] instanceof Integer)) throw new IllegalArgumentException( "The first argument has to be an error number (int)"); System.arraycopy(arguments, 0, parameters, 0, arguments.length); } /** * Sets the custom message of this upload response. * * Methods automatically determines how many arguments are set and puts the * message at the end. * * @param customMassage * the custom message of this upload response */ public void setCustomMessage(final String customMassage) { if (Utils.isNotEmpty(customMassage)) { if (parameters.length == 1) { Object errorNumber = parameters[0]; parameters = new Object[4]; parameters[0] = errorNumber; parameters[1] = null; parameters[2] = null; } parameters[3] = customMassage; } } /** Creates an OK response. */ public static UploadResponse getOK(String fileUrl) { return new UploadResponse(EN_OK, fileUrl); } /** Creates a FILE RENAMED warning. */ public static UploadResponse getFileRenamedWarning(String fileUrl, String newFileName) { LocalizedMessages lm = LocalizedMessages.getInstance(ThreadLocalData .getRequest()); return new UploadResponse(EN_FILE_RENAMED_WARNING, fileUrl, newFileName, lm.getFileRenamedWarning(newFileName)); } /** Creates a INVALID FILE TYPE error. */ public static UploadResponse getInvalidFileTypeError() { LocalizedMessages lm = LocalizedMessages.getInstance(ThreadLocalData .getRequest()); return new UploadResponse(EN_INVALID_FILE_TYPE_ERROR, lm .getInvalidFileTypeSpecified()); } /** Creates a INVALID COMMAND error. */ public static UploadResponse getInvalidCommandError() { LocalizedMessages lm = LocalizedMessages.getInstance(ThreadLocalData .getRequest()); return new UploadResponse(EN_CUSTOM_ERROR, null, null, lm .getInvalidCommandSpecified()); } /** Creates a INVALID RESOURCE TYPE error. */ public static UploadResponse getInvalidResourceTypeError() { LocalizedMessages lm = LocalizedMessages.getInstance(ThreadLocalData .getRequest()); return new UploadResponse(EN_CUSTOM_ERROR, null, null, lm .getInvalidResouceTypeSpecified()); } /** Creates a INVALID CURRENT FOLDER error. */ public static UploadResponse getInvalidCurrentFolderError() { LocalizedMessages lm = LocalizedMessages.getInstance(ThreadLocalData .getRequest()); return new UploadResponse(EN_CUSTOM_ERROR, null, null, lm .getInvalidCurrentFolderSpecified()); } /** Creates a FILE UPLOAD DISABLED error. */ public static UploadResponse getFileUploadDisabledError() { LocalizedMessages lm = LocalizedMessages.getInstance(ThreadLocalData .getRequest()); return new UploadResponse(EN_SECURITY_ERROR, null, null, lm .getFileUploadDisabled()); } /** Creates a FILE UPLOAD WRITE error. */ public static UploadResponse getFileUploadWriteError() { LocalizedMessages lm = LocalizedMessages.getInstance(ThreadLocalData .getRequest()); return new UploadResponse(EN_CUSTOM_ERROR, null, null, lm .getFileUploadWriteError()); } /** * Creates the HTML/JS representation of this upload response. * * @return HTML/JS representation of this upload response */ @Override public String toString() { StringBuffer sb = new StringBuffer(400); sb.append(""); return sb.toString(); } }