185efb15529d45e32fea8de03c38a968c157c8262chrismair/* 285efb15529d45e32fea8de03c38a968c157c8262chrismair * Copyright 2007 the original author or authors. 385efb15529d45e32fea8de03c38a968c157c8262chrismair * 485efb15529d45e32fea8de03c38a968c157c8262chrismair * Licensed under the Apache License, Version 2.0 (the "License"); 585efb15529d45e32fea8de03c38a968c157c8262chrismair * you may not use this file except in compliance with the License. 685efb15529d45e32fea8de03c38a968c157c8262chrismair * You may obtain a copy of the License at 785efb15529d45e32fea8de03c38a968c157c8262chrismair * 885efb15529d45e32fea8de03c38a968c157c8262chrismair * http://www.apache.org/licenses/LICENSE-2.0 985efb15529d45e32fea8de03c38a968c157c8262chrismair * 1085efb15529d45e32fea8de03c38a968c157c8262chrismair * Unless required by applicable law or agreed to in writing, software 1185efb15529d45e32fea8de03c38a968c157c8262chrismair * distributed under the License is distributed on an "AS IS" BASIS, 1285efb15529d45e32fea8de03c38a968c157c8262chrismair * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1385efb15529d45e32fea8de03c38a968c157c8262chrismair * See the License for the specific language governing permissions and 1485efb15529d45e32fea8de03c38a968c157c8262chrismair * limitations under the License. 1585efb15529d45e32fea8de03c38a968c157c8262chrismair */ 1685efb15529d45e32fea8de03c38a968c157c8262chrismairpackage org.mockftpserver.stub.command; 1785efb15529d45e32fea8de03c38a968c157c8262chrismair 1885efb15529d45e32fea8de03c38a968c157c8262chrismairimport org.apache.log4j.Logger; 1985efb15529d45e32fea8de03c38a968c157c8262chrismairimport org.mockftpserver.core.command.Command; 2085efb15529d45e32fea8de03c38a968c157c8262chrismairimport org.mockftpserver.core.command.CommandHandler; 2185efb15529d45e32fea8de03c38a968c157c8262chrismairimport org.mockftpserver.core.command.InvocationRecord; 2285efb15529d45e32fea8de03c38a968c157c8262chrismairimport org.mockftpserver.core.session.Session; 2385efb15529d45e32fea8de03c38a968c157c8262chrismairimport org.mockftpserver.core.util.Assert; 2485efb15529d45e32fea8de03c38a968c157c8262chrismair 2585efb15529d45e32fea8de03c38a968c157c8262chrismair/** 2685efb15529d45e32fea8de03c38a968c157c8262chrismair * CommandHandler for the RETR (Retrieve) command. Return the configured file contents on the data 2785efb15529d45e32fea8de03c38a968c157c8262chrismair * connection, along with two replies on the control connection: a reply code of 150 and 2885efb15529d45e32fea8de03c38a968c157c8262chrismair * another of 226. By default, return an empty file (i.e., a zero-length byte[]). You can 2985efb15529d45e32fea8de03c38a968c157c8262chrismair * customize the returned file contents by setting the <code>fileContents</code> property, 3085efb15529d45e32fea8de03c38a968c157c8262chrismair * specified either as a String or as a byte array. 3185efb15529d45e32fea8de03c38a968c157c8262chrismair * <p> 3285efb15529d45e32fea8de03c38a968c157c8262chrismair * Each invocation record stored by this CommandHandler includes the following data element key/values: 3385efb15529d45e32fea8de03c38a968c157c8262chrismair * <ul> 3485efb15529d45e32fea8de03c38a968c157c8262chrismair * <li>{@link #PATHNAME_KEY} ("pathname") - the pathname of the file submitted on the invocation (the first command parameter) 3585efb15529d45e32fea8de03c38a968c157c8262chrismair * </ul> 3685efb15529d45e32fea8de03c38a968c157c8262chrismair * 3785efb15529d45e32fea8de03c38a968c157c8262chrismair * @version $Revision$ - $Date$ 3885efb15529d45e32fea8de03c38a968c157c8262chrismair * 3985efb15529d45e32fea8de03c38a968c157c8262chrismair * @author Chris Mair 4085efb15529d45e32fea8de03c38a968c157c8262chrismair */ 4185efb15529d45e32fea8de03c38a968c157c8262chrismairpublic final class RetrCommandHandler extends AbstractStubDataCommandHandler implements CommandHandler { 4285efb15529d45e32fea8de03c38a968c157c8262chrismair 4385efb15529d45e32fea8de03c38a968c157c8262chrismair private static final Logger LOG = Logger.getLogger(RetrCommandHandler.class); 4485efb15529d45e32fea8de03c38a968c157c8262chrismair public static final String PATHNAME_KEY = "pathname"; 4585efb15529d45e32fea8de03c38a968c157c8262chrismair 4685efb15529d45e32fea8de03c38a968c157c8262chrismair private byte[] fileContents = new byte[0]; 4785efb15529d45e32fea8de03c38a968c157c8262chrismair 4885efb15529d45e32fea8de03c38a968c157c8262chrismair /** 4985efb15529d45e32fea8de03c38a968c157c8262chrismair * Create new uninitialized instance 5085efb15529d45e32fea8de03c38a968c157c8262chrismair */ 5185efb15529d45e32fea8de03c38a968c157c8262chrismair public RetrCommandHandler() { 5285efb15529d45e32fea8de03c38a968c157c8262chrismair } 5385efb15529d45e32fea8de03c38a968c157c8262chrismair 5485efb15529d45e32fea8de03c38a968c157c8262chrismair /** 5585efb15529d45e32fea8de03c38a968c157c8262chrismair * Create new instance using the specified fileContents 5685efb15529d45e32fea8de03c38a968c157c8262chrismair * @param fileContents - the file contents 5785efb15529d45e32fea8de03c38a968c157c8262chrismair * @throws AssertFailedException - if the fileContents is null 5885efb15529d45e32fea8de03c38a968c157c8262chrismair */ 5985efb15529d45e32fea8de03c38a968c157c8262chrismair public RetrCommandHandler(String fileContents) { 6085efb15529d45e32fea8de03c38a968c157c8262chrismair setFileContents(fileContents); 6185efb15529d45e32fea8de03c38a968c157c8262chrismair } 6285efb15529d45e32fea8de03c38a968c157c8262chrismair 6385efb15529d45e32fea8de03c38a968c157c8262chrismair /** 6485efb15529d45e32fea8de03c38a968c157c8262chrismair * Create new instance using the specified fileContents 6585efb15529d45e32fea8de03c38a968c157c8262chrismair * @param fileContents - the file contents 6685efb15529d45e32fea8de03c38a968c157c8262chrismair * @throws AssertFailedException - if the fileContents is null 6785efb15529d45e32fea8de03c38a968c157c8262chrismair */ 6885efb15529d45e32fea8de03c38a968c157c8262chrismair public RetrCommandHandler(byte[] fileContents) { 6985efb15529d45e32fea8de03c38a968c157c8262chrismair setFileContents(fileContents); 7085efb15529d45e32fea8de03c38a968c157c8262chrismair } 7185efb15529d45e32fea8de03c38a968c157c8262chrismair 7285efb15529d45e32fea8de03c38a968c157c8262chrismair /** 7385efb15529d45e32fea8de03c38a968c157c8262chrismair * @see org.mockftpserver.stub.command.AbstractStubDataCommandHandler#beforeProcessData(org.mockftpserver.core.command.Command, org.mockftpserver.core.session.Session, org.mockftpserver.core.command.InvocationRecord) 7485efb15529d45e32fea8de03c38a968c157c8262chrismair */ 7585efb15529d45e32fea8de03c38a968c157c8262chrismair protected void beforeProcessData(Command command, Session session, InvocationRecord invocationRecord) throws Exception { 7685efb15529d45e32fea8de03c38a968c157c8262chrismair String filename = command.getRequiredString(0); 7785efb15529d45e32fea8de03c38a968c157c8262chrismair invocationRecord.set(PATHNAME_KEY, filename); 7885efb15529d45e32fea8de03c38a968c157c8262chrismair } 7985efb15529d45e32fea8de03c38a968c157c8262chrismair 8085efb15529d45e32fea8de03c38a968c157c8262chrismair /** 8185efb15529d45e32fea8de03c38a968c157c8262chrismair * @see org.mockftpserver.stub.command.AbstractStubDataCommandHandler#processData(org.mockftpserver.core.command.Command, org.mockftpserver.core.session.Session, org.mockftpserver.core.command.InvocationRecord) 8285efb15529d45e32fea8de03c38a968c157c8262chrismair */ 8385efb15529d45e32fea8de03c38a968c157c8262chrismair protected void processData(Command command, Session session, InvocationRecord invocationRecord) { 8485efb15529d45e32fea8de03c38a968c157c8262chrismair LOG.info("Sending " + fileContents.length + " bytes"); 8585efb15529d45e32fea8de03c38a968c157c8262chrismair session.sendData(fileContents, fileContents.length); 8685efb15529d45e32fea8de03c38a968c157c8262chrismair } 8785efb15529d45e32fea8de03c38a968c157c8262chrismair 8885efb15529d45e32fea8de03c38a968c157c8262chrismair /** 8985efb15529d45e32fea8de03c38a968c157c8262chrismair * Set the file contents to return from subsequent command invocations 9085efb15529d45e32fea8de03c38a968c157c8262chrismair * @param fileContents - the fileContents to set 9185efb15529d45e32fea8de03c38a968c157c8262chrismair * @throws AssertFailedException - if the fileContents is null 9285efb15529d45e32fea8de03c38a968c157c8262chrismair */ 9385efb15529d45e32fea8de03c38a968c157c8262chrismair public void setFileContents(String fileContents) { 9485efb15529d45e32fea8de03c38a968c157c8262chrismair Assert.notNull(fileContents, "fileContents"); 9585efb15529d45e32fea8de03c38a968c157c8262chrismair setFileContents(fileContents.getBytes()); 9685efb15529d45e32fea8de03c38a968c157c8262chrismair } 9785efb15529d45e32fea8de03c38a968c157c8262chrismair 9885efb15529d45e32fea8de03c38a968c157c8262chrismair /** 9985efb15529d45e32fea8de03c38a968c157c8262chrismair * Set the file contents to return from subsequent command invocations 10085efb15529d45e32fea8de03c38a968c157c8262chrismair * @param fileContents - the file contents 10185efb15529d45e32fea8de03c38a968c157c8262chrismair * @throws AssertFailedException - if the fileContents is null 10285efb15529d45e32fea8de03c38a968c157c8262chrismair */ 10385efb15529d45e32fea8de03c38a968c157c8262chrismair public void setFileContents(byte[] fileContents) { 10485efb15529d45e32fea8de03c38a968c157c8262chrismair Assert.notNull(fileContents, "fileContents"); 10585efb15529d45e32fea8de03c38a968c157c8262chrismair this.fileContents = fileContents; 10685efb15529d45e32fea8de03c38a968c157c8262chrismair } 10785efb15529d45e32fea8de03c38a968c157c8262chrismair 10885efb15529d45e32fea8de03c38a968c157c8262chrismair} 109