Update ves-agent dependency
[vfc/nfvo/driver/ems.git] / ems / boco / src / main / java / org / onap / vfc / nfvo / emsdriver / commons / ftp / FTPSrv.java
1 /*
2  * Copyright 2017 BOCO Corporation.  CMCC Technologies Co., Ltd
3  *
4  * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
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.onap.vfc.nfvo.emsdriver.commons.ftp;
17
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20 import org.apache.commons.net.ftp.*;
21 import org.onap.vfc.nfvo.emsdriver.commons.utils.StringUtil;
22
23 import java.io.BufferedOutputStream;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.util.TimeZone;
27
28
29 public class FTPSrv implements FTPInterface {
30     private Logger log = LoggerFactory.getLogger(FTPSrv.class);
31     private FTPClient ftpClient = null;
32
33
34     /**
35      * login FTP
36      *
37      * @param host
38      * @param port
39      * @param user
40      * @param pwd
41      * @param encode
42      * @param timeout
43      * @throws Exception
44      */
45     public void login(String host, int port, String user, String pwd, String encode, boolean isPassiveMode, int timeout) throws IOException {
46         ftpClient = new FTPClient();
47
48         FTPClientConfig ftpClientConfig = new FTPClientConfig();
49         ftpClientConfig.setServerTimeZoneId(TimeZone.getDefault().getID());
50         this.ftpClient.setControlEncoding("GBK");
51         this.ftpClient.configure(ftpClientConfig);
52
53         if (encode != null && encode.length() > 0) {
54             ftpClient.setControlEncoding(encode);
55         }
56
57         ftpClient.connect(host, port);
58         int reply = this.ftpClient.getReplyCode();
59         if (!FTPReply.isPositiveCompletion(reply)) {
60             this.ftpClient.disconnect();
61             return;
62         }
63
64         if (!ftpClient.login(user, pwd)) {
65             throw new IOException("login[" + host + "],port[" + port + "] fail, please check user and password");
66         }
67         if (isPassiveMode) {
68             ftpClient.enterLocalPassiveMode();
69         } else {
70             ftpClient.enterLocalActiveMode();
71         }
72         ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
73         this.ftpClient.setBufferSize(1024 * 2);
74         this.ftpClient.setDataTimeout(3 * 60 * 1000);
75         try {
76             this.ftpClient.setSoTimeout(timeout);
77         } catch (Exception e) {
78             log.error(" StackTrace :", e);
79         }
80     }
81
82
83     /**
84      * logout
85      */
86     public void logout() {
87         if (ftpClient != null) {
88             try {
89                 ftpClient.logout();
90                 ftpClient.disconnect();
91             } catch (Exception e) {
92                     log.error(" StackTrace :", e);
93             }
94             ftpClient = null;
95         }
96     }
97
98
99     public boolean chdir(String dir) {
100         boolean sucess = false;
101         try {
102             if (ftpClient.changeWorkingDirectory(dir)) {
103                 sucess = true;
104             } else {
105                 sucess = false;
106             }
107         } catch (IOException e) {
108             log.error("chdir dir =" + dir + " is error" + StringUtil.getStackTrace(e));
109             sucess = false;
110         }
111
112         return sucess;
113     }
114
115
116     public boolean downloadFile(String remoteFile, String localFile) {
117         boolean sucess = false;
118         try (
119             BufferedOutputStream toLfileOutput = new BufferedOutputStream(new FileOutputStream(localFile))){
120             ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
121             if (ftpClient.retrieveFile(remoteFile, toLfileOutput)) {
122                 sucess = true;
123             } else {
124                 sucess = false;
125             }
126         } catch (Exception ioe) {
127             sucess = false;
128             log.error("downloadFile remoteFile =" + remoteFile + " is fail ", ioe);
129         } 
130
131         return sucess;
132     }
133
134
135     public RemoteFile[] list() {
136         AFtpRemoteFile[] ftpRemoteFiles = null;
137         String currdir = null;
138         try {
139             currdir = ftpClient.printWorkingDirectory();
140             if (!currdir.endsWith("/")) {
141                 currdir = currdir + "/";
142             }
143             FTPFile[] rfileList = null;
144             rfileList = ftpClient.listFiles(currdir);
145             ftpRemoteFiles = new AFtpRemoteFile[rfileList.length];
146             for (int i = 0; i < rfileList.length; i++) {
147                 ftpRemoteFiles[i] = new AFtpRemoteFile(rfileList[i], ftpClient, currdir);
148             }
149         } catch (IOException e) {
150             log.error("Ftp list currdir = " + currdir + " is fail " + StringUtil.getStackTrace(e));
151         }
152         return ftpRemoteFiles;
153     }
154
155
156     @Override
157     public String pwd() throws IOException {
158         return ftpClient.printWorkingDirectory();
159     }
160
161 }
162