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 set the UserAccount and 30 * current directory in the session, and reply with 230</li> 31 * <li>Otherwise, set the username in the session and reply with 331</li> 32 * </ol> 33 * 34 * @author Chris Mair 35 * @version $Revision$ - $Date$ 36 */ 37public class UserCommandHandler extends AbstractFakeCommandHandler { 38 39 protected void handle(Command command, Session session) { 40 String username = command.getRequiredParameter(0); 41 UserAccount userAccount = getServerConfiguration().getUserAccount(username); 42 43 if (userAccount != null) { 44 if (!validateUserAccount(username, session)) { 45 return; 46 } 47 48 // If the UserAccount is configured to not require password for login 49 if (!userAccount.isPasswordRequiredForLogin()) { 50 login(userAccount, session, ReplyCodes.USER_LOGGED_IN_OK, "user.loggedIn"); 51 return; 52 } 53 } 54 session.setAttribute(SessionKeys.USERNAME, username); 55 sendReply(session, ReplyCodes.USER_NEED_PASSWORD_OK, "user.needPassword"); 56 } 57 58}