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 java.io.BufferedOutputStream;
25 import java.io.FileOutputStream;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.OutputStream;
29 import java.text.DateFormat;
30 import java.text.SimpleDateFormat;
31 import java.util.Arrays;
32 import java.util.Collections;
33 import java.util.Comparator;
34 import java.util.Date;
35 import java.util.List;
36 import java.util.stream.Collectors;
38 import org.apache.commons.io.output.ByteArrayOutputStream;
39 import org.apache.commons.net.ftp.FTP;
40 import org.apache.commons.net.ftp.FTPClient;
41 import org.apache.commons.net.ftp.FTPFile;
42 import org.apache.commons.net.ftp.FTPReply;
44 public class AttFtpClient {
46 private static final AttFtpClient instance = new AttFtpClient();
48 public static AttFtpClient getInstance() {
52 private FTPClient apacheFtpClient;
54 private AttFtpClient() {
55 apacheFtpClient = new FTPClient();
59 public void init(String server, int port, String user, String pass) {
62 apacheFtpClient.connect(server, port);
63 showServerReply(apacheFtpClient);
66 int replyCode = apacheFtpClient.getReplyCode();
67 if (!FTPReply.isPositiveCompletion(replyCode)) {
68 System.out.println("Connect failed");
72 boolean success = apacheFtpClient.login(user, pass);
73 showServerReply(apacheFtpClient);
76 System.out.println("Could not login to the server");
81 // apacheFtpClient.enterLocalPassiveMode();
82 // apacheFtpClient.setFileType(FTP.BINARY_FILE_TYPE);
84 } catch (IOException ex) {
85 System.out.println("Oops! Something wrong happened");
91 public File retrieveLastModifiedFileFromFTP() throws IOException {
92 FTPFile[] files1 = retrieveListOfFile();
94 // sort list by TimeStamp
95 List<FTPFile> sorted = Arrays.asList(files1).stream()
96 .sorted((e1, e2) -> e1.getTimestamp().compareTo(e2.getTimestamp())).collect(Collectors.toList());
97 printFileDetailsList(sorted);
99 // retrieve file from FTP
100 FTPFile ftpFile = sorted.get(sorted.size() - 1);
102 return retrieveFileFromFTP(ftpFile);
106 public FTPFile[] retrieveListOfFile() throws IOException {
107 // Lists files and directories
108 FTPFile[] files = apacheFtpClient.listFiles("");
114 public File retrieveFileFromFTP(FTPFile ftpFile) throws IOException {
116 File downloadFile1 = new File("tmp");
117 OutputStream outputStream1 = new BufferedOutputStream(new FileOutputStream(downloadFile1));
118 boolean success = apacheFtpClient.retrieveFile(ftpFile.getName(), outputStream1);
119 outputStream1.close();
122 System.out.println("File #1 has been downloaded successfully.");
126 return downloadFile1;
130 public void deleteFilesFromFTPserver() throws IOException {
131 FTPFile[] files = retrieveListOfFile();
135 public void terminateClient() throws IOException {
137 String status = apacheFtpClient.getStatus();
139 // logs out and disconnects from server
141 if (apacheFtpClient.isConnected()) {
142 apacheFtpClient.logout();
143 apacheFtpClient.disconnect();
145 } catch (IOException ex) {
146 ex.printStackTrace();
150 private void printFileDetailsList(List<FTPFile> list) {
151 DateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
153 for (FTPFile ftpFile : list) {
155 String details = ftpFile.getName();
156 if (ftpFile.isDirectory()) {
157 details = "[" + details + "]";
159 details += "\t\t" + ftpFile.getSize();
160 details += "\t\t" + dateFormater.format(ftpFile.getTimestamp().getTime());
162 System.out.println(details);
166 private void printNames(FTPFile[] files) {
167 if (files != null && files.length > 0) {
168 for (FTPFile aFile : files) {
169 System.out.println(aFile);
174 private void showServerReply(FTPClient ftpClient) {
175 String[] replies = ftpClient.getReplyStrings();
176 if (replies != null && replies.length > 0) {
177 for (String aReply : replies) {
178 System.out.println("SERVER: " + aReply);
183 public class LastModifiedComparator implements Comparator<FTPFile> {
185 public int compare(FTPFile f1, FTPFile f2) {
186 return f1.getTimestamp().compareTo(f2.getTimestamp());
190 public FTPFile getMaxLastModified(FTPFile[] ftpFiles) {
191 return Collections.max(Arrays.asList(ftpFiles), new LastModifiedComparator());
194 public static void displayFiles(File[] files) {
195 for (File file : files) {
196 System.out.printf("File: %-20s Last Modified:" + new Date(file.lastModified()) + "\n", file.getName());
200 public void deleteFiles(FTPFile[] files) {
202 for (FTPFile file : files) {
204 boolean deleted = false;
206 deleted = apacheFtpClient.deleteFile(file.getName());
207 } catch (IOException e) {
208 // TODO Auto-generated catch block
213 System.out.println("The file was deleted successfully.");
215 System.out.println("Could not delete theĀ file, it may not exist.");