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