100dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair/*
200dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * Copyright 2008 the original author or authors.
300dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair *
400dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * Licensed under the Apache License, Version 2.0 (the "License");
500dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * you may not use this file except in compliance with the License.
600dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * You may obtain a copy of the License at
700dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair *
800dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair *      http://www.apache.org/licenses/LICENSE-2.0
900dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair *
1000dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * Unless required by applicable law or agreed to in writing, software
1100dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * distributed under the License is distributed on an "AS IS" BASIS,
1200dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1300dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * See the License for the specific language governing permissions and
1400dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * limitations under the License.
1500dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair */
1600dc7bdcf1df9e86789d963984dfc6912a8854c6chrismairpackage org.mockftpserver.fake.command;
1700dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair
1800dc7bdcf1df9e86789d963984dfc6912a8854c6chrismairimport org.mockftpserver.core.command.Command;
1900dc7bdcf1df9e86789d963984dfc6912a8854c6chrismairimport org.mockftpserver.core.command.ReplyCodes;
2000dc7bdcf1df9e86789d963984dfc6912a8854c6chrismairimport org.mockftpserver.core.session.Session;
2100dc7bdcf1df9e86789d963984dfc6912a8854c6chrismairimport org.mockftpserver.core.session.SessionKeys;
2200dc7bdcf1df9e86789d963984dfc6912a8854c6chrismairimport org.mockftpserver.fake.UserAccount;
2300dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair
2400dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair/**
2500dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * CommandHandler for the USER command. Handler logic:
2600dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * <ol>
2700dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * <li>If the required pathname parameter is missing, then reply with 501</li>
2800dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * <li>If the user account configured for the named user is not valid, then reply with 530</li>
2900dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * <li>If the named user does not need a password for login, then set the UserAccount and
3000dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * current directory in the session, and reply with 230</li>
3100dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * <li>Otherwise, set the username in the session and reply with 331</li>
3200dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * </ol>
3300dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair *
3400dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * @author Chris Mair
3500dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * @version $Revision$ - $Date$
3600dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair */
3700dc7bdcf1df9e86789d963984dfc6912a8854c6chrismairpublic class UserCommandHandler extends AbstractFakeCommandHandler {
3800dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair
3900dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair    protected void handle(Command command, Session session) {
4000dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair        String username = command.getRequiredParameter(0);
4100dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair        UserAccount userAccount = getServerConfiguration().getUserAccount(username);
4200dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair
4300dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair        if (userAccount != null) {
4400dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair            if (!validateUserAccount(username, session)) {
4500dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair                return;
4600dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair            }
4700dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair
4800dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair            // If the UserAccount is configured to not require password for login
4900dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair            if (!userAccount.isPasswordRequiredForLogin()) {
5000dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair                login(userAccount, session, ReplyCodes.USER_LOGGED_IN_OK, "user.loggedIn");
5100dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair                return;
5200dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair            }
5300dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair        }
5400dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair        session.setAttribute(SessionKeys.USERNAME, username);
5500dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair        sendReply(session, ReplyCodes.USER_NEED_PASSWORD_OK, "user.needPassword");
5600dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair    }
5700dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair
5800dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair}