cleaned up, formatted and made the project runnable after decompilation. Also added new configurations for UCB and NABARD servres

This commit is contained in:
2025-05-11 17:27:11 +05:30
commit 34d1647126
9 changed files with 681 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
dependency-reduced-pom.xml
target/

69
pom.xml Normal file
View File

@@ -0,0 +1,69 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.main</groupId>
<artifactId>SSHFileToCbs_PROD</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<exec.mainClass>com.Main.Main</exec.mainClass>
</properties>
<dependencies>
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.3</version>
</dependency>
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.54</version>
</dependency>
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>23.5.0.24.07</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- not using Maven Compiler Plugin as it is not necessay -->
<!-- <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<release>1.8</release>
</configuration>
</plugin> -->
<!-- Maven Shade plugin to build fat or uber jar -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.Main.Main</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@@ -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;
}
}
}

View File

@@ -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");
}
}
}

View File

@@ -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();
}
}
}

View File

@@ -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");
}
}
}
}
}

View File

@@ -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();
}
}
}
}

View File

@@ -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<ChannelSftp.LsEntry> 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<ChannelSftp.LsEntry> 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();
}
}
}

View File

@@ -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