public class FakeFtpServer extends AbstractFtpServer implements ServerConfiguration
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:
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 theserverControlPort 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
ThesystemName 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 theAbstractFtpServer.setReplyTextBaseName(String) method.LOG, REPLY_TEXT_BASENAME, serverSocketFactory| Constructor and Description |
|---|
FakeFtpServer() |
| Modifier and Type | Method and Description |
|---|---|
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. |
createSession, getCommandHandler, getReplyTextBundle, getServerControlPort, isShutdown, isStarted, numberOfSessions, run, setCommandHandler, setCommandHandlers, setReplyTextBaseName, setServerControlPort, start, stoppublic FileSystem getFileSystem()
getFileSystem in interface ServerConfigurationFileSystem for this serverpublic void setFileSystem(FileSystem fileSystem)
public String getSystemName()
getSystemName in interface ServerConfigurationpublic void setSystemName(String systemName)
public Map getHelpText()
public void setHelpText(Map helpText)
protected void initializeCommandHandler(CommandHandler commandHandler)
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.initializeCommandHandler in class AbstractFtpServercommandHandler - - the CommandHandler to initializepublic UserAccount getUserAccount(String username)
getUserAccount in interface ServerConfigurationusername - - the user nameUserAccount configured for this server for the specified user namepublic String getHelpText(String name)
getHelpText in interface ServerConfigurationname - - the command name; may be empty or null to indicate a request for the default help textpublic void addUserAccount(UserAccount userAccount)
username already exists,
it will be replaced.userAccount - - the UserAccount to addpublic void setUserAccounts(List userAccountList)
userAccountList to the set of UserAccounts.userAccountList - - the List of UserAccount objects to addpublic String getSystemStatus()
getSystemStatus in interface ServerConfigurationpublic void setSystemStatus(String systemStatus)
systemStatus - - the system status description textCopyright © 2016. All rights reserved.