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 * @author Chris Mair
37 * @version $Revision$ - $Date$
38 * @see SystCommandHandler
39 */
40public class StatCommandHandler extends AbstractStubCommandHandler implements CommandHandler {
41
42    public static final String PATHNAME_KEY = "pathname";
43
44    private String status = "";
45
46    /**
47     * Constructor.
48     */
49    public StatCommandHandler() {
50        // Do not initialize replyCode -- will be set dynamically
51    }
52
53    public void handleCommand(Command command, Session session, InvocationRecord invocationRecord) {
54        String pathname = command.getOptionalString(0);
55        invocationRecord.set(PATHNAME_KEY, pathname);
56
57        // Only use dynamic reply code if the replyCode property was NOT explicitly set
58        if (replyCode == 0) {
59            int code = (pathname == null) ? ReplyCodes.STAT_SYSTEM_OK : ReplyCodes.STAT_FILE_OK;
60            sendReply(session, code, replyMessageKey, replyText, new String[]{status});
61        } else {
62            sendReply(session, status);
63        }
64    }
65
66    /**
67     * Set the contents of the status to send back as the reply text for this command
68     *
69     * @param status - the status
70     */
71    public void setStatus(String status) {
72        this.status = status;
73    }
74
75}
76