org.mockftpserver.fake
Class FakeFtpServer

java.lang.Object
  extended by org.mockftpserver.core.server.AbstractFtpServer
      extended by org.mockftpserver.fake.FakeFtpServer
All Implemented Interfaces:
Runnable, ServerConfiguration

public class FakeFtpServer
extends AbstractFtpServer
implements ServerConfiguration

FakeFtpServer is the top-level class for a "fake" implementation of an FTP Server, suitable for testing FTP client code or standing in for a live FTP server.

FakeFtpServer provides a high-level abstraction for an FTP Server and is suitable for most testing and simulation scenarios. You define a filesystem (internal, in-memory) containing an arbitrary set of files and directories. These files and directories can (optionally) have associated access permissions. You also configure a set of one or more user accounts that control which users can login to the FTP server, and their home (default) directories. The user account is also used when assigning file and directory ownership for new files.

FakeFtpServer processes FTP client requests and responds with reply codes and reply messages consistent with its configuration and the contents of its internal filesystem, including file and directory permissions, if they have been configured.

FakeFtpServer can be fully configured programmatically or within the Spring Framework or other dependency-injection container.

In general the steps for setting up and starting the FakeFtpServer are:

  1. Create a new FakeFtpServer instance, and optionally set the server control port.
  2. Create and configure a FileSystem, and attach to the FakeFtpServer instance.
  3. Create and configure one or more UserAccount objects and attach to the FakeFtpServer instance.
  4. Start the FakeFtpServer instance.

Example Code


 FakeFtpServer fakeFtpServer = new FakeFtpServer();

 FileSystem fileSystem = new WindowsFakeFileSystem();
 fileSystem.add(new DirectoryEntry("c:\\"));
 fileSystem.add(new DirectoryEntry("c:\\data"));
 fileSystem.add(new FileEntry("c:\\data\\file1.txt", "abcdef 1234567890"));
 fileSystem.add(new FileEntry("c:\\data\\run.exe"));
 fakeFtpServer.setFileSystem(fileSystem);

 // Create UserAccount with username, password, home-directory
 UserAccount userAccount = new UserAccount("joe", "joe123", "c:\\");
 fakeFtpServer.addUserAccounts(userAccount);

 fakeFtpServer.start();
 

Example Code with Permissions

You can optionally set the permissions and owner/group for each file and directory, as in the following example.

 FileSystem fileSystem = new UnixFakeFileSystem();
 DirectoryEntry directoryEntry1 = new DirectoryEntry("/");
 directoryEntry1.setPermissions(new Permissions("rwxrwx---"));
 directoryEntry1.setOwner("joe");
 directoryEntry1.setGroup("dev");

 DirectoryEntry directoryEntry2 = new DirectoryEntry("/data");
 directoryEntry2.setPermissions(Permissions.ALL);
 directoryEntry2.setOwner("joe");
 directoryEntry2.setGroup("dev");

 FileEntry fileEntry1 = new FileEntry("/data/file1.txt", "abcdef 1234567890");
 fileEntry1.setPermissionsFromString("rw-rw-rw-");
 fileEntry1.setOwner("joe");
 fileEntry1.setGroup("dev");

 FileEntry fileEntry2 = new FileEntry("/data/run.exe");
 fileEntry2.setPermissionsFromString("rwxrwx---");
 fileEntry2.setOwner("mary");
 fileEntry2.setGroup("dev");

 fileSystem.add(directoryEntry1);
 fileSystem.add(directoryEntry2);
 fileSystem.add(fileEntry1);
 fileSystem.add(fileEntry2);

 FakeFtpServer fakeFtpServer = new FakeFtpServer();
 fakeFtpServer.setFileSystem(fileSystem);

 // Create UserAccount with username, password, home-directory
 UserAccount userAccount = new UserAccount("joe", "joe123", "/");
 fakeFtpServer.addUserAccounts(userAccount);

 fakeFtpServer.start();
 

FTP Server Control Port

By default, FakeFtpServer binds to the server control port of 21. You can use a different server control port by setting the serverControlPort property. If you specify a value of 0, then a free port number will be chosen automatically; call getServerControlPort() AFTER start() has been called to determine the actual port number being used. Using a non-default port number is usually necessary when running on Unix or some other system where that port number is already in use or cannot be bound from a user process.

Other Configuration

The systemName property specifies the value returned by the SYST command. Note that this is typically used by an FTP client to determine how to parse system-dependent reply text, such as directory listings. This value defaults to "WINDOWS".

The helpText property specifies a Map of help text replies sent by the HELP command. The keys in that Map correspond to the command names passed as parameters to the HELP command. An entry with the key of an empty string ("") indicates the text used as the default help text when no command name parameter is specified for the HELP command.

FTP Command Reply Text ResourceBundle

The default text asociated with each FTP command reply code is contained within the "ReplyText.properties" ResourceBundle file. You can customize these messages by providing a locale-specific ResourceBundle file on the CLASSPATH, according to the normal lookup rules of the ResourceBundle class (e.g., "ReplyText_de.properties"). Alternatively, you can completely replace the ResourceBundle file by calling the calling the AbstractFtpServer.setReplyTextBaseName(String) method.

Version:
$Revision: 255 $ - $Date: 2011-06-05 21:23:55 -0400 (Sun, 05 Jun 2011) $
Author:
Chris Mair

Field Summary
 
Fields inherited from class org.mockftpserver.core.server.AbstractFtpServer
LOG, REPLY_TEXT_BASENAME, serverSocketFactory
 
Constructor Summary
FakeFtpServer()
           
 
Method Summary
 void addUserAccount(UserAccount userAccount)
          Add a single UserAccount.
 FileSystem getFileSystem()
           
 Map getHelpText()
           
 String getHelpText(String name)
          Return the help text for a command or the default help text if no command name is specified
 String getSystemName()
           
 String getSystemStatus()
          Return the system status description
 UserAccount getUserAccount(String username)
           
protected  void initializeCommandHandler(CommandHandler commandHandler)
          Initialize a CommandHandler that has been registered to this server.
 void setFileSystem(FileSystem fileSystem)
           
 void setHelpText(Map helpText)
           
 void setSystemName(String systemName)
           
 void setSystemStatus(String systemStatus)
          Set the system status description text, used by the STAT command handler.
 void setUserAccounts(List userAccountList)
          Add the UserAccount objects in the userAccountList to the set of UserAccounts.
 
Methods inherited from class org.mockftpserver.core.server.AbstractFtpServer
createSession, getCommandHandler, getReplyTextBundle, getServerControlPort, isShutdown, isStarted, run, setCommandHandler, setCommandHandlers, setReplyTextBaseName, setServerControlPort, start, stop
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FakeFtpServer

public FakeFtpServer()
Method Detail

getFileSystem

public FileSystem getFileSystem()
Specified by:
getFileSystem in interface ServerConfiguration
Returns:
the FileSystem for this server

setFileSystem

public void setFileSystem(FileSystem fileSystem)

getSystemName

public String getSystemName()
Specified by:
getSystemName in interface ServerConfiguration
Returns:
the System Name for this server (used by the SYST command)

setSystemName

public void setSystemName(String systemName)

getHelpText

public Map getHelpText()

setHelpText

public void setHelpText(Map helpText)

initializeCommandHandler

protected void initializeCommandHandler(CommandHandler commandHandler)
Initialize a CommandHandler that has been registered to this server. If the CommandHandler implements the ServerConfigurationAware interface, then set its ServerConfiguration property to this. If the CommandHandler implements the ReplyTextBundleAware interface, then set its replyTextBundle property using the reply text bundle for this server.

Specified by:
initializeCommandHandler in class AbstractFtpServer
Parameters:
commandHandler - - the CommandHandler to initialize

getUserAccount

public UserAccount getUserAccount(String username)
Specified by:
getUserAccount in interface ServerConfiguration
Parameters:
username - - the user name
Returns:
the UserAccount configured for this server for the specified user name

getHelpText

public String getHelpText(String name)
Return the help text for a command or the default help text if no command name is specified

Specified by:
getHelpText in interface ServerConfiguration
Parameters:
name - - the command name; may be empty or null to indicate a request for the default help text
Returns:
the help text for the named command or the default help text if no name is supplied

addUserAccount

public void addUserAccount(UserAccount userAccount)
Add a single UserAccount. If an account with the same username already exists, it will be replaced.

Parameters:
userAccount - - the UserAccount to add

setUserAccounts

public void setUserAccounts(List userAccountList)
Add the UserAccount objects in the userAccountList to the set of UserAccounts.

Parameters:
userAccountList - - the List of UserAccount objects to add

getSystemStatus

public String getSystemStatus()
Return the system status description

Specified by:
getSystemStatus in interface ServerConfiguration
Returns:
the system status

setSystemStatus

public void setSystemStatus(String systemStatus)
Set the system status description text, used by the STAT command handler.

Parameters:
systemStatus - - the system status description text


Copyright © 2014. All rights reserved.