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.apache.log4j.Logger; 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; 24 25/** 26 * CommandHandler for the RETR (Retrieve) command. Return the configured file contents on the data 27 * connection, along with two replies on the control connection: a reply code of 150 and 28 * another of 226. By default, return an empty file (i.e., a zero-length byte[]). You can 29 * customize the returned file contents by setting the <code>fileContents</code> property, 30 * specified either as a String or as a byte array. 31 * <p> 32 * Each invocation record stored by this CommandHandler includes the following data element key/values: 33 * <ul> 34 * <li>{@link #PATHNAME_KEY} ("pathname") - the pathname of the file submitted on the invocation (the first command parameter) 35 * </ul> 36 * 37 * @version $Revision$ - $Date$ 38 * 39 * @author Chris Mair 40 */ 41public final class RetrCommandHandler extends AbstractStubDataCommandHandler implements CommandHandler { 42 43 private static final Logger LOG = Logger.getLogger(RetrCommandHandler.class); 44 public static final String PATHNAME_KEY = "pathname"; 45 46 private byte[] fileContents = new byte[0]; 47 48 /** 49 * Create new uninitialized instance 50 */ 51 public RetrCommandHandler() { 52 } 53 54 /** 55 * Create new instance using the specified fileContents 56 * @param fileContents - the file contents 57 * @throws AssertFailedException - if the fileContents is null 58 */ 59 public RetrCommandHandler(String fileContents) { 60 setFileContents(fileContents); 61 } 62 63 /** 64 * Create new instance using the specified fileContents 65 * @param fileContents - the file contents 66 * @throws AssertFailedException - if the fileContents is null 67 */ 68 public RetrCommandHandler(byte[] fileContents) { 69 setFileContents(fileContents); 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 String filename = command.getRequiredString(0); 77 invocationRecord.set(PATHNAME_KEY, filename); 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 LOG.info("Sending " + fileContents.length + " bytes"); 85 session.sendData(fileContents, fileContents.length); 86 } 87 88 /** 89 * Set the file contents to return from subsequent command invocations 90 * @param fileContents - the fileContents to set 91 * @throws AssertFailedException - if the fileContents is null 92 */ 93 public void setFileContents(String fileContents) { 94 Assert.notNull(fileContents, "fileContents"); 95 setFileContents(fileContents.getBytes()); 96 } 97 98 /** 99 * Set the file contents to return from subsequent command invocations 100 * @param fileContents - the file contents 101 * @throws AssertFailedException - if the fileContents is null 102 */ 103 public void setFileContents(byte[] fileContents) { 104 Assert.notNull(fileContents, "fileContents"); 105 this.fileContents = fileContents; 106 } 107 108} 109