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