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