ListCommandHandler.java revision ef13cb2302efe30bdff397e11aae379cbb419b9b
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;
22import org.mockftpserver.fake.filesystem.FileSystemEntry;
23
24import java.util.ArrayList;
25import java.util.Iterator;
26import 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 an error occurs during processing, then send a reply of 451 and terminate</li>
34 * <li>If the optional pathname parameter is missing, then send a directory listing for
35 * the current directory across the data connection</li>
36 * <li>Otherwise, if the optional pathname parameter specifies a directory or group of files,
37 * then send a directory listing for the specified directory across the data connection</li>
38 * <li>Otherwise, if the optional pathname parameter specifies a filename, then send information
39 * for the specified file across the data connection</li>
40 * <li>Send a final reply with 226</li>
41 * </ol>
42 *
43 * @author Chris Mair
44 * @version $Revision: 136 $ - $Date: 2008-10-23 22:17:29 -0400 (Thu, 23 Oct 2008) $
45 */
46public class ListCommandHandler extends AbstractFakeCommandHandler {
47
48    protected void handle(Command command, Session session) {
49        verifyLoggedIn(session);
50        sendReply(session, ReplyCodes.TRANSFER_DATA_INITIAL_OK);
51
52        String path = getRealPath(session, command.getParameter(0));
53
54        // User must have read permission to the path
55        if (getFileSystem().exists(path)) {
56            this.replyCodeForFileSystemException = ReplyCodes.READ_FILE_ERROR;
57            verifyReadPermission(session, path);
58        }
59
60        this.replyCodeForFileSystemException = ReplyCodes.SYSTEM_ERROR;
61        List fileEntries = getFileSystem().listFiles(path);
62        Iterator iter = fileEntries.iterator();
63        List lines = new ArrayList();
64        while (iter.hasNext()) {
65            FileSystemEntry entry = (FileSystemEntry) iter.next();
66            lines.add(getFileSystem().formatDirectoryListing(entry));
67        }
68//        List lines = fileEntries.collect { this.fileSystem.formatDirectoryListing(it) }
69        String result = StringUtil.join(lines, endOfLine());
70
71        session.openDataConnection();
72        LOG.info("Sending [" + result + "]");
73        session.sendData(result.toString().getBytes(), result.length());
74        session.closeDataConnection();
75
76        sendReply(session, ReplyCodes.TRANSFER_DATA_FINAL_OK);
77    }
78
79}