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 */ 16package org.mockftpserver.stub.command; 17 18import org.mockftpserver.core.MockFtpServerException; 19import org.mockftpserver.core.command.Command; 20import org.mockftpserver.core.command.CommandHandler; 21import org.mockftpserver.core.command.InvocationRecord; 22import org.mockftpserver.core.session.Session; 23import org.mockftpserver.core.util.Assert; 24import org.mockftpserver.core.util.AssertFailedException; 25 26import java.io.IOException; 27import java.io.InputStream; 28 29/** 30 * CommandHandler for the RETR command. Returns the contents of the specified file on the 31 * data connection, along with two replies on the control connection: a reply code of 150 and 32 * another of 226. 33 * <p/> 34 * The <code>file</code> property specifies the pathname for the file whose contents should 35 * be returned from this command. The file path is relative to the CLASSPATH (using the 36 * ClassLoader for this class). 37 * <p/> 38 * An exception is thrown if the <code>file</code> property has not been set or if the specified 39 * file does not exist or cannot be read. 40 * <p/> 41 * Each invocation record stored by this CommandHandler includes the following data element key/values: 42 * <ul> 43 * <li>{@link #PATHNAME_KEY} ("pathname") - the pathname of the file submitted on the invocation (the first command parameter) 44 * </ul> 45 * 46 * @author Chris Mair 47 * @version $Revision$ - $Date$ 48 */ 49public class FileRetrCommandHandler extends AbstractStubDataCommandHandler implements CommandHandler { 50 51 public static final String PATHNAME_KEY = "pathname"; 52 static final int BUFFER_SIZE = 512; // package-private for testing 53 54 private String file; 55 56 /** 57 * Create new uninitialized instance 58 */ 59 public FileRetrCommandHandler() { 60 } 61 62 /** 63 * Create new instance using the specified file pathname 64 * 65 * @param file - the path to the file 66 * @throws AssertFailedException - if the file is null 67 */ 68 public FileRetrCommandHandler(String file) { 69 setFile(file); 70 } 71 72 /** 73 * @see org.mockftpserver.stub.command.AbstractStubDataCommandHandler#beforeProcessData(org.mockftpserver.core.command.Command, org.mockftpserver.core.session.Session, org.mockftpserver.core.command.InvocationRecord) 74 */ 75 protected void beforeProcessData(Command command, Session session, InvocationRecord invocationRecord) throws Exception { 76 Assert.notNull(file, "file"); 77 invocationRecord.set(PATHNAME_KEY, command.getRequiredParameter(0)); 78 } 79 80 /** 81 * @see org.mockftpserver.stub.command.AbstractStubDataCommandHandler#processData(org.mockftpserver.core.command.Command, org.mockftpserver.core.session.Session, org.mockftpserver.core.command.InvocationRecord) 82 */ 83 protected void processData(Command command, Session session, InvocationRecord invocationRecord) { 84 InputStream inputStream = getClass().getClassLoader().getResourceAsStream(file); 85 Assert.notNull(inputStream, "InputStream for [" + file + "]"); 86 byte[] buffer = new byte[BUFFER_SIZE]; 87 try { 88 int numBytes; 89 while ((numBytes = inputStream.read(buffer)) != -1) { 90 LOG.trace("Sending " + numBytes + " bytes..."); 91 session.sendData(buffer, numBytes); 92 } 93 } 94 catch (IOException e) { 95 throw new MockFtpServerException(e); 96 } 97 } 98 99 /** 100 * Set the path of the file whose contents should be returned when this command is 101 * invoked. The path is relative to the CLASSPATH. 102 * 103 * @param file - the path to the file 104 * @throws AssertFailedException - if the file is null 105 */ 106 public void setFile(String file) { 107 Assert.notNull(file, "file"); 108 this.file = file; 109 } 110 111} 112