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