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