sonar fix: Exception handling in FTPInterface.java
[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.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
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 Log log = LogFactory.getLog(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 //              ftpClient.setParserFactory(new ExtendsDefaultFTPFileEntryParserFactory());
53
54         if (encode != null && encode.length() > 0) {
55             ftpClient.setControlEncoding(encode);
56         }
57
58         ftpClient.connect(host, port);
59         int reply = this.ftpClient.getReplyCode();
60         if (!FTPReply.isPositiveCompletion(reply)) {
61             this.ftpClient.disconnect();
62             return;
63         }
64
65         if (!ftpClient.login(user, pwd)) {
66             throw new IOException("login[" + host + "],port[" + port + "] fail, please check user and password");
67         }
68         if (isPassiveMode) {
69             ftpClient.enterLocalPassiveMode();
70         } else {
71             ftpClient.enterLocalActiveMode();
72         }
73         ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
74         this.ftpClient.setBufferSize(1024 * 2);
75         this.ftpClient.setDataTimeout(3 * 60 * 1000);
76         try {
77             this.ftpClient.setSoTimeout(timeout);
78         } catch (Exception e) {
79             //e.printStackTrace();
80             log.error(" StackTrace :", e);
81         }
82     }
83
84
85     /**
86      * logout
87      */
88     public void logout() {
89         if (ftpClient != null) {
90             try {
91                 ftpClient.logout();
92                 ftpClient.disconnect();
93             } catch (Exception e) {
94             }
95             ftpClient = null;
96         }
97     }
98
99
100     public boolean chdir(String dir) {
101         boolean sucess = false;
102         try {
103             if (ftpClient.changeWorkingDirectory(dir)) {
104                 sucess = true;
105             } else {
106                 sucess = false;
107             }
108         } catch (IOException e) {
109             log.error("chdir dir =" + dir + " is error" + StringUtil.getStackTrace(e));
110             sucess = false;
111         }
112
113         return sucess;
114     }
115
116
117     public boolean downloadFile(String remoteFile, String localFile) {
118         boolean sucess = false;
119         BufferedOutputStream toLfileOutput = null;
120         try {
121             toLfileOutput = new BufferedOutputStream(new FileOutputStream(localFile));
122             ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
123             if (ftpClient.retrieveFile(remoteFile, toLfileOutput)) {
124                 sucess = true;
125             } else {
126                 sucess = false;
127             }
128         } catch (Exception ioe) {
129             sucess = false;
130             log.error("downloadFile remoteFile =" + remoteFile + " is fail ", ioe);
131         } finally {
132             if (toLfileOutput != null)
133                 try {
134                     toLfileOutput.close();
135                 } catch (IOException e) {
136                 }
137         }
138
139         return sucess;
140     }
141
142
143     public RemoteFile[] list() {
144         AFtpRemoteFile[] ftpRemoteFiles = null;
145         String currdir = null;
146         try {
147             currdir = ftpClient.printWorkingDirectory();
148             if (currdir.endsWith("/") == false) {
149                 currdir = currdir + "/";
150             }
151             FTPFile[] rfileList = null;
152             rfileList = ftpClient.listFiles(currdir);
153             ftpRemoteFiles = new AFtpRemoteFile[rfileList.length];
154             for (int i = 0; i < rfileList.length; i++) {
155                 ftpRemoteFiles[i] = new AFtpRemoteFile(rfileList[i], ftpClient, currdir);
156             }
157         } catch (IOException e) {
158             log.error("Ftp list currdir = " + currdir + " is fail " + StringUtil.getStackTrace(e));
159         }
160         return ftpRemoteFiles;
161     }
162
163
164     @Override
165     public String pwd() throws IOException {
166         String returnValue = ftpClient.printWorkingDirectory();
167         return returnValue;
168     }
169
170 //      public boolean store(String localFile, String remoteFile) {
171 //              
172 //              boolean sucess = false;
173 //              FileInputStream lfileInput = null;
174 //              try {
175 //                      lfileInput = new FileInputStream(localFile);
176 //                      ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
177 //                      
178 //                      if (ftpClient.storeFile(remoteFile, lfileInput)){
179 //                              sucess = true;
180 //                      }else{
181 //                              sucess = false;
182 //                      }
183 //              } catch (Exception ioe) {
184 //                      sucess = false;
185 //                      log.error("store localFile = "+localFile+" is fail "+StringUtil.getStackTrace(ioe));
186 //              } finally {
187 //                      if (lfileInput != null)
188 //                              try {
189 //                                      lfileInput.close();
190 //                              } catch (IOException e) {
191 //                              }
192 //              }
193 //              return sucess;
194 //      }
195
196 }
197