1 /**
2 * This file is part of Waarp Project.
3 *
4 * Copyright 2009, Frederic Bregier, and individual contributors by the @author tags. See the
5 * COPYRIGHT.txt in the distribution for a full listing of individual contributors.
6 *
7 * All Waarp Project is free software: you can redistribute it and/or modify it under the terms of
8 * the GNU General Public License as published by the Free Software Foundation, either version 3 of
9 * the License, or (at your option) any later version.
10 *
11 * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
12 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13 * Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along with Waarp . If not, see
16 * <http://www.gnu.org/licenses/>.
17 */
18 package org.waarp.ftp.filesystembased;
19
20 import org.waarp.common.command.NextCommandReply;
21 import org.waarp.common.command.ReplyCode;
22 import org.waarp.common.command.exception.Reply421Exception;
23 import org.waarp.common.command.exception.Reply502Exception;
24 import org.waarp.common.command.exception.Reply530Exception;
25 import org.waarp.common.file.filesystembased.FilesystemBasedAuthImpl;
26 import org.waarp.ftp.core.file.FtpAuth;
27 import org.waarp.ftp.core.file.FtpDir;
28 import org.waarp.ftp.core.session.FtpSession;
29
30 /**
31 * Filesystem implementation of a AuthInterface
32 *
33 * @author Frederic Bregier
34 *
35 */
36 public abstract class FilesystemBasedFtpAuth extends FilesystemBasedAuthImpl implements FtpAuth {
37
38 /**
39 * Account name
40 */
41 protected String account = null;
42
43 /**
44 *
45 * @param session
46 */
47 public FilesystemBasedFtpAuth(FtpSession session) {
48 super(session);
49 }
50
51 /**
52 * @return the account
53 */
54 public String getAccount() {
55 return account;
56 }
57
58 /**
59 * Set the account according to any implementation and could set the rootFromAuth. If NOOP is
60 * returned, isIdentifed must be TRUE.
61 *
62 * @param account
63 * @return (NOOP,230) if the Account is OK, else return the following command that must follow
64 * and the associated reply
65 * @throws Reply421Exception
66 * if there is a problem during the authentication
67 * @throws Reply530Exception
68 * if there is a problem during the authentication
69 * @throws Reply502Exception
70 * if there is a problem during the authentication
71 */
72 protected abstract NextCommandReply setBusinessAccount(String account)
73 throws Reply421Exception, Reply530Exception, Reply502Exception;
74
75 /**
76 * @param account
77 * the account to set
78 * @return (NOOP,230) if the Account is OK, else return the following command that must follow
79 * and the associated reply
80 * @throws Reply421Exception
81 * if there is a problem during the authentication
82 * @throws Reply530Exception
83 * if there is a problem during the authentication
84 * @throws Reply502Exception
85 */
86 public NextCommandReply setAccount(String account)
87 throws Reply421Exception, Reply530Exception, Reply502Exception {
88 NextCommandReply next = setBusinessAccount(account);
89 this.account = account;
90 if (next.reply == ReplyCode.REPLY_230_USER_LOGGED_IN) {
91 setRootFromAuth();
92 session.getDir().initAfterIdentification();
93 }
94 return next;
95 }
96
97 /**
98 * Set the root relative Path from current status of Authentication (should be the highest level
99 * for the current authentication). If setBusinessRootFromAuth returns null, by default set
100 * /user or /user/account.
101 *
102 * @exception Reply421Exception
103 * if the business root is not available
104 */
105 private void setRootFromAuth() throws Reply421Exception {
106 rootFromAuth = setBusinessRootFromAuth();
107 if (rootFromAuth == null) {
108 if (account == null) {
109 rootFromAuth = FtpDir.SEPARATOR + user;
110 } else {
111 rootFromAuth = FtpDir.SEPARATOR + user +
112 FtpDir.SEPARATOR + account;
113 }
114 }
115 }
116
117 /**
118 * Clean object
119 *
120 */
121 public void clear() {
122 super.clear();
123 account = null;
124 }
125
126 @Override
127 public String getBaseDirectory() {
128 return ((FtpSession) getSession()).getConfiguration()
129 .getBaseDirectory();
130 }
131 }