FakeFtpServer.java revision bd576ae311a45a994ae6b457fb2e5bb0ffe0d6b5
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.server.AbstractFtpServer;
21import org.mockftpserver.fake.command.*;
22import org.mockftpserver.fake.filesystem.FileSystem;
23
24import java.util.HashMap;
25import java.util.List;
26import java.util.Map;
27
28/**
29 * <b>FakeFtpServer</b> is the top-level class for a "fake" implementation of an FTP Server,
30 * suitable for testing FTP client code or standing in for a live FTP server.
31 * <p/>
32 * <b>FakeFtpServer</b> provides a high-level abstraction for an FTP Server and is suitable
33 * for most testing and simulation scenarios. You define a filesystem (internal, in-memory) containing
34 * an arbitrary set of files and directories. These files and directories can (optionally) have
35 * associated access permissions. You also configure a set of one or more user accounts that
36 * control which users can login to the FTP server, and their home (default) directories. The
37 * user account is also used when assigning file and directory ownership for new files.
38 * <p> <b>FakeFtpServer</b> processes FTP client requests and responds with reply codes and
39 * reply messages consistent with its configuration and the contents of its internal filesystem,
40 * including file and directory permissions, if they have been configured.
41 * <p/>
42 * <b>FakeFtpServer</b> can be fully configured programmatically or within the
43 * <b>Spring Framework</b> or other dependency-injection container.
44 * <p/>
45 * In general the steps for setting up and starting the <b>FakeFtpServer</b> are:
46 * <ol>
47 * <li>Create a new <b>FakeFtpServer</b> instance, and optionally set the server control port.</li>
48 * <li>Create and configure a <b>FileSystem</b>, and attach to the <b>FakeFtpServer</b> instance.</li>
49 * <li>Create and configure one or more <b>UserAccount</b> objects and attach to the <b>FakeFtpServer</b> instance.</li>
50 * </ol>
51 * <h4>Example Code</h4>
52 * <pre><code>
53 * FileSystem fileSystem = new WindowsFakeFileSystem();
54 * DirectoryEntry directoryEntry1 = new DirectoryEntry("c:\\");
55 * directoryEntry1.setPermissions(new Permissions("rwxrwx---"));
56 * directoryEntry1.setOwner("joe");
57 * directoryEntry1.setGroup("dev");
58 * <p/>
59 * DirectoryEntry directoryEntry2 = new DirectoryEntry("c:\\data");
60 * directoryEntry2.setPermissions(Permissions.ALL);
61 * directoryEntry2.setOwner("joe");
62 * directoryEntry2.setGroup("dev");
63 * <p/>
64 * FileEntry fileEntry1 = new FileEntry("c:\\data\\file1.txt", "abcdef 1234567890");
65 * fileEntry1.setPermissionsFromString("rw-rw-rw-");
66 * fileEntry1.setOwner("joe");
67 * fileEntry1.setGroup("dev");
68 * <p/>
69 * FileEntry fileEntry2 = new FileEntry("c:\\data\\run.exe");
70 * fileEntry2.setPermissionsFromString("rwxrwx---");
71 * fileEntry2.setOwner("mary");
72 * fileEntry2.setGroup("dev");
73 * <p/>
74 * fileSystem.add(directoryEntry1);
75 * fileSystem.add(directoryEntry2);
76 * fileSystem.add(fileEntry1);
77 * fileSystem.add(fileEntry2);
78 * <p/>
79 * FakeFtpServer fakeFtpServer = new FakeFtpServer();
80 * fakeFtpServer.setFileSystem(fileSystem);
81 * <p/>
82 * UserAccount userAccount = new UserAccount();
83 * userAccount.setUsername("joe");
84 * userAccount.setPassword("joe123");
85 * userAccount.setHomeDirectory("c:\\");
86 * List userAccounts = Collections.singletonList(userAccount);
87 * fakeFtpServer.setUserAccounts(userAccounts);
88 * <p/>
89 * fakeFtpServer.start();
90 * </code></pre>
91 * <p/>
92 * <h4>FTP Command Reply Text ResourceBundle</h4>
93 * <p/>
94 * The default text asociated with each FTP command reply code is contained within the
95 * "ReplyText.properties" ResourceBundle file. You can customize these messages by providing a
96 * locale-specific ResourceBundle file on the CLASSPATH, according to the normal lookup rules of
97 * the ResourceBundle class (e.g., "ReplyText_de.properties"). Alternatively, you can
98 * completely replace the ResourceBundle file by calling the calling the
99 * {@link #setReplyTextBaseName(String)} method.
100 *
101 * @author Chris Mair
102 * @version $Revision: 143 $ - $Date: 2008-10-31 21:07:23 -0400 (Fri, 31 Oct 2008) $
103 */
104public class FakeFtpServer extends AbstractFtpServer implements ServerConfiguration {
105
106    private FileSystem fileSystem;
107    private String systemName = "WINDOWS";
108    private Map helpText = new HashMap();
109    private Map userAccounts = new HashMap();
110
111    public FileSystem getFileSystem() {
112        return fileSystem;
113    }
114
115    public void setFileSystem(FileSystem fileSystem) {
116        this.fileSystem = fileSystem;
117    }
118
119    public String getSystemName() {
120        return systemName;
121    }
122
123    public void setSystemName(String systemName) {
124        this.systemName = systemName;
125    }
126
127    public Map getHelpText() {
128        return helpText;
129    }
130
131    public void setHelpText(Map helpText) {
132        this.helpText = helpText;
133    }
134
135    public FakeFtpServer() {
136        setCommandHandler(CommandNames.ACCT, new AcctCommandHandler());
137        setCommandHandler(CommandNames.ABOR, new AborCommandHandler());
138        setCommandHandler(CommandNames.ALLO, new AlloCommandHandler());
139        setCommandHandler(CommandNames.APPE, new AppeCommandHandler());
140        setCommandHandler(CommandNames.CONNECT, new ConnectCommandHandler());
141        setCommandHandler(CommandNames.CWD, new CwdCommandHandler());
142        setCommandHandler(CommandNames.CDUP, new CdupCommandHandler());
143        setCommandHandler(CommandNames.DELE, new DeleCommandHandler());
144        setCommandHandler(CommandNames.HELP, new HelpCommandHandler());
145        setCommandHandler(CommandNames.LIST, new ListCommandHandler());
146        setCommandHandler(CommandNames.MKD, new MkdCommandHandler());
147        setCommandHandler(CommandNames.MODE, new ModeCommandHandler());
148        setCommandHandler(CommandNames.NLST, new NlstCommandHandler());
149        setCommandHandler(CommandNames.NOOP, new NoopCommandHandler());
150        setCommandHandler(CommandNames.PASS, new PassCommandHandler());
151        setCommandHandler(CommandNames.PASV, new PasvCommandHandler());
152        setCommandHandler(CommandNames.PWD, new PwdCommandHandler());
153        setCommandHandler(CommandNames.PORT, new PortCommandHandler());
154        setCommandHandler(CommandNames.QUIT, new QuitCommandHandler());
155        setCommandHandler(CommandNames.REIN, new ReinCommandHandler());
156        setCommandHandler(CommandNames.REST, new RestCommandHandler());
157        setCommandHandler(CommandNames.RETR, new RetrCommandHandler());
158        setCommandHandler(CommandNames.RMD, new RmdCommandHandler());
159        setCommandHandler(CommandNames.RNFR, new RnfrCommandHandler());
160        setCommandHandler(CommandNames.RNTO, new RntoCommandHandler());
161        setCommandHandler(CommandNames.SITE, new SiteCommandHandler());
162        setCommandHandler(CommandNames.STOR, new StorCommandHandler());
163        setCommandHandler(CommandNames.STOU, new StouCommandHandler());
164        setCommandHandler(CommandNames.STRU, new StruCommandHandler());
165        setCommandHandler(CommandNames.SYST, new SystCommandHandler());
166        setCommandHandler(CommandNames.TYPE, new TypeCommandHandler());
167        setCommandHandler(CommandNames.USER, new UserCommandHandler());
168        setCommandHandler(CommandNames.XPWD, new PwdCommandHandler());
169    }
170
171    /**
172     * Initialize a CommandHandler that has been registered to this server. If the CommandHandler implements
173     * the <code>ServerConfigurationAware</code> interface, then set its <code>ServerConfiguration</code>
174     * property to <code>this</code>.
175     *
176     * @param commandHandler - the CommandHandler to initialize
177     */
178    protected void initializeCommandHandler(CommandHandler commandHandler) {
179        if (commandHandler instanceof ServerConfigurationAware) {
180            ServerConfigurationAware sca = (ServerConfigurationAware) commandHandler;
181            sca.setServerConfiguration(this);
182        }
183    }
184
185    /**
186     * @return the {@link UserAccount}        configured for this server for the specified user name
187     */
188    public UserAccount getUserAccount(String username) {
189        return (UserAccount) userAccounts.get(username);
190    }
191
192    /**
193     * Return the help text for a command or the default help text if no command name is specified
194     *
195     * @param name - the command name; may be empty or null to indicate  a request for the default help text
196     * @return the help text for the named command or the default help text if no name is supplied
197     */
198    public String getHelpText(String name) {
199        String key = name == null ? "" : name;
200        return (String) helpText.get(key);
201    }
202
203    /**
204     * Add a single UserAccount. If an account with the same <code>username</code> already exists,
205     * it will be replaced.
206     *
207     * @param userAccount - the UserAccount to add
208     */
209    public void addUserAccount(UserAccount userAccount) {
210        userAccounts.put(userAccount.getUsername(), userAccount);
211    }
212
213    /**
214     * Add the UserAccount objects in the <code>userAccountList</code> to the set of UserAccounts.
215     *
216     * @param userAccountList - the List of UserAccount objects to add
217     */
218    public void setUserAccounts(List userAccountList) {
219        for (int i = 0; i < userAccountList.size(); i++) {
220            UserAccount userAccount = (UserAccount) userAccountList.get(i);
221            userAccounts.put(userAccount.getUsername(), userAccount);
222        }
223    }
224
225}