package uk.ac.ebi.ddi.extservices.net;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class FtpUtils {

    private static final Logger LOGGER = LoggerFactory.getLogger(FtpUtils.class);

    public static List<String> getListFiles(FTPClient client, String path, String... ignoreDir) {
        List<String> result = new ArrayList<>();
        for (String dir : ignoreDir) {
            if (path.equals(dir)) {
                return result;
            }
        }
        try {
            if (!client.changeWorkingDirectory(path)) {
                return Collections.emptyList();
            }
            FTPFile[] ftpFiles = client.listFiles();
            for (FTPFile file : ftpFiles) {
                if (!file.getName().equals(".") && !file.getName().equals("..")) {
                    if (file.isDirectory()) {
                        result.addAll(getListFiles(client, file.getName(), ignoreDir));
                    } else {
                        String link = String.format("%s/%s", client.printWorkingDirectory(), file.getName());
                        result.add(link);
                    }
                }
            }
            client.changeToParentDirectory();
        } catch (Exception e) {
            LOGGER.error("Exception occurred when listing ftp files of {}, ", path, e);
        }
        return result;
    }
}

@@ -179,16 +179,44 @@
 ctory;%0A%0A
+import java.io.IOException;%0A
 import j
@@ -484,32 +484,51 @@
 ng... ignoreDir)
+ throws IOException
  %7B%0A        List%3C
@@ -697,26 +697,8 @@
   %7D%0A
-        try %7B%0A    
     
@@ -753,28 +753,24 @@
             
-    
 return Colle
@@ -785,28 +785,24 @@
 mptyList();%0A
-    
         %7D%0A  
@@ -807,20 +807,16 @@
         
-    
 FTPFile%5B
@@ -852,28 +852,24 @@
 ();%0A        
-   
- 
 for (FTPFile
@@ -884,28 +884,24 @@
 ftpFiles) %7B%0A
-    
             
@@ -979,28 +979,24 @@
             
- 
-   
 if (file.isD
@@ -1005,28 +1005,24 @@
 ectory()) %7B%0A
-    
             
@@ -1109,20 +1109,16 @@
         
-    
 %7D else %7B
@@ -1138,20 +1138,16 @@
         
- 
-   
 String l
@@ -1236,36 +1236,32 @@
                 
-    
 result.add(link)
@@ -1274,32 +1274,26 @@
             
-    %7D%0A  
+%7D%0A
             
@@ -1292,31 +1292,26 @@
         
-  %7D%0A   
+%7D%0A
         
  %7D%0A     
@@ -1294,39 +1294,34 @@
       %7D%0A        
- 
 %7D%0A
-    
         client.c
@@ -1350,138 +1350,8 @@
 ();%0A
-        %7D catch (Exception e) %7B%0A            LOGGER.error(%22Exception occurred when listing ftp files of %7B%7D, %22, path, e);%0A        %7D%0A
     

Throw exception right away if it happen to avoid repeatedly throwing exceptions.