org.mockftpserver.stub
Class StubFtpServer

java.lang.Object
  extended by org.mockftpserver.core.server.AbstractFtpServer
      extended by org.mockftpserver.stub.StubFtpServer
All Implemented Interfaces:
Runnable

public class StubFtpServer
extends AbstractFtpServer

StubFtpServer is the top-level class for a "stub" implementation of an FTP Server, suitable for testing FTP client code or standing in for a live FTP server. It supports the main FTP commands by defining handlers for each of the corresponding low-level FTP server commands (e.g. RETR, DELE, LIST). These handlers implement the 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 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.

Retrieving Command Handlers

You can retrieve the existing CommandHandler 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 existing CommandHandler 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 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
StubFtpServer()
          Create a new instance.
 
Method Summary
protected  void initializeCommandHandler(CommandHandler commandHandler)
          Initialize a CommandHandler that has been registered to this server.
 
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

StubFtpServer

public StubFtpServer()
Create a new instance. Initialize the default command handlers and reply text ResourceBundle.

Method Detail

initializeCommandHandler

protected void initializeCommandHandler(CommandHandler commandHandler)
Description copied from class: AbstractFtpServer
Initialize a CommandHandler that has been registered to this server. What "initialization" means is dependent on the subclass implementation.

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


Copyright © 2014. All rights reserved.