summaryrefslogtreecommitdiff
path: root/MockFtpServer/src/main/groovy/org/mockftpserver/fake/command/AbstractFakeCommandHandler.groovy
blob: 6928dde56daf699ffa140587f7e957e186f84511 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
 * Copyright 2008 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.mockftpserver.fake.command

import java.text.MessageFormat
import org.apache.log4j.Logger
import org.mockftpserver.core.CommandSyntaxException
import org.mockftpserver.core.IllegalStateException
import org.mockftpserver.core.NotLoggedInException
import org.mockftpserver.core.command.Command
import org.mockftpserver.core.command.CommandHandler
import org.mockftpserver.core.command.ReplyCodes
import org.mockftpserver.core.session.Session
import org.mockftpserver.core.session.SessionKeys
import org.mockftpserver.fake.filesystem.ExistingFileOperationException
import org.mockftpserver.fake.filesystem.FileSystem
import org.mockftpserver.fake.filesystem.FileSystemException
import org.mockftpserver.fake.filesystem.InvalidFilenameException
import org.mockftpserver.fake.filesystem.NewFileOperationException
import org.mockftpserver.fake.server.ServerConfiguration
import org.mockftpserver.fake.server.ServerConfigurationAware
import org.mockftpserver.fake.user.UserAccount

/**
 * Abstract superclass for CommandHandler classes for the "Fake" server.
 *
 * @version $Revision$ - $Date$
 *
 * @author Chris Mair
 */
abstract class AbstractFakeCommandHandler implements CommandHandler, ServerConfigurationAware {

    final Logger LOG = Logger.getLogger(this.class)
    ServerConfiguration serverConfiguration

    /**
     * Reply code sent back when a FileSystemException is caught by the         {@link #handleCommand(Command, Session)}
     * This defaults to ReplyCodes.EXISTING_FILE_ERROR (550). 
     */
    int replyCodeForFileSystemException = ReplyCodes.EXISTING_FILE_ERROR

    /**
     * Use template method to centralize and ensure common validation
     */
    void handleCommand(Command command, Session session) {
        assert serverConfiguration != null
        assert command != null
        assert session != null

        try {
            handle(command, session)
        }
        catch (CommandSyntaxException e) {
            handleException(command, session, e, ReplyCodes.COMMAND_SYNTAX_ERROR)
        }
        catch (IllegalStateException e) {
            handleException(command, session, e, ReplyCodes.ILLEGAL_STATE)
        }
        catch (NotLoggedInException e) {
            handleException(command, session, e, ReplyCodes.NOT_LOGGED_IN)
        }
        catch (ExistingFileOperationException e) {
            handleException(command, session, e, ReplyCodes.EXISTING_FILE_ERROR, [e.path])
        }
        catch (NewFileOperationException e) {
            handleException(command, session, e, ReplyCodes.NEW_FILE_ERROR, [e.path])
        }
        catch (InvalidFilenameException e) {
            handleException(command, session, e, ReplyCodes.FILENAME_NOT_VALID, [e.path])
        }
        catch (FileSystemException e) {
            handleException(command, session, e, replyCodeForFileSystemException, [e.path])
        }
    }

    /**
     * Convenience method to return the FileSystem stored in the ServerConfiguration
     */
    protected FileSystem getFileSystem() {
        serverConfiguration.fileSystem
    }

    /**
     * Subclasses must implement this
     */
    protected abstract void handle(Command command, Session session)

    // -------------------------------------------------------------------------
    // Utility methods for subclasses
    // -------------------------------------------------------------------------

    /**
     * Send a reply for this command on the control connection.
     *
     * The reply code is designated by the <code>replyCode</code> property, and the reply text
     * is retrieved from the <code>replyText</code> ResourceBundle, using the specified messageKey.
     *
     * @param session - the Session
     * @param replyCode - the reply code
     * @param messageKey - the resource bundle key for the reply text
     * @param args - the optional message arguments; defaults to []
     *
     * @throws AssertionError - if session is null
     *
     * @see MessageFormat
     */
    protected void sendReply(Session session, int replyCode, String messageKey, List args = []) {
        assert session
        assertValidReplyCode(replyCode);

        String text = getTextForKey(messageKey)
        String replyText = (args) ? MessageFormat.format(text, args as Object[]) : text;

        String replyTextToLog = (replyText == null) ? "" : " " + replyText;
        // TODO change to LOG.debug()
        def argsToLog = (args) ? " args=$args" : ""
        LOG.info("Sending reply [" + replyCode + replyTextToLog + "]" + argsToLog);
        session.sendReply(replyCode, replyText);
    }

    /**
     * Send a reply for this command on the control connection.
     *
     * The reply code is designated by the <code>replyCode</code> property, and the reply text
     * is retrieved from the <code>replyText</code> ResourceBundle, using the reply code as the key.
     *
     * @param session - the Session
     * @param replyCode - the reply code
     * @param args - the optional message arguments; defaults to []
     *
     * @throws AssertionError - if session is null
     *
     * @see MessageFormat
     */
    protected void sendReply(Session session, int replyCode, List args = []) {
        sendReply(session, replyCode, replyCode.toString(), args)
    }

    /**
     * Handle the exception caught during handleCommand()
     * @param command - the Command
     * @param session - the Session
     * @param exception - the caught exception
     * @param replyCode - the reply code that should be sent back
     * @param args - the optional args for the reply (message)
     *
     */
    private handleException(Command command, Session session, Throwable exception, int replyCode, args = []) {
        LOG.warn("Error handling command: $command; ${exception}", exception)
        sendReply(session, replyCode, args)
    }

    /**
     * Assert that the specified number is a valid reply code
     * @param replyCode - the reply code to check
     *
     * @throws AssertionError - if the replyCode is invalid
     */
    protected void assertValidReplyCode(int replyCode) {
        assert replyCode > 0, "The number [" + replyCode + "] is not a valid reply code"
    }

    /**
     * Return the value of the named attribute within the session.
     * @param session - the Session
     * @param name - the name of the session attribute to retrieve
     * @return the value of the named session attribute
     * @throws IllegalStateException - if the Session does not contain the named attribute
     */
    protected Object getRequiredSessionAttribute(Session session, String name) {
        Object value = session.getAttribute(name)
        if (value == null) {
            throw new IllegalStateException("Session missing required attribute [$name]")
        }
        return value
    }

    /**
     * Verify that the current user (if any) has already logged in successfully.
     * @param session - the Session
     */
    protected void verifyLoggedIn(Session session) {
        if (session.getAttribute(SessionKeys.USER_ACCOUNT) == null) {
            throw new NotLoggedInException("User has not logged in")
        }
    }

    /**
     * Verify that the specified condition related to the file system is true,
     * otherwise throw a FileSystemException.
     *
     * @param condition - the condition that must be true
     * @param path - the path involved in the operation; this will be included in the
     * 		error message if the condition is not true.
     * @throws FileSystemException - if the condition is not true
     */
    protected void verifyFileSystemCondition(condition, path) {
        if (!condition) {
            throw new FileSystemException((String) path, "path [$path]")
        }
    }

    /**
     * Return the full, absolute path for the specified abstract pathname.
     * If path is null, return the current directory (stored in the session). If
     * path represents an absolute path, then return path as is. Otherwise, path
     * is relative, so assemble the full path from the current directory
     * and the specified relative path.
     * @param Session - the Session
     * @param path - the abstract pathname; may be null
     * @return the resulting full, absolute path
     */
    protected String getRealPath(Session session, String path) {
        def currentDirectory = session.getAttribute(SessionKeys.CURRENT_DIRECTORY)
        if (path == null) {
            return currentDirectory
        }
        if (fileSystem.isAbsolute(path)) {
            return path
        }
        return fileSystem.path(currentDirectory, path)
    }

    /**
     * Return the end-of-line character(s) used when building multi-line responses
     */
    protected String endOfLine() {
        "\n"
    }

    private String getTextForKey(key) {
        try {
            return serverConfiguration.replyTextBundle.getString(key.toString())
        }
        catch (MissingResourceException e) {
            // No reply text is mapped for the specified key
            LOG.warn("No reply text defined for key [${key.toString()}]");
            return null;
        }
    }

    // -------------------------------------------------------------------------
    // Login Support (used by USER and PASS commands)
    // -------------------------------------------------------------------------

    /**
     * Validate the UserAccount for the specified username. If valid, return true. If the UserAccount does
     * not exist or is invalid, log an error message, send back a reply code of 530 with an appropriate
     * error message, and return false. A UserAccount is considered invalid if the homeDirectory property
     * is not set or is set to a non-existent directory.
     * @param username - the username
     * @param session - the session; used to send back an error reply if necessary
     * @return true only if the UserAccount for the named user is valid
     */
    protected boolean validateUserAccount(String username, Session session) {
        def userAccount = serverConfiguration.getUserAccount(username)
        if (userAccount == null || !userAccount.valid) {
            LOG.error("UserAccount missing or not valid for username [$username]: $userAccount")
            sendReply(session, ReplyCodes.USER_ACCOUNT_NOT_VALID, "userAccountNotValid", [username])
            return false
        }

        def home = userAccount.homeDirectory
        if (!getFileSystem().isDirectory(home)) {
            LOG.error("Home directory configured for username [$username] is not valid: $home")
            sendReply(session, ReplyCodes.USER_ACCOUNT_NOT_VALID, "homeDirectoryNotValid", [username, home])
            return false
        }

        return true
    }

    /**
     * Log in the specified user for the current session. Send back a reply of 230 and set the UserAccount
     * and current directory (homeDirectory) in the session
     * @param userAccount - the userAccount for the user to be logged in
     * @param session - the session
     */
    protected void login(UserAccount userAccount, Session session) {
        sendReply(session, ReplyCodes.PASS_OK)
        session.setAttribute(SessionKeys.USER_ACCOUNT, userAccount)
        session.setAttribute(SessionKeys.CURRENT_DIRECTORY, userAccount.homeDirectory)
    }

}