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.ConnectCommandHandler; 21import org.mockftpserver.core.command.ReplyTextBundleUtil; 22import org.mockftpserver.core.command.UnsupportedCommandHandler; 23import org.mockftpserver.core.server.AbstractFtpServer; 24import org.mockftpserver.stub.command.*; 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 the 42 * <a href="http://www.springframework.org/">Spring Framework</a> 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>FTP Server Control Port</h4> 52 * By default, <b>StubFtpServer</b> binds to the server control port of 21. You can use a different server 53 * control port by setting the the <code>serverControlPort</code> property. This is usually necessary 54 * when running on Unix or some other system where that port number is already in use or cannot be bound 55 * from a user process. 56 * <h4>Retrieving Command Handlers</h4> 57 * You can retrieve the existing {@link CommandHandler} defined for an FTP server command 58 * by calling the {@link #getCommandHandler(String)} method, passing in the FTP server 59 * command name. For example: 60 * <pre><code> 61 * PwdCommandHandler pwdCommandHandler = (PwdCommandHandler) stubFtpServer.getCommandHandler("PWD"); 62 * </code></pre> 63 * <p/> 64 * <h4>Replacing Command Handlers</h4> 65 * You can replace the existing {@link CommandHandler} defined for an FTP server command 66 * by calling the {@link #setCommandHandler(String, CommandHandler)} method, passing 67 * in the FTP server command name and {@link CommandHandler} instance. For example: 68 * <pre><code> 69 * PwdCommandHandler pwdCommandHandler = new PwdCommandHandler(); 70 * pwdCommandHandler.setDirectory("some/dir"); 71 * stubFtpServer.setCommandHandler("PWD", pwdCommandHandler); 72 * </code></pre> 73 * You can also replace multiple command handlers at once by using the {@link #setCommandHandlers(java.util.Map)} 74 * method. That is especially useful when configuring the server through the <b>Spring Framework</b>. 75 * <h4>FTP Command Reply Text ResourceBundle</h4> 76 * <p/> 77 * The default text asociated with each FTP command reply code is contained within the 78 * "ReplyText.properties" ResourceBundle file. You can customize these messages by providing a 79 * locale-specific ResourceBundle file on the CLASSPATH, according to the normal lookup rules of 80 * the ResourceBundle class (e.g., "ReplyText_de.properties"). Alternatively, you can 81 * completely replace the ResourceBundle file by calling the calling the 82 * {@link #setReplyTextBaseName(String)} method. 83 * 84 * @author Chris Mair 85 * @version $Revision$ - $Date$ 86 */ 87public class StubFtpServer extends AbstractFtpServer { 88 89 /** 90 * Create a new instance. Initialize the default command handlers and 91 * reply text ResourceBundle. 92 */ 93 public StubFtpServer() { 94 PwdCommandHandler pwdCommandHandler = new PwdCommandHandler(); 95 96 // Initialize the default CommandHandler mappings 97 setCommandHandler(CommandNames.ABOR, new AborCommandHandler()); 98 setCommandHandler(CommandNames.ACCT, new AcctCommandHandler()); 99 setCommandHandler(CommandNames.ALLO, new AlloCommandHandler()); 100 setCommandHandler(CommandNames.APPE, new AppeCommandHandler()); 101 setCommandHandler(CommandNames.PWD, pwdCommandHandler); // same as XPWD 102 setCommandHandler(CommandNames.CONNECT, new ConnectCommandHandler()); 103 setCommandHandler(CommandNames.CWD, new CwdCommandHandler()); 104 setCommandHandler(CommandNames.CDUP, new CdupCommandHandler()); 105 setCommandHandler(CommandNames.DELE, new DeleCommandHandler()); 106 setCommandHandler(CommandNames.EPRT, new EprtCommandHandler()); 107 setCommandHandler(CommandNames.EPSV, new EpsvCommandHandler()); 108 setCommandHandler(CommandNames.HELP, new HelpCommandHandler()); 109 setCommandHandler(CommandNames.LIST, new ListCommandHandler()); 110 setCommandHandler(CommandNames.MKD, new MkdCommandHandler()); 111 setCommandHandler(CommandNames.MODE, new ModeCommandHandler()); 112 setCommandHandler(CommandNames.NOOP, new NoopCommandHandler()); 113 setCommandHandler(CommandNames.NLST, new NlstCommandHandler()); 114 setCommandHandler(CommandNames.PASS, new PassCommandHandler()); 115 setCommandHandler(CommandNames.PASV, new PasvCommandHandler()); 116 setCommandHandler(CommandNames.PORT, new PortCommandHandler()); 117 setCommandHandler(CommandNames.RETR, new RetrCommandHandler()); 118 setCommandHandler(CommandNames.QUIT, new QuitCommandHandler()); 119 setCommandHandler(CommandNames.REIN, new ReinCommandHandler()); 120 setCommandHandler(CommandNames.REST, new RestCommandHandler()); 121 setCommandHandler(CommandNames.RMD, new RmdCommandHandler()); 122 setCommandHandler(CommandNames.RNFR, new RnfrCommandHandler()); 123 setCommandHandler(CommandNames.RNTO, new RntoCommandHandler()); 124 setCommandHandler(CommandNames.SITE, new SiteCommandHandler()); 125 setCommandHandler(CommandNames.SMNT, new SmntCommandHandler()); 126 setCommandHandler(CommandNames.STAT, new StatCommandHandler()); 127 setCommandHandler(CommandNames.STOR, new StorCommandHandler()); 128 setCommandHandler(CommandNames.STOU, new StouCommandHandler()); 129 setCommandHandler(CommandNames.STRU, new StruCommandHandler()); 130 setCommandHandler(CommandNames.SYST, new SystCommandHandler()); 131 setCommandHandler(CommandNames.TYPE, new TypeCommandHandler()); 132 setCommandHandler(CommandNames.USER, new UserCommandHandler()); 133 setCommandHandler(CommandNames.UNSUPPORTED, new UnsupportedCommandHandler()); 134 setCommandHandler(CommandNames.XPWD, pwdCommandHandler); // same as PWD 135 } 136 137 //------------------------------------------------------------------------- 138 // Abstract method implementation 139 //------------------------------------------------------------------------- 140 141 protected void initializeCommandHandler(CommandHandler commandHandler) { 142 ReplyTextBundleUtil.setReplyTextBundleIfAppropriate(commandHandler, getReplyTextBundle()); 143 } 144 145}