1ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair/*
2ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair * Copyright 2008 the original author or authors.
3ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair *
4ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair * Licensed under the Apache License, Version 2.0 (the "License");
5ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair * you may not use this file except in compliance with the License.
6ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair * You may obtain a copy of the License at
7ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair *
8ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair *      http://www.apache.org/licenses/LICENSE-2.0
9ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair *
10ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair * Unless required by applicable law or agreed to in writing, software
11ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair * distributed under the License is distributed on an "AS IS" BASIS,
12ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair * See the License for the specific language governing permissions and
14ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair * limitations under the License.
15ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair */
16ef13cb2302efe30bdff397e11aae379cbb419b9bchrismairpackage org.mockftpserver.fake.command;
17ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair
18ef13cb2302efe30bdff397e11aae379cbb419b9bchrismairimport org.mockftpserver.core.command.Command;
19ef13cb2302efe30bdff397e11aae379cbb419b9bchrismairimport org.mockftpserver.core.command.ReplyCodes;
20ef13cb2302efe30bdff397e11aae379cbb419b9bchrismairimport org.mockftpserver.core.session.Session;
21ef13cb2302efe30bdff397e11aae379cbb419b9bchrismairimport org.mockftpserver.core.util.StringUtil;
22ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair
23ef13cb2302efe30bdff397e11aae379cbb419b9bchrismairimport java.util.Arrays;
24ef13cb2302efe30bdff397e11aae379cbb419b9bchrismairimport java.util.List;
25ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair
26ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair/**
27ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair * CommandHandler for the HELP command. Handler logic:
28ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair * <ol>
29ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair * <li>If the optional command-name parameter is specified, then reply with 214 along with the
30ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair * help text configured for that command (or empty if none)</li>
31ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair * <li>Otherwise, reply with 214 along with the configured default help text that has been configured
32ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair * (or empty if none)</li>
33ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair * </ol>
34c561a9b3509b5021ddb2e2f8165e903909281269chrismair * <p/>
35c561a9b3509b5021ddb2e2f8165e903909281269chrismair * The help text is configured within the {@link org.mockftpserver.fake.FakeFtpServer}.
36ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair *
37ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair * @author Chris Mair
382a0a3f946dba517a01cc26278f905156857c9c91chrismair * @version $Revision$ - $Date$
39bd576ae311a45a994ae6b457fb2e5bb0ffe0d6b5chrismair * @see org.mockftpserver.fake.ServerConfiguration
40bd576ae311a45a994ae6b457fb2e5bb0ffe0d6b5chrismair * @see org.mockftpserver.fake.FakeFtpServer
41ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair */
42ef13cb2302efe30bdff397e11aae379cbb419b9bchrismairpublic class HelpCommandHandler extends AbstractFakeCommandHandler {
43ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair
44ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair    protected void handle(Command command, Session session) {
45ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair        List parameters = Arrays.asList(command.getParameters());
46ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair        String key = StringUtil.join(parameters, " ");
47ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair        String help = getServerConfiguration().getHelpText(key);
48ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair        if (help == null) {
49ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair            sendReply(session, ReplyCodes.HELP_OK, "help.noHelpTextDefined", list(key));
50ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair        } else {
51ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair            sendReply(session, ReplyCodes.HELP_OK, "help", list(help));
52ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair        }
53ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair    }
54ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair
55ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair}