All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.mockftpserver.stub.StubFtpServer Maven / Gradle / Ivy

/*
 * Copyright 2007 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.mockftpserver.stub;

import org.mockftpserver.core.command.CommandHandler;
import org.mockftpserver.core.command.CommandNames;
import org.mockftpserver.core.command.ConnectCommandHandler;
import org.mockftpserver.core.command.ReplyTextBundleUtil;
import org.mockftpserver.core.command.UnsupportedCommandHandler;
import org.mockftpserver.core.server.AbstractFtpServer;
import org.mockftpserver.stub.command.*;

/**
 * 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 {@link 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 {@link 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 {@link CommandHandler} defined for an FTP server command * by calling the {@link #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 {@link CommandHandler} defined for an FTP server command * by calling the {@link #setCommandHandler(String, CommandHandler)} method, passing * in the FTP server command name and {@link 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 {@link #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 * {@link #setReplyTextBaseName(String)} method. * * @author Chris Mair * @version $Revision: 255 $ - $Date: 2011-06-05 21:23:55 -0400 (Sun, 05 Jun 2011) $ */ public class StubFtpServer extends AbstractFtpServer { /** * Create a new instance. Initialize the default command handlers and * reply text ResourceBundle. */ public StubFtpServer() { PwdCommandHandler pwdCommandHandler = new PwdCommandHandler(); // Initialize the default CommandHandler mappings setCommandHandler(CommandNames.ABOR, new AborCommandHandler()); setCommandHandler(CommandNames.ACCT, new AcctCommandHandler()); setCommandHandler(CommandNames.ALLO, new AlloCommandHandler()); setCommandHandler(CommandNames.APPE, new AppeCommandHandler()); setCommandHandler(CommandNames.PWD, pwdCommandHandler); // same as XPWD setCommandHandler(CommandNames.CONNECT, new ConnectCommandHandler()); setCommandHandler(CommandNames.CWD, new CwdCommandHandler()); setCommandHandler(CommandNames.CDUP, new CdupCommandHandler()); setCommandHandler(CommandNames.DELE, new DeleCommandHandler()); setCommandHandler(CommandNames.EPRT, new EprtCommandHandler()); setCommandHandler(CommandNames.EPSV, new EpsvCommandHandler()); setCommandHandler(CommandNames.HELP, new HelpCommandHandler()); setCommandHandler(CommandNames.LIST, new ListCommandHandler()); setCommandHandler(CommandNames.MKD, new MkdCommandHandler()); setCommandHandler(CommandNames.MODE, new ModeCommandHandler()); setCommandHandler(CommandNames.NOOP, new NoopCommandHandler()); setCommandHandler(CommandNames.NLST, new NlstCommandHandler()); setCommandHandler(CommandNames.PASS, new PassCommandHandler()); setCommandHandler(CommandNames.PASV, new PasvCommandHandler()); setCommandHandler(CommandNames.PORT, new PortCommandHandler()); setCommandHandler(CommandNames.RETR, new RetrCommandHandler()); setCommandHandler(CommandNames.QUIT, new QuitCommandHandler()); setCommandHandler(CommandNames.REIN, new ReinCommandHandler()); setCommandHandler(CommandNames.REST, new RestCommandHandler()); setCommandHandler(CommandNames.RMD, new RmdCommandHandler()); setCommandHandler(CommandNames.RNFR, new RnfrCommandHandler()); setCommandHandler(CommandNames.RNTO, new RntoCommandHandler()); setCommandHandler(CommandNames.SITE, new SiteCommandHandler()); setCommandHandler(CommandNames.SMNT, new SmntCommandHandler()); setCommandHandler(CommandNames.STAT, new StatCommandHandler()); setCommandHandler(CommandNames.STOR, new StorCommandHandler()); setCommandHandler(CommandNames.STOU, new StouCommandHandler()); setCommandHandler(CommandNames.STRU, new StruCommandHandler()); setCommandHandler(CommandNames.SYST, new SystCommandHandler()); setCommandHandler(CommandNames.TYPE, new TypeCommandHandler()); setCommandHandler(CommandNames.USER, new UserCommandHandler()); setCommandHandler(CommandNames.UNSUPPORTED, new UnsupportedCommandHandler()); setCommandHandler(CommandNames.XPWD, pwdCommandHandler); // same as PWD } //------------------------------------------------------------------------- // Abstract method implementation //------------------------------------------------------------------------- protected void initializeCommandHandler(CommandHandler commandHandler) { ReplyTextBundleUtil.setReplyTextBundleIfAppropriate(commandHandler, getReplyTextBundle()); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy