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 * @author Chris Mair 36 * @version $Revision$ - $Date$ 37 */ 38 public class NlstCommandHandler extends AbstractStubDataCommandHandler implements CommandHandler { 39 40 public static final String PATHNAME_KEY = "pathname"; 41 42 private String directoryListing = ""; 43 44 /** 45 * @see org.mockftpserver.stub.command.AbstractStubDataCommandHandler#beforeProcessData(org.mockftpserver.core.command.Command, org.mockftpserver.core.session.Session, org.mockftpserver.core.command.InvocationRecord) 46 */ 47 protected void beforeProcessData(Command command, Session session, InvocationRecord invocationRecord) throws Exception { 48 invocationRecord.set(PATHNAME_KEY, command.getOptionalString(0)); 49 } 50 51 /** 52 * @see org.mockftpserver.stub.command.AbstractStubDataCommandHandler#processData(org.mockftpserver.core.command.Command, org.mockftpserver.core.session.Session, org.mockftpserver.core.command.InvocationRecord) 53 */ 54 protected void processData(Command command, Session session, InvocationRecord invocationRecord) { 55 session.sendData(directoryListing.getBytes(), directoryListing.length()); 56 } 57 58 /** 59 * Set the contents of the directoryListing to send back on the data connection for this command. 60 * The passed-in value is trimmed automatically. 61 * 62 * @param directoryListing - the directoryListing to set 63 */ 64 public void setDirectoryListing(String directoryListing) { 65 this.directoryListing = directoryListing.trim(); 66 } 67 68 } 69