Archive

Archive for the ‘Apache Mina’ Category

How to start a simple embedded java based FTP server

February 11, 2015 2 comments

I recently needed to run a simple FTP server for a test, and found a nice solution: Apache FTP Server. This is a side-project of Apache MINA.

Look at the bottom of the download page to find the maven dependency coordinates (GAV).

Once you have ftpserver-core on the classpath you can start a simple FTP server on a non-root port, with user authentication using this code:


import org.apache.ftpserver.FtpServer;
import org.apache.ftpserver.FtpServerFactory;
import org.apache.ftpserver.ftplet.FtpException;
import org.apache.ftpserver.ftplet.UserManager;
import org.apache.ftpserver.listener.ListenerFactory;
import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory;
import org.apache.ftpserver.usermanager.impl.BaseUser;

public class FTPTestServer {
	
	public static void main(String[] args) throws FtpException {
		PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();
		UserManager userManager = userManagerFactory.createUserManager();
		BaseUser user = new BaseUser();
		user.setName("username");
		user.setPassword("password");
		user.setHomeDirectory("/tmp");
		userManager.save(user);
		
		ListenerFactory listenerFactory = new ListenerFactory();
		listenerFactory.setPort(2221);
		
		FtpServerFactory factory = new FtpServerFactory();
		factory.setUserManager(userManager);
		factory.addListener("default", listenerFactory.createListener());
		
		FtpServer server = factory.createServer();
		server.start();
	}
}


Categories: Apache Mina Tags: , ,

How to use the MINA IoFilter

August 18, 2008 4 comments

While rewriting a personal project using Apache Mina I found out that there was very limited documentation about how to chain up IoFilters and ProtocolCodecs properly. I’m using a text based protocol that uses line delimiters to indicate message boundaries. Mina comes with a TextLineCodec that does the splitting for you, resulting in your handler receiving String objects, instead of IoBuffers.

Great of course, but my idea was to line up the TextLineCodec filter with my own custom codec that creates nice command objects for my handler, and does some exception handling in case an invalid message is received. That way my handler code stays clean of any low-level protocol concerns and only works with high level objects. After several hours of fiddling around trying to create my own ProtocolCodec implementation I was unable to get it done. The interfaces didn’t allow me to pass on something else than an IoBuffer, not what i call a high-level object. After a mail conversation with Trustin Lee i decided to directly implement the IoFilter interface. But let’s start with the IoHandler.

Read more…

Categories: Apache Mina, Java