| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.mockftpserver.fake.command; |
| 17 | |
|
| 18 | |
import org.mockftpserver.core.command.Command; |
| 19 | |
import org.mockftpserver.core.command.ReplyCodes; |
| 20 | |
import org.mockftpserver.core.session.Session; |
| 21 | |
import org.mockftpserver.core.session.SessionKeys; |
| 22 | |
import org.mockftpserver.core.util.IoUtil; |
| 23 | |
import org.mockftpserver.fake.filesystem.FileEntry; |
| 24 | |
import org.mockftpserver.fake.filesystem.FileSystemEntry; |
| 25 | |
import org.mockftpserver.fake.filesystem.FileSystemException; |
| 26 | |
|
| 27 | |
import java.io.ByteArrayOutputStream; |
| 28 | |
import java.io.IOException; |
| 29 | |
import java.io.InputStream; |
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | 0 | public class RetrCommandHandler extends AbstractFakeCommandHandler { |
| 48 | |
|
| 49 | |
protected void handle(Command command, Session session) { |
| 50 | 0 | verifyLoggedIn(session); |
| 51 | 0 | this.replyCodeForFileSystemException = ReplyCodes.READ_FILE_ERROR; |
| 52 | |
|
| 53 | 0 | String path = getRealPath(session, command.getRequiredParameter(0)); |
| 54 | 0 | FileSystemEntry entry = getFileSystem().getEntry(path); |
| 55 | 0 | verifyFileSystemCondition(entry != null, path, "filesystem.doesNotExist"); |
| 56 | 0 | verifyFileSystemCondition(!entry.isDirectory(), path, "filesystem.isNotAFile"); |
| 57 | 0 | FileEntry fileEntry = (FileEntry) entry; |
| 58 | |
|
| 59 | |
|
| 60 | 0 | verifyReadPermission(session, path); |
| 61 | |
|
| 62 | |
|
| 63 | 0 | verifyExecutePermission(session, getFileSystem().getParent(path)); |
| 64 | |
|
| 65 | 0 | sendReply(session, ReplyCodes.TRANSFER_DATA_INITIAL_OK); |
| 66 | 0 | InputStream input = fileEntry.createInputStream(); |
| 67 | 0 | session.openDataConnection(); |
| 68 | 0 | byte[] bytes = null; |
| 69 | |
try { |
| 70 | 0 | bytes = IoUtil.readBytes(input); |
| 71 | 0 | } |
| 72 | 0 | catch (IOException e) { |
| 73 | 0 | LOG.error("Error reading from file [" + fileEntry.getPath() + "]", e); |
| 74 | 0 | throw new FileSystemException(fileEntry.getPath(), null, e); |
| 75 | |
} |
| 76 | |
finally { |
| 77 | 0 | try { |
| 78 | 0 | input.close(); |
| 79 | |
} |
| 80 | 0 | catch (IOException e) { |
| 81 | 0 | LOG.error("Error closing InputStream for file [" + fileEntry.getPath() + "]", e); |
| 82 | 0 | } |
| 83 | 0 | } |
| 84 | |
|
| 85 | 0 | if (isAsciiMode(session)) { |
| 86 | 0 | bytes = convertLfToCrLf(bytes); |
| 87 | |
} |
| 88 | 0 | session.sendData(bytes, bytes.length); |
| 89 | 0 | session.closeDataConnection(); |
| 90 | 0 | sendReply(session, ReplyCodes.TRANSFER_DATA_FINAL_OK); |
| 91 | 0 | } |
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
protected byte[] convertLfToCrLf(byte[] bytes) { |
| 100 | 0 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 101 | 0 | char lastChar = ' '; |
| 102 | 0 | for (int i = 0; i < bytes.length; i++) { |
| 103 | 0 | char ch = (char) bytes[i]; |
| 104 | 0 | if (ch == '\n' && lastChar != '\r') { |
| 105 | 0 | out.write('\r'); |
| 106 | 0 | out.write('\n'); |
| 107 | |
} else { |
| 108 | 0 | out.write(bytes[i]); |
| 109 | |
} |
| 110 | 0 | lastChar = ch; |
| 111 | |
} |
| 112 | 0 | return out.toByteArray(); |
| 113 | |
} |
| 114 | |
|
| 115 | |
private boolean isAsciiMode(Session session) { |
| 116 | |
|
| 117 | 0 | return session.getAttribute(SessionKeys.ASCII_TYPE) != Boolean.FALSE; |
| 118 | |
} |
| 119 | |
|
| 120 | |
} |