Changeset 1623
- Timestamp:
- 02/25/08 14:38:34 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
FCKeditor.Java/branches/2.4/src/main/java/net/fckeditor/connector/ConnectorServlet.java
r1607 r1623 56 56 * Servlet to upload and browse files.<br> 57 57 * 58 * This servlet accepts 4 commands used to retrieve and create files and folders 59 * from a serverdirectory. The allowed commands are:58 * This servlet accepts 4 commands used to retrieve and create files and folders from a server 59 * directory. The allowed commands are: 60 60 * <ul> 61 * <li>GetFolders: Retrive the list of directory under the current folder 62 * <li>GetFoldersAndFiles: Retrive the list of files and directory under the 63 * current folder 61 * <li>GetFolders: Retrive the list of directory under the current folder 62 * <li>GetFoldersAndFiles: Retrive the list of files and directory under the current folder 64 63 * <li>CreateFolder: Create a new directory under the current folder 65 64 * <li>FileUpload: Send a new file to the server (must be sent with a POST) … … 71 70 72 71 private static final long serialVersionUID = -5742008970929377161L; 73 private static final Logger logger = LoggerFactory 74 .getLogger(ConnectorServlet.class); 72 private static final Logger logger = LoggerFactory.getLogger(ConnectorServlet.class); 75 73 76 74 /** 77 75 * Initialize the servlet.<br> 78 * The init parameters will be read and used to adjust the defaults of the 79 * {@link ConfigurationHandler} and the {@link ExtensionsHandler}. 76 * The default directory for user files will be constructed. 80 77 */ 81 78 public void init() throws ServletException, IllegalArgumentException { … … 83 80 String realDefaultUserFilesPath = getServletContext().getRealPath( 84 81 ConnectorHandler.getDefaultUserFilesPath()); 85 82 86 83 File defaultUserFilesDir = new File(realDefaultUserFilesPath); 87 File fileDir = new File(defaultUserFilesDir, ResourceTypeHandler.FILE.getPath());88 File flashDir = new File(defaultUserFilesDir, ResourceTypeHandler.FLASH.getPath());89 File imageDir = new File(defaultUserFilesDir, ResourceTypeHandler.IMAGE.getPath());90 File mediaDir = new File(defaultUserFilesDir, ResourceTypeHandler.MEDIA.getPath());91 84 if (!defaultUserFilesDir.exists()) { 92 85 defaultUserFilesDir.mkdirs(); 93 86 } 94 95 fileDir.mkdir();96 flashDir.mkdir();97 imageDir.mkdir();98 mediaDir.mkdir();99 100 87 logger.info("ConnectorServlet successfull initialized!"); 101 88 } … … 107 94 * connector?Command=CommandName&Type=ResourceType&CurrentFolder=FolderPath<br> 108 95 * <br> 109 * It executes the command and then return the results to the client in XML 110 * format. 96 * It executes the command and then return the results to the client in XML format. 111 97 * 112 98 */ 113 99 public void doGet(HttpServletRequest request, HttpServletResponse response) 114 throws ServletException, IOException {100 throws ServletException, IOException { 115 101 logger.debug("Entering ConnectorServlet#doGet"); 116 102 … … 129 115 logger.debug("Parameter Type: {}", typeStr); 130 116 131 String currentPath = constructTypeBasedFolderString(fileType, 132 currentFolderStr, request); 117 String currentPath = constructTypeBasedFolderString(fileType, currentFolderStr, request); 133 118 String currentDirPath = getServletContext().getRealPath(currentPath); 134 119 135 120 File currentDir = new File(currentDirPath); 136 if (!currentDir.exists() 137 && SessionDataHandler.isEnabledForFileBrowsing(request)) { 121 if (!currentDir.exists() && SessionDataHandler.isEnabledForFileBrowsing(request)) { 138 122 currentDir.mkdirs(); 139 123 logger.debug("Dir successfully created: {}", currentDirPath); 140 124 } 141 125 142 XmlResponse xr = new XmlResponse(commandStr, fileType, currentFolderStr, currentPath, request); 143 126 XmlResponse xr = new XmlResponse(commandStr, fileType, currentFolderStr, currentPath, 127 request); 128 144 129 if (!SessionDataHandler.isEnabledForFileBrowsing(request)) { 145 130 xr.setError(1, "The current user isn't authorized for file browsing!"); … … 183 168 * connector?Command=FileUpload&Type=ResourceType&CurrentFolder=FolderPath<br> 184 169 * <br> 185 * It store the file (renaming it in case a file with the same name exists) 186 * and then return anHTML file with a javascript command in it.170 * It store the file (renaming it in case a file with the same name exists) and then return an 171 * HTML file with a javascript command in it. 187 172 * 188 173 */ 189 174 public void doPost(HttpServletRequest request, HttpServletResponse response) 190 throws ServletException, IOException {175 throws ServletException, IOException { 191 176 logger.debug("Entering Connector#doPost"); 192 177 … … 207 192 logger.warn("Unknown Type requested: {}", typeStr); 208 193 194 ResourceTypeHandler fileType = ResourceTypeHandler.getDefaultResourceType(typeStr); 195 196 // if this is a QuickUpload-Request, 'commandStr' and 'currentFolderStr' are empty and have 197 // to preset and construct the full resource type path !! 209 198 if (isEmpty(commandStr) && isEmpty(currentFolderStr)) { 210 199 commandStr = "QuickUpload"; 211 200 currentFolderStr = "/"; 212 } 213 214 ResourceTypeHandler fileType = ResourceTypeHandler.getDefaultResourceType(typeStr); 201 String quickUploadFolder = constructTypeBasedFolderString(fileType, currentFolderStr, request); 202 File file = new File(getServletContext().getRealPath(quickUploadFolder)); 203 if (!file.exists()) 204 file.mkdirs(); 205 } 215 206 216 207 UploadResponse ur = null; 217 208 218 if (!SessionDataHandler.isEnabledForFileUpload(request)) { 219 ur = new UploadResponse(UploadResponse.EN_SECURITY_ERROR, null, 220 null, 221 "The current user isn't authorized for uploading files!"); 222 } else if (isEmpty(commandStr) || isEmpty(currentFolderStr) 223 || isEmpty(typeStr)) 209 if (!Utils.isValidPath(currentFolderStr)) { 210 ur = new UploadResponse(UploadResponse.EN_SECURITY_ERROR, null, null, 211 "'currentFolderStr' isn't valid!"); 212 } else if (!SessionDataHandler.isEnabledForFileUpload(request)) { 213 ur = new UploadResponse(UploadResponse.EN_SECURITY_ERROR, null, null, 214 "The current user isn't authorized for uploading files!"); 215 } else if (isEmpty(commandStr) || isEmpty(currentFolderStr) || isEmpty(typeStr)) 224 216 ur = UploadResponse.UR_BAD_REQUEST; 225 217 else if (!commandStr.matches("(File|Quick)Upload")) … … 227 219 else { 228 220 229 String currentPath = constructTypeBasedFolderString(fileType, 230 currentFolderStr, request); 231 String currentDirPath = getServletContext() 232 .getRealPath(currentPath); 233 234 if (!isValidPath(currentFolderStr) 235 || !(new File(currentDirPath).exists())) 221 String currentPath = constructTypeBasedFolderString(fileType, currentFolderStr, request); 222 String currentDirPath = getServletContext().getRealPath(currentPath); 223 224 if (!isValidPath(currentFolderStr) || !(new File(currentDirPath).exists())) 236 225 ur = UploadResponse.UR_BAD_REQUEST; 237 226 else { … … 249 238 String extension = FilenameUtils.getExtension(filename); 250 239 251 boolean validExtension = ExtensionsHandler.isAllowed( 252 fileType, extension); 240 boolean validExtension = ExtensionsHandler.isAllowed(fileType, extension); 253 241 254 242 if (!validExtension) … … 264 252 int counter = 1; 265 253 while (pathToSave.exists()) { 266 newFilename = baseName + "(" + counter + ")" + "." 267 + extension; 254 newFilename = baseName + "(" + counter + ")" + "." + extension; 268 255 pathToSave = new File(currentDirPath, newFilename); 269 256 counter++; … … 272 259 uplFile.write(pathToSave); 273 260 if (isEmpty(newFilename)) { 274 ur = new UploadResponse(UploadResponse.EN_OK, 275 Utils.constructServerAddress(request, currentPath)276 + filename);261 ur = new UploadResponse(UploadResponse.EN_OK, Utils 262 .constructServerAddress(request, currentPath) 263 + filename); 277 264 } else { 278 ur = new UploadResponse(UploadResponse.EN_RENAMED, 279 Utils.constructServerAddress(request, currentPath)280 + newFilename, newFilename);265 ur = new UploadResponse(UploadResponse.EN_RENAMED, Utils 266 .constructServerAddress(request, currentPath) 267 + newFilename, newFilename); 281 268 } 282 269 } … … 296 283 297 284 private String constructTypeBasedFolderString(final ResourceTypeHandler fileType, 298 final String currentFolderString, final HttpServletRequest request) {285 final String currentFolderString, final HttpServletRequest request) { 299 286 StringBuffer sb = new StringBuffer(); 300 287 sb.append(ConnectorHandler.getUserFilesPath(request)); … … 303 290 return replaceAll(sb.toString(), "//", "/"); 304 291 } 305 292 306 293 }
Note: See TracChangeset
for help on using the changeset viewer.
