View Javadoc
1   /*
2    * Copyright 2011 The Kuali Foundation.
3    *
4    * Licensed under the Educational Community License, Version 1.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl1.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.module.purap.transmission.service.impl;
17  
18  import com.jcraft.jsch.Channel;
19  import com.jcraft.jsch.ChannelSftp;
20  import com.jcraft.jsch.JSch;
21  import com.jcraft.jsch.Session;
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.apache.commons.net.ftp.FTP;
25  import org.apache.commons.net.ftp.FTPClient;
26  import org.apache.commons.net.ftp.FTPReply;
27  import org.kuali.ole.module.purap.document.service.OlePurapService;
28  import org.kuali.ole.module.purap.transmission.service.TransmissionService;
29  import org.kuali.ole.sys.OLEConstants;
30  import org.kuali.ole.sys.context.SpringContext;
31  import org.kuali.rice.core.api.config.property.ConfigurationService;
32  
33  import java.io.FileInputStream;
34  import java.io.FileOutputStream;
35  
36  /**
37   * This class is for performing operations on SFTP Server
38   */
39  public class TransmissionServiceImpl implements TransmissionService {
40      private static Log LOG = LogFactory.getLog(TransmissionServiceImpl.class);
41      protected OlePurapService olePurapService;
42  
43      public OlePurapService getOlePurapService() {
44          if (olePurapService == null) {
45              olePurapService = SpringContext.getBean(OlePurapService.class);
46          }
47          return olePurapService;
48      }
49  
50      @Override
51      public void doSFTPUpload(String sftpHostname, String sftpUsername, String sftpPassword, String file, String fileName) {
52          LOG.trace("************************************doSFTPUpload() started************************************");
53  
54          JSch jsch = new JSch();
55          Session session = null;
56          try {
57              session = jsch.getSession(sftpUsername, sftpHostname, 22);
58              session.setConfig("StrictHostKeyChecking", "no");
59              session.setPassword(sftpPassword);
60              session.connect();
61  
62              Channel channel = session.openChannel("sftp");
63              channel.connect();
64              ChannelSftp sftpChannel = (ChannelSftp) channel;
65              sftpChannel.mkdir("");
66              sftpChannel.put(new FileInputStream(file), fileName);
67              sftpChannel.exit();
68              session.disconnect();
69          } catch (Exception e) {
70              LOG.error("Exception performing SFTP upload of " + file + " to " + sftpHostname, e);
71              throw new RuntimeException(e);
72          }
73  
74          LOG.trace("************************************doSFTPUpload() completed************************************");
75      }
76  
77      /**
78       * This method is to perform file upload
79       *
80       * @param ftpHostname
81       * @param ftpUsername
82       * @param ftpPassword
83       * @param file
84       * @param fileName
85       */
86      @Override
87      public void doFTPUpload(String ftpHostname, String ftpUsername, String ftpPassword, String file, String fileName) {
88          LOG.trace("************************************doFTPUpload() started************************************");
89          FTPClient ftpClient = new FTPClient();
90          FileInputStream inputStream = null;
91          FileOutputStream outputStream = null;
92          try {
93              ftpClient.connect(ftpHostname);
94              ftpClient.login(ftpUsername, ftpPassword);
95              ftpClient.enterLocalPassiveMode();
96              int reply = ftpClient.getReplyCode();
97              if (FTPReply.isPositiveCompletion(reply)) {
98                  LOG.debug("Connected to FTP server.");
99              } 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 }