blob: b8e89e88764ed87e15913beaec28942c30899f53 [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;
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 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$ - $Date$
46 */
47public class ListCommandHandler extends AbstractFakeCommandHandler {
48
49 protected void handle(Command command, Session session) {
50 verifyLoggedIn(session);
51 sendReply(session, ReplyCodes.TRANSFER_DATA_INITIAL_OK);
52
53 String path = getRealPath(session, command.getParameter(0));
54
55 // User must have read permission to the path
56 if (getFileSystem().exists(path)) {
57 this.replyCodeForFileSystemException = ReplyCodes.READ_FILE_ERROR;
58 verifyReadPermission(session, path);
59 }
60
61 this.replyCodeForFileSystemException = ReplyCodes.SYSTEM_ERROR;
62 List fileEntries = getFileSystem().listFiles(path);
63 Iterator iter = fileEntries.iterator();
64 List lines = new ArrayList();
65 while (iter.hasNext()) {
66 FileSystemEntry entry = (FileSystemEntry) iter.next();
67 lines.add(getFileSystem().formatDirectoryListing(entry));
68 }
69 String result = StringUtil.join(lines, endOfLine());
70 result += result.length() > 0 ? endOfLine() : "";
71
72 session.openDataConnection();
73 LOG.info("Sending [" + result + "]");
74 session.sendData(result.getBytes(), result.length());
75 session.closeDataConnection();
76
77 sendReply(session, ReplyCodes.TRANSFER_DATA_FINAL_OK);
78 }
79
80}