public class StubFtpServer extends AbstractFtpServer
CommandHandler
interface.
StubFtpServer works out of the box with default command handlers that return
success reply codes and empty data (for retrieved files, directory listings, etc.).
The command handler for any command can be easily configured to return custom data
or reply codes. Or it can be replaced with a custom CommandHandler
implementation. This allows simulation of a complete range of both success and
failure scenarios. The command handlers can also be interrogated to verify command
invocation data such as command parameters and timestamps.
StubFtpServer can be fully configured programmatically or within the Spring Framework or similar container.
Starting the StubFtpServer
Here is how to start the StubFtpServer with the default configuration.
StubFtpServer stubFtpServer = new StubFtpServer();
stubFtpServer.start();
FTP Server Control Port
By default, StubFtpServer 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.
Retrieving Command Handlers
You can retrieve the existingCommandHandler
defined for an FTP server command
by calling the AbstractFtpServer.getCommandHandler(String)
method, passing in the FTP server
command name. For example:
PwdCommandHandler pwdCommandHandler = (PwdCommandHandler) stubFtpServer.getCommandHandler("PWD");
Replacing Command Handlers
You can replace the existingCommandHandler
defined for an FTP server command
by calling the AbstractFtpServer.setCommandHandler(String, CommandHandler)
method, passing
in the FTP server command name and CommandHandler
instance. For example:
PwdCommandHandler pwdCommandHandler = new PwdCommandHandler();
pwdCommandHandler.setDirectory("some/dir");
stubFtpServer.setCommandHandler("PWD", pwdCommandHandler);
You can also replace multiple command handlers at once by using the AbstractFtpServer.setCommandHandlers(java.util.Map)
method. That is especially useful when configuring the server through the Spring Framework.
FTP Command Reply Text ResourceBundle
The default text associated 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.
LOG, REPLY_TEXT_BASENAME, serverSocketFactory
Constructor and Description |
---|
StubFtpServer()
Create a new instance.
|
Modifier and Type | Method and Description |
---|---|
protected void |
initializeCommandHandler(CommandHandler commandHandler)
Initialize a CommandHandler that has been registered to this server.
|
createSession, getCommandHandler, getReplyTextBundle, getServerControlPort, isShutdown, isStarted, numberOfSessions, run, setCommandHandler, setCommandHandlers, setReplyTextBaseName, setServerControlPort, start, stop
public StubFtpServer()
protected void initializeCommandHandler(CommandHandler commandHandler)
AbstractFtpServer
initializeCommandHandler
in class AbstractFtpServer
commandHandler
- - the CommandHandler to initializeCopyright © 2016. All rights reserved.