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.session.Session;
22
23/**
24 * CommandHandler for the LIST command. Return the configured directory listing on the data
25 * connection, along with two replies on the control connection: a reply code of 150 and
26 * another of 226. By default, return an empty directory listing. You can customize the
27 * returned directory listing by setting the <code>directoryListing</code> property.
28 * <p/>
29 * The interpretation of the value returned from this command is dependent upon the value returned
30 * by the SYST command. The format of the directory listing should match the format associated with
31 * the system named by the SYST command. For example, if the SYST command returns "WINDOWS", then
32 * the directory listing value from this command should match the Windows-specific format. See the
33 * <code>SystCommandHandler</code> to control the value returned for the SYST command.
34 * <p/>
35 * Here is an example value for <code>directoryListing</code> when the <code>SystCommandHandler</code>
36 * returns "WINDOWS". Note that multiple entries are separated by "\n":
37 * <code><pre>
38 *      CommandHandler listCommandHandler = new ListCommandHandler();
39 *      listCommandHandler.setDirectoryListing("11-09-01 12:30PM 406348 File2350.log\n" +
40 *          "11-01-01 1:30PM &lt;DIR&gt;  archive");
41 * </pre></code>
42 * <p/>
43 * And here is an example value for <code>directoryListing</code> when the <code>SystCommandHandler</code>
44 * returns "UNIX". Note that multiple entries are separated by "\n":
45 * <code><pre>
46 *      CommandHandler listCommandHandler = new ListCommandHandler();
47 *      listCommandHandler.setDirectoryListing("drwxrwxrwx  1 none     none                   0 Mar 20  2010 archive\n" +
48 *          "-rwxrwxrwx  1 none     none                  19 Mar 20  2010 abc.txt");
49 * </pre></code>
50 * <p/>
51 * Each invocation record stored by this CommandHandler includes the following data element key/values:
52 * <ul>
53 * <li>{@link #PATHNAME_KEY} ("pathname") - the pathname of the directory (or file) submitted on the
54 * invocation (the first command parameter); this parameter is optional, so the value may be null.
55 * </ul>
56 *
57 * @author Chris Mair
58 * @version $Revision$ - $Date$
59 * @see SystCommandHandler
60 */
61public class ListCommandHandler extends AbstractStubDataCommandHandler implements CommandHandler {
62
63    public static final String PATHNAME_KEY = "pathname";
64
65    private String directoryListing = "";
66
67    /**
68     * @see org.mockftpserver.stub.command.AbstractStubDataCommandHandler#beforeProcessData(org.mockftpserver.core.command.Command, org.mockftpserver.core.session.Session, org.mockftpserver.core.command.InvocationRecord)
69     */
70    protected void beforeProcessData(Command command, Session session, InvocationRecord invocationRecord) throws Exception {
71        invocationRecord.set(PATHNAME_KEY, command.getOptionalString(0));
72    }
73
74    /**
75     * @see org.mockftpserver.stub.command.AbstractStubDataCommandHandler#processData(org.mockftpserver.core.command.Command, org.mockftpserver.core.session.Session, org.mockftpserver.core.command.InvocationRecord)
76     */
77    protected void processData(Command command, Session session, InvocationRecord invocationRecord) {
78        session.sendData(directoryListing.getBytes(), directoryListing.length());
79    }
80
81    /**
82     * Set the contents of the directoryListing to send back on the data connection for this command.
83     * The passed-in value is trimmed automatically.
84     *
85     * @param directoryListing - the directoryListing to set
86     */
87    public void setDirectoryListing(String directoryListing) {
88        this.directoryListing = directoryListing.trim();
89    }
90
91}
92