From 34d1647126265339a71eec2b83b521ce44678aea Mon Sep 17 00:00:00 2001 From: asif Date: Sun, 11 May 2025 17:27:11 +0530 Subject: [PATCH] cleaned up, formatted and made the project runnable after decompilation. Also added new configurations for UCB and NABARD servres --- .gitignore | 2 + pom.xml | 69 ++++++++ src/main/java/com/Main/Main.java | 121 +++++++++++++ .../java/com/fetchFile/FetchReportFile.java | 66 +++++++ src/main/java/com/moveFile/Movefile.java | 17 ++ src/main/java/com/sendFile/SendLocalfile.java | 76 ++++++++ .../sendFile/protocol/FTP/FTPProtocol.java | 144 +++++++++++++++ .../sendFile/protocol/SSH/SSHProtocol.java | 165 ++++++++++++++++++ src/main/resources/config.properties | 21 +++ 9 files changed, 681 insertions(+) create mode 100644 .gitignore create mode 100644 pom.xml create mode 100644 src/main/java/com/Main/Main.java create mode 100644 src/main/java/com/fetchFile/FetchReportFile.java create mode 100644 src/main/java/com/moveFile/Movefile.java create mode 100644 src/main/java/com/sendFile/SendLocalfile.java create mode 100644 src/main/java/com/sendFile/protocol/FTP/FTPProtocol.java create mode 100644 src/main/java/com/sendFile/protocol/SSH/SSHProtocol.java create mode 100644 src/main/resources/config.properties diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ca37d98 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +dependency-reduced-pom.xml +target/ diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..e4f253a --- /dev/null +++ b/pom.xml @@ -0,0 +1,69 @@ + + + 4.0.0 + com.main + SSHFileToCbs_PROD + 1.0 + jar + + UTF-8 + 1.8 + 1.8 + com.Main.Main + + + + + commons-net + commons-net + 3.3 + + + com.jcraft + jsch + 0.1.54 + + + com.oracle.database.jdbc + ojdbc8 + 23.5.0.24.07 + + + + + + + + + + + org.apache.maven.plugins + maven-shade-plugin + 3.2.4 + + + package + + shade + + + + + com.Main.Main + + + + + + + + + + diff --git a/src/main/java/com/Main/Main.java b/src/main/java/com/Main/Main.java new file mode 100644 index 0000000..baa52a0 --- /dev/null +++ b/src/main/java/com/Main/Main.java @@ -0,0 +1,121 @@ +package com.Main; + +import java.io.InputStream; +import java.util.Properties; +import java.util.concurrent.TimeUnit; +import com.fetchFile.FetchReportFile; +import com.sendFile.SendLocalfile; + +public class Main { + public static void main(String[] args) { + String user = ""; + String password = ""; + String host = ""; + String local_folder_path = ""; + String local_archive_path = ""; + String local_report_path = ""; + String remote_file_path = ""; + String remote_report_path = ""; + String remote_report_pattern = ""; + String transfer_protocol = ""; + String remote_failed_path = ""; + String local_failed_path = ""; + int port = 22; + long sleep = 5L; + + try { + InputStream input = Main.class.getClassLoader().getResourceAsStream("config.properties"); + Properties prop = new Properties(); + if (input == null) { + System.out.println("Sorry, unable to find config.properties"); + return; + } + prop.load(input); + + host = prop.getProperty("REMOTE_HOST"); + user = prop.getProperty("REMOTE_USER"); + password = prop.getProperty("REMOTE_PASS"); + port = Integer.parseInt(prop.getProperty("REMOTE_PORT")); + sleep = Long.parseLong(prop.getProperty("SLEEP_TIME_MINS")); + local_folder_path = prop.getProperty("LOCAL_FOLDER_PATH"); + local_archive_path = prop.getProperty("ARCHIVE_FOLDER_PATH"); + local_report_path = prop.getProperty("LOCAL_REPORT_PATH"); + remote_file_path = prop.getProperty("REMOTE_INPUT_FILE_PATH"); + remote_report_path = prop.getProperty("REMOTE_OUTPUT_FILE_PATH"); + transfer_protocol = prop.getProperty("TRANSFER_PROTOCOL"); + remote_report_pattern = prop.getProperty("REMOTE_REPORT_PATTERN"); + remote_failed_path = prop.getProperty("REMOTE_FAILURE_FILE_PATH"); + local_failed_path = prop.getProperty("LOCAL_FAILED_PATH"); + + System.out.println("Config Properties read:"); + System.out.println( + "REMOTE_HOST: " + + host + + "\nREMOTE_USER: " + + user + + "\nREMOTE_PASS: " + + password + + "\nREMOTE_PORT: " + + port + + "\nREMOTE_FILE_PATH: " + + remote_file_path + + "\nSLEEP_TIME: " + + sleep + + "\nLOCAL_FOLDER_PATH: " + + local_folder_path + + "\nTRANSFER_PROTOCOL: " + + transfer_protocol); + + } catch (Exception e) { + e.printStackTrace(); + } + + SendLocalfile send = + new SendLocalfile( + user, + password, + host, + local_folder_path, + remote_file_path, + transfer_protocol, + port, + local_archive_path); + FetchReportFile fetch = + new FetchReportFile( + user, + password, + host, + local_report_path, + remote_report_path, + transfer_protocol, + port, + remote_report_pattern); + FetchReportFile fetchFailed = + new FetchReportFile( + user, + password, + host, + local_failed_path, + remote_failed_path, + transfer_protocol, + port, + remote_report_pattern); + + try { + while (true) { + System.out.println("___________________________________________________"); + send.sendFiles(); + fetch.fetchFiles(); + fetchFailed.fetchFiles(); + System.out.println("Sleeping for:" + sleep + " Minutes"); + System.out.println("___________________________________________________"); + TimeUnit.MINUTES.sleep(sleep); + } + + } catch (Exception e) { + + System.out.println(e); + return; + } + } +} \ No newline at end of file diff --git a/src/main/java/com/fetchFile/FetchReportFile.java b/src/main/java/com/fetchFile/FetchReportFile.java new file mode 100644 index 0000000..f397e7f --- /dev/null +++ b/src/main/java/com/fetchFile/FetchReportFile.java @@ -0,0 +1,66 @@ +package com.fetchFile; + +import com.sendFile.protocol.FTP.FTPProtocol; +import com.sendFile.protocol.SSH.SSHProtocol; + +public class FetchReportFile { + String user; + String password; + String host; + String local_folder_path; + String remote_file_path; + String transfer_protocol; + String remote_report_pattern; + int port; + + public FetchReportFile( + String user, + String password, + String host, + String local_folder_path, + String remote_file_path, + String transfer_protocol, + int port, + String remote_report_pattern) { + this.user = user; + this.password = password; + this.host = host; + this.local_folder_path = local_folder_path; + this.remote_file_path = remote_file_path; + this.transfer_protocol = transfer_protocol; + this.port = port; + this.remote_report_pattern = remote_report_pattern; + } + + public void fetchFiles() { + if (this.transfer_protocol.equals("SSH")) { + SSHProtocol ssh = + new SSHProtocol( + this.user, + this.password, + this.host, + this.port, + this.local_folder_path, + this.remote_file_path, + "", + ""); + ssh.fetchFileSSH(this.remote_report_pattern); + } else if (this.transfer_protocol.equals("FTP")) { + FTPProtocol ftp = + new FTPProtocol( + this.user, + this.password, + this.host, + this.port, + this.local_folder_path, + this.remote_file_path, + "", + ""); + + ftp.fetchFileFTP(this.remote_report_pattern); + } else { + + System.out.println("Invalid TRANSFER_PROTOCOL. Must be one of FTP or SSH"); + } + } +} \ No newline at end of file diff --git a/src/main/java/com/moveFile/Movefile.java b/src/main/java/com/moveFile/Movefile.java new file mode 100644 index 0000000..1849aa9 --- /dev/null +++ b/src/main/java/com/moveFile/Movefile.java @@ -0,0 +1,17 @@ +package com.moveFile; + +import java.io.File; + +public class Movefile { + public void movefile(File inpFile, String archivePath) { + try { + if (inpFile.renameTo(new File(archivePath + inpFile.getName()))) { + System.out.println("File is moved successful!"); + } else { + System.out.println("File is failed to move!"); + } + } catch (Exception e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/src/main/java/com/sendFile/SendLocalfile.java b/src/main/java/com/sendFile/SendLocalfile.java new file mode 100644 index 0000000..756f2f4 --- /dev/null +++ b/src/main/java/com/sendFile/SendLocalfile.java @@ -0,0 +1,76 @@ +package com.sendFile; + +import java.io.File; +import com.sendFile.protocol.FTP.FTPProtocol; +import com.sendFile.protocol.SSH.SSHProtocol; + +public class SendLocalfile { + String user; + String password; + String host; + String local_folder_path; + String archive_path; + String remote_file_path; + String transfer_protocol; + int port; + + public SendLocalfile( + String user, + String password, + String host, + String local_folder_path, + String remote_file_path, + String transfer_protocol, + int port, + String archivePath) { + this.user = user; + this.password = password; + this.host = host; + this.local_folder_path = local_folder_path; + this.remote_file_path = remote_file_path; + this.transfer_protocol = transfer_protocol; + this.port = port; + this.archive_path = archivePath; + } + + public void sendFiles() { + File folder = new File(this.local_folder_path); + File[] listOfFiles = folder.listFiles(); + + for (File file : listOfFiles) { + if (file.isFile()) { + System.out.println("Going to send file: " + file.getAbsolutePath()); + if (this.transfer_protocol.equals("SSH")) { + + SSHProtocol ssh = + new SSHProtocol( + this.user, + this.password, + this.host, + this.port, + file.getAbsolutePath(), + this.remote_file_path, + file.getName(), + this.archive_path); + ssh.sendFileSSH(); + } else if (this.transfer_protocol.equals("FTP")) { + + FTPProtocol ftp = + new FTPProtocol( + this.user, + this.password, + this.host, + this.port, + file.getAbsolutePath(), + this.remote_file_path, + file.getName(), + this.archive_path); + ftp.sendFileFTP(); + } else { + + System.out.println("Invalid TRANSFER_PROTOCOL. Must be one of FTP or SSH"); + } + } + } + } +} \ No newline at end of file diff --git a/src/main/java/com/sendFile/protocol/FTP/FTPProtocol.java b/src/main/java/com/sendFile/protocol/FTP/FTPProtocol.java new file mode 100644 index 0000000..8e20d27 --- /dev/null +++ b/src/main/java/com/sendFile/protocol/FTP/FTPProtocol.java @@ -0,0 +1,144 @@ +package com.sendFile.protocol.FTP; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.InputStream; +import java.io.OutputStream; +import com.moveFile.Movefile; +import org.apache.commons.net.ftp.FTPClient; +import org.apache.commons.net.ftp.FTPFile; + +public class FTPProtocol { + String user; + String password; + String host; + int port; + String local_fileName; + String remote_filePath; + String remote_fileName; + String archive_path; + + public FTPProtocol( + String user, + String password, + String host, + int port, + String local_fileName, + String remote_filePath, + String remote_fileName, + String archive_path) { + this.user = user; + this.password = password; + this.host = host; + this.port = port; + this.local_fileName = local_fileName; + this.remote_filePath = remote_filePath; + this.remote_fileName = remote_fileName; + this.archive_path = archive_path; + } + + public void sendFileFTP() { + System.out.println("Going to SEND files from remote Server via FTP"); + FTPClient ftpClient = new FTPClient(); + try { + System.out.println( + "Creating SSH Session: user@host:port |--->" + + this.user + + "@" + + this.host + + ":" + + this.port); + ftpClient.connect(this.host, this.port); + ftpClient.login(this.user, this.password); + ftpClient.enterLocalPassiveMode(); + + ftpClient.setFileType(2); + + File firstLocalFile = new File(this.local_fileName); + + String[] fileTokens = this.remote_fileName.split("-"); + + System.out.println( + "Going to create brancheise folder:" + fileTokens[0] + " at " + this.remote_filePath); + + ftpClient.changeWorkingDirectory(this.remote_filePath); + try { + ftpClient.makeDirectory(fileTokens[0]); + } catch (Exception ex) { + System.out.println("Folder not created: " + ex.getMessage()); + } + ftpClient.changeWorkingDirectory(fileTokens[0]); + + InputStream inputStream = new FileInputStream(firstLocalFile); + + System.out.println("Start uploading file"); + boolean done = ftpClient.storeFile(fileTokens[1], inputStream); + inputStream.close(); + if (done) { + System.out.println("The file is uploaded successfully."); + Movefile mv = new Movefile(); + mv.movefile(firstLocalFile, this.archive_path); + } + } catch (Exception ex) { + System.out.println("Error: " + ex.getMessage()); + ex.printStackTrace(); + } finally { + try { + if (ftpClient.isConnected()) { + ftpClient.logout(); + ftpClient.disconnect(); + } + } catch (Exception ex) { + ex.printStackTrace(); + } + } + } + + public void fetchFileFTP(String remote_report_pattern) { + System.out.println("Going to FETCH files from remote Server via FTP"); + + FTPClient ftpClient = new FTPClient(); + + try { + ftpClient.connect(this.host, this.port); + ftpClient.login(this.user, this.password); + ftpClient.enterLocalPassiveMode(); + + ftpClient.setFileType(2); + + ftpClient.changeWorkingDirectory(this.remote_fileName); + ftpClient.makeDirectory("archive"); + System.out.println("Current directory is " + ftpClient.printWorkingDirectory()); + FTPFile[] ftpFiles = ftpClient.listFiles(); + + if (ftpFiles != null && ftpFiles.length > 0) { + for (FTPFile file : ftpFiles) { + if (file.isFile()) { + + System.out.println("File is " + file.getName()); + + OutputStream output = new FileOutputStream(this.local_fileName + file.getName()); + + ftpClient.retrieveFile(file.getName(), output); + + output.close(); + + ftpClient.rename(file.getName(), "archive/" + file.getName()); + } + } + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + if (ftpClient.isConnected()) { + ftpClient.logout(); + ftpClient.disconnect(); + } + } catch (Exception ex) { + ex.printStackTrace(); + } + } + } +} \ No newline at end of file diff --git a/src/main/java/com/sendFile/protocol/SSH/SSHProtocol.java b/src/main/java/com/sendFile/protocol/SSH/SSHProtocol.java new file mode 100644 index 0000000..0b78fe2 --- /dev/null +++ b/src/main/java/com/sendFile/protocol/SSH/SSHProtocol.java @@ -0,0 +1,165 @@ +package com.sendFile.protocol.SSH; + +import com.jcraft.jsch.ChannelSftp; +import com.jcraft.jsch.JSch; +import com.jcraft.jsch.Session; +import java.io.File; +import java.util.Properties; +import java.util.Vector; +import com.moveFile.Movefile; + +public class SSHProtocol { + String user; + String password; + String host; + int port; + String local_fileName; + String remote_filePath; + String remote_fileName; + String archive_path; + String local_folder_path_bkp; + + public SSHProtocol( + String user, + String password, + String host, + int port, + String local_fileName, + String remote_filePath, + String remote_fileName, + String archive_path) { + this.user = user; + this.password = password; + this.host = host; + this.port = port; + this.local_fileName = local_fileName; + this.remote_filePath = remote_filePath; + this.remote_fileName = remote_fileName; + this.archive_path = archive_path; + } + + public void sendFileSSH() { + try { + System.out.println("Going to SEND files from remote Server via SSH"); + JSch jsch = new JSch(); + System.out.println( + "Creating SSH Session: user@host:port |--->" + + this.user + + "@" + + this.host + + ":" + + this.port); + Session session = jsch.getSession(this.user, this.host, this.port); + session.setPassword(this.password); + + Properties config = new Properties(); + config.put("StrictHostKeyChecking", "no"); + session.setConfig(config); + System.out.println("Establishing Connection..."); + session.connect(); + System.out.println("Connection established."); + System.out.println("Creating SFTP Channel."); + ChannelSftp sftpChannel = (ChannelSftp) session.openChannel("sftp"); + sftpChannel.connect(); + System.out.println("SFTP Channel created."); + System.out.println( + "SFTP -- Local File Name: " + + this.local_fileName + + " Remote File Name: " + + this.remote_fileName); + + String[] fileTokens = this.remote_fileName.split("-"); + + System.out.println( + "Going to create brancheise folder:" + fileTokens[0] + " at " + this.remote_filePath); + sftpChannel.cd(this.remote_filePath); + try { + sftpChannel.mkdir(fileTokens[0]); + } catch (Exception ex) { + System.out.println("Folder not created: " + ex.getMessage()); + } + sftpChannel.cd(fileTokens[0]); + + sftpChannel.put(this.local_fileName, fileTokens[1]); + System.out.println("File copied to remote server Successfully, archiving file"); + + Movefile mv = new Movefile(); + mv.movefile(new File(this.local_fileName), this.archive_path); + System.out.println("File archived to: " + this.archive_path); + sftpChannel.disconnect(); + session.disconnect(); + System.out.println("Disconnect SFTP Channel"); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + + public void fetchFileSSH(String remote_report_pattern) { + try { + String local_folder_path_bkp = "/home/ec2-user/PRODFILES/reportFiles/processed_bkp"; + System.out.println("Going to FETCH files from remote Server"); + JSch jsch = new JSch(); + System.out.println( + "Creating SSH Session: user@host:port |--->" + + this.user + + "@" + + this.host + + ":" + + this.port); + Session session = jsch.getSession(this.user, this.host, this.port); + session.setPassword(this.password); + + Properties config = new Properties(); + config.put("StrictHostKeyChecking", "no"); + session.setConfig(config); + System.out.println("Establishing Connection..."); + session.connect(); + System.out.println("Connection established."); + System.out.println("Creating SFTP Channel."); + ChannelSftp sftpChannel = (ChannelSftp) session.openChannel("sftp"); + sftpChannel.connect(); + System.out.println("SFTP Channel created."); + + sftpChannel.cd(this.remote_filePath); + System.out.println(sftpChannel.pwd()); + String rootPath = sftpChannel.pwd(); + Vector list = sftpChannel.ls(sftpChannel.pwd()); + + for (ChannelSftp.LsEntry entry : list) { + + System.out.println( + "Current File/Folder: " + entry.getFilename() + " isDir" + entry.getAttrs().isDir()); + if (entry.getAttrs().isDir()) { + if (entry.getFilename().equals(".") || entry.getFilename().equals("..")) { + System.out.println("Skipping . and .."); + continue; + } + System.out.println("Found folder: " + entry.getFilename()); + + System.out.println("cd: " + rootPath + "/" + entry.getFilename()); + sftpChannel.cd(rootPath + "/" + entry.getFilename()); + System.out.println(sftpChannel.pwd()); + Vector subDirFileList = sftpChannel.ls(remote_report_pattern); + for (ChannelSftp.LsEntry subDirFiles : subDirFileList) { + if (!(new File(local_folder_path_bkp + "/" + subDirFiles.getFilename())).exists()) { + sftpChannel.get( + subDirFiles.getFilename(), this.local_fileName + subDirFiles.getFilename()); + System.out.println( + "File: " + subDirFiles.getFilename() + " copied to :" + this.local_fileName); + + continue; + } + System.out.println( + "File: " + subDirFiles.getFilename() + " already present in local path"); + } + } + } + + sftpChannel.disconnect(); + session.disconnect(); + System.out.println("Disconnect SFTP Channel"); + } catch (Exception e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/src/main/resources/config.properties b/src/main/resources/config.properties new file mode 100644 index 0000000..b92af45 --- /dev/null +++ b/src/main/resources/config.properties @@ -0,0 +1,21 @@ +# REMOTE SFTP SERVER CREDENTIALS +REMOTE_HOST_UCB=142.79.249.123 +REMOTE_HOST_NABARD=142.79.249.234 +REMOTE_USER=ipkssftp +REMOTE_PASS=Wnb10U11BE7N26 +REMOTE_PORT=4650 + +# LOCAL BULK FILE PATHS +LOCAL_FOLDER_PATH=/home/ec2-user/PRODFILES/ +ARCHIVE_FOLDER_PATH=/home/ec2-user/PRODFILES/archive/ +LOCAL_REPORT_PATH=/home/ec2-user/PRODFILES/reportFiles/ +LOCAL_FAILED_PATH=/home/ec2-user/PRODFILES/failedFiles/ +# REMOTE PATHS +REMOTE_REPORT_PATTERN=BLK_* +REMOTE_INPUT_FILE_PATH=IPKS_FILES/FROMIPKS/ +REMOTE_OUTPUT_FILE_PATH=IPKS_FILES/TOIPKS +REMOTE_FAILURE_FILE_PATH=IPKS_FILES/FAILURE + +# APPLICATION MANAGEMENT +SLEEP_TIME_MINS=30 +TRANSFER_PROTOCOL=SSH