Search code examples
javaftp-server

Apache FTPServer -- restricting commands like 'DELE'


I am using the Apache Mina FTPServer. As mentioned in the documentation, I am extending the DefaultFtplet class. I am putting the prints (SOPs) in some events mentioned in the documentation like onConnect, etc. and everything is working fine.

Now, I want to restrict the DELE command, so as per documentation I had overridden the onDeleteStart method, but client is getting hung and disconnecting without any of my messages.

Since I was not able to find any more specific documentation for such scenario, following is my code:

@Override
public FtpletResult onDeleteStart(FtpSession session, FtpRequest request) throws FtpException, IOException {
    System.out.println("\n\n\nonDeleteStart\n\n\n");

    FtpReply reply = new FtpReply() {

        @Override
        public String getMessage() {
            return "Deletion not supported";
        }

        @Override
        public int getCode() {
            return FtpReply.REPLY_450_REQUESTED_FILE_ACTION_NOT_TAKEN;
        }
    };
    session.write(reply); // Not sure if this is the right way!

    return FtpletResult.SKIP;
}

Please let me know what am I missing here, or is this the right way to do even?

Client (ftp command line, and WinSCP) is not getting the message from getMessage(), and hanging and later disconnecting


Solution

  • You can use DefaultFtpReply instead of creating new FtpReply();

    FtpReply reply = new DefaultFtpReply(FtpReply.REPLY_450_REQUESTED_FILE_ACTION_NOT_TAKEN, 
    "Deletion not supported");
    session.write(reply );