1/*
2 * Copyright 2008 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.mockftpserver.fake.command;
17
18import org.mockftpserver.core.command.Command;
19import org.mockftpserver.core.command.ReplyCodes;
20import org.mockftpserver.core.session.Session;
21import org.mockftpserver.core.session.SessionKeys;
22import org.mockftpserver.fake.UserAccount;
23
24/**
25 * CommandHandler for the USER command. Handler logic:
26 * <ol>
27 * <li>If the required pathname parameter is missing, then reply with 501</li>
28 * <li>If the user account configured for the named user is not valid, then reply with 530</li>
29 * <li>If the named user does not need a password for login, then reply with 230</li>
30 * <li>Otherwise, reply with 331</li>
31 * </ol>
32 *
33 * @author Chris Mair
34 * @version $Revision: 89 $ - $Date: 2008-08-02 08:07:44 -0400 (Sat, 02 Aug 2008) $
35 */
36public class UserCommandHandler extends AbstractFakeCommandHandler {
37
38    protected void handle(Command command, Session session) {
39        String username = command.getRequiredParameter(0);
40        UserAccount userAccount = getServerConfiguration().getUserAccount(username);
41
42        if (userAccount != null) {
43            if (!validateUserAccount(username, session)) {
44                return;
45            }
46
47            // If the UserAccount is configured to not require password for login
48            if (!userAccount.isPasswordRequiredForLogin()) {
49                login(userAccount, session, ReplyCodes.USER_LOGGED_IN_OK, "user.loggedIn");
50                return;
51            }
52        }
53        session.setAttribute(SessionKeys.USERNAME, username);
54        sendReply(session, ReplyCodes.USER_NEED_PASSWORD_OK, "user.needPassword");
55    }
56
57}