[jboss-svn-commits] JBL Code SVN: r7556 - labs/jbossesb/workspace/b_georges/product/core/rosetta/src/org/jboss/internal/soa/esb/util
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Sun Nov 12 10:57:46 EST 2006
Author: b_georges
Date: 2006-11-12 10:57:44 -0500 (Sun, 12 Nov 2006)
New Revision: 7556
Modified:
labs/jbossesb/workspace/b_georges/product/core/rosetta/src/org/jboss/internal/soa/esb/util/SecureFtpImpl.java
Log:
Implemented all methods using jsch api.
There are few things I need to check:
PASSIVE/ACTIVE mode
and
Transfer Types.
Modified: labs/jbossesb/workspace/b_georges/product/core/rosetta/src/org/jboss/internal/soa/esb/util/SecureFtpImpl.java
===================================================================
--- labs/jbossesb/workspace/b_georges/product/core/rosetta/src/org/jboss/internal/soa/esb/util/SecureFtpImpl.java 2006-11-12 15:15:01 UTC (rev 7555)
+++ labs/jbossesb/workspace/b_georges/product/core/rosetta/src/org/jboss/internal/soa/esb/util/SecureFtpImpl.java 2006-11-12 15:57:44 UTC (rev 7556)
@@ -4,95 +4,287 @@
package org.jboss.internal.soa.esb.util;
import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Vector;
+import org.jboss.soa.esb.helpers.ConfigTree;
+import org.jboss.soa.esb.helpers.KeyValuePair;
import org.jboss.soa.esb.util.RemoteFileSystem;
+import com.jcraft.jsch.Channel;
+import com.jcraft.jsch.ChannelSftp;
+import com.jcraft.jsch.JSch;
+import com.jcraft.jsch.Session;
+import com.jcraft.jsch.SftpATTRS;
+import com.jcraft.jsch.UserInfo;
+import com.jcraft.jsch.ChannelSftp.LsEntry;
+
/**
- * @author geo
- *
+ *
+ * Implementation of sftp (Secure FTP over SSH) Based on JSch from JCraft
+ * http://www.jcraft.com/
+ *
+ * @author b_georges
+ *
*/
+
public class SecureFtpImpl implements RemoteFileSystem {
- /* (non-Javadoc)
+ private static final String TMP_SUFFIX = ".rosettaPart";
+
+ private ConfigTree m_oParms;
+
+ private String m_sFtpServer, m_sUser, m_sPasswd;
+
+ private String m_sRemoteDir, m_sLocalDir;
+
+ private int m_iPort;
+
+ // TODO: add support for this if appropriate.
+ private boolean m_bPassive;
+
+ /** The objects implementing ssh */
+ private JSch m_oJSch = new JSch();
+
+ private Session session = null;
+
+ private Channel m_oChannel = null;
+
+ private ChannelSftp m_oSftpChannel = null;
+
+ // TODO: add support for keys We will do without it for now
+ private String m_sPrivateKeyFileName = null;
+
+ private boolean m_bConnected;
+
+ public SecureFtpImpl(ConfigTree p_oP, boolean p_bConnect) throws Exception {
+ m_oParms = p_oP;
+ initialize(p_bConnect);
+ }
+
+ public SecureFtpImpl(List<KeyValuePair> attribs, boolean connect)
+ throws Exception {
+ m_oParms = new ConfigTree("fromProps");
+ for (KeyValuePair oCurr : attribs)
+ m_oParms.setAttribute(oCurr.getKey(), oCurr.getValue());
+ initialize(connect);
+ }
+
+ private void initialize(boolean bConnect) throws Exception {
+ checkParms();
+ if (bConnect) {
+ session = m_oJSch.getSession(m_sUser, m_sFtpServer, m_iPort);
+
+ // username and password will be given via UserInfo interface.
+ UserInfo ui = new SecureFtpUserInfo("");
+ session.setUserInfo(ui);
+
+ session.connect();
+
+ m_oChannel = session.openChannel("sftp");
+
+ m_oChannel.connect();
+
+ m_oSftpChannel = (ChannelSftp) m_oChannel;
+
+ for (int i1 = 0; i1 < 10 && !session.isConnected(); i1++)
+ Thread.sleep(200);
+ if (!session.isConnected())
+ throw new Exception("Can't connect to FTP server");
+
+ m_bConnected = this.session.isConnected();
+ }
+ // TODO set connection Mode [PASSIVE|ACTIVE]using m_bPassive ?
+
+ }
+
+ private void checkParms() throws Exception {
+ m_sFtpServer = m_oParms.getAttribute(PARMS_FTP_SERVER);
+ if (null == m_sFtpServer)
+ throw new Exception("No FTP server specified");
+
+ m_sUser = m_oParms.getAttribute(PARMS_USER);
+ if (null == m_sUser)
+ throw new Exception("No username specified for FTP");
+
+ m_sPasswd = m_oParms.getAttribute(PARMS_PASSWD);
+ if (null == m_sPasswd)
+ throw new Exception("No password specified for FTP");
+
+ m_sRemoteDir = m_oParms.getAttribute(PARMS_REMOTE_DIR);
+ if (null == m_sRemoteDir)
+ m_sRemoteDir = "";
+
+ m_sLocalDir = m_oParms.getAttribute(PARMS_LOCAL_DIR);
+ if (null == m_sLocalDir)
+ m_sLocalDir = ".";
+
+ String sAux = m_oParms.getAttribute(PARMS_PORT);
+ m_iPort = (null == sAux) ? 22 : Integer.parseInt(sAux);
+
+ boolean bAscii = false;
+ sAux = m_oParms.getAttribute(PARMS_ASCII);
+ if (null != sAux)
+ bAscii = Boolean.parseBoolean(sAux);
+
+ m_bPassive = false;
+ sAux = m_oParms.getAttribute(PARMS_PASSIVE);
+ m_bPassive = (null != sAux) && Boolean.parseBoolean(sAux);
+
+ return;
+ }
+
+ /*
+ * Deletes a file on the SFTP-Server
+ *
+ * @param fileName The file's Name to be removed from the SFTP-Server
+ *
* @see org.jboss.soa.esb.util.RemoteFileSystem#deleteRemoteFile(java.lang.String)
*/
public void deleteRemoteFile(String p_sFile) throws Exception {
- // TODO Auto-generated method stub
-
+ m_oSftpChannel.rm(getRemoteDir() + "/" + new File(p_sFile).getName());
}
- /* (non-Javadoc)
- * @see org.jboss.soa.esb.util.RemoteFileSystem#downloadFile(java.lang.String, java.lang.String)
+ /*
+ * Deletes a file on the SFTP-Server
+ *
+ * @param fileName The file to be removed from the SFTP-Server
+ *
+ * @see org.jboss.soa.esb.util.RemoteFileSystem#remoteDelete(java.io.File)
*/
- public void downloadFile(String p_sFile, String p_sFinalName)
- throws Exception {
- // TODO Auto-generated method stub
-
+ public void remoteDelete(File p_oFile) throws Exception {
+ m_oSftpChannel.rm(fileToFtpString(p_oFile));
}
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
+ *
* @see org.jboss.soa.esb.util.RemoteFileSystem#getFileListFromRemoteDir(java.lang.String)
*/
public String[] getFileListFromRemoteDir(String p_sSuffix) throws Exception {
- // TODO Auto-generated method stub
- return null;
+ String sSuffix = (null == p_sSuffix) ? "*" : "*" + p_sSuffix;
+ List<String> lFileList = new ArrayList<String>();
+ Vector vFileList = m_oSftpChannel.ls(sSuffix);
+ if (vFileList != null) {
+ for (int i = 0; i < vFileList.size(); i++) {
+ Object obj = vFileList.elementAt(i);
+ if (obj instanceof LsEntry) {
+ SftpATTRS oSftAttr = ((LsEntry) obj).getAttrs();
+ if (!oSftAttr.isDir()) {
+ lFileList.add(((LsEntry) obj).getFilename());
+ }
+ }
+ }
+ }
+ return (String[]) lFileList.toArray();
}
- /* (non-Javadoc)
- * @see org.jboss.soa.esb.util.RemoteFileSystem#getRemoteDir()
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.jboss.soa.esb.util.RemoteFileSystem#setRemoteDir(java.lang.String)
*/
- public String getRemoteDir() {
- // TODO Auto-generated method stub
- return null;
+ public void setRemoteDir(String p_sDir) throws Exception {
+ m_oSftpChannel.cd(p_sDir);
}
- /* (non-Javadoc)
- * @see org.jboss.soa.esb.util.RemoteFileSystem#quit()
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.jboss.soa.esb.util.RemoteFileSystem#renameInRemoteDir(java.lang.String,
+ * java.lang.String)
*/
- public void quit() {
- // TODO Auto-generated method stub
+ public void renameInRemoteDir(String p_sFrom, String p_sTo)
+ throws Exception {
+ String sRmtFrom = new File(p_sFrom).getName();
+ String sRmtTo = new File(p_sTo).getName();
+
+ try {
+ m_oSftpChannel.rename(getRemoteDir() + "/" + sRmtFrom,
+ getRemoteDir() + "/" + sRmtTo);
+ } catch (Exception e) {
+ String sMess = this.getClass().getSimpleName()
+ + " can't rename in remote directory <" + e.getMessage()
+ + ">";
+ throw new Exception(sMess);
+ }
}
- /* (non-Javadoc)
- * @see org.jboss.soa.esb.util.RemoteFileSystem#remoteDelete(java.io.File)
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.jboss.soa.esb.util.RemoteFileSystem#remoteRename(java.io.File,
+ * java.io.File)
*/
- public void remoteDelete(File p_oFile) throws Exception {
- // TODO Auto-generated method stub
-
+ public void remoteRename(File p_oFrom, File p_oTo) throws Exception {
+ try {
+ m_oSftpChannel.rename(fileToFtpString(p_oFrom),
+ fileToFtpString(p_oTo));
+ } catch (Exception e) {
+ String sMess = this.getClass().getSimpleName()
+ + " can't rename in remote directory <" + e.getMessage()
+ + ">";
+ throw new Exception(sMess);
+ }
}
- /* (non-Javadoc)
- * @see org.jboss.soa.esb.util.RemoteFileSystem#remoteRename(java.io.File, java.io.File)
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.jboss.soa.esb.util.RemoteFileSystem#uploadFile(java.io.File,
+ * java.lang.String)
*/
- public void remoteRename(File p_oFrom, File p_oTo) throws Exception {
- // TODO Auto-generated method stub
-
+ public void uploadFile(File p_oFile, String p_sRemoteName) throws Exception {
+ String sRemoteOK = getRemoteDir() + "/" + p_sRemoteName;
+ String sRemoteTmp = sRemoteOK + TMP_SUFFIX;
+ // m_oSftpChannel.setType(m_oXferType);
+ m_oSftpChannel.put(fileToFtpString(p_oFile), sRemoteTmp);
+ m_oSftpChannel.rename(sRemoteTmp, sRemoteOK);
}
- /* (non-Javadoc)
- * @see org.jboss.soa.esb.util.RemoteFileSystem#renameInRemoteDir(java.lang.String, java.lang.String)
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.jboss.soa.esb.util.RemoteFileSystem#downloadFile(java.lang.String,
+ * java.lang.String)
*/
- public void renameInRemoteDir(String p_sFrom, String p_sTo)
+ public void downloadFile(String p_sFile, String p_sFinalName)
throws Exception {
- // TODO Auto-generated method stub
+ File oLocalDir = new File(m_sLocalDir);
+ File oLclFile= File.createTempFile("Rosetta_",TMP_SUFFIX,oLocalDir);
+ try { oLclFile.delete(); }
+ catch (Exception e) {}
+ //TODO check if we have to set the Transfer Type with JSch impl => m_oXferType
+ m_oSftpChannel.get(fileToFtpString(oLclFile),p_sFile);
+
+ File oNew = new File(oLocalDir,p_sFinalName);
+ if (oNew.exists()) oNew.delete();
+ oLclFile.renameTo(oNew);
}
- /* (non-Javadoc)
- * @see org.jboss.soa.esb.util.RemoteFileSystem#setRemoteDir(java.lang.String)
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.jboss.soa.esb.util.RemoteFileSystem#getRemoteDir()
*/
- public void setRemoteDir(String p_sDir) throws Exception {
- // TODO Auto-generated method stub
-
+ public String getRemoteDir() {
+ return m_sRemoteDir;
}
- /* (non-Javadoc)
- * @see org.jboss.soa.esb.util.RemoteFileSystem#uploadFile(java.io.File, java.lang.String)
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.jboss.soa.esb.util.RemoteFileSystem#quit()
*/
- public void uploadFile(File p_oFile, String p_sRemoteName) throws Exception {
- // TODO Auto-generated method stub
+ public void quit() {
+ m_oSftpChannel.quit();
+ }
+ public static String fileToFtpString(File p_oF) {
+ return (null == p_oF) ? null : p_oF.toString().replace("\\", "/");
}
}
More information about the jboss-svn-commits
mailing list