/* * Copyright 2008 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.fake; 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.fake.command.*; import org.mockftpserver.fake.filesystem.FileSystem; import java.util.HashMap; import java.util.List; import java.util.Map; /** * FakeFtpServer is the top-level class for a "fake" implementation of an FTP Server, * suitable for testing FTP client code or standing in for a live FTP server. *
* FakeFtpServer provides a high-level abstraction for an FTP Server and is suitable * for most testing and simulation scenarios. You define a filesystem (internal, in-memory) containing * an arbitrary set of files and directories. These files and directories can (optionally) have * associated access permissions. You also configure a set of one or more user accounts that * control which users can login to the FTP server, and their home (default) directories. The * user account is also used when assigning file and directory ownership for new files. *FakeFtpServer processes FTP client requests and responds with reply codes and * reply messages consistent with its configuration and the contents of its internal filesystem, * including file and directory permissions, if they have been configured. *
* FakeFtpServer can be fully configured programmatically or within the * Spring Framework or other dependency-injection container. * * In general the steps for setting up and starting the FakeFtpServer are: *
* FakeFtpServer fakeFtpServer = new FakeFtpServer();
*
* FileSystem fileSystem = new WindowsFakeFileSystem();
* fileSystem.add(new DirectoryEntry("c:\\"));
* fileSystem.add(new DirectoryEntry("c:\\data"));
* fileSystem.add(new FileEntry("c:\\data\\file1.txt", "abcdef 1234567890"));
* fileSystem.add(new FileEntry("c:\\data\\run.exe"));
* fakeFtpServer.setFileSystem(fileSystem);
*
* // Create UserAccount with username, password, home-directory
* UserAccount userAccount = new UserAccount("joe", "joe123", "c:\\");
* fakeFtpServer.addUserAccounts(userAccount);
*
* fakeFtpServer.start();
*
*
*
* FileSystem fileSystem = new UnixFakeFileSystem();
* DirectoryEntry directoryEntry1 = new DirectoryEntry("/");
* directoryEntry1.setPermissions(new Permissions("rwxrwx---"));
* directoryEntry1.setOwner("joe");
* directoryEntry1.setGroup("dev");
*
* DirectoryEntry directoryEntry2 = new DirectoryEntry("/data");
* directoryEntry2.setPermissions(Permissions.ALL);
* directoryEntry2.setOwner("joe");
* directoryEntry2.setGroup("dev");
*
* FileEntry fileEntry1 = new FileEntry("/data/file1.txt", "abcdef 1234567890");
* fileEntry1.setPermissionsFromString("rw-rw-rw-");
* fileEntry1.setOwner("joe");
* fileEntry1.setGroup("dev");
*
* FileEntry fileEntry2 = new FileEntry("/data/run.exe");
* fileEntry2.setPermissionsFromString("rwxrwx---");
* fileEntry2.setOwner("mary");
* fileEntry2.setGroup("dev");
*
* fileSystem.add(directoryEntry1);
* fileSystem.add(directoryEntry2);
* fileSystem.add(fileEntry1);
* fileSystem.add(fileEntry2);
*
* FakeFtpServer fakeFtpServer = new FakeFtpServer();
* fakeFtpServer.setFileSystem(fileSystem);
*
* // Create UserAccount with username, password, home-directory
* UserAccount userAccount = new UserAccount("joe", "joe123", "/");
* fakeFtpServer.addUserAccounts(userAccount);
*
* fakeFtpServer.start();
*
*
* serverControlPort
property. This 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.
*
* systemName
property specifies the value returned by the SYST
* command. Note that this is typically used by an FTP client to determine how to parse
* system-dependent reply text, such as directory listings. This value defaults to "WINDOWS"
.
*
* The helpText
property specifies a Map of help text replies sent by the
* HELP
command. The keys in that Map correspond to the command names passed as
* parameters to the HELP
command. An entry with the key of an empty string ("") indicates the
* text used as the default help text when no command name parameter is specified for the HELP
command.
*
* ServerConfigurationAware
interface, then set its
* ServerConfiguration
property to this
.
*
* If the CommandHandler implements the ReplyTextBundleAware
interface, then set its
* replyTextBundle
property using the reply text bundle for this server.
*
* @param commandHandler - the CommandHandler to initialize
*/
protected void initializeCommandHandler(CommandHandler commandHandler) {
if (commandHandler instanceof ServerConfigurationAware) {
ServerConfigurationAware sca = (ServerConfigurationAware) commandHandler;
sca.setServerConfiguration(this);
}
ReplyTextBundleUtil.setReplyTextBundleIfAppropriate(commandHandler, getReplyTextBundle());
}
/**
* @return the {@link UserAccount} configured for this server for the specified user name
*/
public UserAccount getUserAccount(String username) {
return (UserAccount) userAccounts.get(username);
}
/**
* Return the help text for a command or the default help text if no command name is specified
*
* @param name - the command name; may be empty or null to indicate a request for the default help text
* @return the help text for the named command or the default help text if no name is supplied
*/
public String getHelpText(String name) {
String key = name == null ? "" : name;
return (String) helpText.get(key);
}
/**
* Add a single UserAccount. If an account with the same username
already exists,
* it will be replaced.
*
* @param userAccount - the UserAccount to add
*/
public void addUserAccount(UserAccount userAccount) {
userAccounts.put(userAccount.getUsername(), userAccount);
}
/**
* Add the UserAccount objects in the userAccountList
to the set of UserAccounts.
*
* @param userAccountList - the List of UserAccount objects to add
*/
public void setUserAccounts(List userAccountList) {
for (int i = 0; i < userAccountList.size(); i++) {
UserAccount userAccount = (UserAccount) userAccountList.get(i);
userAccounts.put(userAccount.getUsername(), userAccount);
}
}
/**
* Return the system status description
*
* @return the system status
*/
public String getSystemStatus() {
return systemStatus;
}
/**
* Set the system status description text, used by the STAT command handler.
*
* @param systemStatus - the system status description text
*/
public void setSystemStatus(String systemStatus) {
this.systemStatus = systemStatus;
}
}