[jboss-svn-commits] JBL Code SVN: r7240 - labs/jbossesb/workspace/b_georges/product/core/rosetta/src/org/jboss/soa/esb/util
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Mon Oct 30 18:33:49 EST 2006
Author: b_georges
Date: 2006-10-30 18:33:48 -0500 (Mon, 30 Oct 2006)
New Revision: 7240
Added:
labs/jbossesb/workspace/b_georges/product/core/rosetta/src/org/jboss/soa/esb/util/SFtpClientUtil.java
Log:
Added example of Secure FTP implementation to be used later with FtpClientUtil. This should stay in workspace. It is not for trunk.
Added: labs/jbossesb/workspace/b_georges/product/core/rosetta/src/org/jboss/soa/esb/util/SFtpClientUtil.java
===================================================================
--- labs/jbossesb/workspace/b_georges/product/core/rosetta/src/org/jboss/soa/esb/util/SFtpClientUtil.java 2006-10-30 23:22:24 UTC (rev 7239)
+++ labs/jbossesb/workspace/b_georges/product/core/rosetta/src/org/jboss/soa/esb/util/SFtpClientUtil.java 2006-10-30 23:33:48 UTC (rev 7240)
@@ -0,0 +1,154 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+
+package org.jboss.soa.esb.util;
+
+import java.io.InputStream;
+import java.io.PipedInputStream;
+
+import org.apache.log4j.Logger;
+
+import com.jcraft.jsch.Channel;
+import com.jcraft.jsch.ChannelSftp;
+import com.jcraft.jsch.JSch;
+import com.jcraft.jsch.JSchException;
+import com.jcraft.jsch.Session;
+import com.jcraft.jsch.SftpException;
+
+/**
+ * Secure FTP transfers
+ * <p>Description: Implements a simple set of FTP functionality
+ * Parameters to establish the Secure FTP connection are provided at construction time
+ * and cannot change during the lifetime of the object
+ * <br/>Hides low level details. Current implementation is based on the
+ * "jcraft" library
+ * Note that this class is for my workspace only and is not supposed to superseed the FtpClientUtil
+ * Instead the good stuff will be taken out of this one and integrated with the FtpClientUtil
+ * </p>
+ * @author: $Author$
+ * @version: $Rev:$
+ */
+public class SFtpClientUtil extends JSch
+{
+ private static Logger log = Logger.getLogger( SFtpClientUtil.class.getName());
+
+ private String host = null;
+ private String user = null;
+ private String password = null;
+ private Session session = null;
+ private ChannelSftp sftpChannel = null;
+ private boolean isConnected = false;
+ private String privateKeyFileName = null;
+
+ /**
+ * Constructor
+ *
+ * @param host
+ * @param user
+ * @param password
+ */
+ public SFtpClientUtil( String host, String user, String password )
+ {
+ this.host = host;
+ this.user = user;
+ this.password = password;
+ }
+
+ /**
+ * Gets a file from the SFTP-Server as Stream (currentdir is ~home).
+ *
+ * @param directory The files directory
+ * @param fileName The filename
+ * @return
+ */
+ public InputStream getFileAsSteam( String directory, String fileName )
+ {
+ if( isConnected )
+ {
+ try
+ {
+ this.sftpChannel.cd( directory );
+ PipedInputStream fileStream = (PipedInputStream)this.sftpChannel.get( fileName );
+ return fileStream;
+ }
+ catch( SftpException e )
+ {
+ log.error( "Unable to get: " + fileName + " Message: " + e );
+ }
+ }
+ else
+ {
+ log.warn( "You must call connect() before you can get a file! " );
+ }
+ return null;
+ }
+
+
+ /**
+ * Connects to the SFTP-Server
+ */
+ public void connect()
+ {
+ try
+ {
+ this.session = this.getSession( this.user, this.host, 22 );
+ //username and password will be given via UserInfo interface.
+ //this.session.setUserInfo( new UserInfo() );
+ this.session.connect();
+ Channel channel = session.openChannel( "sftp" );
+ channel.connect();
+ this.sftpChannel = (ChannelSftp)channel;
+ isConnected = this.session.isConnected();
+ }
+ catch( JSchException e )
+ {
+ log.error( "Unable to connect to " + host + " Message= " + e );
+ }
+ }
+
+ /**
+ * Disconnects from the server.
+ */
+ public void disconnect()
+ {
+ if( this.session != null )
+ {
+ this.session.disconnect();
+ }
+ }
+
+ /**
+ * @return Returns the privateKeyFileName.
+ */
+ public String getPrivateKeyFileName()
+ {
+ return privateKeyFileName;
+ }
+
+ /**
+ * @param privateKeyFileName The privateKeyFileName to set.
+ */
+ public void setPrivateKeyFileName( String privateKeyFileName )
+ {
+ this.privateKeyFileName = privateKeyFileName;
+ }
+}
\ No newline at end of file
More information about the jboss-svn-commits
mailing list