1fba2635c088b01af59300e8a02611bb788ee3bffchrismair/*
2fba2635c088b01af59300e8a02611bb788ee3bffchrismair * Copyright 2008 the original author or authors.
3fba2635c088b01af59300e8a02611bb788ee3bffchrismair *
4fba2635c088b01af59300e8a02611bb788ee3bffchrismair * Licensed under the Apache License, Version 2.0 (the "License");
5fba2635c088b01af59300e8a02611bb788ee3bffchrismair * you may not use this file except in compliance with the License.
6fba2635c088b01af59300e8a02611bb788ee3bffchrismair * You may obtain a copy of the License at
7fba2635c088b01af59300e8a02611bb788ee3bffchrismair *
8fba2635c088b01af59300e8a02611bb788ee3bffchrismair *      http://www.apache.org/licenses/LICENSE-2.0
9fba2635c088b01af59300e8a02611bb788ee3bffchrismair *
10fba2635c088b01af59300e8a02611bb788ee3bffchrismair * Unless required by applicable law or agreed to in writing, software
11fba2635c088b01af59300e8a02611bb788ee3bffchrismair * distributed under the License is distributed on an "AS IS" BASIS,
12fba2635c088b01af59300e8a02611bb788ee3bffchrismair * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13fba2635c088b01af59300e8a02611bb788ee3bffchrismair * See the License for the specific language governing permissions and
14fba2635c088b01af59300e8a02611bb788ee3bffchrismair * limitations under the License.
15fba2635c088b01af59300e8a02611bb788ee3bffchrismair */
16fba2635c088b01af59300e8a02611bb788ee3bffchrismairpackage org.mockftpserver.fake.command;
17fba2635c088b01af59300e8a02611bb788ee3bffchrismair
18fba2635c088b01af59300e8a02611bb788ee3bffchrismairimport org.mockftpserver.core.command.Command;
19fba2635c088b01af59300e8a02611bb788ee3bffchrismairimport org.mockftpserver.core.command.ReplyCodes;
20fba2635c088b01af59300e8a02611bb788ee3bffchrismairimport org.mockftpserver.core.session.Session;
21fba2635c088b01af59300e8a02611bb788ee3bffchrismairimport org.mockftpserver.core.session.SessionKeys;
22fba2635c088b01af59300e8a02611bb788ee3bffchrismairimport org.mockftpserver.fake.UserAccount;
23fba2635c088b01af59300e8a02611bb788ee3bffchrismair
24fba2635c088b01af59300e8a02611bb788ee3bffchrismair/**
25fba2635c088b01af59300e8a02611bb788ee3bffchrismair * CommandHandler for the PASS command. Handler logic:
26fba2635c088b01af59300e8a02611bb788ee3bffchrismair * <ol>
27fba2635c088b01af59300e8a02611bb788ee3bffchrismair * <li>If the required password parameter is missing, then reply with 501</li>
28fba2635c088b01af59300e8a02611bb788ee3bffchrismair * <li>If this command was not preceded by a valid USER command, then reply with 503</li>
29fba2635c088b01af59300e8a02611bb788ee3bffchrismair * <li>If the user account configured for the named user does not exist or is not valid, then reply with 530</li>
30fba2635c088b01af59300e8a02611bb788ee3bffchrismair * <li>If the specified password is not correct, then reply with 530</li>
31fba2635c088b01af59300e8a02611bb788ee3bffchrismair * <li>Otherwise, reply with 230</li>
32fba2635c088b01af59300e8a02611bb788ee3bffchrismair * </ol>
33fba2635c088b01af59300e8a02611bb788ee3bffchrismair *
34fba2635c088b01af59300e8a02611bb788ee3bffchrismair * @author Chris Mair
35fba2635c088b01af59300e8a02611bb788ee3bffchrismair * @version $Revision$ - $Date$
36fba2635c088b01af59300e8a02611bb788ee3bffchrismair */
37fba2635c088b01af59300e8a02611bb788ee3bffchrismairpublic class PassCommandHandler extends AbstractFakeCommandHandler {
38fba2635c088b01af59300e8a02611bb788ee3bffchrismair
39fba2635c088b01af59300e8a02611bb788ee3bffchrismair    protected void handle(Command command, Session session) {
40fba2635c088b01af59300e8a02611bb788ee3bffchrismair        String password = command.getRequiredParameter(0);
41fba2635c088b01af59300e8a02611bb788ee3bffchrismair        String username = (String) getRequiredSessionAttribute(session, SessionKeys.USERNAME);
42fba2635c088b01af59300e8a02611bb788ee3bffchrismair
43fba2635c088b01af59300e8a02611bb788ee3bffchrismair        if (validateUserAccount(username, session)) {
44fba2635c088b01af59300e8a02611bb788ee3bffchrismair            UserAccount userAccount = getServerConfiguration().getUserAccount(username);
45fba2635c088b01af59300e8a02611bb788ee3bffchrismair            if (userAccount.isValidPassword(password)) {
46fba2635c088b01af59300e8a02611bb788ee3bffchrismair                int replyCode = (userAccount.isAccountRequiredForLogin()) ? ReplyCodes.PASS_NEED_ACCOUNT : ReplyCodes.PASS_OK;
47fba2635c088b01af59300e8a02611bb788ee3bffchrismair                String replyMessageKey = (userAccount.isAccountRequiredForLogin()) ? "pass.needAccount" : "pass";
48fba2635c088b01af59300e8a02611bb788ee3bffchrismair                login(userAccount, session, replyCode, replyMessageKey);
49fba2635c088b01af59300e8a02611bb788ee3bffchrismair            } else {
50fba2635c088b01af59300e8a02611bb788ee3bffchrismair                sendReply(session, ReplyCodes.PASS_LOG_IN_FAILED, "pass.loginFailed");
51fba2635c088b01af59300e8a02611bb788ee3bffchrismair            }
52fba2635c088b01af59300e8a02611bb788ee3bffchrismair        }
53fba2635c088b01af59300e8a02611bb788ee3bffchrismair    }
54fba2635c088b01af59300e8a02611bb788ee3bffchrismair
55fba2635c088b01af59300e8a02611bb788ee3bffchrismair}