ListCommandHandler.java revision 3b7668ee59bf567db6bce593a9090f65db171fdb
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.
33 * <p>
34 * Each invocation record stored by this CommandHandler includes the following data element key/values:
35 * <ul>
36 * <li>{@link #PATHNAME_KEY} ("pathname") - the pathname of the directory (or file) submitted on the
37 * invocation (the first command parameter); this parameter is optional, so the value may be null.
38 * </ul>
39 *
40 * @author Chris Mair
41 * @version $Revision$ - $Date$
42 * @see SystCommandHandler
43 */
44public class ListCommandHandler extends AbstractStubDataCommandHandler implements CommandHandler {
45
46    public static final String PATHNAME_KEY = "pathname";
47
48    private String directoryListing = "";
49
50    /**
51     * @see org.mockftpserver.stub.command.AbstractStubDataCommandHandler#beforeProcessData(org.mockftpserver.core.command.Command, org.mockftpserver.core.session.Session, org.mockftpserver.core.command.InvocationRecord)
52     */
53    protected void beforeProcessData(Command command, Session session, InvocationRecord invocationRecord) throws Exception {
54        invocationRecord.set(PATHNAME_KEY, command.getOptionalString(0));
55    }
56
57    /**
58     * @see org.mockftpserver.stub.command.AbstractStubDataCommandHandler#processData(org.mockftpserver.core.command.Command, org.mockftpserver.core.session.Session, org.mockftpserver.core.command.InvocationRecord)
59     */
60    protected void processData(Command command, Session session, InvocationRecord invocationRecord) {
61        session.sendData(directoryListing.getBytes(), directoryListing.length());
62    }
63
64    /**
65     * Set the contents of the directoryListing to send back on the data connection for this command.
66     * The passed-in value is trimmed automatically.
67     *
68     * @param directoryListing - the directoryListing to set
69     */
70    public void setDirectoryListing(String directoryListing) {
71        this.directoryListing = directoryListing.trim();
72    }
73
74}
75