org.mockftpserver.fake.command
Class AbstractFakeCommandHandler

java.lang.Object
  extended by org.mockftpserver.core.command.AbstractCommandHandler
      extended by org.mockftpserver.fake.command.AbstractFakeCommandHandler
All Implemented Interfaces:
CommandHandler, ReplyTextBundleAware, ServerConfigurationAware
Direct Known Subclasses:
AborCommandHandler, AbstractStoreFileCommandHandler, AcctCommandHandler, AlloCommandHandler, CdupCommandHandler, CwdCommandHandler, DeleCommandHandler, EprtCommandHandler, EpsvCommandHandler, HelpCommandHandler, ListCommandHandler, MkdCommandHandler, ModeCommandHandler, NlstCommandHandler, NoopCommandHandler, PassCommandHandler, PasvCommandHandler, PortCommandHandler, PwdCommandHandler, QuitCommandHandler, ReinCommandHandler, RestCommandHandler, RetrCommandHandler, RmdCommandHandler, RnfrCommandHandler, RntoCommandHandler, SiteCommandHandler, SmntCommandHandler, StatCommandHandler, StruCommandHandler, SystCommandHandler, TypeCommandHandler, UserCommandHandler

public abstract class AbstractFakeCommandHandler
extends AbstractCommandHandler
implements ServerConfigurationAware

Abstract superclass for CommandHandler classes for the "Fake" server.

Version:
$Revision: 214 $ - $Date: 2008-12-26 20:00:19 -0500 (Fri, 26 Dec 2008) $
Author:
Chris Mair

Field Summary
protected static String INTERNAL_ERROR_KEY
           
protected  int replyCodeForFileSystemException
          Reply code sent back when a FileSystemException is caught by the handleCommand(Command, Session) This defaults to ReplyCodes.EXISTING_FILE_ERROR (550).
 
Fields inherited from class org.mockftpserver.core.command.AbstractCommandHandler
LOG
 
Constructor Summary
AbstractFakeCommandHandler()
           
 
Method Summary
protected  String defaultIfNullOrEmpty(String string, String defaultString)
          Return the string unless it is null or empty, in which case return the defaultString.
protected  String endOfLine()
          Return the end-of-line character(s) used when building multi-line responses
protected  FileSystem getFileSystem()
          Convenience method to return the FileSystem stored in the ServerConfiguration
protected  String getRealPath(Session session, String path)
          Return the full, absolute path for the specified abstract pathname.
protected  Object getRequiredSessionAttribute(Session session, String name)
          Return the value of the named attribute within the session.
 ServerConfiguration getServerConfiguration()
           
protected  UserAccount getUserAccount(Session session)
           
protected abstract  void handle(Command command, Session session)
          Handle the specified command for the session.
 void handleCommand(Command command, Session session)
          Use template method to centralize and ensure common validation
protected  List list(Object item)
          Convenience method to return a List with the specified single item
protected  List list(Object item1, Object item2)
          Convenience method to return a List with the specified two items
protected  void login(UserAccount userAccount, Session session, int replyCode, String replyMessageKey)
          Log in the specified user for the current session.
protected  boolean notNullOrEmpty(String string)
          Return true if the specified string is null or empty
protected  void sendReply(Session session, int replyCode)
          Send a reply for this command on the control connection.
protected  void sendReply(Session session, int replyCode, List args)
          Send a reply for this command on the control connection.
protected  void sendReply(Session session, int replyCode, String messageKey)
          Send a reply for this command on the control connection.
protected  void sendReply(Session session, int replyCode, String messageKey, List args)
          Send a reply for this command on the control connection.
 void setServerConfiguration(ServerConfiguration serverConfiguration)
           
protected  boolean validateUserAccount(String username, Session session)
          Validate the UserAccount for the specified username.
protected  void verifyExecutePermission(Session session, String path)
          Verify that the current user has execute permission to the specified path
protected  void verifyFileSystemCondition(boolean condition, String path, String messageKey)
          Verify that the specified condition related to the file system is true, otherwise throw a FileSystemException.
protected  void verifyLoggedIn(Session session)
          Verify that the current user (if any) has already logged in successfully.
protected  void verifyReadPermission(Session session, String path)
          Verify that the current user has read permission to the specified path
protected  void verifyWritePermission(Session session, String path)
          Verify that the current user has write permission to the specified path
 
Methods inherited from class org.mockftpserver.core.command.AbstractCommandHandler
assertValidReplyCode, getReplyTextBundle, quotes, setReplyTextBundle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

INTERNAL_ERROR_KEY

protected static final String INTERNAL_ERROR_KEY
See Also:
Constant Field Values

replyCodeForFileSystemException

protected int replyCodeForFileSystemException
Reply code sent back when a FileSystemException is caught by the handleCommand(Command, Session) This defaults to ReplyCodes.EXISTING_FILE_ERROR (550).

Constructor Detail

AbstractFakeCommandHandler

public AbstractFakeCommandHandler()
Method Detail

getServerConfiguration

public ServerConfiguration getServerConfiguration()
Specified by:
getServerConfiguration in interface ServerConfigurationAware

setServerConfiguration

public void setServerConfiguration(ServerConfiguration serverConfiguration)
Specified by:
setServerConfiguration in interface ServerConfigurationAware

handleCommand

public void handleCommand(Command command,
                          Session session)
Use template method to centralize and ensure common validation

Specified by:
handleCommand in interface CommandHandler
Parameters:
command - - the Command to be handled
session - - the session on which the Command was submitted

getFileSystem

protected FileSystem getFileSystem()
Convenience method to return the FileSystem stored in the ServerConfiguration

Returns:
the FileSystem

handle

protected abstract void handle(Command command,
                               Session session)
Handle the specified command for the session. All checked exceptions are expected to be wrapped or handled by the caller.

Parameters:
command - - the Command to be handled
session - - the session on which the Command was submitted

sendReply

protected void sendReply(Session session,
                         int replyCode,
                         String messageKey)
Send a reply for this command on the control connection.

The reply code is designated by the replyCode property, and the reply text is retrieved from the replyText ResourceBundle, using the specified messageKey.

Parameters:
session - - the Session
replyCode - - the reply code
messageKey - - the resource bundle key for the reply text
Throws:
AssertionError - - if session is null
See Also:
MessageFormat

sendReply

protected void sendReply(Session session,
                         int replyCode,
                         String messageKey,
                         List args)
Send a reply for this command on the control connection.

The reply code is designated by the replyCode property, and the reply text is retrieved from the replyText ResourceBundle, using the specified messageKey.

Parameters:
session - - the Session
replyCode - - the reply code
messageKey - - the resource bundle key for the reply text
args - - the optional message arguments; defaults to []
Throws:
AssertionError - - if session is null
See Also:
MessageFormat

sendReply

protected void sendReply(Session session,
                         int replyCode)
Send a reply for this command on the control connection.

The reply code is designated by the replyCode property, and the reply text is retrieved from the replyText ResourceBundle, using the reply code as the key.

Parameters:
session - - the Session
replyCode - - the reply code
Throws:
AssertionError - - if session is null
See Also:
MessageFormat

sendReply

protected void sendReply(Session session,
                         int replyCode,
                         List args)
Send a reply for this command on the control connection.

The reply code is designated by the replyCode property, and the reply text is retrieved from the replyText ResourceBundle, using the reply code as the key.

Parameters:
session - - the Session
replyCode - - the reply code
args - - the optional message arguments; defaults to []
Throws:
AssertionError - - if session is null
See Also:
MessageFormat

getRequiredSessionAttribute

protected Object getRequiredSessionAttribute(Session session,
                                             String name)
Return the value of the named attribute within the session.

Parameters:
session - - the Session
name - - the name of the session attribute to retrieve
Returns:
the value of the named session attribute
Throws:
IllegalStateException - - if the Session does not contain the named attribute

verifyLoggedIn

protected void verifyLoggedIn(Session session)
Verify that the current user (if any) has already logged in successfully.

Parameters:
session - - the Session

getUserAccount

protected UserAccount getUserAccount(Session session)
Parameters:
session - - the Session
Returns:
the UserAccount stored in the specified session; may be null

verifyFileSystemCondition

protected void verifyFileSystemCondition(boolean condition,
                                         String path,
                                         String messageKey)
Verify that the specified condition related to the file system is true, otherwise throw a FileSystemException.

Parameters:
condition - - the condition that must be true
path - - the path involved in the operation; this will be included in the error message if the condition is not true.
messageKey - - the message key for the exception message
Throws:
FileSystemException - - if the condition is not true

verifyExecutePermission

protected void verifyExecutePermission(Session session,
                                       String path)
Verify that the current user has execute permission to the specified path

Parameters:
session - - the Session
path - - the file system path
Throws:
FileSystemException - - if the condition is not true

verifyWritePermission

protected void verifyWritePermission(Session session,
                                     String path)
Verify that the current user has write permission to the specified path

Parameters:
session - - the Session
path - - the file system path
Throws:
FileSystemException - - if the condition is not true

verifyReadPermission

protected void verifyReadPermission(Session session,
                                    String path)
Verify that the current user has read permission to the specified path

Parameters:
session - - the Session
path - - the file system path
Throws:
FileSystemException - - if the condition is not true

getRealPath

protected String getRealPath(Session session,
                             String path)
Return the full, absolute path for the specified abstract pathname. If path is null, return the current directory (stored in the session). If path represents an absolute path, then return path as is. Otherwise, path is relative, so assemble the full path from the current directory and the specified relative path.

Parameters:
session - - the Session
path - - the abstract pathname; may be null
Returns:
the resulting full, absolute path

endOfLine

protected String endOfLine()
Return the end-of-line character(s) used when building multi-line responses

Returns:
"\r\n"

validateUserAccount

protected boolean validateUserAccount(String username,
                                      Session session)
Validate the UserAccount for the specified username. If valid, return true. If the UserAccount does not exist or is invalid, log an error message, send back a reply code of 530 with an appropriate error message, and return false. A UserAccount is considered invalid if the homeDirectory property is not set or is set to a non-existent directory.

Parameters:
username - - the username
session - - the session; used to send back an error reply if necessary
Returns:
true only if the UserAccount for the named user is valid

login

protected void login(UserAccount userAccount,
                     Session session,
                     int replyCode,
                     String replyMessageKey)
Log in the specified user for the current session. Send back a reply of 230 with a message indicated by the replyMessageKey and set the UserAccount and current directory (homeDirectory) in the session.

Parameters:
userAccount - - the userAccount for the user to be logged in
session - - the session
replyCode - - the reply code to send
replyMessageKey - - the message key for the reply text

list

protected List list(Object item)
Convenience method to return a List with the specified single item

Parameters:
item - - the single item in the returned List
Returns:
a new List with that single item

list

protected List list(Object item1,
                    Object item2)
Convenience method to return a List with the specified two items

Parameters:
item1 - - the first item in the returned List
item2 - - the second item in the returned List
Returns:
a new List with the specified items

notNullOrEmpty

protected boolean notNullOrEmpty(String string)
Return true if the specified string is null or empty

Parameters:
string - - the String to check; may be null
Returns:
true only if the specified String is null or empyt

defaultIfNullOrEmpty

protected String defaultIfNullOrEmpty(String string,
                                      String defaultString)
Return the string unless it is null or empty, in which case return the defaultString.

Parameters:
string - - the String to check; may be null
defaultString - - the value to return if string is null or empty
Returns:
string if not null and not empty; otherwise return defaultString


Copyright © 2014. All rights reserved.