JAL-1740 implemented auto-update of BioJsMSA, introduced FTP client for downloading...
[jalview.git] / src / jalview / ftp / FtpClient.java
1 package jalview.ftp;
2
3 import java.io.BufferedOutputStream;
4 import java.io.File;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7 import java.io.OutputStream;
8 import java.net.SocketException;
9
10 import org.apache.commons.net.ftp.FTPClient;
11 import org.apache.commons.net.ftp.FTPFile;
12
13 public class FtpClient
14 {
15
16   public static boolean authenticateUser(FTPClient ftpClient,
17           String username, String password)
18   {
19     boolean loggedIn = false;
20
21     try
22     {
23       loggedIn = ftpClient.login(username, password);
24     } catch (IOException e)
25     {
26       e.printStackTrace();
27     }
28     disconectFTP(ftpClient);
29
30     return loggedIn;
31   }
32
33   public static FTPClient getFtpClient(String serverHost)
34   {
35     FTPClient ftpClient = new FTPClient();
36     try
37     {
38       ftpClient.connect(serverHost);
39     } catch (SocketException e)
40     {
41       e.printStackTrace();
42     } catch (IOException e)
43     {
44       e.printStackTrace();
45     }
46     return ftpClient;
47   }
48
49   public static void disconectFTP(FTPClient ftpClient)
50   {
51     try
52     {
53       if (ftpClient.isConnected())
54       {
55         ftpClient.logout();
56         ftpClient.disconnect();
57       }
58     } catch (IOException e)
59     {
60       e.printStackTrace();
61     }
62   }
63
64   public static void listDirectory(FTPClient ftpClient, String parentDir,
65           String currentDir, int level) throws IOException
66   {
67     String dirToList = parentDir;
68     if (!currentDir.equals(""))
69     {
70       dirToList += "/" + currentDir;
71     }
72     FTPFile[] subFiles = ftpClient.listFiles(dirToList);
73     if (subFiles != null && subFiles.length > 0)
74     {
75       for (FTPFile aFile : subFiles)
76       {
77         String currentFileName = aFile.getName();
78         if (currentFileName.equals(".") || currentFileName.equals(".."))
79         {
80           // skip parent directory and directory itself
81           continue;
82         }
83         for (int i = 0; i < level; i++)
84         {
85           System.out.print("\t");
86         }
87         if (aFile.isDirectory())
88         {
89           System.out.println("[" + currentFileName + "]");
90           listDirectory(ftpClient, dirToList, currentFileName, level + 1);
91         }
92         else
93         {
94           System.out.println(currentFileName);
95         }
96       }
97     }
98   }
99
100   public static boolean downloadFile(FTPClient client, String remoteFile,
101           String local)
102   {
103     boolean success = false;
104     File downloadFile = new File(local);
105     OutputStream outputStream = null;
106     try
107     {
108       outputStream = new BufferedOutputStream(new FileOutputStream(
109               downloadFile));
110
111       success = client.retrieveFile(remoteFile, outputStream);
112     } catch (IOException e)
113     {
114       e.printStackTrace();
115     } finally
116     {
117       try
118       {
119         if (outputStream != null)
120         {
121           outputStream.close();
122         }
123       } catch (IOException e)
124       {
125         e.printStackTrace();
126       }
127     }
128     if (success)
129     {
130       System.out.println(remoteFile + " has been downloaded successfully.");
131     }
132     return success;
133   }
134 }