160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair/*
260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * Copyright 2008 the original author or authors.
360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair *
460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * Licensed under the Apache License, Version 2.0 (the "License");
560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * you may not use this file except in compliance with the License.
660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * You may obtain a copy of the License at
760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair *
860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair *      http://www.apache.org/licenses/LICENSE-2.0
960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair *
1060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * Unless required by applicable law or agreed to in writing, software
1160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * distributed under the License is distributed on an "AS IS" BASIS,
1260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * See the License for the specific language governing permissions and
1460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * limitations under the License.
1560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair */
1660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairpackage org.mockftpserver.fake.command;
1760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
1860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairimport org.mockftpserver.core.command.Command;
1960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairimport org.mockftpserver.core.command.ReplyCodes;
2060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairimport org.mockftpserver.core.session.Session;
2160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairimport org.mockftpserver.fake.filesystem.DirectoryEntry;
2260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
2360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair/**
2460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * CommandHandler for the MKD command. Handler logic:
2560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * <ol>
2660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * <li>If the user has not logged in, then reply with 530</li>
2760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * <li>If the required pathname parameter is missing, then reply with 501</li>
2860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * <li>If the parent directory of the specified pathname does not exist, then reply with 550</li>
2960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * <li>If the pathname parameter specifies an existing file or directory, or if the create directory fails, then reply with 550</li>
3060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * <li>If the current user does not have write and execute access to the parent directory, then reply with 550</li>
3160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * <li>Otherwise, reply with 257</li>
3260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * </ol>
3360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * The supplied pathname may be absolute or relative to the current directory.
3460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair *
3560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * @author Chris Mair
3660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * @version $Revision$ - $Date$
3760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair */
3860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairpublic class MkdCommandHandler extends AbstractFakeCommandHandler {
3960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
4060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    protected void handle(Command command, Session session) {
4160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        verifyLoggedIn(session);
4260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        String path = getRealPath(session, command.getRequiredParameter(0));
4360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        String parent = getFileSystem().getParent(path);
4460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
4560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        this.replyCodeForFileSystemException = ReplyCodes.READ_FILE_ERROR;
4660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        verifyFileSystemCondition(getFileSystem().exists(parent), parent, "filesystem.doesNotExist");
4760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        verifyFileSystemCondition(!getFileSystem().exists(path), path, "filesystem.alreadyExists");
4860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
4960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        // User must have write permission to the parent directory
5060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        verifyWritePermission(session, parent);
5160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
5260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        // User must have execute permission to the parent directory
5360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        verifyExecutePermission(session, parent);
5460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
5560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        DirectoryEntry dirEntry = new DirectoryEntry(path);
5660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        getFileSystem().add(dirEntry);
5760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        dirEntry.setPermissions(getUserAccount(session).getDefaultPermissionsForNewDirectory());
5860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
5960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        sendReply(session, ReplyCodes.MKD_OK, "mkd", list(path));
6060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
6160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
6260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair}