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.fake.command.AbstractFakeCommandHandler
19import org.mockftpserver.core.command.Command
20import org.mockftpserver.core.session.Session
21import org.mockftpserver.core.session.SessionKeys
22import org.mockftpserver.core.command.ReplyCodes
23import org.mockftpserver.core.session.SessionKeys
24/**
25 * CommandHandler for the NLST command. Handler logic:
26 * <ol>
27 *  <li>If the user has not logged in, then reply with 530 and terminate</li>
28 *  <li>Send an initial reply of 150</li>
29 *  <li>If the optional pathname parameter is missing, then send a directory listing for
30 *  		the current directory across the data connection</li>
31 *  <li>Otherwise, if the optional pathname parameter specifies a directory or group of files,
32 *  		then send a directory listing for the specified directory across the data connection</li>
33 *  <li>Otherwise, if the pathname parameter does not specify an existing directory or group of files,
34 *  		then send an empty response across the data connection</li>
35 *  <li>Send a final reply with 226</li>
36 * </ol>
37 * The directory listing sent includes filenames only, separated by end-of-line characters.
38 *
39 * @version $Revision: $ - $Date: $
40 *
41 * @author Chris Mair
42 */
43class NlstCommandHandler extends AbstractFakeCommandHandler {
44
45    protected void handle(Command command, Session session) {
46        verifyLoggedIn(session)
47        sendReply(session, ReplyCodes.SEND_DATA_INITIAL_OK)
48
49        def path = getRealPath(session, command.getParameter(0))
50        def names = this.fileSystem.listNames(path)
51        def directoryListing = names.join(endOfLine())
52        session.sendData(directoryListing.getBytes(), directoryListing.length())
53
54        sendReply(session, ReplyCodes.SEND_DATA_FINAL_OK)
55    }
56
57}