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