[exo-jcr-commits] exo-jcr SVN: r2261 - in jcr/branches/1.14.x: exo.jcr.component.ftp/src/main/java/org/exoplatform/services/ftp/command and 2 other directories.

do-not-reply at jboss.org do-not-reply at jboss.org
Fri Apr 9 10:25:13 EDT 2010


Author: areshetnyak
Date: 2010-04-09 10:25:13 -0400 (Fri, 09 Apr 2010)
New Revision: 2261

Modified:
   jcr/branches/1.14.x/exo.jcr.component.ftp/src/main/java/org/exoplatform/services/ftp/FtpTextUtils.java
   jcr/branches/1.14.x/exo.jcr.component.ftp/src/main/java/org/exoplatform/services/ftp/command/CmdMkd.java
   jcr/branches/1.14.x/exo.jcr.component.ftp/src/main/java/org/exoplatform/services/ftp/command/CmdStor.java
   jcr/branches/1.14.x/exo.jcr.component.ftp/src/main/java/org/exoplatform/services/ftp/config/FtpConfig.java
   jcr/branches/1.14.x/exo.jcr.component.ftp/src/main/java/org/exoplatform/services/ftp/config/FtpConfigImpl.java
   jcr/branches/1.14.x/exo.jcr.framework.ftpclient/src/test/java/org/exoplatform/frameworks/ftpclient/cmdtests/MKDTest.java
   jcr/branches/1.14.x/exo.jcr.framework.ftpclient/src/test/java/org/exoplatform/frameworks/ftpclient/cmdtests/STORTest.java
Log:
EXOJCR-634 : The "replace forbidden chars" mechanism was implemented.
By default "replace forbidden chars" mechanism is enabled.
By default the forbidden chars is ':', '[', ']', '*', ''', '"', '|'.
By default the forbidden chars will be replaced to '_'.

How to configuration in ftp config when need change defaults config :

<value-param>
<name>replace-forbidden-chars</name>
<value>true</value>
</value-param>

<value-param>
<name>forbidden-chars</name>
<value>:[]*'"|</value>
</value-param>

<value-param>
<name>replace-char</name>
<value>_</value>
</value-param>

Modified: jcr/branches/1.14.x/exo.jcr.component.ftp/src/main/java/org/exoplatform/services/ftp/FtpTextUtils.java
===================================================================
--- jcr/branches/1.14.x/exo.jcr.component.ftp/src/main/java/org/exoplatform/services/ftp/FtpTextUtils.java	2010-04-09 14:16:55 UTC (rev 2260)
+++ jcr/branches/1.14.x/exo.jcr.component.ftp/src/main/java/org/exoplatform/services/ftp/FtpTextUtils.java	2010-04-09 14:25:13 UTC (rev 2261)
@@ -73,4 +73,31 @@
       return result;
    }
 
+   public static String replaceForbiddenChars(String strVal, String forbiddenChars, char replaceChar)
+   {
+      char[] result = new char[strVal.length()];
+
+      for (int i = 0; i < strVal.length(); i++)
+      {
+         boolean replaced = false;
+
+         for (int j = 0; j < forbiddenChars.length(); j++)
+         {
+            if (strVal.charAt(i) == forbiddenChars.charAt(j))
+            {
+               result[i] = replaceChar;
+               replaced = true;
+               break;
+            }
+         }
+
+         if (!replaced)
+         {
+            result[i] = strVal.charAt(i);
+         }
+      }
+
+      return new String(result);
+   }
+
 }

Modified: jcr/branches/1.14.x/exo.jcr.component.ftp/src/main/java/org/exoplatform/services/ftp/command/CmdMkd.java
===================================================================
--- jcr/branches/1.14.x/exo.jcr.component.ftp/src/main/java/org/exoplatform/services/ftp/command/CmdMkd.java	2010-04-09 14:16:55 UTC (rev 2260)
+++ jcr/branches/1.14.x/exo.jcr.component.ftp/src/main/java/org/exoplatform/services/ftp/command/CmdMkd.java	2010-04-09 14:25:13 UTC (rev 2261)
@@ -19,6 +19,8 @@
 package org.exoplatform.services.ftp.command;
 
 import org.exoplatform.services.ftp.FtpConst;
+import org.exoplatform.services.ftp.FtpTextUtils;
+import org.exoplatform.services.ftp.config.FtpConfig;
 import org.exoplatform.services.log.ExoLogger;
 import org.exoplatform.services.log.Log;
 
@@ -62,6 +64,9 @@
          reply(String.format(FtpConst.Replyes.REPLY_550, srcPath));
          return;
       }
+      
+      FtpConfig ftpConfig = clientSession().getFtpServer().getConfiguration();
+      boolean replaceForbiddenChars =ftpConfig.isReplaceForbiddenChars();
 
       try
       {
@@ -73,6 +78,11 @@
          {
             String curPathName = newPath.get(i);
 
+            if (replaceForbiddenChars)
+            {
+              curPathName = FtpTextUtils.replaceForbiddenChars(curPathName, ftpConfig.getForbiddenChars(), ftpConfig.getReplaceChar());
+            }
+
             if (parentNode.hasNode(curPathName))
             {
                parentNode = parentNode.getNode(curPathName);

Modified: jcr/branches/1.14.x/exo.jcr.component.ftp/src/main/java/org/exoplatform/services/ftp/command/CmdStor.java
===================================================================
--- jcr/branches/1.14.x/exo.jcr.component.ftp/src/main/java/org/exoplatform/services/ftp/command/CmdStor.java	2010-04-09 14:16:55 UTC (rev 2260)
+++ jcr/branches/1.14.x/exo.jcr.component.ftp/src/main/java/org/exoplatform/services/ftp/command/CmdStor.java	2010-04-09 14:25:13 UTC (rev 2261)
@@ -20,6 +20,7 @@
 
 import org.exoplatform.commons.utils.MimeTypeResolver;
 import org.exoplatform.services.ftp.FtpConst;
+import org.exoplatform.services.ftp.FtpTextUtils;
 import org.exoplatform.services.ftp.config.FtpConfig;
 import org.exoplatform.services.jcr.util.IdGenerator;
 import org.exoplatform.services.log.ExoLogger;
@@ -88,6 +89,16 @@
       try
       {
          ArrayList<String> newPath = clientSession().getFullPath(fileName);
+         
+         FtpConfig ftpConfig = clientSession().getFtpServer().getConfiguration();
+         if (ftpConfig.isReplaceForbiddenChars())
+         {
+            String fName = newPath.get(newPath.size()-1);
+            String newfName = FtpTextUtils.replaceForbiddenChars(fName, ftpConfig.getForbiddenChars(), ftpConfig.getReplaceChar());
+            
+            fileName  = fileName.substring(0, fileName.indexOf(fName)) + newfName;
+         }
+         
          Session curSession = clientSession().getSession(newPath.get(0));
 
          Node resourceNode = getExistedFileNode(curSession, fileName);

Modified: jcr/branches/1.14.x/exo.jcr.component.ftp/src/main/java/org/exoplatform/services/ftp/config/FtpConfig.java
===================================================================
--- jcr/branches/1.14.x/exo.jcr.component.ftp/src/main/java/org/exoplatform/services/ftp/config/FtpConfig.java	2010-04-09 14:16:55 UTC (rev 2260)
+++ jcr/branches/1.14.x/exo.jcr.component.ftp/src/main/java/org/exoplatform/services/ftp/config/FtpConfig.java	2010-04-09 14:25:13 UTC (rev 2261)
@@ -61,4 +61,9 @@
 
    PortalContainer getPortalContainer();
 
+   boolean isReplaceForbiddenChars();
+   
+   String getForbiddenChars();
+   
+   char getReplaceChar();
 }

Modified: jcr/branches/1.14.x/exo.jcr.component.ftp/src/main/java/org/exoplatform/services/ftp/config/FtpConfigImpl.java
===================================================================
--- jcr/branches/1.14.x/exo.jcr.component.ftp/src/main/java/org/exoplatform/services/ftp/config/FtpConfigImpl.java	2010-04-09 14:16:55 UTC (rev 2260)
+++ jcr/branches/1.14.x/exo.jcr.component.ftp/src/main/java/org/exoplatform/services/ftp/config/FtpConfigImpl.java	2010-04-09 14:25:13 UTC (rev 2261)
@@ -91,11 +91,58 @@
     * "download-speed-limit".
     */
    public static final String INIT_PARAM_DOWNLOAD_SPEED_LIMIT = "download-speed-limit";
+   
+   /**
+    * replace-forbidden-chars
+    */
+   public static final String INIT_PARAM_REPLACE_FORBIDDEN_CHARS = "replace-forbidden-chars";
+   
+   /**
+    * forbidden-chars 
+    */
+   public static final String INIT_PARAM_FORBIDDEN_CHARS      = "forbidden-chars"; 
+   
+   /**
+    * replace-char
+    */
+   public static final String INIT_PARAM_REPLACE_CHAR         = "replace-char";
 
    /**
     * "timeout".
     */
    public static final String INIT_PARAM_TIME_OUT = "timeout";
+   
+   /**
+    * According JCR specification  JSR-170 .
+    * See 4.6 Path Syntax:
+    * Any Unicode character except: '/', ':', '[', ']', '*', ''', '"', '|' 
+    */
+   public static final String DEFAULT_JCR_FORBIDDEN_CHARS     = ":[]*'\"|"; 
+      
+   /**
+    * The all forbidden chars will replaced '_' by default.
+    */
+   public static final char DEFAULT_REPLACE_CHAR              = '_';
+     
+   /**
+    * The replace forbidden chars is enable by default.
+    */
+   public static final boolean DEFAULT_REPLACE_FORBIDDEN_CHARS = true;
+   
+   /**
+    * Forbidden chars.
+    */
+   public String              _forbiddenChars                 = DEFAULT_JCR_FORBIDDEN_CHARS;
+   
+   /**
+    * Replace char.
+    */
+   public char                _replaceChar                    = DEFAULT_REPLACE_CHAR;
+   
+   /**
+    * Replace forbidden chars.
+    */
+   public boolean             _replaceForbiddenChars          = DEFAULT_REPLACE_FORBIDDEN_CHARS;
 
    /**
     * Command port.
@@ -264,7 +311,25 @@
          _needTimeOut = true;
          _timeOutValue = new Integer(pTimeOut.getValue());
       }
+      
+      ValueParam pReplaceForbiddenChars = params.getValueParam(INIT_PARAM_REPLACE_FORBIDDEN_CHARS);
+      if (pReplaceForbiddenChars != null)
+      {
+         _replaceForbiddenChars = new Boolean(pReplaceForbiddenChars.getValue());
+      }
 
+      ValueParam pForbiddenChars = params.getValueParam(INIT_PARAM_FORBIDDEN_CHARS);
+      if (pForbiddenChars != null)
+      {
+         _forbiddenChars = pForbiddenChars.getValue();
+      }
+
+      ValueParam pReplaceChar = params.getValueParam(INIT_PARAM_REPLACE_CHAR);
+      if (pReplaceChar != null)
+      {
+         _replaceChar = pReplaceChar.getValue().charAt(0);
+      }
+
       ExoContainer container = context.getContainer();
       if (container instanceof PortalContainer)
       {
@@ -300,6 +365,10 @@
          {
             log.debug(INIT_PARAM_TIME_OUT + ".value = " + _timeOutValue);
          }
+         
+         log.debug(INIT_PARAM_REPLACE_FORBIDDEN_CHARS + " = " + _replaceForbiddenChars);
+         log.debug(INIT_PARAM_FORBIDDEN_CHARS + " = " + _forbiddenChars);
+         log.debug(INIT_PARAM_REPLACE_CHAR + " = " + _replaceChar);
       }
 
    }
@@ -383,5 +452,20 @@
    {
       return _portalContainer;
    }
+   
+   public String getForbiddenChars()
+   {
+     return _forbiddenChars;
+   }
+    
+   public char getReplaceChar()
+   {
+     return _replaceChar;
+   }
+    
+   public boolean isReplaceForbiddenChars()
+   {
+     return _replaceForbiddenChars;
+   }
 
 }

Modified: jcr/branches/1.14.x/exo.jcr.framework.ftpclient/src/test/java/org/exoplatform/frameworks/ftpclient/cmdtests/MKDTest.java
===================================================================
--- jcr/branches/1.14.x/exo.jcr.framework.ftpclient/src/test/java/org/exoplatform/frameworks/ftpclient/cmdtests/MKDTest.java	2010-04-09 14:16:55 UTC (rev 2260)
+++ jcr/branches/1.14.x/exo.jcr.framework.ftpclient/src/test/java/org/exoplatform/frameworks/ftpclient/cmdtests/MKDTest.java	2010-04-09 14:25:13 UTC (rev 2261)
@@ -89,5 +89,38 @@
       client.close();
       log.info("Complete.\r\n");
    }
+   
+   public void testForbiddenCharsMKD() throws Exception {
+      log.info("Test...");
 
+      FtpClientSession client = FtpTestConfig.getTestFtpClient();
+      client.connect();
+
+      {
+        CmdUser cmdUser = new CmdUser(FtpTestConfig.USER_ID);
+        assertEquals(FtpConst.Replyes.REPLY_331, client.executeCommand(cmdUser));
+      }
+
+      {
+        CmdPass cmdPass = new CmdPass(FtpTestConfig.USER_PASS);
+        assertEquals(FtpConst.Replyes.REPLY_230, client.executeCommand(cmdPass));
+      }
+
+      {
+        CmdCwd cmdCwd = new CmdCwd("production");
+        assertEquals(FtpConst.Replyes.REPLY_250, client.executeCommand(cmdCwd));
+
+        String folderName = "test_folder_123" + ":[]*'\"|" + "123";
+
+        CmdMkd cmdMkd = new CmdMkd(folderName);
+        assertEquals(FtpConst.Replyes.REPLY_257, client.executeCommand(cmdMkd));
+        
+        CmdRmd cmdRmd = new CmdRmd("test_folder_123" + "_______" + "123");
+        assertEquals(FtpConst.Replyes.REPLY_250, client.executeCommand(cmdRmd));
+      }
+
+      client.close();
+      log.info("Complete.\r\n");
+    }
+
 }

Modified: jcr/branches/1.14.x/exo.jcr.framework.ftpclient/src/test/java/org/exoplatform/frameworks/ftpclient/cmdtests/STORTest.java
===================================================================
--- jcr/branches/1.14.x/exo.jcr.framework.ftpclient/src/test/java/org/exoplatform/frameworks/ftpclient/cmdtests/STORTest.java	2010-04-09 14:16:55 UTC (rev 2260)
+++ jcr/branches/1.14.x/exo.jcr.framework.ftpclient/src/test/java/org/exoplatform/frameworks/ftpclient/cmdtests/STORTest.java	2010-04-09 14:25:13 UTC (rev 2261)
@@ -120,5 +120,42 @@
       client.close();
       log.info("Complete.\r\n");
    }
+   
+   public void testForbiddenChars_STOR() throws Exception {
+      log.info("Test...");
 
+      FtpClientSession client = FtpTestConfig.getTestFtpClient();
+      client.connect();
+
+      byte[] fileContent = "THIS FILE CONTENT".getBytes();
+
+      // login
+      {
+        assertEquals(FtpConst.Replyes.REPLY_331,
+                     client.executeCommand(new CmdUser(FtpTestConfig.USER_ID)));
+        assertEquals(FtpConst.Replyes.REPLY_230,
+                     client.executeCommand(new CmdPass(FtpTestConfig.USER_PASS)));
+      }
+
+      String fileName = "test_stor_file_" + ":[]*'\"|" + ".txt";
+
+      // desired reply - 125 Data connection already open; Transfer starting
+      // 226 Transfer complete
+      {
+        assertEquals(FtpConst.Replyes.REPLY_250, client.executeCommand(new CmdCwd("production")));
+        assertEquals(FtpConst.Replyes.REPLY_227, client.executeCommand(new CmdPasv()));
+
+        CmdStor cmdStor = new CmdStor(fileName);
+        cmdStor.setFileContent(fileContent);
+        assertEquals(FtpConst.Replyes.REPLY_226, client.executeCommand(cmdStor));
+      }
+
+      {
+        assertEquals(FtpConst.Replyes.REPLY_250, client.executeCommand(new CmdDele("test_stor_file_" + "_______" + ".txt")));
+      }
+
+      client.close();
+      log.info("Complete.\r\n");
+    }
+
 }



More information about the exo-jcr-commits mailing list