| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| NlstCommandHandler |
|
| 3.0;3 |
| 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 | ||
| 23 | import java.util.List; | |
| 24 | ||
| 25 | /** | |
| 26 | * CommandHandler for the NLST command. Handler logic: | |
| 27 | * <ol> | |
| 28 | * <li>If the user has not logged in, then reply with 530 and terminate</li> | |
| 29 | * <li>Send an initial reply of 150</li> | |
| 30 | * <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> | |
| 31 | * <li>If an error occurs during processing, then send a reply of 451 and terminate</li> | |
| 32 | * <li>If the optional pathname parameter is missing, then send a directory listing for | |
| 33 | * the current directory across the data connection</li> | |
| 34 | * <li>Otherwise, if the optional pathname parameter specifies a directory or group of files, | |
| 35 | * then send a directory listing for the specified directory across the data connection</li> | |
| 36 | * <li>Otherwise, if the pathname parameter does not specify an existing directory or group of files, | |
| 37 | * then send an empty response across the data connection</li> | |
| 38 | * <li>Send a final reply with 226</li> | |
| 39 | * </ol> | |
| 40 | * The directory listing sent includes filenames only, separated by end-of-line characters. | |
| 41 | * | |
| 42 | * @author Chris Mair | |
| 43 | * @version $Revision: 220 $ - $Date: 2009-02-07 23:05:06 -0500 (Sat, 07 Feb 2009) $ | |
| 44 | */ | |
| 45 | 82 | public class NlstCommandHandler extends AbstractFakeCommandHandler { |
| 46 | ||
| 47 | protected void handle(Command command, Session session) { | |
| 48 | 11 | verifyLoggedIn(session); |
| 49 | 10 | sendReply(session, ReplyCodes.TRANSFER_DATA_INITIAL_OK); |
| 50 | 10 | String path = getRealPath(session, command.getParameter(0)); |
| 51 | ||
| 52 | // User must have read permission to the path | |
| 53 | 10 | if (getFileSystem().exists(path)) { |
| 54 | 9 | this.replyCodeForFileSystemException = ReplyCodes.READ_FILE_ERROR; |
| 55 | 9 | verifyReadPermission(session, path); |
| 56 | } | |
| 57 | ||
| 58 | 9 | this.replyCodeForFileSystemException = ReplyCodes.SYSTEM_ERROR; |
| 59 | 9 | List names = getFileSystem().listNames(path); |
| 60 | 8 | String directoryListing = StringUtil.join(names, endOfLine()); |
| 61 | 8 | directoryListing += directoryListing.length() > 0 ? endOfLine() : ""; |
| 62 | ||
| 63 | 8 | session.openDataConnection(); |
| 64 | 8 | session.sendData(directoryListing.getBytes(), directoryListing.length()); |
| 65 | 8 | session.closeDataConnection(); |
| 66 | ||
| 67 | 8 | sendReply(session, ReplyCodes.TRANSFER_DATA_FINAL_OK); |
| 68 | 8 | } |
| 69 | ||
| 70 | } |