DeleCommandHandler.java revision 3736b2d78b2d799ad9318e5295e2204c21e21a44
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;
21ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair
22ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair/**
23ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair * CommandHandler for the DELE command. Handler logic:
24ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair * <ol>
25ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair * <li>If the user has not logged in, then reply with 530</li>
26ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair * <li>If the required pathname parameter is missing, then reply with 501</li>
273736b2d78b2d799ad9318e5295e2204c21e21a44chrismair * <li>If the pathname parameter does not specify an existing file then reply with 550</li>
283736b2d78b2d799ad9318e5295e2204c21e21a44chrismair * <li>Otherwise, delete the named file and reply with 250</li>
29ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair * </ol>
30ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair * The supplied pathname may be absolute or relative to the current directory.
31ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair *
32ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair * @author Chris Mair
332a0a3f946dba517a01cc26278f905156857c9c91chrismair * @version $Revision$ - $Date$
34ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair */
35ef13cb2302efe30bdff397e11aae379cbb419b9bchrismairpublic class DeleCommandHandler extends AbstractFakeCommandHandler {
36ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair
37ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair    protected void handle(Command command, Session session) {
38ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair        verifyLoggedIn(session);
39ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair        String path = getRealPath(session, command.getRequiredParameter(0));
40ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair
41ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair        this.replyCodeForFileSystemException = ReplyCodes.READ_FILE_ERROR;
42ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair        verifyFileSystemCondition(getFileSystem().isFile(path), path, "filesystem.isNotAFile");
43ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair
44ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair        // User must have write permission to the parent directory
45ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair        verifyWritePermission(session, getFileSystem().getParent(path));
46ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair
47ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair        getFileSystem().delete(path);
48ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair        sendReply(session, ReplyCodes.DELE_OK, "dele", list(path));
49ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair    }
50ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair
51ef13cb2302efe30bdff397e11aae379cbb419b9bchrismair}