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 PASS command. Handler logic:
26b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair * <ol>
27b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair * <li>If the required password parameter is missing, then reply with 501</li>
28b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair * <li>If this command was not preceded by a valid USER command, then reply with 503</li>
29b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair * <li>If the user account configured for the named user does not exist or is not valid, then reply with 530</li>
30b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair * <li>If the specified password is not correct, then reply with 530</li>
31b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair * <li>Otherwise, reply with 230</li>
32b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair * </ol>
33b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair *
34b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair * @author Chris Mair
35b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair * @version $Revision$ - $Date$
36b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair */
37b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismairpublic class PassCommandHandler extends AbstractFakeCommandHandler {
38b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair
39b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair    protected void handle(Command command, Session session) {
40b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        String password = command.getRequiredParameter(0);
41b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        String username = (String) getRequiredSessionAttribute(session, SessionKeys.USERNAME);
42b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair
43b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        if (validateUserAccount(username, session)) {
44b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair            UserAccount userAccount = getServerConfiguration().getUserAccount(username);
45b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair            if (userAccount.isValidPassword(password)) {
46b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair                int replyCode = (userAccount.isAccountRequiredForLogin()) ? ReplyCodes.PASS_NEED_ACCOUNT : ReplyCodes.PASS_OK;
47b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair                String replyMessageKey = (userAccount.isAccountRequiredForLogin()) ? "pass.needAccount" : "pass";
48b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair                login(userAccount, session, replyCode, replyMessageKey);
49b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair            } else {
50b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair                sendReply(session, ReplyCodes.PASS_LOG_IN_FAILED, "pass.loginFailed");
51b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair            }
52b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair        }
53b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair    }
54b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair
55b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair}