Coverage Report - net.fckeditor.response.UploadResponse
 
Classes in this File Line Coverage Branch Coverage Complexity
UploadResponse
68%
33/48
87%
14/16
1,909
 
 1  
 /*
 2  
  * FCKeditor - The text editor for Internet - http://www.fckeditor.net
 3  
  * Copyright (C) 2004-2010 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.localization.LocalizedMessages;
 24  
 import net.fckeditor.requestcycle.ThreadLocalData;
 25  
 import net.fckeditor.tool.Utils;
 26  
 
 27  
 /**
 28  
  * Represents the <a href="http://docs.fckeditor.net/FCKeditor_2.x/Developers_Guide/Server_Side_Integration#FileUpload_.28HTML.29"
 29  
  * target="_blank">HTML/JS response</a> for the File Browser <code>POST</code>
 30  
  * requests.
 31  
  * <p>
 32  
  * This class utilizes varags to reflect the JavaScript callback function in an
 33  
  * optimal way. However, varargs can be tricky. You can always omit passed
 34  
  * parameters from right to left but any violation may result in an exception or
 35  
  * a failed callback.
 36  
  * </p>
 37  
  * <p>
 38  
  * For example: <code>window.parent.OnUploadCompleted(101,'some/url/file.img','file.img','no error')</code> can be mapped with
 39  
  * 
 40  
  * <code>UploadResponse ur = new UploadResponse(SC_SOME_ERROR,"/some/url/file.img","file.img","no error")</code>
 41  
  * .
 42  
  * </p>
 43  
  * <p>
 44  
  * But <code>window.parent.OnUploadCompleted(101,'some/url/file.img','no error')</code> is an illegal callback and
 45  
  * will fail.
 46  
  * </p>
 47  
  * 
 48  
  * @version $Id: UploadResponse.java 4785 2009-12-21 20:10:28Z mosipov $
 49  
  */
 50  
 public class UploadResponse {
 51  
 
 52  
         /** JavaScript callback parameters */
 53  
         protected Object[] parameters;
 54  
 
 55  
         /** Error number OK */
 56  
         public static final int EN_OK = 0;
 57  
 
 58  
         /** Error number CUSTOM ERROR */
 59  
         public static final int EN_CUSTOM_ERROR = 1;
 60  
 
 61  
         /** Error number CUSTOM WARNING */
 62  
         public static final int EN_CUSTOM_WARNING = 101;
 63  
 
 64  
         /** Error number FILE RENAMED WARNING */
 65  
         public static final int EN_FILE_RENAMED_WARNING = 201;
 66  
 
 67  
         /** Error number INVALID FILE TYPE */
 68  
         public static final int EN_INVALID_FILE_TYPE_ERROR = 202;
 69  
 
 70  
         /** Error number SECURITY ERROR */
 71  
         public static final int EN_SECURITY_ERROR = 203;
 72  
 
 73  
         /**
 74  
          * Constructs a response with a varying amount of arguments.
 75  
          * <p>
 76  
          * Use the predefined error numbers or upload responses, if possible. If you
 77  
          * need to set error number and message only, use this constructor with the
 78  
          * first argument only and call
 79  
          * {@link UploadResponse#setCustomMessage(String)}.
 80  
          * </p>
 81  
          * 
 82  
          * @param arguments
 83  
          *            possible argument order:
 84  
          *            <code>int errorNumber, String fileUrl, String filename, String customMessage</code>
 85  
          *            . Omit from right to left.
 86  
          * @throws IllegalArgumentException
 87  
          *             if amount of arguments is less than 1 and above 4
 88  
          * @throws IllegalArgumentException
 89  
          *             if the first argument is not an error number (int)
 90  
          */
 91  8
         public UploadResponse(Object... arguments) {
 92  8
                 if (arguments.length < 1 || arguments.length > 4)
 93  2
                         throw new IllegalArgumentException(
 94  
                                         "The amount of arguments has to be between 1 and 4");
 95  
 
 96  6
                 parameters = new Object[arguments.length];
 97  
 
 98  6
                 if (!(arguments[0] instanceof Integer))
 99  1
                         throw new IllegalArgumentException(
 100  
                                         "The first argument has to be an error number (int)");
 101  
 
 102  5
                 System.arraycopy(arguments, 0, parameters, 0, arguments.length);
 103  5
         }
 104  
 
 105  
         /**
 106  
          * Sets the custom message of this upload response.
 107  
          * 
 108  
          * Methods automatically determines how many arguments are set and puts the
 109  
          * message at the end.
 110  
          * 
 111  
          * @param customMassage
 112  
          *            the custom message of this upload response
 113  
          */
 114  
         public void setCustomMessage(final String customMassage) {
 115  1
                 if (Utils.isNotEmpty(customMassage)) {
 116  1
                         if (parameters.length == 1) {
 117  1
                                 Object errorNumber = parameters[0];
 118  1
                                 parameters = new Object[4];
 119  1
                                 parameters[0] = errorNumber;
 120  1
                                 parameters[1] = null;
 121  1
                                 parameters[2] = null;
 122  
                         }
 123  1
                         parameters[3] = customMassage;
 124  
                 }
 125  1
         }
 126  
 
 127  
         /** Creates an <code>OK</code> response. */
 128  
         public static UploadResponse getOK(String fileUrl) {
 129  0
                 return new UploadResponse(EN_OK, fileUrl);
 130  
         }
 131  
 
 132  
         /** Creates a <code>FILE RENAMED</code> warning. */
 133  
         public static UploadResponse getFileRenamedWarning(String fileUrl,
 134  
                         String newFileName) {
 135  0
                 LocalizedMessages lm = LocalizedMessages.getInstance(ThreadLocalData
 136  
                                 .getRequest());
 137  0
                 return new UploadResponse(EN_FILE_RENAMED_WARNING, fileUrl,
 138  
                                 newFileName, lm.getFileRenamedWarning(newFileName));
 139  
         }
 140  
 
 141  
         /** Creates a <code>INVALID FILE TYPE</code> error. */
 142  
         public static UploadResponse getInvalidFileTypeError() {
 143  0
                 LocalizedMessages lm = LocalizedMessages.getInstance(ThreadLocalData
 144  
                                 .getRequest());
 145  0
                 return new UploadResponse(EN_INVALID_FILE_TYPE_ERROR, lm
 146  
                                 .getInvalidFileTypeSpecified());
 147  
         }
 148  
 
 149  
         /** Creates a <code>INVALID COMMAND</code> error. */
 150  
         public static UploadResponse getInvalidCommandError() {
 151  0
                 LocalizedMessages lm = LocalizedMessages.getInstance(ThreadLocalData
 152  
                                 .getRequest());
 153  0
                 return new UploadResponse(EN_CUSTOM_ERROR, null, null, lm
 154  
                                 .getInvalidCommandSpecified());
 155  
         }
 156  
 
 157  
         /** Creates a <code>INVALID RESOURCE TYPE</code> error. */
 158  
         public static UploadResponse getInvalidResourceTypeError() {
 159  0
                 LocalizedMessages lm = LocalizedMessages.getInstance(ThreadLocalData
 160  
                                 .getRequest());
 161  0
                 return new UploadResponse(EN_CUSTOM_ERROR, null, null, lm
 162  
                                 .getInvalidResouceTypeSpecified());
 163  
         }
 164  
 
 165  
         /** Creates a <code>INVALID CURRENT FOLDER</code> error. */
 166  
         public static UploadResponse getInvalidCurrentFolderError() {
 167  0
                 LocalizedMessages lm = LocalizedMessages.getInstance(ThreadLocalData
 168  
                                 .getRequest());
 169  0
                 return new UploadResponse(EN_CUSTOM_ERROR, null, null, lm
 170  
                                 .getInvalidCurrentFolderSpecified());
 171  
         }
 172  
 
 173  
         /** Creates a <code>FILE UPLOAD DISABLED</code> error. */
 174  
         public static UploadResponse getFileUploadDisabledError() {
 175  0
                 LocalizedMessages lm = LocalizedMessages.getInstance(ThreadLocalData
 176  
                                 .getRequest());
 177  0
                 return new UploadResponse(EN_SECURITY_ERROR, null, null, lm
 178  
                                 .getFileUploadDisabled());
 179  
         }
 180  
 
 181  
         /** Creates a <code>FILE UPLOAD WRITE</code> error. */
 182  
         public static UploadResponse getFileUploadWriteError() {
 183  0
                 LocalizedMessages lm = LocalizedMessages.getInstance(ThreadLocalData
 184  
                                 .getRequest());
 185  0
                 return new UploadResponse(EN_CUSTOM_ERROR, null, null, lm
 186  
                                 .getFileUploadWriteError());
 187  
         }
 188  
 
 189  
         /**
 190  
          * Creates the HTML/JS representation of this upload response.
 191  
          * 
 192  
          * @return HTML/JS representation of this upload response
 193  
          */
 194  
         @Override
 195  
         public String toString() {
 196  5
                 StringBuffer sb = new StringBuffer(400);
 197  5
                 sb.append("<script type=\"text/javascript\">\n");
 198  
                 // Compressed version of the document.domain automatic fix script.
 199  
                 // The original script can be found at
 200  
                 // [fckeditor_dir]/_dev/domain_fix_template.js
 201  5
                 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  5
                 sb.append("window.parent.OnUploadCompleted(");
 203  
 
 204  19
                 for (Object parameter : parameters) {
 205  14
                         if (parameter instanceof Integer) {
 206  5
                                 sb.append(parameter);
 207  
                         } else {
 208  9
                                 sb.append("'");
 209  9
                                 if (parameter != null)
 210  4
                                         sb.append(parameter);
 211  9
                                 sb.append("'");
 212  
                         }
 213  14
                         sb.append(",");
 214  
                 }
 215  
 
 216  5
                 sb.deleteCharAt(sb.length() - 1);
 217  5
                 sb.append(");\n");
 218  5
                 sb.append("</script>");
 219  
 
 220  5
                 return sb.toString();
 221  
         }
 222  
 }