| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| FileRetrCommandHandler |
|
| 1.6;1.6 |
| 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.MockFtpServerException; | |
| 19 | import org.mockftpserver.core.command.Command; | |
| 20 | import org.mockftpserver.core.command.CommandHandler; | |
| 21 | import org.mockftpserver.core.command.InvocationRecord; | |
| 22 | import org.mockftpserver.core.session.Session; | |
| 23 | import org.mockftpserver.core.util.Assert; | |
| 24 | import org.mockftpserver.core.util.AssertFailedException; | |
| 25 | ||
| 26 | import java.io.IOException; | |
| 27 | import 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: 194 $ - $Date: 2008-12-07 08:53:58 -0500 (Sun, 07 Dec 2008) $ | |
| 48 | */ | |
| 49 | public 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 | 5 | public FileRetrCommandHandler() { |
| 60 | 5 | } |
| 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 | 1 | public FileRetrCommandHandler(String file) { |
| 69 | 1 | setFile(file); |
| 70 | 0 | } |
| 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 | 3 | Assert.notNull(file, "file"); |
| 77 | 2 | invocationRecord.set(PATHNAME_KEY, command.getRequiredParameter(0)); |
| 78 | 1 | } |
| 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 | 1 | InputStream inputStream = getClass().getClassLoader().getResourceAsStream(file); |
| 85 | 1 | Assert.notNull(inputStream, "InputStream for [" + file + "]"); |
| 86 | 1 | byte[] buffer = new byte[BUFFER_SIZE]; |
| 87 | try { | |
| 88 | int numBytes; | |
| 89 | 7 | while ((numBytes = inputStream.read(buffer)) != -1) { |
| 90 | 6 | LOG.trace("Sending " + numBytes + " bytes..."); |
| 91 | 6 | session.sendData(buffer, numBytes); |
| 92 | } | |
| 93 | } | |
| 94 | 0 | catch (IOException e) { |
| 95 | 0 | throw new MockFtpServerException(e); |
| 96 | 1 | } |
| 97 | 1 | } |
| 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 | 4 | Assert.notNull(file, "file"); |
| 108 | 2 | this.file = file; |
| 109 | 2 | } |
| 110 | ||
| 111 | } |