2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.ci.tests.execute.setup;
23 import org.apache.commons.net.ftp.FTPClient;
24 import org.apache.commons.net.ftp.FTPFile;
25 import org.apache.commons.net.ftp.FTPReply;
28 import java.text.DateFormat;
29 import java.text.SimpleDateFormat;
31 import java.util.stream.Collectors;
33 public class AttFtpClient {
35 private static final AttFtpClient instance = new AttFtpClient();
37 public static AttFtpClient getInstance() {
41 private FTPClient apacheFtpClient;
43 private AttFtpClient() {
44 apacheFtpClient = new FTPClient();
48 public void init(String server, int port, String user, String pass) {
51 apacheFtpClient.connect(server, port);
52 showServerReply(apacheFtpClient);
55 int replyCode = apacheFtpClient.getReplyCode();
56 if (!FTPReply.isPositiveCompletion(replyCode)) {
57 System.out.println("Connect failed");
61 boolean success = apacheFtpClient.login(user, pass);
62 showServerReply(apacheFtpClient);
65 System.out.println("Could not login to the server");
70 // apacheFtpClient.enterLocalPassiveMode();
71 // apacheFtpClient.setFileType(FTP.BINARY_FILE_TYPE);
73 } catch (IOException ex) {
74 System.out.println("Oops! Something wrong happened");
80 public File retrieveLastModifiedFileFromFTP() throws IOException {
81 FTPFile[] files1 = retrieveListOfFile();
83 // sort list by TimeStamp
84 List<FTPFile> sorted = Arrays.asList(files1).stream()
85 .sorted((e1, e2) -> e1.getTimestamp().compareTo(e2.getTimestamp())).collect(Collectors.toList());
86 printFileDetailsList(sorted);
88 // retrieve file from FTP
89 FTPFile ftpFile = sorted.get(sorted.size() - 1);
91 return retrieveFileFromFTP(ftpFile);
95 public FTPFile[] retrieveListOfFile() throws IOException {
96 // Lists files and directories
97 FTPFile[] files = apacheFtpClient.listFiles("");
103 public File retrieveFileFromFTP(FTPFile ftpFile) throws IOException {
105 File downloadFile1 = new File("tmp");
106 OutputStream outputStream1 = new BufferedOutputStream(new FileOutputStream(downloadFile1));
107 boolean success = apacheFtpClient.retrieveFile(ftpFile.getName(), outputStream1);
108 outputStream1.close();
111 System.out.println("File #1 has been downloaded successfully.");
115 return downloadFile1;
119 public void deleteFilesFromFTPserver() throws IOException {
120 FTPFile[] files = retrieveListOfFile();
124 public void terminateClient() throws IOException {
126 String status = apacheFtpClient.getStatus();
128 // logs out and disconnects from server
130 if (apacheFtpClient.isConnected()) {
131 apacheFtpClient.logout();
132 apacheFtpClient.disconnect();
134 } catch (IOException ex) {
135 ex.printStackTrace();
139 private void printFileDetailsList(List<FTPFile> list) {
140 DateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
142 for (FTPFile ftpFile : list) {
144 String details = ftpFile.getName();
145 if (ftpFile.isDirectory()) {
146 details = "[" + details + "]";
148 details += "\t\t" + ftpFile.getSize();
149 details += "\t\t" + dateFormater.format(ftpFile.getTimestamp().getTime());
151 System.out.println(details);
155 private void printNames(FTPFile[] files) {
156 if (files != null && files.length > 0) {
157 for (FTPFile aFile : files) {
158 System.out.println(aFile);
163 private void showServerReply(FTPClient ftpClient) {
164 String[] replies = ftpClient.getReplyStrings();
165 if (replies != null && replies.length > 0) {
166 for (String aReply : replies) {
167 System.out.println("SERVER: " + aReply);
172 public class LastModifiedComparator implements Comparator<FTPFile> {
174 public int compare(FTPFile f1, FTPFile f2) {
175 return f1.getTimestamp().compareTo(f2.getTimestamp());
179 public FTPFile getMaxLastModified(FTPFile[] ftpFiles) {
180 return Collections.max(Arrays.asList(ftpFiles), new LastModifiedComparator());
183 public static void displayFiles(File[] files) {
184 for (File file : files) {
185 System.out.printf("File: %-20s Last Modified:" + new Date(file.lastModified()) + "\n", file.getName());
189 public void deleteFiles(FTPFile[] files) {
191 for (FTPFile file : files) {
193 boolean deleted = false;
195 deleted = apacheFtpClient.deleteFile(file.getName());
196 } catch (IOException e) {
201 System.out.println("The file was deleted successfully.");
203 System.out.println("Could not delete theĀ file, it may not exist.");