StubFtpServer.java revision abc66ab652b34d39ea5a00a75b1d7c7cc157a84f
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;
25import java.util.ResourceBundle;
26
27/**
28 * <b>StubFtpServer</b> is the top-level class for a "stub" implementation of an FTP Server,
29 * suitable for testing FTP client code or standing in for a live FTP server. It supports
30 * the main FTP commands by defining handlers for each of the corresponding low-level FTP
31 * server commands (e.g. RETR, DELE, LIST). These handlers implement the {@link CommandHandler}
32 * interface.
33 * <p/>
34 * <b>StubFtpServer</b> works out of the box with default command handlers that return
35 * success reply codes and empty data (for retrieved files, directory listings, etc.).
36 * The command handler for any command can be easily configured to return custom data
37 * or reply codes. Or it can be replaced with a custom {@link CommandHandler}
38 * implementation. This allows simulation of a complete range of both success and
39 * failure scenarios. The command handlers can also be interrogated to verify command
40 * invocation data such as command parameters and timestamps.
41 * <p/>
42 * <b>StubFtpServer</b> can be fully configured programmatically or within a Spring Framework
43 * ({@link http://www.springframework.org/}) or similar container.
44 * <p/>
45 * <h4>Starting the StubFtpServer</h4>
46 * Here is how to start the <b>StubFtpServer</b> with the default configuration.
47 * <pre><code>
48 * StubFtpServer stubFtpServer = new StubFtpServer();
49 * stubFtpServer.start();
50 * </code></pre>
51 * <p/>
52 * <h4>Retrieving Command Handlers</h4>
53 * You can retrieve the existing {@link CommandHandler} defined for an FTP server command
54 * by calling the {@link #getCommandHandler(String)} method, passing in the FTP server
55 * command name. For example:
56 * <pre><code>
57 * PwdCommandHandler pwdCommandHandler = (PwdCommandHandler) stubFtpServer.getCommandHandler("PWD");
58 * </code></pre>
59 * <p/>
60 * <h4>Replacing Command Handlers</h4>
61 * You can replace the existing {@link CommandHandler} defined for an FTP server command
62 * by calling the {@link #setCommandHandler(String, CommandHandler)} method, passing
63 * in the FTP server command name and {@link CommandHandler} instance. For example:
64 * <pre><code>
65 * PwdCommandHandler pwdCommandHandler = new PwdCommandHandler();
66 * pwdCommandHandler.setDirectory("some/dir");
67 * stubFtpServer.setCommandHandler("PWD", pwdCommandHandler);
68 * </code></pre>
69 * You can also replace multiple command handlers at once by using the {@link #setCommandHandlers(Map)}
70 * method. That is especially useful when configuring the server through the <b>Spring Framework</b>.
71 * <h4>FTP Command Reply Text ResourceBundle</h4>
72 * <p/>
73 * The default text asociated with each FTP command reply code is contained within the
74 * "ReplyText.properties" ResourceBundle file. You can customize these messages by providing a
75 * locale-specific ResourceBundle file on the CLASSPATH, according to the normal lookup rules of
76 * the ResourceBundle class (e.g., "ReplyText_de.properties"). Alternatively, you can
77 * completely replace the ResourceBundle file by calling the calling the
78 * {@link #setReplyTextBaseName(String)} method.
79 *
80 * @author Chris Mair
81 * @version $Revision$ - $Date$
82 */
83public class StubFtpServer extends AbstractFtpServer {
84
85    ResourceBundle replyTextBundle;             // non-private for testing only
86
87    /**
88     * Create a new instance. Initialize the default command handlers and
89     * reply text ResourceBundle.
90     */
91    public StubFtpServer() {
92        replyTextBundle = ResourceBundle.getBundle(REPLY_TEXT_BASENAME);
93
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.HELP, new HelpCommandHandler());
107        setCommandHandler(CommandNames.LIST, new ListCommandHandler());
108        setCommandHandler(CommandNames.MKD, new MkdCommandHandler());
109        setCommandHandler(CommandNames.MODE, new ModeCommandHandler());
110        setCommandHandler(CommandNames.NOOP, new NoopCommandHandler());
111        setCommandHandler(CommandNames.NLST, new NlstCommandHandler());
112        setCommandHandler(CommandNames.PASS, new PassCommandHandler());
113        setCommandHandler(CommandNames.PASV, new PasvCommandHandler());
114        setCommandHandler(CommandNames.PORT, new PortCommandHandler());
115        setCommandHandler(CommandNames.RETR, new RetrCommandHandler());
116        setCommandHandler(CommandNames.QUIT, new QuitCommandHandler());
117        setCommandHandler(CommandNames.REIN, new ReinCommandHandler());
118        setCommandHandler(CommandNames.REST, new RestCommandHandler());
119        setCommandHandler(CommandNames.RMD, new RmdCommandHandler());
120        setCommandHandler(CommandNames.RNFR, new RnfrCommandHandler());
121        setCommandHandler(CommandNames.RNTO, new RntoCommandHandler());
122        setCommandHandler(CommandNames.SITE, new SiteCommandHandler());
123        setCommandHandler(CommandNames.SMNT, new SmntCommandHandler());
124        setCommandHandler(CommandNames.STAT, new StatCommandHandler());
125        setCommandHandler(CommandNames.STOR, new StorCommandHandler());
126        setCommandHandler(CommandNames.STOU, new StouCommandHandler());
127        setCommandHandler(CommandNames.STRU, new StruCommandHandler());
128        setCommandHandler(CommandNames.SYST, new SystCommandHandler());
129        setCommandHandler(CommandNames.TYPE, new TypeCommandHandler());
130        setCommandHandler(CommandNames.USER, new UserCommandHandler());
131        setCommandHandler(CommandNames.XPWD, pwdCommandHandler);           // same as PWD
132    }
133
134    /**
135     * Set the reply text ResourceBundle to a new ResourceBundle with the specified base name,
136     * accessible on the CLASSPATH. See {@link ResourceBundle#getBundle(String)}.
137     *
138     * @param baseName - the base name of the resource bundle, a fully qualified class name
139     */
140    public void setReplyTextBaseName(String baseName) {
141        replyTextBundle = ResourceBundle.getBundle(baseName);
142    }
143
144    //-------------------------------------------------------------------------
145    // Abstract method implementation
146    //-------------------------------------------------------------------------
147
148    protected void initializeCommandHandler(CommandHandler commandHandler) {
149        ReplyTextBundleUtil.setReplyTextBundleIfAppropriate(commandHandler, replyTextBundle);
150    }
151
152}