extensions/net.sf.basedb.relax/trunk/src/net/sf/basedb/relax/bash/ScriptUtil.java

Code
Comments
Other
Rev Date Author Line
4577 19 Sep 17 nicklas 1 package net.sf.basedb.relax.bash;
4577 19 Sep 17 nicklas 2
4577 19 Sep 17 nicklas 3 import net.sf.basedb.core.InvalidDataException;
4577 19 Sep 17 nicklas 4
4577 19 Sep 17 nicklas 5 /**
4577 19 Sep 17 nicklas 6   Utility methods for script generation.
4577 19 Sep 17 nicklas 7
4577 19 Sep 17 nicklas 8   @author nicklas
4577 19 Sep 17 nicklas 9 */
4577 19 Sep 17 nicklas 10 public final class ScriptUtil 
4577 19 Sep 17 nicklas 11 {
4577 19 Sep 17 nicklas 12   
4577 19 Sep 17 nicklas 13   // No instances
4577 19 Sep 17 nicklas 14   private ScriptUtil()
4577 19 Sep 17 nicklas 15   {}
4577 19 Sep 17 nicklas 16   
4577 19 Sep 17 nicklas 17
4577 19 Sep 17 nicklas 18   /**
4577 19 Sep 17 nicklas 19     We only allow letters, numbers, dot, underscore and forward slash in path names.
4577 19 Sep 17 nicklas 20     The path must be at least two levels and not contain a double dot (..).
4577 19 Sep 17 nicklas 21   */
4577 19 Sep 17 nicklas 22   public static String checkValidPath(String path, boolean isDirectory, boolean twoLevels)
4577 19 Sep 17 nicklas 23   {
4577 19 Sep 17 nicklas 24     if (path == null)
4577 19 Sep 17 nicklas 25     {
4577 19 Sep 17 nicklas 26       throw new NullPointerException("path");
4577 19 Sep 17 nicklas 27     }
4577 19 Sep 17 nicklas 28     // Get rid of trailing '/'
4577 19 Sep 17 nicklas 29     if (path.endsWith("/")) path = path.substring(0, path.length()-1);
4577 19 Sep 17 nicklas 30     // Check valid characters
4577 19 Sep 17 nicklas 31     if (!path.matches("[a-zA-Z0-9._\\-/]+"))
4577 19 Sep 17 nicklas 32     {
4577 19 Sep 17 nicklas 33       throw new InvalidDataException("Path may only contain [a-zA-Z0-9._-/]: " + path);
4577 19 Sep 17 nicklas 34     }
4577 19 Sep 17 nicklas 35     if (twoLevels)
4577 19 Sep 17 nicklas 36     {
4577 19 Sep 17 nicklas 37       // Path must be at least two levels
4577 19 Sep 17 nicklas 38       if (!path.matches("/[a-zA-Z0-9._\\-]+/[a-zA-Z0-9._\\-].*"))
4577 19 Sep 17 nicklas 39       {
4577 19 Sep 17 nicklas 40         throw new InvalidDataException("Path must be at least two levels deep: " + path);
4577 19 Sep 17 nicklas 41       }
4577 19 Sep 17 nicklas 42     }
4577 19 Sep 17 nicklas 43     else
4577 19 Sep 17 nicklas 44     {
4577 19 Sep 17 nicklas 45       // Path must start with '/'
4577 19 Sep 17 nicklas 46       if (!path.startsWith("/")) 
4577 19 Sep 17 nicklas 47       {
4577 19 Sep 17 nicklas 48         throw new InvalidDataException("Path must start with '/': " + path);
4577 19 Sep 17 nicklas 49       }
4577 19 Sep 17 nicklas 50     }
4577 19 Sep 17 nicklas 51     // Not allowed to move up to parent directory
4577 19 Sep 17 nicklas 52     if (path.contains(".."))
4577 19 Sep 17 nicklas 53     {
4577 19 Sep 17 nicklas 54       throw new InvalidDataException("Path may not contain '..': " + path);
4577 19 Sep 17 nicklas 55     }
4577 19 Sep 17 nicklas 56     return path;
4577 19 Sep 17 nicklas 57   }
4577 19 Sep 17 nicklas 58   
4577 19 Sep 17 nicklas 59   /**
4577 19 Sep 17 nicklas 60     Checks that a value is allowed as a file/directory name.
4577 19 Sep 17 nicklas 61     We only allow letters, numbers, dot and underscore names.
4577 19 Sep 17 nicklas 62     It may not contain a double dot (..).
4577 19 Sep 17 nicklas 63   */
4577 19 Sep 17 nicklas 64   public static String checkValidFilename(String filename)
4577 19 Sep 17 nicklas 65   {
4577 19 Sep 17 nicklas 66     if (filename == null)
4577 19 Sep 17 nicklas 67     {
4577 19 Sep 17 nicklas 68       throw new NullPointerException("filename");
4577 19 Sep 17 nicklas 69     }
4577 19 Sep 17 nicklas 70     if (!filename.matches("[a-zA-Z0-9._\\-]+"))
4577 19 Sep 17 nicklas 71     {
4577 19 Sep 17 nicklas 72       throw new InvalidDataException("File name may only contain [a-zA-Z0-9._-]: " + filename);
4577 19 Sep 17 nicklas 73     }
4577 19 Sep 17 nicklas 74     // Not allowed to move up to parent directory
4577 19 Sep 17 nicklas 75     if (filename.contains(".."))
4577 19 Sep 17 nicklas 76     {
4577 19 Sep 17 nicklas 77       throw new InvalidDataException("File name may not contain '..': " + filename);
4577 19 Sep 17 nicklas 78     }
4577 19 Sep 17 nicklas 79     return filename;
4577 19 Sep 17 nicklas 80   }
4577 19 Sep 17 nicklas 81   
4577 19 Sep 17 nicklas 82   /**
4577 19 Sep 17 nicklas 83     Script parameters may only contain letters, numbers, dot, underscore and hyphen.
4577 19 Sep 17 nicklas 84   */
4577 19 Sep 17 nicklas 85   public static String checkValidScriptParameter(String param)
4577 19 Sep 17 nicklas 86   {
4577 19 Sep 17 nicklas 87     if (param != null && !param.matches("[a-zA-Z0-9._\\-]+"))
4577 19 Sep 17 nicklas 88     {
4577 19 Sep 17 nicklas 89       throw new InvalidDataException("Parameter may only contain [a-zA-Z0-9._-]: " + param);
4577 19 Sep 17 nicklas 90     }
4577 19 Sep 17 nicklas 91     return param;
4577 19 Sep 17 nicklas 92   }
4577 19 Sep 17 nicklas 93
4577 19 Sep 17 nicklas 94   
4577 19 Sep 17 nicklas 95 }