View Javadoc

1   /*
2    ------------------------------
3    Hermes FTP Server
4    Copyright (c) 2006 Lars Behnke
5    ------------------------------
6   
7    This file is part of Hermes FTP Server.
8   
9    Hermes FTP Server is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; either version 2 of the License, or
12   (at your option) any later version.
13  
14   Foobar is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   GNU General Public License for more details.
18  
19   You should have received a copy of the GNU General Public License
20   along with Foobar; if not, write to the Free Software
21   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22   */
23  
24  package net.sf.hermesftp.usermanager.model;
25  
26  import java.util.Properties;
27  
28  import net.sf.hermesftp.exception.FtpConfigException;
29  import net.sf.hermesftp.utils.VarMerger;
30  
31  import org.apache.commons.io.FilenameUtils;
32  import org.springframework.util.AntPathMatcher;
33  
34  /***
35   * Represents the permission on one or more file pathes. The paths a configured using ant style path
36   * naming conventions.
37   *
38   * @author Lars Behnke
39   *
40   */
41  public class PermissionData {
42  
43      private String template;
44  
45      private int permission;
46  
47      /***
48       * Getter method for the java bean <code>permission</code>.
49       *
50       * @return Returns the value of the java bean <code>permission</code>.
51       */
52      public int getPermission() {
53          return permission;
54      }
55  
56      /***
57       * Setter method for the java bean <code>permission</code>.
58       *
59       * @param permission The value of permission to set.
60       */
61      public void setPermission(int permission) {
62          this.permission = permission;
63      }
64  
65      /***
66       * Getter method for the java bean <code>template</code>.
67       *
68       * @return Returns the value of the java bean <code>template</code>.
69       */
70      public String getTemplate() {
71          return template;
72      }
73  
74      /***
75       * Setter method for the java bean <code>template</code>.
76       *
77       * @param template The value of template to set.
78       */
79      public void setTemplate(String template) {
80          this.template = template;
81      }
82  
83      /***
84       * Fills the placeholders in the path template and checks if the passed path matches the
85       * template.
86       *
87       * @param checkPath The path to check.
88       * @param ftproot The ftp root folder.
89       * @param username The username.
90       * @return True, if the path matches the configured pattern.
91       * @throws FtpConfigException Error on reading or processing a configuration file.
92       */
93      public boolean matches(String checkPath, String ftproot, String username)
94                                                                               throws FtpConfigException {
95          if (checkPath == null) {
96              return false;
97          }
98          AntPathMatcher pathMatcher = new AntPathMatcher();
99          String antPath = replacePlaceholders(ftproot, username);
100         antPath = FilenameUtils.normalizeNoEndSeparator(antPath);
101         antPath = FilenameUtils.separatorsToUnix(antPath);
102         checkPath = FilenameUtils.normalizeNoEndSeparator(checkPath);
103         checkPath = FilenameUtils.separatorsToUnix(checkPath);
104         return pathMatcher.match(antPath, checkPath);
105 
106     }
107 
108     private String replacePlaceholders(String ftproot, String username) throws FtpConfigException {
109         VarMerger varMerger = new VarMerger(getTemplate());
110         Properties props = new Properties();
111         props.setProperty("ftproot", FilenameUtils.separatorsToUnix(ftproot));
112         props.setProperty("user", username);
113         varMerger.merge(props);
114         if (!varMerger.isReplacementComplete()) {
115             throw new FtpConfigException("Unresolved placeholders in user configuration file found.");
116         }
117         return varMerger.getText();
118     }
119 }