Changeset 1690
- Timestamp:
- 2008-03-13 14:08:42 (6 months ago)
- Location:
- FCKeditor.Java/branches/2.4/src
- Files:
-
- 1 added
- 5 modified
-
main/java/net/fckeditor/connector/ConnectorServlet.java (modified) (7 diffs)
-
main/java/net/fckeditor/connector/Messages.java (added)
-
main/java/net/fckeditor/response/UploadResponse.java (modified) (3 diffs)
-
main/java/net/fckeditor/response/XmlResponse.java (modified) (1 diff)
-
main/java/net/fckeditor/tool/UtilsFile.java (modified) (3 diffs)
-
test/java/net/fckeditor/tool/UploadResponseTest.java (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
FCKeditor.Java/branches/2.4/src/main/java/net/fckeditor/connector/ConnectorServlet.java
r1674 r1690 79 79 80 80 File defaultUserFilesDir = new File(realDefaultUserFilesPath); 81 if (!defaultUserFilesDir.exists()) {82 defaultUserFilesDir.mkdirs();83 }81 UtilsFile.checkDirAndCreate(defaultUserFilesDir); 82 83 // FIXME wrong spelling 84 84 logger.info("ConnectorServlet successfull initialized!"); 85 85 } … … 113 113 XmlResponse xr; 114 114 115 if (! CommandHandler.isValidForGet(commandStr))115 if (!SessionDataHandler.isEnabledForFileBrowsing(request)) 116 116 xr = new XmlResponse(XmlResponse.EN_ERROR, 117 XmlResponse.CM_INVALID_COMMAND);118 else if (! SessionDataHandler.isEnabledForFileBrowsing(request))117 Messages.NOT_AUTHORIZED_FOR_BROWSING); 118 else if (!CommandHandler.isValidForGet(commandStr)) 119 119 xr = new XmlResponse(XmlResponse.EN_ERROR, 120 XmlResponse.CM_NOT_AUTHORIZED);120 Messages.INVALID_COMMAND); 121 121 else if (typeStr != null && !ResourceTypeHandler.isValid(typeStr)) 122 122 xr = new XmlResponse(XmlResponse.EN_ERROR, 123 XmlResponse.CM_INVALID_TYPE);123 Messages.INVALID_TYPE); 124 124 else if (!UtilsFile.isValidPath(currentFolderStr)) 125 xr = new XmlResponse(XmlResponse.EN_INVALID_FOLDER_NAME); 125 xr = new XmlResponse(XmlResponse.EN_ERROR, 126 Messages.INVALID_CURRENT_FOLDER); 126 127 else { 127 128 CommandHandler command = CommandHandler.getCommand(commandStr); 128 129 ResourceTypeHandler resourceType = ResourceTypeHandler 129 130 .getDefaultResourceType(typeStr); 130 // TODO clean up this folder handling mess somehow?! 131 131 132 String typePath = constructResourceTypeUrl(resourceType, request); 132 133 String typeDirPath = getServletContext().getRealPath(typePath); … … 134 135 135 136 File typeDir = new File(typeDirPath); 136 if (!typeDir.exists()) { 137 typeDir.mkdirs(); 138 logger.debug("Type dir '{}' successfully created", typeDirPath); 139 } 137 UtilsFile.checkDirAndCreate(typeDir); 140 138 141 139 File currentDir = new File(typeDir, currentFolderStr); … … 204 202 205 203 logger.debug("Parameter Command: {}", commandStr); 204 logger.debug("Parameter Type: {}", typeStr); 206 205 logger.debug("Parameter CurrentFolder: {}", currentFolderStr); 207 logger.debug("Parameter Type: {}", typeStr); 208 209 if (!ResourceTypeHandler.isValid(typeStr)) 210 logger.warn("Unknown Type requested: {}", typeStr); 211 212 ResourceTypeHandler fileType = ResourceTypeHandler.getDefaultResourceType(typeStr); 213 206 207 UploadResponse ur; 208 209 // FIXME improve comment 214 210 // if this is a QuickUpload-Request, 'commandStr' and 'currentFolderStr' are empty and have 215 211 // to preset and construct the full resource type path !! … … 217 213 commandStr = "QuickUpload"; 218 214 currentFolderStr = "/"; 219 String quickUploadFolder = constructResponseUrl(fileType, currentFolderStr,220 request);221 File file = new File(getServletContext().getRealPath(quickUploadFolder));222 if (!file.exists())223 file.mkdirs();224 215 } 225 226 UploadResponse ur = null; 227 228 if (Utils.isEmpty(commandStr) || Utils.isEmpty(currentFolderStr) 229 || Utils.isEmpty(typeStr)) 230 ur = UploadResponse.UR_BAD_REQUEST; 216 217 if (!SessionDataHandler.isEnabledForFileUpload(request)) 218 ur = new UploadResponse(UploadResponse.EN_SECURITY_ERROR, null, 219 null, Messages.NOT_AUTHORIZED_FOR_UPLOAD); 231 220 else if (!CommandHandler.isValidForPost(commandStr)) 232 ur = UploadResponse.UR_SECURITY_ERROR; 233 else if (!UtilsFile.isValidPath(currentFolderStr)) { 234 ur = new UploadResponse(UploadResponse.EN_SECURITY_ERROR, null, null, 235 "'currentFolderStr' isn't valid!"); 236 } else if (!SessionDataHandler.isEnabledForFileUpload(request)) { 237 ur = new UploadResponse(UploadResponse.EN_SECURITY_ERROR, null, null, 238 "The current user isn't authorized for uploading files!"); 239 } else { 240 String currentPath = constructResponseUrl(fileType, currentFolderStr, request); 241 File currentDirPath = new File(getServletContext().getRealPath(currentPath)); 242 243 if (!UtilsFile.isValidPath(currentFolderStr) || !currentDirPath.exists()) 244 ur = UploadResponse.UR_BAD_REQUEST; 221 ur = new UploadResponse(UploadResponse.EN_ERROR, null, null, 222 Messages.INVALID_COMMAND); 223 else if (typeStr != null && !ResourceTypeHandler.isValid(typeStr)) 224 ur = new UploadResponse(UploadResponse.EN_ERROR, null, null, 225 Messages.INVALID_TYPE); 226 else if (!UtilsFile.isValidPath(currentFolderStr)) 227 ur = new UploadResponse(UploadResponse.EN_ERROR, null, null, 228 Messages.INVALID_CURRENT_FOLDER); 229 else { 230 ResourceTypeHandler resourceType = ResourceTypeHandler.getDefaultResourceType(typeStr); 231 232 String typePath = constructResourceTypeUrl(resourceType, request); 233 String typeDirPath = getServletContext().getRealPath(typePath); 234 String currentPath = typePath + currentFolderStr; 235 236 File typeDir = new File(typeDirPath); 237 UtilsFile.checkDirAndCreate(typeDir); 238 239 File currentDir = new File(typeDir, currentFolderStr); 240 241 if (!currentDir.exists()) 242 ur = new UploadResponse(UploadResponse.EN_ERROR, null, null, 243 Messages.INVALID_CURRENT_FOLDER); 245 244 else { 246 245 247 246 String newFilename = null; 248 247 FileItemFactory factory = new DiskFileItemFactory(); 249 248 ServletFileUpload upload = new ServletFileUpload(factory); 249 250 250 try { 251 251 252 List<FileItem> items = upload.parseRequest(request); 252 253 … … 258 259 String extension = FilenameUtils.getExtension(filename); 259 260 260 boolean validExtension = ExtensionsHandler.isAllowed(fileType, extension); 261 262 if (!validExtension) 263 ur = UploadResponse.UR_INVALID_EXTENSION; 261 if (!ExtensionsHandler.isAllowed(resourceType, extension)) 262 ur = new UploadResponse(UploadResponse.EN_INVALID_EXTENSION); 264 263 else { 265 264 266 265 // construct an unique file name 267 File pathToSave = new File(currentDir Path, filename);266 File pathToSave = new File(currentDir, filename); 268 267 int counter = 1; 269 268 while (pathToSave.exists()) { 270 newFilename = baseName.concat("(").concat(String.valueOf(counter)) 271 .concat(")").concat(".").concat(extension); 272 pathToSave = new File(currentDirPath, newFilename); 269 newFilename = baseName.concat("(").concat( 270 String.valueOf(counter)).concat(")") 271 .concat(".").concat(extension); 272 pathToSave = new File(currentDir, newFilename); 273 273 counter++; 274 274 } 275 275 276 if (Utils.isEmpty(newFilename)) {276 if (Utils.isEmpty(newFilename)) 277 277 ur = new UploadResponse(UploadResponse.EN_OK, Utils 278 .constructServerAddress(request, currentPath).concat(filename)); 279 } else { 280 ur = new UploadResponse(UploadResponse.EN_RENAMED, Utils 281 .constructServerAddress(request, currentPath).concat( 282 newFilename), newFilename); 283 } 278 .constructServerAddress(request, 279 currentPath).concat(filename)); 280 else 281 ur = new UploadResponse(UploadResponse.EN_RENAMED, 282 Utils.constructServerAddress(request, 283 currentPath).concat(newFilename), 284 newFilename); 284 285 285 286 // secure image check 286 if ( fileType.equals(ResourceTypeHandler.IMAGE) && ConnectorHandler.isSecureImageUploads()) {287 boolean check = UtilsFile.isImage(uplFile.getInputStream());288 if ( check) {287 if (resourceType.equals(ResourceTypeHandler.IMAGE) 288 && ConnectorHandler.isSecureImageUploads()) { 289 if (UtilsFile.isImage(uplFile.getInputStream())) 289 290 uplFile.write(pathToSave); 290 }else {291 else { 291 292 uplFile.delete(); 292 ur = UploadResponse.UR_INVALID_EXTENSION; 293 ur = new UploadResponse( 294 UploadResponse.EN_INVALID_EXTENSION); 293 295 } 294 } else {296 } else 295 297 uplFile.write(pathToSave); 296 }297 298 298 299 } 299 } catch (FileUploadException ex) {300 ur = UploadResponse.UR_BAD_REQUEST;301 300 } catch (Exception e) { 302 ur = UploadResponse.UR_SECURITY_ERROR;301 ur = new UploadResponse(UploadResponse.EN_SECURITY_ERROR); 303 302 } 304 303 } 305 } 304 305 } 306 306 307 out.print(ur); 307 308 out.flush(); … … 322 323 323 324 /** 324 * TODO document me 325 * TODO document me! 325 326 * @param resourceType 326 327 * @param request -
FCKeditor.Java/branches/2.4/src/main/java/net/fckeditor/response/UploadResponse.java
r1571 r1690 21 21 package net.fckeditor.response; 22 22 23 import net.fckeditor.connector.Messages; 23 24 import net.fckeditor.tool.Utils; 24 25 … … 55 56 public static final int EN_ERROR = 1; 56 57 57 /** Error number WARNING */58 public static final int EN_WARNING = 101;59 60 58 /** Error number RENAMED */ 61 59 public static final int EN_RENAMED = 201; … … 66 64 /** Error number SECURITY ERROR */ 67 65 public static final int EN_SECURITY_ERROR = 203; 68 69 /** Error number GENERIC ERROR */70 public static final int EN_GENERIC_NUMBER = -1;71 72 /** HTTP 400 Status text */73 public static final String CM_HTTP_400 = "400 Bad request";74 75 /** UploadResponse INVALID EXTENSION */76 public static final UploadResponse UR_INVALID_EXTENSION = new UploadResponse(77 EN_INVALID_EXTENSION);78 79 /** UploadResponse SECURITY ERROR */80 public static final UploadResponse UR_SECURITY_ERROR = new UploadResponse(81 EN_SECURITY_ERROR);82 83 /** UploadResponse GENERIC NUMBER */84 public static final UploadResponse UR_GENERIC_NUMBER = new UploadResponse(85 EN_GENERIC_NUMBER);86 87 /** UploadResponse BAD REQUEST */88 public static final UploadResponse UR_BAD_REQUEST = new UploadResponse(89 EN_ERROR, null, null, CM_HTTP_400);90 66 91 67 /** -
FCKeditor.Java/branches/2.4/src/main/java/net/fckeditor/response/XmlResponse.java
r1667 r1690 76 76 /** Error number UNKNOWN ERROR */ 77 77 public static final int EN_UKNOWN = 110; 78 79 /** Error message NOT AUTHORIZED FOR BROWSING */80 public static final String CM_NOT_AUTHORIZED = "The current user isn't authorized for file browsing!";81 82 /** Error message INVALID TYPE SUPPLIED */83 public static final String CM_INVALID_TYPE = "Invalid resource type specified";84 85 /** Error message INVALID TYPE SUPPLIED */86 public static final String CM_INVALID_COMMAND = "Invalid command specified";87 78 88 79 /** -
FCKeditor.Java/branches/2.4/src/main/java/net/fckeditor/tool/UtilsFile.java
r1650 r1690 21 21 package net.fckeditor.tool; 22 22 23 import java.io.File; 23 24 import java.io.InputStream; 24 25 25 26 import org.apache.commons.io.FilenameUtils; 26 27 import org.devlib.schmidt.imageinfo.ImageInfo; 28 import org.slf4j.Logger; 29 import org.slf4j.LoggerFactory; 27 30 28 31 import net.fckeditor.handlers.ConnectorHandler; … … 34 37 */ 35 38 public class UtilsFile { 39 40 private static final Logger logger = LoggerFactory.getLogger(UtilsFile.class); 36 41 37 42 /** … … 125 130 return filename.matches("[^\\.]+\\.[^\\.]+"); 126 131 } 132 133 /** 134 * TODO - document me! 135 * @param dir 136 */ 137 public static void checkDirAndCreate(File dir) { 138 if (!dir.exists()) { 139 dir.mkdirs(); 140 logger.debug("Dir '{}' successfully created", dir); 141 } 142 } 127 143 128 144 } -
FCKeditor.Java/branches/2.4/src/test/java/net/fckeditor/tool/UploadResponseTest.java
r1571 r1690 23 23 new UploadResponse("1"); 24 24 } 25 25 /* 26 26 @Test 27 27 public void onlyErrorNumber() throws Exception { … … 31 31 assertEquals(expected, actual.toString()); 32 32 } 33 33 */ 34 34 @Test 35 35 public void fourArguments() throws Exception { … … 47 47 assertEquals(expected, actual.toString()); 48 48 } 49 49 /* 50 50 @Test 51 51 public void customMessage() throws Exception { … … 56 56 assertEquals(expected, actual.toString()); 57 57 } 58 58 */ 59 /* 59 60 @Test 60 61 public void nullArguments() throws Exception { … … 64 65 assertEquals(expected, actual.toString()); 65 66 } 66 67 */ 67 68 }