UserCommandHandler.java revision 93102446a7b7c3d17888064b4e2e4e5cb534e6d0
1/*
2 * Copyright 2007 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.stub.command;
17
18import java.util.ArrayList;
19import java.util.List;
20
21import org.mockftpserver.core.command.Command;
22import org.mockftpserver.core.command.CommandHandler;
23import org.mockftpserver.core.command.InvocationRecord;
24import org.mockftpserver.core.command.ReplyCodes;
25import org.mockftpserver.core.session.Session;
26
27/**
28 * CommandHandler for the USER command. The <code>passwordRequired</code> property defaults to true,
29 * indicating that a password is required following the user name. If true, this command handler
30 * returns a reply of 331. If false, return a reply of 230.
31 * <p>
32 * Each invocation record stored by this CommandHandler includes the following data element key/values:
33 * <ul>
34 *    <li>{@link #USERNAME_KEY} ("username") - the user name submitted on the invocation (the first command parameter)
35 * </ul>
36 *
37 * @version $Revision: 93 $ - $Date: 2007-10-30 19:26:16 -0400 (Tue, 30 Oct 2007) $
38 *
39 * @author Chris Mair
40 */
41public final class UserCommandHandler extends AbstractStubCommandHandler implements CommandHandler {
42
43    public static final String USERNAME_KEY = "username";
44
45    private boolean passwordRequired = true;
46    private List usernames = new ArrayList();
47
48    /**
49     * Constructor.
50     */
51    public UserCommandHandler() {
52        // Do not initialize replyCode -- will be set dynamically
53    }
54
55    /**
56     * @see org.mockftpserver.core.command.CommandHandler#handleCommand(Command, Session, InvocationRecord)
57     */
58    public void handleCommand(Command command, Session session, InvocationRecord invocationRecord) {
59        usernames.add(command.getRequiredString(0));
60        invocationRecord.set(USERNAME_KEY, command.getRequiredString(0));
61
62        // Only use dynamic reply code if the replyCode property was NOT explicitly set
63        if (replyCode == 0) {
64            int code = (passwordRequired) ? ReplyCodes.USER_NEED_PASSWORD_OK : ReplyCodes.USER_LOGGED_IN_OK;
65            sendReply(session, code, replyMessageKey, replyText, null);
66        }
67        else {
68            sendReply(session);
69        }
70    }
71
72    /**
73     * Return true if a password is required at login. See {@link #setPasswordRequired(boolean)}.
74     * @return the passwordRequired flag
75     */
76    public boolean isPasswordRequired() {
77        return passwordRequired;
78    }
79
80    /**
81     * Set true to indicate that a password is required. If true, this command handler returns a reply
82     * of 331. If false, return a reply of 230.
83     * @param passwordRequired - is a password required for login
84     */
85    public void setPasswordRequired(boolean passwordRequired) {
86        this.passwordRequired = passwordRequired;
87    }
88
89}
90