1bda3441225e0607b5ced8b538123fd7c7a417910chrismair/*
2bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Copyright 2008 the original author or authors.
3bda3441225e0607b5ced8b538123fd7c7a417910chrismair *
4bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Licensed under the Apache License, Version 2.0 (the "License");
5bda3441225e0607b5ced8b538123fd7c7a417910chrismair * you may not use this file except in compliance with the License.
6bda3441225e0607b5ced8b538123fd7c7a417910chrismair * You may obtain a copy of the License at
7bda3441225e0607b5ced8b538123fd7c7a417910chrismair *
8bda3441225e0607b5ced8b538123fd7c7a417910chrismair *      http://www.apache.org/licenses/LICENSE-2.0
9bda3441225e0607b5ced8b538123fd7c7a417910chrismair *
10bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Unless required by applicable law or agreed to in writing, software
11bda3441225e0607b5ced8b538123fd7c7a417910chrismair * distributed under the License is distributed on an "AS IS" BASIS,
12bda3441225e0607b5ced8b538123fd7c7a417910chrismair * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13bda3441225e0607b5ced8b538123fd7c7a417910chrismair * See the License for the specific language governing permissions and
14bda3441225e0607b5ced8b538123fd7c7a417910chrismair * limitations under the License.
15bda3441225e0607b5ced8b538123fd7c7a417910chrismair */
16bda3441225e0607b5ced8b538123fd7c7a417910chrismairpackage org.mockftpserver.fake.command;
17bda3441225e0607b5ced8b538123fd7c7a417910chrismair
18bda3441225e0607b5ced8b538123fd7c7a417910chrismairimport org.mockftpserver.core.command.Command;
19bda3441225e0607b5ced8b538123fd7c7a417910chrismairimport org.mockftpserver.core.command.ReplyCodes;
20bda3441225e0607b5ced8b538123fd7c7a417910chrismairimport org.mockftpserver.core.session.Session;
21bda3441225e0607b5ced8b538123fd7c7a417910chrismairimport org.mockftpserver.fake.filesystem.DirectoryEntry;
22bda3441225e0607b5ced8b538123fd7c7a417910chrismair
23bda3441225e0607b5ced8b538123fd7c7a417910chrismair/**
24bda3441225e0607b5ced8b538123fd7c7a417910chrismair * CommandHandler for the MKD command. Handler logic:
25bda3441225e0607b5ced8b538123fd7c7a417910chrismair * <ol>
26bda3441225e0607b5ced8b538123fd7c7a417910chrismair * <li>If the user has not logged in, then reply with 530</li>
27bda3441225e0607b5ced8b538123fd7c7a417910chrismair * <li>If the required pathname parameter is missing, then reply with 501</li>
28bda3441225e0607b5ced8b538123fd7c7a417910chrismair * <li>If the parent directory of the specified pathname does not exist, then reply with 550</li>
29bda3441225e0607b5ced8b538123fd7c7a417910chrismair * <li>If the pathname parameter specifies an existing file or directory, or if the create directory fails, then reply with 550</li>
30bda3441225e0607b5ced8b538123fd7c7a417910chrismair * <li>If the current user does not have write and execute access to the parent directory, then reply with 550</li>
31bda3441225e0607b5ced8b538123fd7c7a417910chrismair * <li>Otherwise, reply with 257</li>
32bda3441225e0607b5ced8b538123fd7c7a417910chrismair * </ol>
33bda3441225e0607b5ced8b538123fd7c7a417910chrismair * The supplied pathname may be absolute or relative to the current directory.
34bda3441225e0607b5ced8b538123fd7c7a417910chrismair *
35bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @author Chris Mair
36bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @version $Revision$ - $Date$
37bda3441225e0607b5ced8b538123fd7c7a417910chrismair */
38bda3441225e0607b5ced8b538123fd7c7a417910chrismairpublic class MkdCommandHandler extends AbstractFakeCommandHandler {
39bda3441225e0607b5ced8b538123fd7c7a417910chrismair
40bda3441225e0607b5ced8b538123fd7c7a417910chrismair    protected void handle(Command command, Session session) {
41bda3441225e0607b5ced8b538123fd7c7a417910chrismair        verifyLoggedIn(session);
42bda3441225e0607b5ced8b538123fd7c7a417910chrismair        String path = getRealPath(session, command.getRequiredParameter(0));
43bda3441225e0607b5ced8b538123fd7c7a417910chrismair        String parent = getFileSystem().getParent(path);
44bda3441225e0607b5ced8b538123fd7c7a417910chrismair
45bda3441225e0607b5ced8b538123fd7c7a417910chrismair        this.replyCodeForFileSystemException = ReplyCodes.READ_FILE_ERROR;
46bda3441225e0607b5ced8b538123fd7c7a417910chrismair        verifyFileSystemCondition(getFileSystem().exists(parent), parent, "filesystem.doesNotExist");
47bda3441225e0607b5ced8b538123fd7c7a417910chrismair        verifyFileSystemCondition(!getFileSystem().exists(path), path, "filesystem.alreadyExists");
48bda3441225e0607b5ced8b538123fd7c7a417910chrismair
49bda3441225e0607b5ced8b538123fd7c7a417910chrismair        // User must have write permission to the parent directory
50bda3441225e0607b5ced8b538123fd7c7a417910chrismair        verifyWritePermission(session, parent);
51bda3441225e0607b5ced8b538123fd7c7a417910chrismair
52bda3441225e0607b5ced8b538123fd7c7a417910chrismair        // User must have execute permission to the parent directory
53bda3441225e0607b5ced8b538123fd7c7a417910chrismair        verifyExecutePermission(session, parent);
54bda3441225e0607b5ced8b538123fd7c7a417910chrismair
55bda3441225e0607b5ced8b538123fd7c7a417910chrismair        DirectoryEntry dirEntry = new DirectoryEntry(path);
56bda3441225e0607b5ced8b538123fd7c7a417910chrismair        getFileSystem().add(dirEntry);
57bda3441225e0607b5ced8b538123fd7c7a417910chrismair        dirEntry.setPermissions(getUserAccount(session).getDefaultPermissionsForNewDirectory());
58bda3441225e0607b5ced8b538123fd7c7a417910chrismair
59bda3441225e0607b5ced8b538123fd7c7a417910chrismair        sendReply(session, ReplyCodes.MKD_OK, "mkd", list(path));
60bda3441225e0607b5ced8b538123fd7c7a417910chrismair    }
61bda3441225e0607b5ced8b538123fd7c7a417910chrismair
62bda3441225e0607b5ced8b538123fd7c7a417910chrismair}