NlstCommandHandler.java revision 2a0a3f946dba517a01cc26278f905156857c9c91
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 */
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 an error occurs during processing, then send a reply of 451 and terminate</li>
31 * <li>If the optional pathname parameter is missing, then send a directory listing for
32 * the current directory across the data connection</li>
33 * <li>Otherwise, if the optional pathname parameter specifies a directory or group of files,
34 * then send a directory listing for the specified directory across the data connection</li>
35 * <li>Otherwise, if the pathname parameter does not specify an existing directory or group of files,
36 * then send an empty response across the data connection</li>
37 * <li>Send a final reply with 226</li>
38 * </ol>
39 * The directory listing sent includes filenames only, separated by end-of-line characters.
40 *
41 * @author Chris Mair
42 * @version $Revision$ - $Date$
43 */
44public class NlstCommandHandler extends AbstractFakeCommandHandler {
45
46    protected void handle(Command command, Session session) {
47        verifyLoggedIn(session);
48        sendReply(session, ReplyCodes.TRANSFER_DATA_INITIAL_OK);
49        String path = getRealPath(session, command.getParameter(0));
50
51        // User must have read permission to the path
52        if (getFileSystem().exists(path)) {
53            this.replyCodeForFileSystemException = ReplyCodes.READ_FILE_ERROR;
54            verifyReadPermission(session, path);
55        }
56
57        this.replyCodeForFileSystemException = ReplyCodes.SYSTEM_ERROR;
58        List names = getFileSystem().listNames(path);
59        String directoryListing = StringUtil.join(names, endOfLine());
60        session.openDataConnection();
61        session.sendData(directoryListing.getBytes(), directoryListing.length());
62        session.closeDataConnection();
63
64        sendReply(session, ReplyCodes.TRANSFER_DATA_FINAL_OK);
65    }
66
67}