blob: dd11065fa1b319a770cde01b1a62e092df3a888e [file] [log] [blame]
chrismair00dc7bd2014-05-11 21:21:28 +00001/*
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 */
16package org.mockftpserver.fake.command;
17
18import org.mockftpserver.core.command.Command;
19import org.mockftpserver.core.command.ReplyCodes;
20import org.mockftpserver.core.session.Session;
21import org.mockftpserver.core.util.StringUtil;
22
23import 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$ - $Date$
44 */
45public class NlstCommandHandler extends AbstractFakeCommandHandler {
46
47 protected void handle(Command command, Session session) {
48 verifyLoggedIn(session);
49 sendReply(session, ReplyCodes.TRANSFER_DATA_INITIAL_OK);
50 String path = getRealPath(session, command.getParameter(0));
51
52 // User must have read permission to the path
53 if (getFileSystem().exists(path)) {
54 this.replyCodeForFileSystemException = ReplyCodes.READ_FILE_ERROR;
55 verifyReadPermission(session, path);
56 }
57
58 this.replyCodeForFileSystemException = ReplyCodes.SYSTEM_ERROR;
59 List names = getFileSystem().listNames(path);
60 String directoryListing = StringUtil.join(names, endOfLine());
61 directoryListing += directoryListing.length() > 0 ? endOfLine() : "";
62
63 session.openDataConnection();
64 session.sendData(directoryListing.getBytes(), directoryListing.length());
65 session.closeDataConnection();
66
67 sendReply(session, ReplyCodes.TRANSFER_DATA_FINAL_OK);
68 }
69
70}