View Javadoc

1   /**
2    * Copyright 2009, Frederic Bregier, and individual contributors by the @author
3    * tags. See the COPYRIGHT.txt in the distribution for a full listing of
4    * individual contributors.
5    *
6    * This is free software; you can redistribute it and/or modify it under the
7    * terms of the GNU Lesser General Public License as published by the Free
8    * Software Foundation; either version 3.0 of the License, or (at your option)
9    * any later version.
10   *
11   * This software is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
14   * details.
15   *
16   * You should have received a copy of the GNU Lesser General Public License
17   * along with this software; if not, write to the Free Software Foundation,
18   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
19   * site: http://www.fsf.org.
20   */
21  package goldengate.ftp.exec.file;
22  
23  import goldengate.common.logging.GgInternalLogger;
24  import goldengate.common.logging.GgInternalLoggerFactory;
25  import goldengate.ftp.exec.exec.AbstractExecutor.CommandExecutor;
26  
27  /**
28   * Simple Authentication based on a previously load XML file.
29   *
30   * @author Frederic Bregier
31   *
32   */
33  public class SimpleAuth {
34      /**
35       * Internal Logger
36       */
37      private static final GgInternalLogger logger = GgInternalLoggerFactory
38              .getLogger(SimpleAuth.class);
39  
40      /**
41       * User name
42       */
43      public String user = null;
44  
45      /**
46       * Password
47       */
48      public String password = null;
49  
50      /**
51       * Multiple accounts
52       */
53      public String[] accounts = null;
54  
55      /**
56       * Is the current user an administrator (which can shutdown or change
57       * bandwidth limitation)
58       */
59      public boolean isAdmin = false;
60      /**
61       * Specific Store command for this user
62       */
63      public String storCmd = null;
64      /**
65       * Specific Store command delay for this user
66       */
67      public long storDelay = 0;
68      /**
69       * Specific Retrieve command for this user
70       */
71      public String retrCmd = null;
72      /**
73       * Specific Retrieve command delay for this user
74       */
75      public long retrDelay = 0;
76      
77      public CommandExecutor commandExecutor = null;
78      
79      /**
80       * @param user
81       * @param password
82       * @param accounts
83       * @param storCmd
84       * @param storDelay
85       * @param retrCmd
86       * @param retrDelay
87       */
88      public SimpleAuth(String user, String password, String[] accounts,
89              String storCmd, long storDelay, String retrCmd, long retrDelay) {
90          this.user = user;
91          this.password = password;
92          this.accounts = accounts;
93          this.storCmd = storCmd;
94          this.storDelay = storDelay;
95          this.retrCmd = retrCmd;
96          this.retrDelay = retrDelay;
97          this.commandExecutor = new CommandExecutor(retrCmd, retrDelay, storCmd, storDelay);
98          logger.info("Executor for "+user+" configured as [RETR: "+
99                  commandExecutor.pretrCMD+":"+commandExecutor.pretrDelay+":"+
100                 commandExecutor.pretrRefused+
101                 "] [STOR: "+commandExecutor.pstorCMD+":"+
102                 commandExecutor.pstorDelay+":"+commandExecutor.pstorRefused+"]");
103     }
104 
105     /**
106      * Is the given password a valid one
107      *
108      * @param newpassword
109      * @return True if the password is valid (or any password is valid)
110      */
111     public boolean isPasswordValid(String newpassword) {
112         if (password == null) {
113             return true;
114         }
115         if (newpassword == null) {
116             return false;
117         }
118         return password.equals(newpassword);
119     }
120 
121     /**
122      * Is the given account a valid one
123      *
124      * @param account
125      * @return True if the account is valid (or any account is valid)
126      */
127     public boolean isAccountValid(String account) {
128         if (accounts == null) {
129             logger.debug("No account needed");
130             return true;
131         }
132         if (account == null) {
133             logger.debug("No account given");
134             return false;
135         }
136         for (String acct: accounts) {
137             if (acct.equals(account)) {
138                 logger.debug("Account found");
139                 return true;
140             }
141         }
142         logger.debug("No account found");
143         return false;
144     }
145 
146     /**
147      *
148      * @param isAdmin
149      *            True if the user should be an administrator
150      */
151     public void setAdmin(boolean isAdmin) {
152         this.isAdmin = isAdmin;
153     }
154 }