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.command;
17
18import org.mockftpserver.core.command.Command;
19import org.mockftpserver.core.command.CommandHandler;
20import org.mockftpserver.core.command.InvocationRecord;
21import org.mockftpserver.core.command.ReplyCodes;
22import org.mockftpserver.core.session.Session;
23
24/**
25 * CommandHandler for the STAT (Status) command. By default, return empty status information,
26 * along with a reply code of 211 if no pathname parameter is specified or 213 if a
27 * pathname is specified. You can customize the returned status information by setting
28 * the <code>status</code> property.
29 * <p>
30 * Each invocation record stored by this CommandHandler includes the following data element key/values:
31 * <ul>
32 *    <li>{@link #PATHNAME_KEY} ("pathname") - the pathname of the directory (or file) submitted on the
33 *          invocation (the first command parameter); this parameter is optional, so the value may be null.
34 * </ul>
35 *
36 * @see SystCommandHandler
37 *
38 * @version $Revision$ - $Date$
39 *
40 * @author Chris Mair
41 */
42public final class StatCommandHandler extends AbstractStubCommandHandler implements CommandHandler {
43
44    public static final String PATHNAME_KEY = "pathname";
45
46    private String status = "";
47
48    /**
49     * Constructor.
50     */
51    public StatCommandHandler() {
52        // Do not initialize replyCode -- will be set dynamically
53    }
54
55    /**
56     * @see org.mockftpserver.core.command.CommandHandler#handleCommand(Command, Session, InvocationRecord)
57     */
58    public void handleCommand(Command command, Session session, InvocationRecord invocationRecord) {
59        String pathname = command.getOptionalString(0);
60        invocationRecord.set(PATHNAME_KEY, pathname);
61
62        // Only use dynamic reply code if the replyCode property was NOT explicitly set
63        if (replyCode == 0) {
64            int code = (pathname == null) ? ReplyCodes.STAT_SYSTEM_OK : ReplyCodes.STAT_FILE_OK;
65            sendReply(session, code, replyMessageKey, replyText, new String[] { status });
66        }
67        else {
68            sendReply(session, status);
69        }
70    }
71
72    /**
73     * Set the contents of the status to send back as the reply text for this command
74     * @param status - the status
75     */
76    public void setStatus(String status) {
77        this.status = status;
78    }
79
80}
81