001/* 002 * Copyright 2011 The Kuali Foundation. 003 * 004 * Licensed under the Educational Community License, Version 1.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl1.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.ole.module.purap.transmission.service.impl; 017 018import com.jcraft.jsch.Channel; 019import com.jcraft.jsch.ChannelSftp; 020import com.jcraft.jsch.JSch; 021import com.jcraft.jsch.Session; 022import org.apache.commons.logging.Log; 023import org.apache.commons.logging.LogFactory; 024import org.apache.commons.net.ftp.FTP; 025import org.apache.commons.net.ftp.FTPClient; 026import org.apache.commons.net.ftp.FTPReply; 027import org.kuali.ole.module.purap.document.service.OlePurapService; 028import org.kuali.ole.module.purap.transmission.service.TransmissionService; 029import org.kuali.ole.sys.OLEConstants; 030import org.kuali.ole.sys.context.SpringContext; 031import org.kuali.rice.core.api.config.property.ConfigurationService; 032 033import java.io.FileInputStream; 034import java.io.FileOutputStream; 035 036/** 037 * This class is for performing operations on SFTP Server 038 */ 039public class TransmissionServiceImpl implements TransmissionService { 040 private static Log LOG = LogFactory.getLog(TransmissionServiceImpl.class); 041 protected OlePurapService olePurapService; 042 043 public OlePurapService getOlePurapService() { 044 if (olePurapService == null) { 045 olePurapService = SpringContext.getBean(OlePurapService.class); 046 } 047 return olePurapService; 048 } 049 050 @Override 051 public void doSFTPUpload(String sftpHostname, String sftpUsername, String sftpPassword, String file, String fileName) { 052 LOG.trace("************************************doSFTPUpload() started************************************"); 053 054 JSch jsch = new JSch(); 055 Session session = null; 056 try { 057 session = jsch.getSession(sftpUsername, sftpHostname, 22); 058 session.setConfig("StrictHostKeyChecking", "no"); 059 session.setPassword(sftpPassword); 060 session.connect(); 061 062 Channel channel = session.openChannel("sftp"); 063 channel.connect(); 064 ChannelSftp sftpChannel = (ChannelSftp) channel; 065 sftpChannel.mkdir(""); 066 sftpChannel.put(new FileInputStream(file), fileName); 067 sftpChannel.exit(); 068 session.disconnect(); 069 } catch (Exception e) { 070 LOG.error("Exception performing SFTP upload of " + file + " to " + sftpHostname, e); 071 throw new RuntimeException(e); 072 } 073 074 LOG.trace("************************************doSFTPUpload() completed************************************"); 075 } 076 077 /** 078 * This method is to perform file upload 079 * 080 * @param ftpHostname 081 * @param ftpUsername 082 * @param ftpPassword 083 * @param file 084 * @param fileName 085 */ 086 @Override 087 public void doFTPUpload(String ftpHostname, String ftpUsername, String ftpPassword, String file, String fileName) { 088 LOG.trace("************************************doFTPUpload() started************************************"); 089 FTPClient ftpClient = new FTPClient(); 090 FileInputStream inputStream = null; 091 FileOutputStream outputStream = null; 092 try { 093 ftpClient.connect(ftpHostname); 094 ftpClient.login(ftpUsername, ftpPassword); 095 ftpClient.enterLocalPassiveMode(); 096 int reply = ftpClient.getReplyCode(); 097 if (FTPReply.isPositiveCompletion(reply)) { 098 LOG.debug("Connected to FTP server."); 099 } else { 100 LOG.debug("FTP server refused connection."); 101 } 102 103 // upload 104 ftpClient.setFileType(FTP.BINARY_FILE_TYPE); 105 String fileLocation = getFileLocation(); 106 if (LOG.isDebugEnabled()) { 107 LOG.debug("File Location in FTP Server================>" + fileLocation); 108 LOG.debug("File source=================================>" + file); 109 LOG.debug("FileName====================================>" + fileName); 110 } 111 ftpClient.mkd(fileLocation); 112 ftpClient.cwd(fileLocation); 113 inputStream = new FileInputStream(file); 114 ftpClient.storeFile(fileName, inputStream); 115 116 ftpClient.logout(); 117 inputStream.close(); 118 } catch (Exception e) { 119 LOG.error("Exception performing SFTP upload of " + file + " to " + ftpHostname, e); 120 throw new RuntimeException(e); 121 } 122 LOG.trace("************************************doFTPUpload() completed************************************"); 123 } 124 125 /** 126 * This method is to get the directory in the Server 127 * 128 * @return 129 */ 130 public String getFileLocation() { 131 String fileLocation = getOlePurapService().getParameter(OLEConstants.VENDOR_DIRECTORY); 132 return fileLocation; 133 } 134}