1bda3441225e0607b5ced8b538123fd7c7a417910chrismair/* 2bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Copyright 2007 the original author or authors. 3bda3441225e0607b5ced8b538123fd7c7a417910chrismair * 4bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Licensed under the Apache License, Version 2.0 (the "License"); 5bda3441225e0607b5ced8b538123fd7c7a417910chrismair * you may not use this file except in compliance with the License. 6bda3441225e0607b5ced8b538123fd7c7a417910chrismair * You may obtain a copy of the License at 7bda3441225e0607b5ced8b538123fd7c7a417910chrismair * 8bda3441225e0607b5ced8b538123fd7c7a417910chrismair * http://www.apache.org/licenses/LICENSE-2.0 9bda3441225e0607b5ced8b538123fd7c7a417910chrismair * 10bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Unless required by applicable law or agreed to in writing, software 11bda3441225e0607b5ced8b538123fd7c7a417910chrismair * distributed under the License is distributed on an "AS IS" BASIS, 12bda3441225e0607b5ced8b538123fd7c7a417910chrismair * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13bda3441225e0607b5ced8b538123fd7c7a417910chrismair * See the License for the specific language governing permissions and 14bda3441225e0607b5ced8b538123fd7c7a417910chrismair * limitations under the License. 15bda3441225e0607b5ced8b538123fd7c7a417910chrismair */ 16bda3441225e0607b5ced8b538123fd7c7a417910chrismairpackage org.mockftpserver.stub.command; 17bda3441225e0607b5ced8b538123fd7c7a417910chrismair 18bda3441225e0607b5ced8b538123fd7c7a417910chrismairimport org.mockftpserver.core.command.Command; 19bda3441225e0607b5ced8b538123fd7c7a417910chrismairimport org.mockftpserver.core.command.CommandHandler; 20bda3441225e0607b5ced8b538123fd7c7a417910chrismairimport org.mockftpserver.core.command.InvocationRecord; 21bda3441225e0607b5ced8b538123fd7c7a417910chrismairimport org.mockftpserver.core.session.Session; 22bda3441225e0607b5ced8b538123fd7c7a417910chrismairimport org.mockftpserver.core.util.Assert; 23bda3441225e0607b5ced8b538123fd7c7a417910chrismair 24bda3441225e0607b5ced8b538123fd7c7a417910chrismair/** 25bda3441225e0607b5ced8b538123fd7c7a417910chrismair * CommandHandler for the RETR (Retrieve) command. Return the configured file contents on the data 26bda3441225e0607b5ced8b538123fd7c7a417910chrismair * connection, along with two replies on the control connection: a reply code of 150 and 27bda3441225e0607b5ced8b538123fd7c7a417910chrismair * another of 226. By default, return an empty file (i.e., a zero-length byte[]). You can 28bda3441225e0607b5ced8b538123fd7c7a417910chrismair * customize the returned file contents by setting the <code>fileContents</code> property, 29bda3441225e0607b5ced8b538123fd7c7a417910chrismair * specified either as a String or as a byte array. 30bda3441225e0607b5ced8b538123fd7c7a417910chrismair * <p/> 31bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Each invocation record stored by this CommandHandler includes the following data element key/values: 32bda3441225e0607b5ced8b538123fd7c7a417910chrismair * <ul> 33bda3441225e0607b5ced8b538123fd7c7a417910chrismair * <li>{@link #PATHNAME_KEY} ("pathname") - the pathname of the file submitted on the invocation (the first command parameter) 34bda3441225e0607b5ced8b538123fd7c7a417910chrismair * </ul> 35bda3441225e0607b5ced8b538123fd7c7a417910chrismair * 36bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @author Chris Mair 37bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @version $Revision$ - $Date$ 38bda3441225e0607b5ced8b538123fd7c7a417910chrismair */ 39bda3441225e0607b5ced8b538123fd7c7a417910chrismairpublic class RetrCommandHandler extends AbstractStubDataCommandHandler implements CommandHandler { 40bda3441225e0607b5ced8b538123fd7c7a417910chrismair 41bda3441225e0607b5ced8b538123fd7c7a417910chrismair public static final String PATHNAME_KEY = "pathname"; 42bda3441225e0607b5ced8b538123fd7c7a417910chrismair 43bda3441225e0607b5ced8b538123fd7c7a417910chrismair private byte[] fileContents = new byte[0]; 44bda3441225e0607b5ced8b538123fd7c7a417910chrismair 45bda3441225e0607b5ced8b538123fd7c7a417910chrismair /** 46bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Create new uninitialized instance 47bda3441225e0607b5ced8b538123fd7c7a417910chrismair */ 48bda3441225e0607b5ced8b538123fd7c7a417910chrismair public RetrCommandHandler() { 49bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 50bda3441225e0607b5ced8b538123fd7c7a417910chrismair 51bda3441225e0607b5ced8b538123fd7c7a417910chrismair /** 52bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Create new instance using the specified fileContents 53bda3441225e0607b5ced8b538123fd7c7a417910chrismair * 54bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @param fileContents - the file contents 55bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @throws org.mockftpserver.core.util.AssertFailedException 56bda3441225e0607b5ced8b538123fd7c7a417910chrismair * - if the fileContents is null 57bda3441225e0607b5ced8b538123fd7c7a417910chrismair */ 58bda3441225e0607b5ced8b538123fd7c7a417910chrismair public RetrCommandHandler(String fileContents) { 59bda3441225e0607b5ced8b538123fd7c7a417910chrismair setFileContents(fileContents); 60bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 61bda3441225e0607b5ced8b538123fd7c7a417910chrismair 62bda3441225e0607b5ced8b538123fd7c7a417910chrismair /** 63bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Create new instance using the specified fileContents 64bda3441225e0607b5ced8b538123fd7c7a417910chrismair * 65bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @param fileContents - the file contents 66bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @throws org.mockftpserver.core.util.AssertFailedException 67bda3441225e0607b5ced8b538123fd7c7a417910chrismair * - if the fileContents is null 68bda3441225e0607b5ced8b538123fd7c7a417910chrismair */ 69bda3441225e0607b5ced8b538123fd7c7a417910chrismair public RetrCommandHandler(byte[] fileContents) { 70bda3441225e0607b5ced8b538123fd7c7a417910chrismair setFileContents(fileContents); 71bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 72bda3441225e0607b5ced8b538123fd7c7a417910chrismair 73bda3441225e0607b5ced8b538123fd7c7a417910chrismair /** 74bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @see org.mockftpserver.stub.command.AbstractStubDataCommandHandler#beforeProcessData(org.mockftpserver.core.command.Command, org.mockftpserver.core.session.Session, org.mockftpserver.core.command.InvocationRecord) 75bda3441225e0607b5ced8b538123fd7c7a417910chrismair */ 76bda3441225e0607b5ced8b538123fd7c7a417910chrismair protected void beforeProcessData(Command command, Session session, InvocationRecord invocationRecord) throws Exception { 77bda3441225e0607b5ced8b538123fd7c7a417910chrismair String filename = command.getRequiredParameter(0); 78bda3441225e0607b5ced8b538123fd7c7a417910chrismair invocationRecord.set(PATHNAME_KEY, filename); 79bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 80bda3441225e0607b5ced8b538123fd7c7a417910chrismair 81bda3441225e0607b5ced8b538123fd7c7a417910chrismair /** 82bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @see org.mockftpserver.stub.command.AbstractStubDataCommandHandler#processData(org.mockftpserver.core.command.Command, org.mockftpserver.core.session.Session, org.mockftpserver.core.command.InvocationRecord) 83bda3441225e0607b5ced8b538123fd7c7a417910chrismair */ 84bda3441225e0607b5ced8b538123fd7c7a417910chrismair protected void processData(Command command, Session session, InvocationRecord invocationRecord) { 85bda3441225e0607b5ced8b538123fd7c7a417910chrismair LOG.info("Sending " + fileContents.length + " bytes"); 86bda3441225e0607b5ced8b538123fd7c7a417910chrismair session.sendData(fileContents, fileContents.length); 87bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 88bda3441225e0607b5ced8b538123fd7c7a417910chrismair 89bda3441225e0607b5ced8b538123fd7c7a417910chrismair /** 90bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Set the file contents to return from subsequent command invocations 91bda3441225e0607b5ced8b538123fd7c7a417910chrismair * 92bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @param fileContents - the fileContents to set 93bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @throws org.mockftpserver.core.util.AssertFailedException 94bda3441225e0607b5ced8b538123fd7c7a417910chrismair * - if the fileContents is null 95bda3441225e0607b5ced8b538123fd7c7a417910chrismair */ 96bda3441225e0607b5ced8b538123fd7c7a417910chrismair public void setFileContents(String fileContents) { 97bda3441225e0607b5ced8b538123fd7c7a417910chrismair Assert.notNull(fileContents, "fileContents"); 98bda3441225e0607b5ced8b538123fd7c7a417910chrismair setFileContents(fileContents.getBytes()); 99bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 100bda3441225e0607b5ced8b538123fd7c7a417910chrismair 101bda3441225e0607b5ced8b538123fd7c7a417910chrismair /** 102bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Set the file contents to return from subsequent command invocations 103bda3441225e0607b5ced8b538123fd7c7a417910chrismair * 104bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @param fileContents - the file contents 105bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @throws org.mockftpserver.core.util.AssertFailedException 106bda3441225e0607b5ced8b538123fd7c7a417910chrismair * - if the fileContents is null 107bda3441225e0607b5ced8b538123fd7c7a417910chrismair */ 108bda3441225e0607b5ced8b538123fd7c7a417910chrismair public void setFileContents(byte[] fileContents) { 109bda3441225e0607b5ced8b538123fd7c7a417910chrismair Assert.notNull(fileContents, "fileContents"); 110bda3441225e0607b5ced8b538123fd7c7a417910chrismair this.fileContents = fileContents; 111bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 112bda3441225e0607b5ced8b538123fd7c7a417910chrismair 113bda3441225e0607b5ced8b538123fd7c7a417910chrismair} 114