117f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair/*
217f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair * Copyright 2007 the original author or authors.
317f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair *
417f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair * Licensed under the Apache License, Version 2.0 (the "License");
517f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair * you may not use this file except in compliance with the License.
617f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair * You may obtain a copy of the License at
717f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair *
817f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair *      http://www.apache.org/licenses/LICENSE-2.0
917f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair *
1017f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair * Unless required by applicable law or agreed to in writing, software
1117f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair * distributed under the License is distributed on an "AS IS" BASIS,
1217f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1317f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair * See the License for the specific language governing permissions and
1417f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair * limitations under the License.
1517f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair */
1617f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismairpackage org.mockftpserver.stub.command;
1717f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair
1817f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismairimport org.mockftpserver.core.command.Command;
1917f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismairimport org.mockftpserver.core.command.CommandHandler;
2017f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismairimport org.mockftpserver.core.command.InvocationRecord;
2117f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismairimport org.mockftpserver.core.session.Session;
2217f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair
2317f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair/**
2417f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair * CommandHandler for the LIST command. Return the configured directory listing on the data
2517f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair * connection, along with two replies on the control connection: a reply code of 150 and
2617f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair * another of 226. By default, return an empty directory listing. You can customize the
2717f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair * returned directory listing by setting the <code>directoryListing</code> property.
2817f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair * <p>
2917f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair * The interpretation of the value returned from this command is dependent upon the value returned
3017f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair * by the SYST command. The format of the directory listing should match the format associated with
3117f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair * the system named by the SYST command. For example, if the SYST command returns "WINDOWS", then
3217f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair * the directory listing value from this command should match the Windows-specific format.
3317f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair * <p>
3417f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair * Each invocation record stored by this CommandHandler includes the following data element key/values:
3517f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair * <ul>
3617f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair *    <li>{@link #PATHNAME_KEY} ("pathname") - the pathname of the directory (or file) submitted on the
3717f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair *          invocation (the first command parameter); this parameter is optional, so the value may be null.
3817f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair * </ul>
3917f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair *
4017f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair * @see SystCommandHandler
4117f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair *
4217f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair * @version $Revision$ - $Date$
4317f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair *
4417f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair * @author Chris Mair
4517f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair */
4617f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismairpublic final class ListCommandHandler extends AbstractStubDataCommandHandler implements CommandHandler {
4717f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair
4817f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair    public static final String PATHNAME_KEY = "pathname";
4917f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair
5017f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair    private String directoryListing = "";
5117f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair
5217f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair    /**
5317f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair     * @see org.mockftpserver.stub.command.AbstractStubDataCommandHandler#beforeProcessData(org.mockftpserver.core.command.Command, org.mockftpserver.core.session.Session, org.mockftpserver.core.command.InvocationRecord)
5417f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair     */
5517f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair    protected void beforeProcessData(Command command, Session session, InvocationRecord invocationRecord) throws Exception {
5617f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair        invocationRecord.set(PATHNAME_KEY, command.getOptionalString(0));
5717f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair    }
5817f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair
5917f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair    /**
6017f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair     * @see org.mockftpserver.stub.command.AbstractStubDataCommandHandler#processData(org.mockftpserver.core.command.Command, org.mockftpserver.core.session.Session, org.mockftpserver.core.command.InvocationRecord)
6117f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair     */
6217f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair    protected void processData(Command command, Session session, InvocationRecord invocationRecord) {
6317f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair        session.sendData(directoryListing.getBytes(), directoryListing.length());
6417f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair    }
6517f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair
6617f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair    /**
6717f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair     * Set the contents of the directoryListing to send back on the data connection for this command.
6817f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair     * The passed-in value is trimmed automatically.
6917f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair     * @param directoryListing - the directoryListing to set
7017f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair     */
7117f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair    public void setDirectoryListing(String directoryListing) {
7217f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair        this.directoryListing = directoryListing.trim();
7317f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair    }
7417f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair
7517f899cea435aaf91624af2a93bc24bcfcd5fc2dchrismair}
76