1 package org.kuali.ole.select.service.transmission;
2
3 import com.jcraft.jsch.*;
4 import org.apache.commons.logging.Log;
5 import org.apache.commons.logging.LogFactory;
6 import org.apache.commons.net.ftp.FTP;
7 import org.apache.commons.net.ftp.FTPClient;
8 import org.apache.commons.net.ftp.FTPReply;
9 import org.kuali.ole.select.service.OleTransmissionService;
10 import org.kuali.ole.vnd.businessobject.VendorDetail;
11 import org.kuali.ole.vnd.businessobject.VendorTransmissionFormatDetail;
12
13 import java.io.FileInputStream;
14 import java.io.FileOutputStream;
15
16
17
18
19
20
21
22
23 public class OleTransmissionServiceImpl implements OleTransmissionService {
24 private static Log LOG = LogFactory.getLog(OleTransmissionServiceImpl.class);
25
26 public void doSFTPUpload(VendorTransmissionFormatDetail vendorTransmissionFormatDetail, String fileLocation, String preferredFileName) {
27 LOG.trace("************************************doSFTPUpload() started************************************");
28
29 JSch jsch = new JSch();
30 Session session = null;
31 String sftpUsername = vendorTransmissionFormatDetail.getVendorEDIConnectionUserName();
32 String sftpHostname = vendorTransmissionFormatDetail.getVendorEDIConnectionAddress();
33 String sftpPassword = vendorTransmissionFormatDetail.getVendorEDIConnectionPassword();
34 VendorDetail vendorDetail = vendorTransmissionFormatDetail.getVendorDetail();
35 try {
36 session = jsch.getSession(sftpUsername, sftpHostname, 22);
37 session.setConfig("StrictHostKeyChecking", "no");
38 session.setPassword(sftpPassword);
39 session.connect();
40
41 Channel channel = session.openChannel("sftp");
42 channel.connect();
43 ChannelSftp sftpChannel = (ChannelSftp) channel;
44 String remoteDir = vendorDetail.getVendorName().toString();
45 try {
46 sftpChannel.cd(ORDER_RECORDS);
47 } catch (SftpException e) {
48 sftpChannel.mkdir(ORDER_RECORDS);
49 sftpChannel.cd(ORDER_RECORDS);
50 }
51 try {
52 sftpChannel.cd(ORDERS_TO_BE_PROCESSED_BY_VENDOR_FOLDER);
53 } catch (SftpException e) {
54 sftpChannel.mkdir(ORDERS_TO_BE_PROCESSED_BY_VENDOR_FOLDER);
55 sftpChannel.cd(ORDERS_TO_BE_PROCESSED_BY_VENDOR_FOLDER);
56 }
57 try {
58 sftpChannel.cd(remoteDir);
59 } catch (SftpException e) {
60 sftpChannel.mkdir(remoteDir);
61 sftpChannel.cd(remoteDir);
62 }
63 sftpChannel.put(new FileInputStream(fileLocation), preferredFileName);
64 sftpChannel.exit();
65 } catch (Exception e) {
66 LOG.error("Exception performing SFTP upload of " + preferredFileName + " to " + sftpHostname, e);
67 throw new RuntimeException(e);
68 } finally {
69 session.disconnect();
70 }
71
72 LOG.trace("************************************doSFTPUpload() completed************************************");
73 }
74
75 public void doFTPUpload(VendorTransmissionFormatDetail vendorTransmissionFormatDetail, String fileLocation, String preferredFileName) {
76 LOG.trace("************************************doFTPUpload() started************************************");
77 FTPClient ftpClient = new FTPClient();
78 FileInputStream inputStream = null;
79 FileOutputStream outputStream = null;
80 String ftpUsername = vendorTransmissionFormatDetail.getVendorEDIConnectionUserName();
81 String ftpHostname = vendorTransmissionFormatDetail.getVendorEDIConnectionAddress();
82 String ftpPassword = vendorTransmissionFormatDetail.getVendorEDIConnectionPassword();
83 VendorDetail vendorDetail = vendorTransmissionFormatDetail.getVendorDetail();
84 try {
85 ftpClient.connect(ftpHostname);
86 ftpClient.login(ftpUsername, ftpPassword);
87 ftpClient.enterLocalPassiveMode();
88 int reply = ftpClient.getReplyCode();
89 if (FTPReply.isPositiveCompletion(reply)) {
90 LOG.debug("Connected to FTP server.");
91 } else {
92 LOG.debug("FTP server refused connection.");
93 }
94
95
96 ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
97 if (LOG.isDebugEnabled()) {
98 LOG.debug("File Location in FTP Server================>" + fileLocation);
99 LOG.debug("File source=================================>" + fileLocation);
100 LOG.debug("FileName====================================>" + preferredFileName);
101 }
102
103 String remoteDir = vendorDetail.getVendorAliases().toString();
104 ftpClient.mkd(remoteDir);
105 ftpClient.cwd(remoteDir);
106 inputStream = new FileInputStream(fileLocation);
107 ftpClient.storeFile(preferredFileName, inputStream);
108
109 ftpClient.logout();
110 inputStream.close();
111 } catch (Exception e) {
112 LOG.error("Exception performing SFTP upload of " + preferredFileName + " to " + ftpHostname, e);
113 throw new RuntimeException(e);
114 }
115 LOG.trace("************************************doFTPUpload() completed************************************");
116 }
117
118
119 @Override
120 public void doSFTPUpload(String sftpHostname, String sftpUsername, String sftpPassword, String file, String fileName) {
121
122
123
124 }
125
126 @Override
127 public void doFTPUpload(String ftpHostname, String ftpUsername, String ftpPassword, String file, String fileName) {
128
129
130
131 }
132 }