Coverage Report - org.mockftpserver.fake.command.ListCommandHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
ListCommandHandler
100%
23/23
100%
6/6
4
 
 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.util.StringUtil;
 22  
 import org.mockftpserver.fake.filesystem.FileSystemEntry;
 23  
 
 24  
 import java.util.ArrayList;
 25  
 import java.util.Iterator;
 26  
 import java.util.List;
 27  
 
 28  
 /**
 29  
  * CommandHandler for the LIST command. Handler logic:
 30  
  * <ol>
 31  
  * <li>If the user has not logged in, then reply with 530 and terminate</li>
 32  
  * <li>Send an initial reply of 150</li>
 33  
  * <li>If the current user does not have read access to the file or directory to be listed, then reply with 550 and terminate</li>
 34  
  * <li>If an error occurs during processing, then send a reply of 451 and terminate</li>
 35  
  * <li>If the optional pathname parameter is missing, then send a directory listing for
 36  
  * the current directory across the data connection</li>
 37  
  * <li>Otherwise, if the optional pathname parameter specifies a directory or group of files,
 38  
  * then send a directory listing for the specified directory across the data connection</li>
 39  
  * <li>Otherwise, if the optional pathname parameter specifies a filename, then send information
 40  
  * for the specified file across the data connection</li>
 41  
  * <li>Send a final reply with 226</li>
 42  
  * </ol>
 43  
  *
 44  
  * @author Chris Mair
 45  
  * @version $Revision: 220 $ - $Date: 2009-02-07 23:05:06 -0500 (Sat, 07 Feb 2009) $
 46  
  */
 47  82
 public class ListCommandHandler extends AbstractFakeCommandHandler {
 48  
 
 49  
     protected void handle(Command command, Session session) {
 50  13
         verifyLoggedIn(session);
 51  12
         sendReply(session, ReplyCodes.TRANSFER_DATA_INITIAL_OK);
 52  
 
 53  12
         String path = getRealPath(session, command.getParameter(0));
 54  
 
 55  
         // User must have read permission to the path
 56  12
         if (getFileSystem().exists(path)) {
 57  11
             this.replyCodeForFileSystemException = ReplyCodes.READ_FILE_ERROR;
 58  11
             verifyReadPermission(session, path);
 59  
         }
 60  
 
 61  11
         this.replyCodeForFileSystemException = ReplyCodes.SYSTEM_ERROR;
 62  11
         List fileEntries = getFileSystem().listFiles(path);
 63  10
         Iterator iter = fileEntries.iterator();
 64  10
         List lines = new ArrayList();
 65  23
         while (iter.hasNext()) {
 66  13
             FileSystemEntry entry = (FileSystemEntry) iter.next();
 67  13
             lines.add(getFileSystem().formatDirectoryListing(entry));
 68  13
         }
 69  10
         String result = StringUtil.join(lines, endOfLine());
 70  10
         result += result.length() > 0 ? endOfLine() : "";
 71  
 
 72  10
         session.openDataConnection();
 73  10
         LOG.info("Sending [" + result + "]");
 74  10
         session.sendData(result.getBytes(), result.length());
 75  10
         session.closeDataConnection();
 76  
 
 77  10
         sendReply(session, ReplyCodes.TRANSFER_DATA_FINAL_OK);
 78  10
     }
 79  
 
 80  
 }