Changeset 1647
- Timestamp:
- 2008-02-28 02:12:10 (9 months ago)
- Location:
- FCKeditor.Java/branches/2.4/src
- Files:
-
- 2 added
- 4 modified
-
main/java/net/fckeditor/connector/ConnectorServlet.java (modified) (3 diffs)
-
main/java/net/fckeditor/handlers/CommandHandler.java (added)
-
main/java/net/fckeditor/response/XmlResponse.java (modified) (8 diffs)
-
main/java/net/fckeditor/tool/UtilsFile.java (modified) (4 diffs)
-
test/java/net/fckeditor/handlers/CommandHandlerTest.java (added)
-
test/java/net/fckeditor/tool/UtilsFileTest.java (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
FCKeditor.Java/branches/2.4/src/main/java/net/fckeditor/connector/ConnectorServlet.java
r1646 r1647 31 31 import javax.servlet.http.HttpServletResponse; 32 32 33 import net.fckeditor.handlers.CommandHandler; 33 34 import net.fckeditor.handlers.ExtensionsHandler; 34 35 import net.fckeditor.handlers.ConnectorHandler; … … 105 106 String typeStr = request.getParameter("Type"); 106 107 String currentFolderStr = request.getParameter("CurrentFolder"); 107 ResourceTypeHandler fileType = ResourceTypeHandler.getDefaultResourceType(typeStr);108 108 109 109 logger.debug("Parameter Command: {}", commandStr); 110 logger.debug("Parameter Type: {}", typeStr); 110 111 logger.debug("Parameter CurrentFolder: {}", currentFolderStr); 111 logger.debug("Parameter Type: {}", typeStr); 112 113 String currentPath = constructTypeBasedFolderString(fileType, currentFolderStr, request); 114 String currentDirPath = getServletContext().getRealPath(currentPath); 115 116 File currentDir = new File(currentDirPath); 117 if (!currentDir.exists() && SessionDataHandler.isEnabledForFileBrowsing(request)) { 118 currentDir.mkdirs(); 119 logger.debug("Dir successfully created: {}", currentDirPath); 120 } 121 122 XmlResponse xr = new XmlResponse(commandStr, fileType, currentFolderStr, currentPath, 123 request); 124 125 if (!SessionDataHandler.isEnabledForFileBrowsing(request)) { 126 xr.setError(XmlResponse.EN_ERROR, XmlResponse.CM_NOT_AUTHORIZED); 127 } else if (commandStr.equals("GetFolders")) { 128 xr.setFolders(currentDir); 129 } else if (commandStr.equals("GetFoldersAndFiles")) { 130 xr.setFolders(currentDir); 131 xr.setFiles(currentDir); 132 } else if (commandStr.equals("CreateFolder")) { 133 String newFolderStr = UtilsFile.sanitizeFolderName(request.getParameter("NewFolderName")); 134 File newFolder = new File(currentDir, newFolderStr); 135 int errorNumber = XmlResponse.EN_UKNOWN; 136 137 if (newFolder.exists()) { 138 errorNumber = XmlResponse.EN_ALREADY_EXISTS; 139 } else { 140 try { 141 errorNumber = (newFolder.mkdir()) ? XmlResponse.EN_OK : XmlResponse.EN_INVALID; 142 } catch (SecurityException e) { 143 errorNumber = XmlResponse.EN_SECURITY_ERROR; 112 113 XmlResponse xr; 114 115 if (!SessionDataHandler.isEnabledForFileBrowsing(request)) 116 xr = new XmlResponse(XmlResponse.EN_ERROR, 117 XmlResponse.CM_NOT_AUTHORIZED); 118 else if (!CommandHandler.isValidForGet(commandStr)) 119 xr = new XmlResponse(XmlResponse.EN_ERROR, 120 XmlResponse.CM_INVALID_COMMAND); 121 else if (typeStr != null && !ResourceTypeHandler.isValid(typeStr)) 122 xr = new XmlResponse(XmlResponse.EN_ERROR, 123 XmlResponse.CM_INVALID_TYPE); 124 else if (!UtilsFile.isValidPath(currentFolderStr)) 125 xr = new XmlResponse(XmlResponse.EN_INVALID_FOLDER_NAME); 126 else { 127 CommandHandler command = CommandHandler.getCommand(commandStr); 128 ResourceTypeHandler resourceType = ResourceTypeHandler 129 .getDefaultResourceType(typeStr); 130 // TODO clean up this folder handling mess somehow?! 131 String typePath = constructSomething(resourceType, request); 132 String typeDirPath = getServletContext().getRealPath(typePath); 133 String currentPath = typePath + currentFolderStr; 134 135 File typeDir = new File(typeDirPath); 136 137 if (!typeDir.exists()) { 138 typeDir.mkdirs(); 139 // FIXME log typePath or typeDirPath??? 140 logger.debug("Type dir '{}' successfully created", typePath); 141 } 142 143 File currentDir = new File(typeDir, currentFolderStr); 144 145 if (!currentDir.exists()) 146 xr = new XmlResponse(XmlResponse.EN_INVALID_FOLDER_NAME); 147 else { 148 149 xr = new XmlResponse(command, resourceType, currentFolderStr, 150 currentPath, request); 151 152 if (command.equals(CommandHandler.GET_FOLDERS)) 153 xr.setFolders(currentDir); 154 else if (command.equals(CommandHandler.GET_FOLDERS_AND_FILES)) 155 xr.setFoldersAndFiles(currentDir); 156 else if (command.equals(CommandHandler.CREATE_FOLDER)) { 157 String newFolderStr = UtilsFile.sanitizeFolderName(request 158 .getParameter("NewFolderName")); 159 logger.debug("Parameter NewFolderName: {}", newFolderStr); 160 161 File newFolder = new File(currentDir, newFolderStr); 162 int errorNumber = XmlResponse.EN_UKNOWN; 163 164 if (newFolder.exists()) { 165 errorNumber = XmlResponse.EN_ALREADY_EXISTS; 166 } else { 167 try { 168 errorNumber = (newFolder.mkdir()) ? XmlResponse.EN_OK 169 : XmlResponse.EN_INVALID_FOLDER_NAME; 170 } catch (SecurityException e) { 171 errorNumber = XmlResponse.EN_SECURITY_ERROR; 172 } 173 } 174 xr.setError(errorNumber); 144 175 } 145 176 } 146 xr.setError(errorNumber);147 177 } 148 178 … … 289 319 sb.append(ConnectorHandler.getUserFilesPath(request)); 290 320 sb.append(fileType.getPath()); 291 sb.append(currentFolderString); 321 if (Utils.isNotEmpty(currentFolderString)) 322 sb.append(currentFolderString); 292 323 return Utils.replaceAll(sb.toString(), "//", "/"); 293 324 } 325 326 /** 327 * TODO document me 328 * FIXME give me a more proper name 329 * @param resourceType 330 * @param request 331 * @return 332 */ 333 private String constructSomething(final ResourceTypeHandler resourceType, final HttpServletRequest request) { 334 return constructTypeBasedFolderString(resourceType, null, request); 335 } 294 336 295 337 } -
FCKeditor.Java/branches/2.4/src/main/java/net/fckeditor/response/XmlResponse.java
r1639 r1647 35 35 import javax.xml.transform.stream.StreamResult; 36 36 37 import net.fckeditor.handlers.CommandHandler; 37 38 import net.fckeditor.handlers.ResourceTypeHandler; 38 39 import net.fckeditor.tool.Utils; … … 43 44 import org.w3c.dom.Element; 44 45 46 /** 47 * static error objects won't probably provided due to performance reasons of 48 * Document instance creation 49 * 50 * TODO document me! 51 * @author mosipov 52 * 53 */ 45 54 public class XmlResponse { 46 55 … … 59 68 public static final int EN_ALREADY_EXISTS = 101; 60 69 61 /** Error number INVALID */62 public static final int EN_INVALID = 102;70 /** Error number INVALID FOLDER NAME */ 71 public static final int EN_INVALID_FOLDER_NAME = 102; 63 72 64 73 /** Error number SECURITY ERROR */ … … 70 79 /** Error message NOT AUTHORIZED FOR BROWSING */ 71 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"; 72 87 73 88 /** … … 77 92 * TODO 78 93 */ 79 public XmlResponse( Stringcommand, ResourceTypeHandler type, String currentFolder,94 public XmlResponse(CommandHandler command, ResourceTypeHandler type, String currentFolder, 80 95 String constructedUrl, HttpServletRequest request) { 81 96 … … 91 106 Element root = document.createElement("Connector"); 92 107 document.appendChild(root); 93 root.setAttribute("command", command );108 root.setAttribute("command", command.toString()); 94 109 root.setAttribute("resourceType", type.toString()); 95 110 … … 101 116 root.appendChild(currentFolderElement); 102 117 118 } 119 120 /** 121 * TODO document me! 122 * @param number 123 * @param text 124 */ 125 public XmlResponse(int number, String text) { 126 try { 127 DocumentBuilderFactory factory = DocumentBuilderFactory 128 .newInstance(); 129 DocumentBuilder builder = factory.newDocumentBuilder(); 130 document = builder.newDocument(); 131 } catch (ParserConfigurationException e) { 132 throw new RuntimeException(e); 133 } 134 135 Element root = document.createElement("Connector"); 136 document.appendChild(root); 137 setError(number, text); 138 } 139 140 /*** 141 * TODO document me! 142 * @param number 143 */ 144 public XmlResponse(int number) { 145 this(number, null); 103 146 } 104 147 … … 161 204 } 162 205 } 163 206 207 public void setFoldersAndFiles(File dir) { 208 setFolders(dir); 209 setFiles(dir); 210 } 211 164 212 @Override 165 213 public String toString() { -
FCKeditor.Java/branches/2.4/src/main/java/net/fckeditor/tool/UtilsFile.java
r1645 r1647 40 40 * 41 41 * @param fileName 42 * @return folder name where \ / | : ? * " < > replaced by '_'42 * @return folder name where \ / | : ? * " < > 'control chars' replaced by '_' 43 43 */ 44 44 public static String sanitizeFileName(final String fileName) { … … 52 52 53 53 // Remove \ / | : ? * " < > with _ 54 return name.replaceAll("\\/|\\/|\\||:|\\?|\\*|\"|<|> ", "_");54 return name.replaceAll("\\/|\\/|\\||:|\\?|\\*|\"|<|>|[\u0000-\u001F]|\u007F", "_"); 55 55 } 56 56 … … 59 59 * 60 60 * @param folderName 61 * @return folder name where . \ / | : ? * " < > replaced by '_'61 * @return folder name where . \ / | : ? * " < > 'control chars' replaced by '_' 62 62 */ 63 63 public static String sanitizeFolderName(final String folderName) { … … 68 68 69 69 // Remove . \ / | : ? * " < > with _ 70 return folderName.replaceAll("\\.|\\/|\\/|\\||:|\\?|\\*|\"|<|> ", "_");70 return folderName.replaceAll("\\.|\\/|\\/|\\||:|\\?|\\*|\"|<|>|[\u0000-\u001F]|\u007F", "_"); 71 71 } 72 72 -
FCKeditor.Java/branches/2.4/src/test/java/net/fckeditor/tool/UtilsFileTest.java
r1645 r1647 163 163 assertEquals("a_b_c_d_e_f_g_h_i_", UtilsFile.sanitizeFolderName("a.b|c<d>e:f?g*h<i>")); 164 164 } 165 166 @Test 167 public void sanitizeFolder02() { 168 assertEquals("a_b_c_d_e_f_g_h_i_", UtilsFile.sanitizeFolderName("a.b|c\u007Fd>e:f\u0005g*h<i>")); 169 } 165 170 166 171 @Test … … 174 179 } 175 180 181 @Test 182 public void sanitizeFile03() { 183 assertEquals("b_c_d_e_f_g_h_i_", UtilsFile.sanitizeFileName("b|c\u007Fd>e:f\u0005g*h<i>")); 184 } 176 185 }