Ftplet – Servlet for Apache FtpServer


I had written a short post on Apache FTP Server in march and i have hinted about Ftplet in that post. Here is a descriptive example of how to write a Ftplet.

First of all, some basics

What is a Ftplet? Ftplet is like a servlet and is used to capture / intercept or handle various Apache FtpServer notification.

What is the Use? Imagine you need to show on a webpage all logged in Users to a FTPServer. You need to have some program to catch the Logon event of a FTPserver. In case of Apache FtpServer we have Ftplet which does that thing for us.

What library files are required? you just need a IDE(i prefer Netbeans) to program and ftplet-api jar file which you can find in the ftpserver downloaded folder, in the commons/lib folder.

How to Write a Ftplet? There are 2 ways to do that, one by extending DefaultFtplet class and overriding the methods you need like onLogin() and the second method is by implementing the Ftplet interface and overriding the corresponding methods.

I will explain the Second method in this post that is to implement the Ftplet interface. Here is a small programmatic example. It is a 4 Step Procedure:

Step 1: Code the FTPlet (MyFtplet.java)

package ftplettest;

import java.io.IOException;
import org.apache.ftpserver.ftplet.FtpException;
import org.apache.ftpserver.ftplet.FtpReply;
import org.apache.ftpserver.ftplet.FtpRequest;
import org.apache.ftpserver.ftplet.FtpSession;
import org.apache.ftpserver.ftplet.Ftplet;
import org.apache.ftpserver.ftplet.FtpletContext;
import org.apache.ftpserver.ftplet.FtpletResult;

/**
 *
 * @author dom
 */
public class MyFtplet implements Ftplet {

    @Override
    public void init(FtpletContext ftpletContext) throws FtpException {

    }

    @Override
    public void destroy() {

    }

    @Override
    public FtpletResult beforeCommand(FtpSession session, FtpRequest request) throws FtpException, IOException {
        System.out.println("COMMAND STARTED -- " + request.getRequestLine());
        return null;
    }

    @Override
    public FtpletResult afterCommand(FtpSession session, FtpRequest request, FtpReply reply) throws FtpException, IOException {
        System.out.println("COMMAND ENDED -- " + request.getRequestLine());
        return null;
    }

    @Override
    public FtpletResult onConnect(FtpSession session) throws FtpException, IOException {
        return null;
    }

    @Override
    public FtpletResult onDisconnect(FtpSession session) throws FtpException, IOException {
       return null;
    }

}

Step 2: Add the ftplet tag to the Config file (ftpd-typical.xml)

After this you need to add the following to the XML config file found in res/conf folder. I am using the typical configuration file(ftpd-typical.xml) in this example.

<ftplets>
  <ftplet name="myftplet">
    <beans:bean class="ftplettest.MyFtplet">
    </beans:bean>
  </ftplet>
</ftplets>

Dont forget to add the namespace for the beans tag to the xml file :

xmlns:beans="http://www.springframework.org/schema/beans"

NOTE: Please check the formatting of the xml (ftpd-typical.xml) file when you paste above namespace.

Step 3: Copy the Class files in classes folder

Copy the ftplettest folder from you Netbeans project/build/classes folder and paste it in <Apache Ftpserver>/commons/classes folder.

Step 4: Run the FtpServer

Finally run the FTPSever, on the windows machine open a command prompt and go to the Apache FtpSever folder and then issue a command

bin/ftpd.bat res/conf/ftpd-typical.xml

thats it you are ready!

Just try to connect to this Ftpserver from another machine and you will see the output of the println statement on the Server Command Prompt.

Further Reading and References:

http://mina.apache.org/ftpserver/documentation.html

Advertisements

About Dominic

J for JAVA more about me : http://about.me/dominicdsouza
This entry was posted in Thechy Stuff. Bookmark the permalink.

6 Responses to Ftplet – Servlet for Apache FtpServer

  1. Mohan says:

    I tried configuring ftplets using XML configuration but , I am getting an error ” invalid content found starting with element “ftplets” .

    here is my configuration :

    i tried replacing namespaces for beans also from ftpd-full.xml , but it didnt workout too

    • Dominic says:

      mohan,
      yes i have made a slight mistake in the above post… instead of package apacheftptest; in the “Step 1: Code the FTPlet” you need to put package ftplettest;… i have rectified it now.
      -dominic

  2. Amit Lal says:

    i tried tapping “STATUS ” or “STAT” command with ftplet but its not happening. Could you please help me out with this.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s