Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GRAD2-2224 #210

Merged
merged 2 commits into from
Jul 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 94 additions & 59 deletions api/src/main/java/ca/bc/gov/educ/api/distribution/util/SFTPUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class SFTPUtils {
Expand Down Expand Up @@ -46,77 +50,110 @@ public class SFTPUtils {

private static Logger logger = LoggerFactory.getLogger(SFTPUtils.class);
//Grad2-1931 - setting SFTP root folder location where it has to pick zip folders from, to send to BC mail - mchintha
public boolean sftpUploadBCMail(Long batchId, String rootFolder) {
String localFile = rootFolder + "/EDGRAD.BATCH."+batchId+".zip";
String remoteFile = BC_MAIL_LOCATION +"EDGRAD.BATCH."+batchId+".zip";
String localControlFile = rootFolder + "/EDGRAD.BATCH."+batchId+".txt";
String remoteControlFile = BC_MAIL_LOCATION+"EDGRAD.BATCH."+batchId+".txt";
Session jschSession = null;

setupBCMailSFTP();

try {
JSch jsch = new JSch();
jsch.setKnownHosts(KNOWN_HOST);
jschSession = jsch.getSession(BCMAIL_SFTP_USERNAME, BCMAIL_REMOTE_HOST, REMOTE_PORT);
jsch.addIdentity(RSA_PRV);
jschSession.connect(SESSION_TIMEOUT);

Channel sftp = jschSession.openChannel("sftp");
sftp.connect(CHANNEL_TIMEOUT);
ChannelSftp channelSftp = (ChannelSftp) sftp;

// transfer file from local to remote server
channelSftp.put(localFile, remoteFile);
channelSftp.put(localControlFile, remoteControlFile);
channelSftp.exit();
return true;
} catch (JSchException | SftpException e) {
logger.debug("Error {} ",e.getLocalizedMessage());
return false;
} finally {
if (jschSession != null) {
jschSession.disconnect();
}
/**
* Method specific for uploading to BCMail
* @param batchId the batch id
* @param rootFolder the root location of the cached files
* @return true if successful
*/
public boolean sftpUploadBCMail(Long batchId, String rootFolder) {
Map<String, List<String>> files = new HashMap<>();
String zipFile = "/EDGRAD.BATCH." + batchId + ".zip";
String controlFile = "/EDGRAD.BATCH." + batchId + ".txt";
String localZipFile = rootFolder + zipFile;
String remoteZipFile = BC_MAIL_LOCATION + zipFile;
String localControlFile = rootFolder + controlFile;
String remoteControlFile = BC_MAIL_LOCATION + controlFile;
files.computeIfAbsent(localZipFile, v -> new ArrayList<>()).add(remoteZipFile);
files.computeIfAbsent(localControlFile, v -> new ArrayList<>()).add(remoteControlFile);
// GRAD2-2224 - Short term solution to also upload files to dev folder for business retrieval.
if(!BC_MAIL_LOCATION.toLowerCase().contains("dev")){
// also upload to dev
String dev = "/Inbox/Dev/";
files.computeIfAbsent(formatPath(localZipFile), v -> new ArrayList<>()).add(formatPath(dev + zipFile));
files.computeIfAbsent(formatPath(localControlFile), v -> new ArrayList<>()).add(formatPath(dev + controlFile));
}
return sftpUpload(files, BCMAIL_SFTP_USERNAME, BCMAIL_REMOTE_HOST, REMOTE_PORT, BCMAIL_PRIVATE_KEY, BCMAIL_PUBLIC_KEY, BCMAIL_KNOWN_HOSTS);
}

private boolean setupBCMailSFTP() {
writeFile(RSA_PRV, BCMAIL_PRIVATE_KEY);
writeFile(RSA_PUB, BCMAIL_PUBLIC_KEY);
writeFile(KNOWN_HOST, BCMAIL_KNOWN_HOSTS);
return true;
}

public boolean sftpUploadTSW(Long batchId,String mincode,String fileName) {
String localFile = EducDistributionApiConstants.TMP_DIR + EducDistributionApiConstants.DEL + batchId + EducDistributionApiConstants.DEL + mincode + EducDistributionApiConstants.DEL + fileName+".pdf";
/**
* Method specific for uploading files to TSW
* @param batchId batch id
* @param mincode mincode
* @param fileName the filename
* @return true if successful
*/
public boolean sftpUploadTSW(Long batchId, String mincode, String fileName) {
Map<String, List<String>> files = new HashMap<>();
String localFile = formatPath(EducDistributionApiConstants.TMP_DIR + EducDistributionApiConstants.DEL + batchId + EducDistributionApiConstants.DEL + mincode + EducDistributionApiConstants.DEL + fileName+".pdf");
String remoteFile = "/$1$dga5037/EDUC/XTD";
String location1 = remoteFile+"/WEB/"+fileName+".pdf";
String location2 = remoteFile+"/TSWSFTP/"+fileName+".pdf";
String location3 = remoteFile+"/WEB/PST/"+fileName+".pdf";
Session jschSession = null;

setupTSWSFTP();
files.computeIfAbsent(localFile, v -> new ArrayList<>()).add(location1);
files.computeIfAbsent(localFile, v -> new ArrayList<>()).add(location2);
files.computeIfAbsent(localFile, v -> new ArrayList<>()).add(location3);
return sftpUpload(files, TSW_SFTP_USERNAME, TSW_REMOTE_HOST, REMOTE_PORT, TSW_PRIVATE_KEY, TSW_PUBLIC_KEY, TSW_KNOWN_HOSTS);
}

/**
* Method for uploading files to an sftp server
* @param files a map of local -> remote file paths. Use of Map<String, List<String>> enables the key (the file to upload)
* to have multiple upload locations (the List<String> represents the upload locations)
* @param userName the username to log into the sftp server
* @param host remote host
* @param port the port to use (usually 22)
* @param privateKey the private key of the client
* @param publicKey the public key of the host
* @param knownHosts known hosts
* @return true if successful
*/
public boolean sftpUpload(
Map<String, List<String>> files,
String userName,
String host,
int port,
String privateKey,
String publicKey,
String knownHosts) {

// set up keys, etc.
writeFile(RSA_PRV, privateKey);
writeFile(RSA_PUB, publicKey);
writeFile(KNOWN_HOST, knownHosts);

// session
Session jschSession = null;
try {
JSch jsch = new JSch();
jsch.setKnownHosts(KNOWN_HOST);
jschSession = jsch.getSession(TSW_SFTP_USERNAME, TSW_REMOTE_HOST, REMOTE_PORT);
jschSession = jsch.getSession(userName, host, port);
jsch.addIdentity(RSA_PRV);
jschSession.connect(SESSION_TIMEOUT);

Channel sftp = jschSession.openChannel("sftp");
sftp.connect(CHANNEL_TIMEOUT);
ChannelSftp channelSftp = (ChannelSftp) sftp;

// transfer file from local to remote server
channelSftp.put(localFile, location1);
channelSftp.put(localFile, location2);
channelSftp.put(localFile, location3);
// transfer file(s) from local to remote server
for (Map.Entry<String, List<String>> entry : files.entrySet()) {
entry.getValue().forEach(
file -> {
try {
channelSftp.put(entry.getKey(), file);
logger.debug("Transferring file via ftp: {}", entry.getKey());
} catch (SftpException e) {
logger.error("Error during sftp transfer {} ", e.getLocalizedMessage());
}
}
);
}
channelSftp.exit();
return true;
} catch (JSchException | SftpException e) {
e.printStackTrace();
} catch (JSchException e) {
logger.error("Error during sftp transfer {} ", e.getLocalizedMessage());
return false;
} finally {
if (jschSession != null) {
Expand All @@ -125,21 +162,19 @@ public boolean sftpUploadTSW(Long batchId,String mincode,String fileName) {
}
}

private boolean setupTSWSFTP() {
writeFile(RSA_PRV, TSW_PRIVATE_KEY);
writeFile(RSA_PUB, TSW_PUBLIC_KEY);
writeFile(KNOWN_HOST, TSW_KNOWN_HOSTS);
return true;
}

public boolean writeFile(String filename, String content) {
private boolean writeFile(String filename, String content) {
try (FileWriter fileWriter = new FileWriter(filename)){
fileWriter.write(content);
logger.debug("Write File Complete! - {} ",filename);
logger.debug("Write File Complete! - {} ", filename);
} catch (IOException e) {
logger.debug("Write File Failed! - {}",filename);
logger.error("SFTP ERROR: Write File Failed! - {}", filename);
e.printStackTrace();
}
return true;
}

private String formatPath(String path){
return path.replaceAll("/+", "/");
}

}
Loading