177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair/*
277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * Copyright 2008 the original author or authors.
377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair *
477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * Licensed under the Apache License, Version 2.0 (the "License");
577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * you may not use this file except in compliance with the License.
677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * You may obtain a copy of the License at
777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair *
877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair *      http://www.apache.org/licenses/LICENSE-2.0
977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair *
1077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * Unless required by applicable law or agreed to in writing, software
1177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * distributed under the License is distributed on an "AS IS" BASIS,
1277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * See the License for the specific language governing permissions and
1477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * limitations under the License.
1577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair */
1677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismairpackage org.mockftpserver.fake.command;
1777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
1877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismairimport org.mockftpserver.core.command.Command;
1977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismairimport org.mockftpserver.core.command.ReplyCodes;
2077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismairimport org.mockftpserver.core.session.Session;
2177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismairimport org.mockftpserver.fake.filesystem.DirectoryEntry;
2277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
2377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair/**
2477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * CommandHandler for the MKD command. Handler logic:
2577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * <ol>
2677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * <li>If the user has not logged in, then reply with 530</li>
2777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * <li>If the required pathname parameter is missing, then reply with 501</li>
2877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * <li>If the parent directory of the specified pathname does not exist, then reply with 550</li>
2977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * <li>If the pathname parameter specifies an existing file or directory, or if the create directory fails, then reply with 550</li>
3077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * <li>If the current user does not have write and execute access to the parent directory, then reply with 550</li>
3177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * <li>Otherwise, reply with 257</li>
3277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * </ol>
3377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * The supplied pathname may be absolute or relative to the current directory.
3477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair *
3577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * @author Chris Mair
3677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * @version $Revision$ - $Date$
3777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair */
3877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismairpublic class MkdCommandHandler extends AbstractFakeCommandHandler {
3977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
4077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    protected void handle(Command command, Session session) {
4177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        verifyLoggedIn(session);
4277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        String path = getRealPath(session, command.getRequiredParameter(0));
4377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        String parent = getFileSystem().getParent(path);
4477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
4577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        this.replyCodeForFileSystemException = ReplyCodes.READ_FILE_ERROR;
4677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        verifyFileSystemCondition(getFileSystem().exists(parent), parent, "filesystem.doesNotExist");
4777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        verifyFileSystemCondition(!getFileSystem().exists(path), path, "filesystem.alreadyExists");
4877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
4977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        // User must have write permission to the parent directory
5077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        verifyWritePermission(session, parent);
5177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
5277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        // User must have execute permission to the parent directory
5377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        verifyExecutePermission(session, parent);
5477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
5577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        DirectoryEntry dirEntry = new DirectoryEntry(path);
5677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        getFileSystem().add(dirEntry);
5777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        dirEntry.setPermissions(getUserAccount(session).getDefaultPermissionsForNewDirectory());
5877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
5977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        sendReply(session, ReplyCodes.MKD_OK, "mkd", list(path));
6077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    }
6177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
6277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair}