Coverage Report - org.mockftpserver.fake.command.RetrCommandHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
RetrCommandHandler
0%
0/42
0%
0/14
3.333
 
 1  
 /*
 2  
  * Copyright 2008 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.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  
  * CommandHandler for the RETR command. Handler logic:
 33  
  * <ol>
 34  
  * <li>If the user has not logged in, then reply with 530 and terminate</li>
 35  
  * <li>If the required pathname parameter is missing, then reply with 501 and terminate</li>
 36  
  * <li>If the pathname parameter does not specify a valid, existing filename, then reply with 550 and terminate</li>
 37  
  * <li>If the current user does not have read access to the file at the specified path or execute permission to its directory, then reply with 550 and terminate</li>
 38  
  * <li>Send an initial reply of 150</li>
 39  
  * <li>Send the contents of the named file across the data connection</li>
 40  
  * <li>If there is an error reading the file, then reply with 550 and terminate</li>
 41  
  * <li>Send a final reply with 226</li>
 42  
  * </ol>
 43  
  *
 44  
  * @author Chris Mair
 45  
  * @version $Revision: 214 $ - $Date: 2008-12-26 20:00:19 -0500 (Fri, 26 Dec 2008) $
 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  
         // User must have read permission to the file
 60  0
         verifyReadPermission(session, path);
 61  
 
 62  
         // User must have execute permission to the parent directory
 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  
      * Within the specified byte array, replace all LF (\n) that are NOT preceded by a CR (\r) into CRLF (\r\n).
 95  
      *
 96  
      * @param bytes - the bytes to be converted
 97  
      * @return the result of converting LF to CRLF
 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  
         // Defaults to true
 117  0
         return session.getAttribute(SessionKeys.ASCII_TYPE) != Boolean.FALSE;
 118  
     }
 119  
 
 120  
 }