Changeset 1647

Show
Ignore:
Timestamp:
2008-02-28 02:12:10 (9 months ago)
Author:
mosipov
Message:

ASSIGNED - #1896: [Connector] Reworking checking of type parameter

Location:
FCKeditor.Java/branches/2.4/src
Files:
2 added
4 modified

Legend:

Unmodified
Added
Removed
  • FCKeditor.Java/branches/2.4/src/main/java/net/fckeditor/connector/ConnectorServlet.java

    r1646 r1647  
    3131import javax.servlet.http.HttpServletResponse; 
    3232 
     33import net.fckeditor.handlers.CommandHandler; 
    3334import net.fckeditor.handlers.ExtensionsHandler; 
    3435import net.fckeditor.handlers.ConnectorHandler; 
     
    105106                String typeStr = request.getParameter("Type"); 
    106107                String currentFolderStr = request.getParameter("CurrentFolder"); 
    107                 ResourceTypeHandler fileType = ResourceTypeHandler.getDefaultResourceType(typeStr); 
    108108 
    109109                logger.debug("Parameter Command: {}", commandStr); 
     110                logger.debug("Parameter Type: {}", typeStr); 
    110111                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); 
    144175                                } 
    145176                        } 
    146                         xr.setError(errorNumber); 
    147177                } 
    148178 
     
    289319                sb.append(ConnectorHandler.getUserFilesPath(request)); 
    290320                sb.append(fileType.getPath()); 
    291                 sb.append(currentFolderString); 
     321                if (Utils.isNotEmpty(currentFolderString)) 
     322                        sb.append(currentFolderString); 
    292323                return Utils.replaceAll(sb.toString(), "//", "/"); 
    293324        } 
     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        } 
    294336 
    295337} 
  • FCKeditor.Java/branches/2.4/src/main/java/net/fckeditor/response/XmlResponse.java

    r1639 r1647  
    3535import javax.xml.transform.stream.StreamResult; 
    3636 
     37import net.fckeditor.handlers.CommandHandler; 
    3738import net.fckeditor.handlers.ResourceTypeHandler; 
    3839import net.fckeditor.tool.Utils; 
     
    4344import org.w3c.dom.Element; 
    4445 
     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 */ 
    4554public class XmlResponse { 
    4655 
     
    5968        public static final int EN_ALREADY_EXISTS = 101; 
    6069 
    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; 
    6372 
    6473        /** Error number SECURITY ERROR */ 
     
    7079        /** Error message NOT AUTHORIZED FOR BROWSING */ 
    7180        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"; 
    7287 
    7388        /** 
     
    7792         *            TODO 
    7893         */ 
    79         public XmlResponse(String command, ResourceTypeHandler type, String currentFolder, 
     94        public XmlResponse(CommandHandler command, ResourceTypeHandler type, String currentFolder, 
    8095                        String constructedUrl, HttpServletRequest request) { 
    8196 
     
    91106                Element root = document.createElement("Connector"); 
    92107                document.appendChild(root); 
    93                 root.setAttribute("command", command); 
     108                root.setAttribute("command", command.toString()); 
    94109                root.setAttribute("resourceType", type.toString()); 
    95110 
     
    101116                root.appendChild(currentFolderElement); 
    102117 
     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); 
    103146        } 
    104147 
     
    161204                } 
    162205        } 
    163  
     206         
     207        public void setFoldersAndFiles(File dir) { 
     208                setFolders(dir); 
     209                setFiles(dir); 
     210        } 
     211         
    164212        @Override 
    165213        public String toString() { 
  • FCKeditor.Java/branches/2.4/src/main/java/net/fckeditor/tool/UtilsFile.java

    r1645 r1647  
    4040     *  
    4141     * @param fileName 
    42      * @return folder name where \ / | : ? * " < > replaced by '_' 
     42     * @return folder name where \ / | : ? * " < > 'control chars' replaced by '_' 
    4343     */ 
    4444    public static String sanitizeFileName(final String fileName) { 
     
    5252     
    5353        // Remove \ / | : ? * " < > with _ 
    54         return name.replaceAll("\\/|\\/|\\||:|\\?|\\*|\"|<|>", "_"); 
     54        return name.replaceAll("\\/|\\/|\\||:|\\?|\\*|\"|<|>|[\u0000-\u001F]|\u007F", "_"); 
    5555    } 
    5656 
     
    5959     *  
    6060     * @param folderName 
    61      * @return folder name where . \ / | : ? * &quot; &lt; &gt; replaced by '_' 
     61     * @return folder name where . \ / | : ? * &quot; &lt; &gt; 'control chars' replaced by '_' 
    6262     */ 
    6363    public static String sanitizeFolderName(final String folderName) { 
     
    6868     
    6969        // Remove . \ / | : ? * " < > with _ 
    70         return folderName.replaceAll("\\.|\\/|\\/|\\||:|\\?|\\*|\"|<|>", "_"); 
     70        return folderName.replaceAll("\\.|\\/|\\/|\\||:|\\?|\\*|\"|<|>|[\u0000-\u001F]|\u007F", "_"); 
    7171    } 
    7272 
  • FCKeditor.Java/branches/2.4/src/test/java/net/fckeditor/tool/UtilsFileTest.java

    r1645 r1647  
    163163        assertEquals("a_b_c_d_e_f_g_h_i_", UtilsFile.sanitizeFolderName("a.b|c<d>e:f?g*h<i>")); 
    164164    } 
     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        } 
    165170 
    166171        @Test 
     
    174179    } 
    175180 
     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        } 
    176185}