1e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair/* 2e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair * Copyright 2007 the original author or authors. 3e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair * 4e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair * Licensed under the Apache License, Version 2.0 (the "License"); 5e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair * you may not use this file except in compliance with the License. 6e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair * You may obtain a copy of the License at 7e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair * 8e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair * http://www.apache.org/licenses/LICENSE-2.0 9e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair * 10e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair * Unless required by applicable law or agreed to in writing, software 11e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair * distributed under the License is distributed on an "AS IS" BASIS, 12e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair * See the License for the specific language governing permissions and 14e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair * limitations under the License. 15e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair */ 16e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismairpackage org.mockftpserver.stub.command; 17e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair 18e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismairimport org.mockftpserver.core.command.Command; 19e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismairimport org.mockftpserver.core.command.CommandHandler; 20e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismairimport org.mockftpserver.core.command.InvocationRecord; 21e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismairimport org.mockftpserver.core.session.Session; 22e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair 23e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair/** 24e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair * CommandHandler for the NLST command. Return the configured directory listing on the data 25e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair * connection, along with two replies on the control connection: a reply code of 150 and 26e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair * another of 226. By default, return an empty directory listing. You can customize the 27e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair * returned directory listing by setting the <code>directoryListing</code> property. 28e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair * <p> 29e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair * Each invocation record stored by this CommandHandler includes the following data element key/values: 30e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair * <ul> 31e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair * <li>{@link #PATHNAME_KEY} ("pathname") - the pathname of the directory (or file) submitted on the 32e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair * invocation (the first command parameter); this parameter is optional, so the value may be null. 33e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair * </ul> 34e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair * 35e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair * @version $Revision$ - $Date$ 36e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair * 37e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair * @author Chris Mair 38e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair */ 39e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismairpublic final class NlstCommandHandler extends AbstractStubDataCommandHandler implements CommandHandler { 40e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair 41e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair public static final String PATHNAME_KEY = "pathname"; 42e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair 43e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair private String directoryListing = ""; 44e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair 45e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair /** 46e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair * @see org.mockftpserver.stub.command.AbstractStubDataCommandHandler#beforeProcessData(org.mockftpserver.core.command.Command, org.mockftpserver.core.session.Session, org.mockftpserver.core.command.InvocationRecord) 47e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair */ 48e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair protected void beforeProcessData(Command command, Session session, InvocationRecord invocationRecord) throws Exception { 49e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair invocationRecord.set(PATHNAME_KEY, command.getOptionalString(0)); 50e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair } 51e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair 52e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair /** 53e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair * @see org.mockftpserver.stub.command.AbstractStubDataCommandHandler#processData(org.mockftpserver.core.command.Command, org.mockftpserver.core.session.Session, org.mockftpserver.core.command.InvocationRecord) 54e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair */ 55e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair protected void processData(Command command, Session session, InvocationRecord invocationRecord) { 56e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair session.sendData(directoryListing.getBytes(), directoryListing.length()); 57e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair } 58e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair 59e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair /** 60e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair * Set the contents of the directoryListing to send back on the data connection for this command. 61e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair * The passed-in value is trimmed automatically. 62e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair * @param directoryListing - the directoryListing to set 63e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair */ 64e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair public void setDirectoryListing(String directoryListing) { 65e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair this.directoryListing = directoryListing.trim(); 66e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair } 67e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair 68e47352fb2508e2b25f003b8df12fa79c3215b4b1chrismair} 69