Posts tagged maven2

SFTP Server communication in Java using Jsch

6

Last week I had to write a Java software component that could connect to a SFTP server, read in flat files, process them through a business component, and then place a generated file back on the same server.

The technical details about the SFTP protocol were unclear to me, so I started reading about it on wikipedia. Basically SFTP is the same as SCP, a secure copy over SSH.

Setup of a development environment

Before anything else, I needed a SFTP server installed for development. I choose the trial version of the complete FTP suite-trial version. This isn’t the only, nor the best SFTP server-suite out there. I can imagine using proftpd, ssh, core ftp, or other ftp software.

The only next big thing I had to do was generate a public/private keypair to authenticate myself, PuTTy gen came to the rescue. In my Server admin screen I linked the key to the user account and I was ready to start developing.

JSch

The search for existing Java libraries that helped me working with this SFTP server, didn’t yield the results I was hoping for. There wasn’t a ‘best’ or ‘winning’ API or framework that emerged from the search results.

I found that Jsch was not only used by some big reference applications ( Ant(1.6 or later), Eclipse(3.0), NetBeans 5.0(and later), Maven Wagon, JIRA, and more), but it was also available in the Maven2 repository as a standalone library. I checked out the examples on their website, and found them to be very clear to me. These are also available on my Maven2 demo-project which you can find below.

All in all, I developed a small ‘push’ and ‘get’ component in my project in notime. You can find the relevant code attached to this post. You might think that this API is very lowlevel: commands as “ls”, “mkdir”, “get”, “cd”, … are all avaible in simple method calls. In my opinion, this is a good thing, because in these matters you want to have full control over your communication with the server.

Demo

This Demo will try connect to a given sftp server, list all directories and files in the home directory and exit. For getPrivateKeyAsByteStream(); implementations, take a look at FileUtils in apache-commons or look for code online.
To run this demo, you should either include Jsch on your classpath, or download my Maven2 project here.

public class SftpDemo {

	private static final String HOST = "127.0.0.1";
	private static final int PORT = 22;
	private static final String USER = "test";
	private static final String PRIVATE_KEY_LOCATION = "C:\\sftpkeys\\private_openssh";

	public static void main(String[] args) throws Exception {
		JSch.setLogger(new MyJschLogger());
		JSch jSch = new JSch();
		final byte[] privateKey = getPrivateKeyAsByteStream();
		final byte[] password = "certPass".getBytes();
		jSch.addIdentity(USER,
				privateKey,
				null,
				password
		);

                // TODO: remove this line in real life. Work with known_hosts!
		Session session = jSch.getSession(USER, HOST, PORT);
		Properties config = new Properties();
		config.put("StrictHostKeyChecking", "no");
		session.setConfig(config);

		session.connect();
		Channel channel = session.openChannel("sftp");
		ChannelSftp sftp = (ChannelSftp) channel;
		sftp.connect();

		final Vector files = sftp.ls(".");
		Iterator itFiles = files.iterator();
		while (itFiles.hasNext()) {
			System.out.println("Index: " + itFiles.next());
		}

		final ByteArrayInputStream in = new ByteArrayInputStream(
				"This is a sample text".getBytes());

		sftp.put(in, "test.txt", ChannelSftp.OVERWRITE);

		sftp.disconnect();
		session.disconnect();

	}

This logger is a standard implementation, taken from the JSch-examples. It’s nice to see what’s happening behind the scenes. You should opt for logging frameworks when using Jsch in your project (and in all other cases too of course).

	static class MyJschLogger implements Logger {
		static java.util.Hashtable name = new java.util.Hashtable();
		static {
			name.put(new Integer(DEBUG), "DEBUG: ");
			name.put(new Integer(INFO), "INFO: ");
			name.put(new Integer(WARN), "WARN: ");
			name.put(new Integer(ERROR), "ERROR: ");
			name.put(new Integer(FATAL), "FATAL: ");
		}

		public boolean isEnabled(int level) {
			return true;
		}

		public void log(int level, String message) {
			System.err.print(name.get(new Integer(level)));
			System.err.println(message);
		}

	}

}

RichFaces 4 Alpha Released

2

Today, the RichFaces 4 Alpha 2 was released. For an overview of what’s new in JSF2 and the possibilities, check out this blog by Andy Schwartz for a complete review with lots of links and documentation.

Quote from the Richfaces Project Page
RichFaces is a component library for JSF and an advanced framework for easily integrating AJAX capabilities into business applications.

  • 100+ AJAX enabled components in two libraries
    • a4j: page centric AJAX controls
    • rich: self contained, ready to use components
  • Whole set of JSF benefits while working with AJAX
  • Skinnability mechanism
  • Component Development Kit (CDK)
  • Dynamic resources handling
  • Testing facilities for components, actions, listeners, and pages
  • Broad cross-browser support
  • Large and active community
JSF 2 and RichFaces 4

We are working hard on RichFaces 4.0 which will have full JSF 2 integration. That is not all though, here is a summary of updates and features:

  • Redesigned modular repository and build system.
  • Simplified Component Development Kit with annotations, faces-config extensions, advanced templates support and more..
  • Ajax framework improvements extending the JSF 2 specification.
  • Component review for consistency, usability, and redesign following semantic HTML principles where possible.
  • Both server-side and client-side performance optimization.
  • Strict code clean-up and review.

I followed the tutorial on how to get a project up and running from this JBoss Community article and started work on a clean skeleton. On this blank project I will be experimenting with JSF2 and richfaces components in the near future.

With this blank project in mind, I created following architecture:

  • Maven2 Project
  • Reference JSF2 API and IMPL
  • RichFaces 4.0.0.Alpha2 component library

You can immediatly start experimenting by importing this Maven2 project in your favourite IDE (mvn eclipse:eclipse goal is configured). Have fun!

Download Maven2 Skeleton

Download WAR

Go to Top