1/* 2 * Copyright 2008 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.fake; 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.fake.command.*; 25import org.mockftpserver.fake.filesystem.FileSystem; 26 27import java.util.HashMap; 28import java.util.List; 29import java.util.Map; 30 31/** 32 * <b>FakeFtpServer</b> is the top-level class for a "fake" implementation of an FTP Server, 33 * suitable for testing FTP client code or standing in for a live FTP server. 34 * <p/> 35 * <b>FakeFtpServer</b> provides a high-level abstraction for an FTP Server and is suitable 36 * for most testing and simulation scenarios. You define a filesystem (internal, in-memory) containing 37 * an arbitrary set of files and directories. These files and directories can (optionally) have 38 * associated access permissions. You also configure a set of one or more user accounts that 39 * control which users can login to the FTP server, and their home (default) directories. The 40 * user account is also used when assigning file and directory ownership for new files. 41 * <p> <b>FakeFtpServer</b> processes FTP client requests and responds with reply codes and 42 * reply messages consistent with its configuration and the contents of its internal filesystem, 43 * including file and directory permissions, if they have been configured. 44 * <p/> 45 * <b>FakeFtpServer</b> can be fully configured programmatically or within the 46 * <a href="http://www.springframework.org/">Spring Framework</a> or other dependency-injection container. 47 * <p/> 48 * In general the steps for setting up and starting the <b>FakeFtpServer</b> are: 49 * <ol> 50 * <li>Create a new <b>FakeFtpServer</b> instance, and optionally set the server control port.</li> 51 * <li>Create and configure a <b>FileSystem</b>, and attach to the <b>FakeFtpServer</b> instance.</li> 52 * <li>Create and configure one or more <b>UserAccount</b> objects and attach to the <b>FakeFtpServer</b> instance.</li> 53 * <li>Start the <b>FakeFtpServer</b> instance.</li> 54 * </ol> 55 * <h4>Example Code</h4> 56 * <pre><code> 57 * FakeFtpServer fakeFtpServer = new FakeFtpServer(); 58 * 59 * FileSystem fileSystem = new WindowsFakeFileSystem(); 60 * fileSystem.add(new DirectoryEntry("c:\\")); 61 * fileSystem.add(new DirectoryEntry("c:\\data")); 62 * fileSystem.add(new FileEntry("c:\\data\\file1.txt", "abcdef 1234567890")); 63 * fileSystem.add(new FileEntry("c:\\data\\run.exe")); 64 * fakeFtpServer.setFileSystem(fileSystem); 65 * 66 * // Create UserAccount with username, password, home-directory 67 * UserAccount userAccount = new UserAccount("joe", "joe123", "c:\\"); 68 * fakeFtpServer.addUserAccounts(userAccount); 69 * 70 * fakeFtpServer.start(); 71 * </code></pre> 72 * 73 * <h4>Example Code with Permissions</h4> 74 * You can optionally set the permissions and owner/group for each file and directory, as in the following example. 75 * <pre><code> 76 * FileSystem fileSystem = new UnixFakeFileSystem(); 77 * DirectoryEntry directoryEntry1 = new DirectoryEntry("/"); 78 * directoryEntry1.setPermissions(new Permissions("rwxrwx---")); 79 * directoryEntry1.setOwner("joe"); 80 * directoryEntry1.setGroup("dev"); 81 * 82 * DirectoryEntry directoryEntry2 = new DirectoryEntry("/data"); 83 * directoryEntry2.setPermissions(Permissions.ALL); 84 * directoryEntry2.setOwner("joe"); 85 * directoryEntry2.setGroup("dev"); 86 * 87 * FileEntry fileEntry1 = new FileEntry("/data/file1.txt", "abcdef 1234567890"); 88 * fileEntry1.setPermissionsFromString("rw-rw-rw-"); 89 * fileEntry1.setOwner("joe"); 90 * fileEntry1.setGroup("dev"); 91 * 92 * FileEntry fileEntry2 = new FileEntry("/data/run.exe"); 93 * fileEntry2.setPermissionsFromString("rwxrwx---"); 94 * fileEntry2.setOwner("mary"); 95 * fileEntry2.setGroup("dev"); 96 * 97 * fileSystem.add(directoryEntry1); 98 * fileSystem.add(directoryEntry2); 99 * fileSystem.add(fileEntry1); 100 * fileSystem.add(fileEntry2); 101 * 102 * FakeFtpServer fakeFtpServer = new FakeFtpServer(); 103 * fakeFtpServer.setFileSystem(fileSystem); 104 * 105 * // Create UserAccount with username, password, home-directory 106 * UserAccount userAccount = new UserAccount("joe", "joe123", "/"); 107 * fakeFtpServer.addUserAccounts(userAccount); 108 * 109 * fakeFtpServer.start(); 110 * </code></pre> 111 * 112 * <h4>FTP Server Control Port</h4> 113 * By default, <b>MockFtpServer</b> binds to the server control port of 21. You can use a different server 114 * control port by setting the the <code>serverControlPort</code> property. This is usually necessary 115 * when running on Unix or some other system where that port number is already in use or cannot be bound 116 * from a user process. 117 * 118 * <h4>Other Configuration</h4> 119 * The <code>systemName</code> property specifies the value returned by the <code>SYST</code> 120 * command. Note that this is typically used by an FTP client to determine how to parse 121 * system-dependent reply text, such as directory listings. This value defaults to <code>"WINDOWS"</code>. 122 * <p/> 123 * The <code>helpText</code> property specifies a <i>Map</i> of help text replies sent by the 124 * <code>HELP</code> command. The keys in that <i>Map</i> correspond to the command names passed as 125 * parameters to the <code>HELP</code> command. An entry with the key of an empty string ("") indicates the 126 * text used as the default help text when no command name parameter is specified for the <code>HELP</code> command. 127 * 128 * <h4>FTP Command Reply Text ResourceBundle</h4> 129 * The default text asociated with each FTP command reply code is contained within the 130 * "ReplyText.properties" ResourceBundle file. You can customize these messages by providing a 131 * locale-specific ResourceBundle file on the CLASSPATH, according to the normal lookup rules of 132 * the ResourceBundle class (e.g., "ReplyText_de.properties"). Alternatively, you can 133 * completely replace the ResourceBundle file by calling the calling the 134 * {@link #setReplyTextBaseName(String)} method. 135 * 136 * @author Chris Mair 137 * @version $Revision$ - $Date$ 138 */ 139public class FakeFtpServer extends AbstractFtpServer implements ServerConfiguration { 140 141 private FileSystem fileSystem; 142 private String systemName = "WINDOWS"; 143 private String systemStatus = "Connected"; 144 private Map helpText = new HashMap(); 145 private Map userAccounts = new HashMap(); 146 147 public FileSystem getFileSystem() { 148 return fileSystem; 149 } 150 151 public void setFileSystem(FileSystem fileSystem) { 152 this.fileSystem = fileSystem; 153 } 154 155 public String getSystemName() { 156 return systemName; 157 } 158 159 public void setSystemName(String systemName) { 160 this.systemName = systemName; 161 } 162 163 public Map getHelpText() { 164 return helpText; 165 } 166 167 public void setHelpText(Map helpText) { 168 this.helpText = helpText; 169 } 170 171 public FakeFtpServer() { 172 setCommandHandler(CommandNames.ACCT, new AcctCommandHandler()); 173 setCommandHandler(CommandNames.ABOR, new AborCommandHandler()); 174 setCommandHandler(CommandNames.ALLO, new AlloCommandHandler()); 175 setCommandHandler(CommandNames.APPE, new AppeCommandHandler()); 176 setCommandHandler(CommandNames.CWD, new CwdCommandHandler()); 177 setCommandHandler(CommandNames.CDUP, new CdupCommandHandler()); 178 setCommandHandler(CommandNames.DELE, new DeleCommandHandler()); 179 setCommandHandler(CommandNames.EPRT, new EprtCommandHandler()); 180 setCommandHandler(CommandNames.EPSV, new EpsvCommandHandler()); 181 setCommandHandler(CommandNames.HELP, new HelpCommandHandler()); 182 setCommandHandler(CommandNames.LIST, new ListCommandHandler()); 183 setCommandHandler(CommandNames.MKD, new MkdCommandHandler()); 184 setCommandHandler(CommandNames.MODE, new ModeCommandHandler()); 185 setCommandHandler(CommandNames.NLST, new NlstCommandHandler()); 186 setCommandHandler(CommandNames.NOOP, new NoopCommandHandler()); 187 setCommandHandler(CommandNames.PASS, new PassCommandHandler()); 188 setCommandHandler(CommandNames.PASV, new PasvCommandHandler()); 189 setCommandHandler(CommandNames.PWD, new PwdCommandHandler()); 190 setCommandHandler(CommandNames.PORT, new PortCommandHandler()); 191 setCommandHandler(CommandNames.QUIT, new QuitCommandHandler()); 192 setCommandHandler(CommandNames.REIN, new ReinCommandHandler()); 193 setCommandHandler(CommandNames.REST, new RestCommandHandler()); 194 setCommandHandler(CommandNames.RETR, new RetrCommandHandler()); 195 setCommandHandler(CommandNames.RMD, new RmdCommandHandler()); 196 setCommandHandler(CommandNames.RNFR, new RnfrCommandHandler()); 197 setCommandHandler(CommandNames.RNTO, new RntoCommandHandler()); 198 setCommandHandler(CommandNames.SITE, new SiteCommandHandler()); 199 setCommandHandler(CommandNames.SMNT, new SmntCommandHandler()); 200 setCommandHandler(CommandNames.STAT, new StatCommandHandler()); 201 setCommandHandler(CommandNames.STOR, new StorCommandHandler()); 202 setCommandHandler(CommandNames.STOU, new StouCommandHandler()); 203 setCommandHandler(CommandNames.STRU, new StruCommandHandler()); 204 setCommandHandler(CommandNames.SYST, new SystCommandHandler()); 205 setCommandHandler(CommandNames.TYPE, new TypeCommandHandler()); 206 setCommandHandler(CommandNames.USER, new UserCommandHandler()); 207 setCommandHandler(CommandNames.XPWD, new PwdCommandHandler()); 208 209 // "Special" Command Handlers 210 setCommandHandler(CommandNames.CONNECT, new ConnectCommandHandler()); 211 setCommandHandler(CommandNames.UNSUPPORTED, new UnsupportedCommandHandler()); 212 } 213 214 /** 215 * Initialize a CommandHandler that has been registered to this server. 216 * 217 * If the CommandHandler implements the <code>ServerConfigurationAware</code> interface, then set its 218 * <code>ServerConfiguration</code> property to <code>this</code>. 219 * 220 * If the CommandHandler implements the <code>ReplyTextBundleAware</code> interface, then set its 221 * <code>replyTextBundle</code> property using the reply text bundle for this server. 222 * 223 * @param commandHandler - the CommandHandler to initialize 224 */ 225 protected void initializeCommandHandler(CommandHandler commandHandler) { 226 if (commandHandler instanceof ServerConfigurationAware) { 227 ServerConfigurationAware sca = (ServerConfigurationAware) commandHandler; 228 sca.setServerConfiguration(this); 229 } 230 231 ReplyTextBundleUtil.setReplyTextBundleIfAppropriate(commandHandler, getReplyTextBundle()); 232 } 233 234 /** 235 * @return the {@link UserAccount} configured for this server for the specified user name 236 */ 237 public UserAccount getUserAccount(String username) { 238 return (UserAccount) userAccounts.get(username); 239 } 240 241 /** 242 * Return the help text for a command or the default help text if no command name is specified 243 * 244 * @param name - the command name; may be empty or null to indicate a request for the default help text 245 * @return the help text for the named command or the default help text if no name is supplied 246 */ 247 public String getHelpText(String name) { 248 String key = name == null ? "" : name; 249 return (String) helpText.get(key); 250 } 251 252 /** 253 * Add a single UserAccount. If an account with the same <code>username</code> already exists, 254 * it will be replaced. 255 * 256 * @param userAccount - the UserAccount to add 257 */ 258 public void addUserAccount(UserAccount userAccount) { 259 userAccounts.put(userAccount.getUsername(), userAccount); 260 } 261 262 /** 263 * Add the UserAccount objects in the <code>userAccountList</code> to the set of UserAccounts. 264 * 265 * @param userAccountList - the List of UserAccount objects to add 266 */ 267 public void setUserAccounts(List userAccountList) { 268 for (int i = 0; i < userAccountList.size(); i++) { 269 UserAccount userAccount = (UserAccount) userAccountList.get(i); 270 userAccounts.put(userAccount.getUsername(), userAccount); 271 } 272 } 273 274 /** 275 * Return the system status description 276 * 277 * @return the system status 278 */ 279 public String getSystemStatus() { 280 return systemStatus; 281 } 282 283 /** 284 * Set the system status description text, used by the STAT command handler. 285 * 286 * @param systemStatus - the system status description text 287 */ 288 public void setSystemStatus(String systemStatus) { 289 this.systemStatus = systemStatus; 290 } 291 292}