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