[jboss-svn-commits] JBL Code SVN: r8393 - in labs/jbossesb/trunk/product/core/rosetta/src/org/jboss: internal/soa/esb/util soa/esb/util

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Mon Dec 18 17:53:01 EST 2006


Author: b_georges
Date: 2006-12-18 17:52:58 -0500 (Mon, 18 Dec 2006)
New Revision: 8393

Modified:
   labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/util/SecureFtpImpl.java
   labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/soa/esb/util/RemoteFileSystem.java
   labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/soa/esb/util/RemoteFileSystemFactory.java
Log:
Added EPR consructor in SecureFtpImpl. 
Changed the RemoteFileSystemFactory to take FileEpr, then to return correct implementation based on URL s protocol


Modified: labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/util/SecureFtpImpl.java
===================================================================
--- labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/util/SecureFtpImpl.java	2006-12-18 22:41:04 UTC (rev 8392)
+++ labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/util/SecureFtpImpl.java	2006-12-18 22:52:58 UTC (rev 8393)
@@ -26,14 +26,22 @@
 package org.jboss.internal.soa.esb.util;
 
 import java.io.File;
+import java.net.MalformedURLException;
+import java.net.URISyntaxException;
+import java.net.URL;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Vector;
 
+import org.apache.log4j.Logger;
 import org.jboss.soa.esb.addressing.eprs.FTPEpr;
+import org.jboss.soa.esb.addressing.eprs.SFTPEpr;
+import org.jboss.soa.esb.common.Environment;
+import org.jboss.soa.esb.common.ModulePropertyManager;
 import org.jboss.soa.esb.helpers.ConfigTree;
 import org.jboss.soa.esb.helpers.KeyValuePair;
 import org.jboss.soa.esb.util.RemoteFileSystem;
+import org.jboss.soa.esb.util.RemoteFileSystemException;
 
 import com.jcraft.jsch.Channel;
 import com.jcraft.jsch.ChannelSftp;
@@ -52,44 +60,39 @@
  * 
  */
 
-public class SecureFtpImpl implements RemoteFileSystem
+public class SecureFtpImpl implements RemoteFileSystem 
 {
 
+	private static final Logger _logger = Logger.getLogger(SecureFtpImpl.class);
+
 	private static final String TMP_SUFFIX = ".rosettaPart";
 
 	private static final String SECURE_CHANNEL = "sftp";
 
-	private ConfigTree m_oParms;
+	// The objects implementing secure FTP over ssh
+	private JSch m_oJSch = new JSch();
 
-	private String m_sFtpServer;
-	private String m_sUser;
-	private String m_sPasswd;
+	private Session session = null;
 
-	private String m_sRemoteDir;
-	private String m_sLocalDir;
+	private Channel m_oChannel = null;
 
-	private int m_iPort;
+	private ChannelSftp m_oSftpChannel = null;
 
-	// TODO: add support for this if appropriate.
+	private URL m_oCertificate = null;
+
 	@SuppressWarnings("unused")
-	private boolean m_bPassive;
+	private boolean m_bConnected, m_bPassive;
 
-	// The objects implementing ssh
-	private JSch m_oJSch = new JSch();
+	private int m_iPort;
 
-	private Session session = null;
+	private SFTPEpr m_oEpr;
 
-	private Channel m_oChannel = null;
+	private ConfigTree m_oParms, m_oTree;
 
-	private ChannelSftp m_oSftpChannel = null;
+	private String m_sFtpServer, m_sUser, m_sPasswd;
 
-	// TODO: add support for keys We will do without it for now
-	@SuppressWarnings("unused")
-	private String m_sPrivateKeyFileName = null;
+	private String m_sRemoteDir, m_sLocalDir;
 
-	@SuppressWarnings("unused")
-	private boolean m_bConnected;
-
 	/*
 	 * Constructor
 	 * 
@@ -98,7 +101,7 @@
 	 * @param connect If true create a new sftp session
 	 * 
 	 */
-	public SecureFtpImpl(ConfigTree p_oP, boolean p_bConnect) throws Exception
+	public SecureFtpImpl(ConfigTree p_oP, boolean p_bConnect) throws Exception 
 	{
 		m_oParms = p_oP;
 		initialize(p_bConnect);
@@ -112,10 +115,69 @@
 	 * @param connect If true create a new sftp session
 	 * 
 	 */
-	public SecureFtpImpl(FTPEpr p_oP, boolean p_bConnect) throws Exception
+	public SecureFtpImpl(SFTPEpr p_oP, boolean p_bConnect) throws Exception 
 	{
-		
-		// TODO initialize
+
+		m_oEpr = (SFTPEpr) p_oP;
+
+		URL url = null;
+		try {
+			url = m_oEpr.getURL();
+		} catch (MalformedURLException e) {
+			throw new RemoteFileSystemException(e);
+		} catch (URISyntaxException e) {
+			throw new RemoteFileSystemException(e);
+		}
+
+		m_sFtpServer = url.getHost();
+
+		String[] sa = null;
+
+		if (url.getUserInfo() != null)
+			sa = url.getUserInfo().split(":");
+
+		if (sa == null)
+			sa = new String[] { "", "" };
+
+		m_sUser = (sa.length < 1) ? "" : sa[0];
+		m_sPasswd = (sa.length < 2) ? "" : sa[1];
+
+		m_sRemoteDir = url.getFile();
+
+		final String tmpdir = System.getProperty("java.io.tmpdir");
+		if ((m_sRemoteDir == null) || (m_sRemoteDir.equals("")))
+			m_sRemoteDir = ModulePropertyManager.getPropertyManager(
+					ModulePropertyManager.TRANSPORTS_MODULE).getProperty(
+							Environment.FTP_REMOTEDIR, tmpdir);
+
+		m_iPort = url.getPort();
+		if (m_iPort < 0)
+			m_iPort = url.getDefaultPort();
+
+		m_sLocalDir = ModulePropertyManager.getPropertyManager(
+				ModulePropertyManager.TRANSPORTS_MODULE).getProperty(
+						Environment.FTP_LOCALDIR, tmpdir);
+
+		m_bPassive = false;
+
+		try {
+			m_bPassive = m_oEpr.getPassive();
+		} catch (URISyntaxException e) {
+			_logger.warn(e);
+		}
+
+		try 
+		{
+			m_oCertificate = m_oEpr.getCertificateURL();
+		} catch (URISyntaxException e) {
+			_logger.warn(e);
+		}
+
+		// TODO there is still a bit of space for improvements here.
+		configTreeFromEpr();
+
+		initialize(p_bConnect);
+
 	}
 
 	/*
@@ -127,19 +189,16 @@
 	 * 
 	 */
 	public SecureFtpImpl(List<KeyValuePair> attribs, boolean connect)
-			throws Exception
-	{
+	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
-	{
+	private void initialize(boolean bConnect) throws Exception {
 		checkParms();
-		if (bConnect)
-		{
+		if (bConnect) {
 			session = m_oJSch.getSession(m_sUser, m_sFtpServer, m_iPort);
 
 			UserInfo ui = new SecureFtpUserInfo("");
@@ -164,19 +223,18 @@
 
 	}
 
-	private void checkParms() throws Exception
-	{
+	private void checkParms() throws Exception {
 		m_sFtpServer = m_oParms.getAttribute(PARMS_FTP_SERVER);
 		if (null == m_sFtpServer)
-			throw new Exception("No FTP server specified");
+			throw new Exception("No SFTP server specified");
 
 		m_sUser = m_oParms.getAttribute(PARMS_USER);
 		if (null == m_sUser)
-			throw new Exception("No username specified for FTP");
+			throw new Exception("No username specified for SFTP");
 
 		m_sPasswd = m_oParms.getAttribute(PARMS_PASSWD);
 		if (null == m_sPasswd)
-			throw new Exception("No password specified for FTP");
+			throw new Exception("No password specified for SFTP");
 
 		m_sRemoteDir = m_oParms.getAttribute(PARMS_REMOTE_DIR);
 		if (null == m_sRemoteDir)
@@ -189,15 +247,11 @@
 		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);
 
+		m_oCertificate = new URL(m_oParms.getAttribute(PARMS_CERTIFICATE));
 		return;
 	}
 
@@ -208,8 +262,7 @@
 	 * 
 	 * @see org.jboss.soa.esb.util.RemoteFileSystem#deleteRemoteFile(java.lang.String)
 	 */
-	public void deleteRemoteFile(String p_sFile) throws Exception
-	{
+	public void deleteRemoteFile(String p_sFile) throws Exception {
 		m_oSftpChannel.rm(getRemoteDir() + "/" + new File(p_sFile).getName());
 	}
 
@@ -220,8 +273,7 @@
 	 * 
 	 * @see org.jboss.soa.esb.util.RemoteFileSystem#remoteDelete(java.io.File)
 	 */
-	public void remoteDelete(File p_oFile) throws Exception
-	{
+	public void remoteDelete(File p_oFile) throws Exception {
 		m_oSftpChannel.rm(FtpUtils.fileToFtpString(p_oFile));
 	}
 
@@ -232,21 +284,16 @@
 	 * 
 	 * @see org.jboss.soa.esb.util.RemoteFileSystem#getFileListFromRemoteDir(java.lang.String)
 	 */
-	public String[] getFileListFromRemoteDir(String p_sSuffix) throws Exception
-	{
+	public String[] getFileListFromRemoteDir(String p_sSuffix) throws Exception {
 		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++)
-			{
+		if (vFileList != null) {
+			for (int i = 0; i < vFileList.size(); i++) {
 				Object obj = vFileList.elementAt(i);
-				if (obj instanceof LsEntry)
-				{
+				if (obj instanceof LsEntry) {
 					SftpATTRS oSftAttr = ((LsEntry) obj).getAttrs();
-					if (!oSftAttr.isDir())
-					{
+					if (!oSftAttr.isDir()) {
 						lFileList.add(((LsEntry) obj).getFilename());
 					}
 				}
@@ -262,8 +309,7 @@
 	 * 
 	 * @see org.jboss.soa.esb.util.RemoteFileSystem#setRemoteDir(java.lang.String)
 	 */
-	public void setRemoteDir(String p_sDir) throws Exception
-	{
+	public void setRemoteDir(String p_sDir) throws Exception {
 		m_oSftpChannel.cd(p_sDir);
 	}
 
@@ -278,21 +324,18 @@
 	 *      java.lang.String)
 	 */
 	public void renameInRemoteDir(String p_sFrom, String p_sTo)
-			throws Exception
-	{
+	throws Exception {
 
 		String sRmtFrom = new File(p_sFrom).getName();
 		String sRmtTo = new File(p_sTo).getName();
 
-		try
-		{
+		try {
 			m_oSftpChannel.rename(getRemoteDir() + "/" + sRmtFrom,
 					getRemoteDir() + "/" + sRmtTo);
-		} catch (Exception e)
-		{
+		} catch (Exception e) {
 			String sMess = this.getClass().getSimpleName()
-					+ " can't rename in remote directory <" + e.getMessage()
-					+ ">";
+			+ " can't rename in remote directory <" + e.getMessage()
+			+ ">";
 			throw new Exception(sMess);
 		}
 	}
@@ -307,17 +350,14 @@
 	 * @see org.jboss.soa.esb.util.RemoteFileSystem#remoteRename(java.io.File,
 	 *      java.io.File)
 	 */
-	public void remoteRename(File p_oFrom, File p_oTo) throws Exception
-	{
-		try
-		{
-			m_oSftpChannel.rename(FtpUtils.fileToFtpString(p_oFrom),
-					FtpUtils.fileToFtpString(p_oTo));
-		} catch (Exception e)
-		{
+	public void remoteRename(File p_oFrom, File p_oTo) throws Exception {
+		try {
+			m_oSftpChannel.rename(FtpUtils.fileToFtpString(p_oFrom), FtpUtils
+					.fileToFtpString(p_oTo));
+		} catch (Exception e) {
 			String sMess = this.getClass().getSimpleName()
-					+ " can't rename in remote directory <" + e.getMessage()
-					+ ">";
+			+ " can't rename in remote directory <" + e.getMessage()
+			+ ">";
 			throw new Exception(sMess);
 		}
 	}
@@ -327,16 +367,15 @@
 	 * 
 	 * @param p_oFile The local file name we want to upload
 	 * 
-	 * @param p_sRemoteName The remote file name [can be the same as p_oFile of course]
+	 * @param p_sRemoteName The remote file name [can be the same as p_oFile of
+	 * course]
 	 * 
 	 * @see org.jboss.soa.esb.util.RemoteFileSystem#uploadFile(java.io.File,
 	 *      java.lang.String)
 	 */
-	public void uploadFile(File p_oFile, String p_sRemoteName) throws Exception
-	{
+	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(FtpUtils.fileToFtpString(p_oFile), sRemoteTmp);
 		m_oSftpChannel.rename(sRemoteTmp, sRemoteOK);
 	}
@@ -352,16 +391,13 @@
 	 *      java.lang.String)
 	 */
 	public void downloadFile(String p_sFile, String p_sFinalName)
-			throws Exception
-	{
+	throws Exception {
 		File oLocalDir = new File(m_sLocalDir);
 		File oLclFile = File.createTempFile("Rosetta_", TMP_SUFFIX, oLocalDir);
 
-		try
-		{
+		try {
 			oLclFile.delete();
-		} catch (Exception e)
-		{
+		} catch (Exception e) {
 		}
 		// TODO check if we have to set the Transfer Type with JSch impl =>
 		// m_oXferType
@@ -378,8 +414,7 @@
 	 * 
 	 * @see org.jboss.soa.esb.util.RemoteFileSystem#getRemoteDir()
 	 */
-	public String getRemoteDir()
-	{
+	public String getRemoteDir() {
 		return m_sRemoteDir;
 	}
 
@@ -388,11 +423,29 @@
 	 * 
 	 * @see org.jboss.soa.esb.util.RemoteFileSystem#quit()
 	 */
-	public void quit()
-	{
+	public void quit() {
 		m_oSftpChannel.quit();
 	}
 
-	
+	private void configTreeFromEpr() throws RemoteFileSystemException {
+		try {
+			m_oTree.setAttribute(RemoteFileSystem.PARMS_FTP_SERVER,
+					m_sFtpServer);
+			m_oTree.setAttribute(RemoteFileSystem.PARMS_USER, m_sUser);
+			m_oTree.setAttribute(RemoteFileSystem.PARMS_PASSWD, m_sPasswd);
+			m_oTree.setAttribute(RemoteFileSystem.PARMS_REMOTE_DIR,
+					m_sRemoteDir);
+			m_oTree.setAttribute(RemoteFileSystem.PARMS_PORT, Integer
+					.toString(m_iPort));
+			m_oTree.setAttribute(RemoteFileSystem.PARMS_LOCAL_DIR, m_sLocalDir);
+			m_oTree.setAttribute(RemoteFileSystem.PARMS_ASCII, Boolean
+					.toString(false));
+			m_oTree.setAttribute(RemoteFileSystem.PARMS_PASSIVE, Boolean
+					.toString(m_bPassive));
+			m_oTree.setAttribute(RemoteFileSystem.PARMS_CERTIFICATE, m_oCertificate.toString());
+		} catch (Exception e) {
+			throw new RemoteFileSystemException(e);
+		}
+	}
 
 }

Modified: labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/soa/esb/util/RemoteFileSystem.java
===================================================================
--- labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/soa/esb/util/RemoteFileSystem.java	2006-12-18 22:41:04 UTC (rev 8392)
+++ labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/soa/esb/util/RemoteFileSystem.java	2006-12-18 22:52:58 UTC (rev 8393)
@@ -48,6 +48,8 @@
 
 	public static final String PARMS_PASSIVE = "ftpPassive";
 
+	public static final String PARMS_CERTIFICATE = "certificate";
+	
 	public String getRemoteDir();
 
 	/**

Modified: labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/soa/esb/util/RemoteFileSystemFactory.java
===================================================================
--- labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/soa/esb/util/RemoteFileSystemFactory.java	2006-12-18 22:41:04 UTC (rev 8392)
+++ labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/soa/esb/util/RemoteFileSystemFactory.java	2006-12-18 22:52:58 UTC (rev 8393)
@@ -32,6 +32,8 @@
 import org.jboss.internal.soa.esb.util.EdtFtpImpl;
 import org.jboss.internal.soa.esb.util.SecureFtpImpl;
 import org.jboss.soa.esb.addressing.eprs.FTPEpr;
+import org.jboss.soa.esb.addressing.eprs.FileEpr;
+import org.jboss.soa.esb.addressing.eprs.SFTPEpr;
 import org.jboss.soa.esb.helpers.ConfigTree;
 import org.xml.sax.SAXException;
 
@@ -41,15 +43,15 @@
 	private RemoteFileSystemFactory() {
 	}
 
-	public static RemoteFileSystem getRemoteFileSystem(FTPEpr p_oEpr, boolean p_bConnect)
+	public static RemoteFileSystem getRemoteFileSystem(FileEpr p_oEpr, boolean p_bConnect)
 			throws RemoteFileSystemException {
 		URL url = null;
 		try {
-			url = p_oEpr.getURL();
+			url =  p_oEpr.getURL();
 			if (url.getProtocol().equals(RemoteFileSystem.FTP_PROTOCOL))
-				return new EdtFtpImpl(p_oEpr, p_bConnect);
+				return new EdtFtpImpl((FTPEpr)p_oEpr, p_bConnect);
 			else if (url.getProtocol().equals(RemoteFileSystem.FTP_PROTOCOL))
-				return new SecureFtpImpl(p_oEpr, p_bConnect);
+				return new SecureFtpImpl((SFTPEpr)p_oEpr, p_bConnect);
 
 		} catch (MalformedURLException e) {
 			throw new RemoteFileSystemException(e);




More information about the jboss-svn-commits mailing list