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