001package org.kuali.ole.select.service.transmission; 002 003import com.jcraft.jsch.*; 004import org.apache.commons.logging.Log; 005import org.apache.commons.logging.LogFactory; 006import org.apache.commons.net.ftp.FTP; 007import org.apache.commons.net.ftp.FTPClient; 008import org.apache.commons.net.ftp.FTPReply; 009import org.kuali.ole.select.service.OleTransmissionService; 010import org.kuali.ole.vnd.businessobject.VendorDetail; 011import org.kuali.ole.vnd.businessobject.VendorTransmissionFormatDetail; 012 013import java.io.FileInputStream; 014import java.io.FileOutputStream; 015 016/** 017 * Created by IntelliJ IDEA. 018 * User: pvsubrah 019 * Date: 11/7/11 020 * Time: 1:51 PM 021 * To change this template use File | Settings | File Templates. 022 */ 023public class OleTransmissionServiceImpl implements OleTransmissionService { 024 private static Log LOG = LogFactory.getLog(OleTransmissionServiceImpl.class); 025 026 public void doSFTPUpload(VendorTransmissionFormatDetail vendorTransmissionFormatDetail, String fileLocation, String preferredFileName) { 027 LOG.trace("************************************doSFTPUpload() started************************************"); 028 029 JSch jsch = new JSch(); 030 Session session = null; 031 String sftpUsername = vendorTransmissionFormatDetail.getVendorEDIConnectionUserName(); 032 String sftpHostname = vendorTransmissionFormatDetail.getVendorEDIConnectionAddress(); 033 String sftpPassword = vendorTransmissionFormatDetail.getVendorEDIConnectionPassword(); 034 VendorDetail vendorDetail = vendorTransmissionFormatDetail.getVendorDetail(); 035 try { 036 session = jsch.getSession(sftpUsername, sftpHostname, 22); 037 session.setConfig("StrictHostKeyChecking", "no"); 038 session.setPassword(sftpPassword); 039 session.connect(); 040 041 Channel channel = session.openChannel("sftp"); 042 channel.connect(); 043 ChannelSftp sftpChannel = (ChannelSftp) channel; 044 String remoteDir = vendorDetail.getVendorName().toString(); 045 try { 046 sftpChannel.cd(ORDER_RECORDS); 047 } catch (SftpException e) { 048 sftpChannel.mkdir(ORDER_RECORDS); 049 sftpChannel.cd(ORDER_RECORDS); 050 } 051 try { 052 sftpChannel.cd(ORDERS_TO_BE_PROCESSED_BY_VENDOR_FOLDER); 053 } catch (SftpException e) { 054 sftpChannel.mkdir(ORDERS_TO_BE_PROCESSED_BY_VENDOR_FOLDER); 055 sftpChannel.cd(ORDERS_TO_BE_PROCESSED_BY_VENDOR_FOLDER); 056 } 057 try { 058 sftpChannel.cd(remoteDir); 059 } catch (SftpException e) { 060 sftpChannel.mkdir(remoteDir); 061 sftpChannel.cd(remoteDir); 062 } 063 sftpChannel.put(new FileInputStream(fileLocation), preferredFileName); 064 sftpChannel.exit(); 065 } catch (Exception e) { 066 LOG.error("Exception performing SFTP upload of " + preferredFileName + " to " + sftpHostname, e); 067 throw new RuntimeException(e); 068 } finally { 069 session.disconnect(); 070 } 071 072 LOG.trace("************************************doSFTPUpload() completed************************************"); 073 } 074 075 public void doFTPUpload(VendorTransmissionFormatDetail vendorTransmissionFormatDetail, String fileLocation, String preferredFileName) { 076 LOG.trace("************************************doFTPUpload() started************************************"); 077 FTPClient ftpClient = new FTPClient(); 078 FileInputStream inputStream = null; 079 FileOutputStream outputStream = null; 080 String ftpUsername = vendorTransmissionFormatDetail.getVendorEDIConnectionUserName(); 081 String ftpHostname = vendorTransmissionFormatDetail.getVendorEDIConnectionAddress(); 082 String ftpPassword = vendorTransmissionFormatDetail.getVendorEDIConnectionPassword(); 083 VendorDetail vendorDetail = vendorTransmissionFormatDetail.getVendorDetail(); 084 try { 085 ftpClient.connect(ftpHostname); 086 ftpClient.login(ftpUsername, ftpPassword); 087 ftpClient.enterLocalPassiveMode(); 088 int reply = ftpClient.getReplyCode(); 089 if (FTPReply.isPositiveCompletion(reply)) { 090 LOG.debug("Connected to FTP server."); 091 } else { 092 LOG.debug("FTP server refused connection."); 093 } 094 095 // upload 096 ftpClient.setFileType(FTP.BINARY_FILE_TYPE); 097 if (LOG.isDebugEnabled()) { 098 LOG.debug("File Location in FTP Server================>" + fileLocation); 099 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 Left blank as OLE providing custom implementation 123 */ 124 } 125 126 @Override 127 public void doFTPUpload(String ftpHostname, String ftpUsername, String ftpPassword, String file, String fileName) { 128 /* 129 Left blank as OLE providing custom implementation 130 */ 131 } 132}