| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| RetrCommandHandler |
|
| 1.0;1 |
| 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 | import org.mockftpserver.core.util.Assert; | |
| 23 | ||
| 24 | /** | |
| 25 | * CommandHandler for the RETR (Retrieve) command. Return the configured file contents on the data | |
| 26 | * connection, along with two replies on the control connection: a reply code of 150 and | |
| 27 | * another of 226. By default, return an empty file (i.e., a zero-length byte[]). You can | |
| 28 | * customize the returned file contents by setting the <code>fileContents</code> property, | |
| 29 | * specified either as a String or as a byte array. | |
| 30 | * <p/> | |
| 31 | * Each invocation record stored by this CommandHandler includes the following data element key/values: | |
| 32 | * <ul> | |
| 33 | * <li>{@link #PATHNAME_KEY} ("pathname") - the pathname of the file submitted on the invocation (the first command parameter) | |
| 34 | * </ul> | |
| 35 | * | |
| 36 | * @author Chris Mair | |
| 37 | * @version $Revision: 194 $ - $Date: 2008-12-07 08:53:58 -0500 (Sun, 07 Dec 2008) $ | |
| 38 | */ | |
| 39 | public class RetrCommandHandler extends AbstractStubDataCommandHandler implements CommandHandler { | |
| 40 | ||
| 41 | public static final String PATHNAME_KEY = "pathname"; | |
| 42 | ||
| 43 | 69 | private byte[] fileContents = new byte[0]; |
| 44 | ||
| 45 | /** | |
| 46 | * Create new uninitialized instance | |
| 47 | */ | |
| 48 | 67 | public RetrCommandHandler() { |
| 49 | 67 | } |
| 50 | ||
| 51 | /** | |
| 52 | * Create new instance using the specified fileContents | |
| 53 | * | |
| 54 | * @param fileContents - the file contents | |
| 55 | * @throws org.mockftpserver.core.util.AssertFailedException | |
| 56 | * - if the fileContents is null | |
| 57 | */ | |
| 58 | 1 | public RetrCommandHandler(String fileContents) { |
| 59 | 1 | setFileContents(fileContents); |
| 60 | 0 | } |
| 61 | ||
| 62 | /** | |
| 63 | * Create new instance using the specified fileContents | |
| 64 | * | |
| 65 | * @param fileContents - the file contents | |
| 66 | * @throws org.mockftpserver.core.util.AssertFailedException | |
| 67 | * - if the fileContents is null | |
| 68 | */ | |
| 69 | 1 | public RetrCommandHandler(byte[] fileContents) { |
| 70 | 1 | setFileContents(fileContents); |
| 71 | 0 | } |
| 72 | ||
| 73 | /** | |
| 74 | * @see org.mockftpserver.stub.command.AbstractStubDataCommandHandler#beforeProcessData(org.mockftpserver.core.command.Command, org.mockftpserver.core.session.Session, org.mockftpserver.core.command.InvocationRecord) | |
| 75 | */ | |
| 76 | protected void beforeProcessData(Command command, Session session, InvocationRecord invocationRecord) throws Exception { | |
| 77 | 7 | String filename = command.getRequiredParameter(0); |
| 78 | 6 | invocationRecord.set(PATHNAME_KEY, filename); |
| 79 | 6 | } |
| 80 | ||
| 81 | /** | |
| 82 | * @see org.mockftpserver.stub.command.AbstractStubDataCommandHandler#processData(org.mockftpserver.core.command.Command, org.mockftpserver.core.session.Session, org.mockftpserver.core.command.InvocationRecord) | |
| 83 | */ | |
| 84 | protected void processData(Command command, Session session, InvocationRecord invocationRecord) { | |
| 85 | 6 | LOG.info("Sending " + fileContents.length + " bytes"); |
| 86 | 6 | session.sendData(fileContents, fileContents.length); |
| 87 | 6 | } |
| 88 | ||
| 89 | /** | |
| 90 | * Set the file contents to return from subsequent command invocations | |
| 91 | * | |
| 92 | * @param fileContents - the fileContents to set | |
| 93 | * @throws org.mockftpserver.core.util.AssertFailedException | |
| 94 | * - if the fileContents is null | |
| 95 | */ | |
| 96 | public void setFileContents(String fileContents) { | |
| 97 | 6 | Assert.notNull(fileContents, "fileContents"); |
| 98 | 4 | setFileContents(fileContents.getBytes()); |
| 99 | 4 | } |
| 100 | ||
| 101 | /** | |
| 102 | * Set the file contents to return from subsequent command invocations | |
| 103 | * | |
| 104 | * @param fileContents - the file contents | |
| 105 | * @throws org.mockftpserver.core.util.AssertFailedException | |
| 106 | * - if the fileContents is null | |
| 107 | */ | |
| 108 | public void setFileContents(byte[] fileContents) { | |
| 109 | 7 | Assert.notNull(fileContents, "fileContents"); |
| 110 | 5 | this.fileContents = fileContents; |
| 111 | 5 | } |
| 112 | ||
| 113 | } |