StubFtpServer.java revision 128e81688f779fe96344b5a47cc4ad29292c381d
1/* 2 * Copyright 2007 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16package org.mockftpserver.stub; 17 18import org.mockftpserver.core.command.CommandHandler; 19import org.mockftpserver.core.command.CommandNames; 20import org.mockftpserver.core.command.ReplyTextBundleUtil; 21import org.mockftpserver.core.server.AbstractFtpServer; 22import org.mockftpserver.stub.command.*; 23 24import java.util.Map; 25 26/** 27 * <b>StubFtpServer</b> is the top-level class for a "stub" implementation of an FTP Server, 28 * suitable for testing FTP client code or standing in for a live FTP server. It supports 29 * the main FTP commands by defining handlers for each of the corresponding low-level FTP 30 * server commands (e.g. RETR, DELE, LIST). These handlers implement the {@link CommandHandler} 31 * interface. 32 * <p/> 33 * <b>StubFtpServer</b> works out of the box with default command handlers that return 34 * success reply codes and empty data (for retrieved files, directory listings, etc.). 35 * The command handler for any command can be easily configured to return custom data 36 * or reply codes. Or it can be replaced with a custom {@link CommandHandler} 37 * implementation. This allows simulation of a complete range of both success and 38 * failure scenarios. The command handlers can also be interrogated to verify command 39 * invocation data such as command parameters and timestamps. 40 * <p/> 41 * <b>StubFtpServer</b> can be fully configured programmatically or within a Spring Framework 42 * ({@link http://www.springframework.org/}) or similar container. 43 * <p/> 44 * <h4>Starting the StubFtpServer</h4> 45 * Here is how to start the <b>StubFtpServer</b> with the default configuration. 46 * <pre><code> 47 * StubFtpServer stubFtpServer = new StubFtpServer(); 48 * stubFtpServer.start(); 49 * </code></pre> 50 * <p/> 51 * <h4>Retrieving Command Handlers</h4> 52 * You can retrieve the existing {@link CommandHandler} defined for an FTP server command 53 * by calling the {@link #getCommandHandler(String)} method, passing in the FTP server 54 * command name. For example: 55 * <pre><code> 56 * PwdCommandHandler pwdCommandHandler = (PwdCommandHandler) stubFtpServer.getCommandHandler("PWD"); 57 * </code></pre> 58 * <p/> 59 * <h4>Replacing Command Handlers</h4> 60 * You can replace the existing {@link CommandHandler} defined for an FTP server command 61 * by calling the {@link #setCommandHandler(String, CommandHandler)} method, passing 62 * in the FTP server command name and {@link CommandHandler} instance. For example: 63 * <pre><code> 64 * PwdCommandHandler pwdCommandHandler = new PwdCommandHandler(); 65 * pwdCommandHandler.setDirectory("some/dir"); 66 * stubFtpServer.setCommandHandler("PWD", pwdCommandHandler); 67 * </code></pre> 68 * You can also replace multiple command handlers at once by using the {@link #setCommandHandlers(Map)} 69 * method. That is especially useful when configuring the server through the <b>Spring Framework</b>. 70 * <h4>FTP Command Reply Text ResourceBundle</h4> 71 * <p/> 72 * The default text asociated with each FTP command reply code is contained within the 73 * "ReplyText.properties" ResourceBundle file. You can customize these messages by providing a 74 * locale-specific ResourceBundle file on the CLASSPATH, according to the normal lookup rules of 75 * the ResourceBundle class (e.g., "ReplyText_de.properties"). Alternatively, you can 76 * completely replace the ResourceBundle file by calling the calling the 77 * {@link #setReplyTextBaseName(String)} method. 78 * 79 * @author Chris Mair 80 * @version $Revision$ - $Date$ 81 */ 82public class StubFtpServer extends AbstractFtpServer { 83 84 /** 85 * Create a new instance. Initialize the default command handlers and 86 * reply text ResourceBundle. 87 */ 88 public StubFtpServer() { 89 PwdCommandHandler pwdCommandHandler = new PwdCommandHandler(); 90 91 // Initialize the default CommandHandler mappings 92 setCommandHandler(CommandNames.ABOR, new AborCommandHandler()); 93 setCommandHandler(CommandNames.ACCT, new AcctCommandHandler()); 94 setCommandHandler(CommandNames.ALLO, new AlloCommandHandler()); 95 setCommandHandler(CommandNames.APPE, new AppeCommandHandler()); 96 setCommandHandler(CommandNames.PWD, pwdCommandHandler); // same as XPWD 97 setCommandHandler(CommandNames.CONNECT, new ConnectCommandHandler()); 98 setCommandHandler(CommandNames.CWD, new CwdCommandHandler()); 99 setCommandHandler(CommandNames.CDUP, new CdupCommandHandler()); 100 setCommandHandler(CommandNames.DELE, new DeleCommandHandler()); 101 setCommandHandler(CommandNames.HELP, new HelpCommandHandler()); 102 setCommandHandler(CommandNames.LIST, new ListCommandHandler()); 103 setCommandHandler(CommandNames.MKD, new MkdCommandHandler()); 104 setCommandHandler(CommandNames.MODE, new ModeCommandHandler()); 105 setCommandHandler(CommandNames.NOOP, new NoopCommandHandler()); 106 setCommandHandler(CommandNames.NLST, new NlstCommandHandler()); 107 setCommandHandler(CommandNames.PASS, new PassCommandHandler()); 108 setCommandHandler(CommandNames.PASV, new PasvCommandHandler()); 109 setCommandHandler(CommandNames.PORT, new PortCommandHandler()); 110 setCommandHandler(CommandNames.RETR, new RetrCommandHandler()); 111 setCommandHandler(CommandNames.QUIT, new QuitCommandHandler()); 112 setCommandHandler(CommandNames.REIN, new ReinCommandHandler()); 113 setCommandHandler(CommandNames.REST, new RestCommandHandler()); 114 setCommandHandler(CommandNames.RMD, new RmdCommandHandler()); 115 setCommandHandler(CommandNames.RNFR, new RnfrCommandHandler()); 116 setCommandHandler(CommandNames.RNTO, new RntoCommandHandler()); 117 setCommandHandler(CommandNames.SITE, new SiteCommandHandler()); 118 setCommandHandler(CommandNames.SMNT, new SmntCommandHandler()); 119 setCommandHandler(CommandNames.STAT, new StatCommandHandler()); 120 setCommandHandler(CommandNames.STOR, new StorCommandHandler()); 121 setCommandHandler(CommandNames.STOU, new StouCommandHandler()); 122 setCommandHandler(CommandNames.STRU, new StruCommandHandler()); 123 setCommandHandler(CommandNames.SYST, new SystCommandHandler()); 124 setCommandHandler(CommandNames.TYPE, new TypeCommandHandler()); 125 setCommandHandler(CommandNames.USER, new UserCommandHandler()); 126 setCommandHandler(CommandNames.XPWD, pwdCommandHandler); // same as PWD 127 } 128 129 //------------------------------------------------------------------------- 130 // Abstract method implementation 131 //------------------------------------------------------------------------- 132 133 protected void initializeCommandHandler(CommandHandler commandHandler) { 134 ReplyTextBundleUtil.setReplyTextBundleIfAppropriate(commandHandler, getReplyTextBundle()); 135 } 136 137}