1 /**
2 This file is part of GoldenGate Project (named also GoldenGate or GG).
3
4 Copyright 2009, Frederic Bregier, and individual contributors by the @author
5 tags. See the COPYRIGHT.txt in the distribution for a full listing of
6 individual contributors.
7
8 All GoldenGate Project is free software: you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as published
10 by the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 GoldenGate is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GoldenGate . If not, see <http://www.gnu.org/licenses/>.
20 */
21 package goldengate.common.file.filesystembased;
22
23 import goldengate.common.command.NextCommandReply;
24 import goldengate.common.command.ReplyCode;
25 import goldengate.common.command.exception.Reply421Exception;
26 import goldengate.common.command.exception.Reply530Exception;
27 import goldengate.common.file.AuthInterface;
28 import goldengate.common.file.DirInterface;
29 import goldengate.common.file.SessionInterface;
30
31 /**
32 * Authentication implementation for Filesystem Based
33 *
34 * @author Frederic Bregier
35 *
36 */
37 public abstract class FilesystemBasedAuthImpl implements AuthInterface {
38 /**
39 * User name
40 */
41 protected String user = null;
42
43 /**
44 * Password
45 */
46 protected String password = null;
47
48 /**
49 * Is Identified
50 */
51 protected boolean isIdentified = false;
52
53 /**
54 * SessionInterface
55 */
56 protected final SessionInterface session;
57
58 /**
59 * Relative Path after Authentication
60 */
61 protected String rootFromAuth = null;
62
63 /**
64 * @param session
65 */
66 public FilesystemBasedAuthImpl(SessionInterface session) {
67 this.session = session;
68 isIdentified = false;
69 }
70
71 /**
72 * @return the session
73 */
74 public SessionInterface getSession() {
75 return session;
76 }
77
78 /**
79 * Set the user according to any implementation and could set the
80 * rootFromAuth. If NOOP is returned, isIdentifed must be TRUE.
81 *
82 * @param user
83 * @return (NOOP,230) if the user is OK, else return the following command
84 * that must follow (usually PASS) and the associated reply
85 * @throws Reply421Exception
86 * if there is a problem during the authentication
87 * @throws Reply530Exception
88 * if there is a problem during the authentication
89 */
90 protected abstract NextCommandReply setBusinessUser(String user)
91 throws Reply421Exception, Reply530Exception;
92
93 /**
94 * @param user
95 * the user to set
96 * @return (NOOP,230) if the user is OK, else return the following command
97 * that must follow (usually PASS) and the associated reply
98 * @throws Reply421Exception
99 * if there is a problem during the authentication
100 * @throws Reply530Exception
101 * if there is a problem during the authentication
102 */
103 public NextCommandReply setUser(String user) throws Reply421Exception,
104 Reply530Exception {
105 NextCommandReply next = setBusinessUser(user);
106 this.user = user;
107 if (next.reply == ReplyCode.REPLY_230_USER_LOGGED_IN) {
108 setRootFromAuth();
109 session.getDir().initAfterIdentification();
110 }
111 return next;
112 }
113
114 /**
115 * @return the user
116 */
117 public String getUser() {
118 return user;
119 }
120
121 /**
122 * Set the password according to any implementation and could set the
123 * rootFromAuth. If NOOP is returned, isIdentifed must be TRUE.
124 *
125 * @param password
126 * @return (NOOP,230) if the Password is OK, else return the following
127 * command that must follow (usually ACCT) and the associated reply
128 * @throws Reply421Exception
129 * if there is a problem during the authentication
130 * @throws Reply530Exception
131 * if there is a problem during the authentication
132 */
133 protected abstract NextCommandReply setBusinessPassword(String password)
134 throws Reply421Exception, Reply530Exception;
135
136 /**
137 * @param password
138 * the password to set
139 * @return (NOOP,230) if the Password is OK, else return the following
140 * command that must follow (usually ACCT) and the associated reply
141 * @throws Reply421Exception
142 * if there is a problem during the authentication
143 * @throws Reply530Exception
144 * if there is a problem during the authentication
145 */
146 public NextCommandReply setPassword(String password)
147 throws Reply421Exception, Reply530Exception {
148 NextCommandReply next = setBusinessPassword(password);
149 this.password = password;
150 if (next.reply == ReplyCode.REPLY_230_USER_LOGGED_IN) {
151 setRootFromAuth();
152 session.getDir().initAfterIdentification();
153 }
154 return next;
155 }
156
157 /**
158 * Set the Authentication to Identified or Not
159 *
160 * @param isIdentified
161 */
162 protected void setIsIdentified(boolean isIdentified) {
163 this.isIdentified = isIdentified;
164 }
165
166 /**
167 * Is the current Authentication OK for full identification. It must be true
168 * after a correct sequence of identification: At most, it is true when
169 * setAccount is OK. It could be positive before (user name only,
170 * user+password only).<br>
171 * In the current implementation, as USER+PASS+ACCT are needed, it will be
172 * true only after a correct ACCT.
173 *
174 * @return True if the user has a positive login, else False
175 */
176 public boolean isIdentified() {
177 return isIdentified;
178 }
179
180 /**
181 *
182 * @return the root relative path from authentication if any or null if the
183 * default is used (default is /user or /user/account)
184 * @exception Reply421Exception
185 * if the business root is not available
186 */
187 protected abstract String setBusinessRootFromAuth()
188 throws Reply421Exception;
189
190 /**
191 * Set the root relative Path from current status of Authentication (should
192 * be the highest level for the current authentication). If
193 * setBusinessRootFromAuth returns null, by default set /user or
194 * /user/account.
195 *
196 * @exception Reply421Exception
197 * if the business root is not available
198 */
199 private void setRootFromAuth() throws Reply421Exception {
200 rootFromAuth = setBusinessRootFromAuth();
201 if (rootFromAuth == null) {
202 rootFromAuth = DirInterface.SEPARATOR + user;
203 }
204 }
205
206 public String getBusinessPath() {
207 return rootFromAuth;
208 }
209
210 /**
211 * Business implementation of clean
212 *
213 */
214 protected abstract void businessClean();
215
216 /**
217 * Clean object
218 *
219 */
220 public void clear() {
221 businessClean();
222 user = null;
223 password = null;
224 rootFromAuth = null;
225 isIdentified = false;
226 }
227
228 /**
229 * Return the full path as a String (with mount point).
230 *
231 * @param path
232 * relative path including business one (may be null or empty)
233 * @return the full path as a String
234 */
235 public String getAbsolutePath(String path) {
236 if (path == null || path.length() == 0) {
237 return getBaseDirectory();
238 }
239 return FilesystemBasedDirImpl.normalizePath(getBaseDirectory() +
240 DirInterface.SEPARATOR + path);
241 }
242
243 /**
244 * Return the relative path from a file (without mount point)
245 *
246 * @param file
247 * (full path with mount point)
248 * @return the relative path from a file
249 */
250 public String getRelativePath(String file) {
251 // Work around Windows path '\'
252 return file.replaceFirst(FilesystemBasedDirImpl
253 .normalizePath(getBaseDirectory()), "");
254 }
255 }