Coverage Report - org.crosswire.jsword.book.install.sword.FTPExample
 
Classes in this File Line Coverage Branch Coverage Complexity
FTPExample
0%
0/64
0%
0/40
5.8
 
 1  
 /**
 2  
  * Distribution License:
 3  
  * JSword is free software; you can redistribute it and/or modify it under
 4  
  * the terms of the GNU Lesser General Public License, version 2.1 or later
 5  
  * as published by the Free Software Foundation. This program is distributed
 6  
  * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 7  
  * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 8  
  * See the GNU Lesser General Public License for more details.
 9  
  *
 10  
  * The License is available on the internet at:
 11  
  *      http://www.gnu.org/copyleft/lgpl.html
 12  
  * or by writing to:
 13  
  *      Free Software Foundation, Inc.
 14  
  *      59 Temple Place - Suite 330
 15  
  *      Boston, MA 02111-1307, USA
 16  
  *
 17  
  * © CrossWire Bible Society, 2013 - 2016
 18  
  *
 19  
  */
 20  
 package org.crosswire.jsword.book.install.sword;
 21  
 
 22  
 import java.io.IOException;
 23  
 
 24  
 import org.apache.commons.net.ftp.FTPClient;
 25  
 import org.apache.commons.net.ftp.FTPFile;
 26  
 import org.apache.commons.net.ftp.FTPReply;
 27  
 
 28  
 /**
 29  
  * Prototype code
 30  
  *
 31  
  * @see gnu.lgpl.License The GNU Lesser General Public License for details.
 32  
  * @author DM Smith
 33  
  */
 34  
 public final class FTPExample {
 35  0
     private FTPExample() { }
 36  
 
 37  
     static void copyDirectory(FTPClient ftpClient, String sourceDir, String module, String destDir, String currentDir) throws IOException {
 38  0
         String dirToList = module;
 39  0
         if (!"".equals(currentDir)) {
 40  0
             dirToList += "/" + currentDir;
 41  
         }
 42  0
         FTPFile[] subFiles = ftpClient.listFiles(sourceDir + '/' + dirToList);
 43  0
         if (subFiles != null && subFiles.length > 0) {
 44  0
             for (FTPFile aFile : subFiles) {
 45  0
                 String currentFileName = aFile.getName();
 46  
                 // skip parent directory and directory itself
 47  0
                 if (".".equals(currentFileName) || "..".equals(currentFileName)) {
 48  0
                     continue;
 49  
                 }
 50  0
                 if (aFile.isFile()) {
 51  0
                     copyFile(ftpClient, sourceDir, dirToList + '/'  + currentFileName, destDir);
 52  0
                 } else if (aFile.isDirectory() && !"lucene".equalsIgnoreCase(currentFileName)) {
 53  0
                     System.out.println("mkdir " + destDir + dirToList + '/' + currentFileName + "/");
 54  0
                     copyDirectory(ftpClient, sourceDir, dirToList, destDir, currentFileName);
 55  
                 }
 56  
             }
 57  
         }
 58  0
     }
 59  
 
 60  
     static void copyFile(FTPClient ftpClient, String sourceDir, String file, String destDir) throws IOException {
 61  0
         System.out.println("cp " + sourceDir + '/' + file + ' ' + destDir + '/' + file);
 62  0
     }
 63  
 
 64  
     static long getSize(FTPClient ftpClient, String sourceDir, String module) throws IOException {
 65  0
         long total = 0;
 66  0
         String dirToList = module;
 67  0
         FTPFile[] subFiles = ftpClient.listFiles(sourceDir + '/' + dirToList);
 68  0
         if (subFiles != null && subFiles.length > 0) {
 69  0
             for (FTPFile aFile : subFiles) {
 70  0
                 String currentFileName = aFile.getName();
 71  0
                 if (".".equals(currentFileName) || "..".equals(currentFileName)) {
 72  
                     // skip parent directory and directory itself
 73  0
                     continue;
 74  
                 }
 75  0
                 if (aFile.isFile()) {
 76  0
                     total += aFile.getSize();
 77  0
                 } else if (aFile.isDirectory() && !"lucene".equalsIgnoreCase(currentFileName)) {
 78  0
                     total += getSize(ftpClient, sourceDir, dirToList);
 79  
                 }
 80  
             }
 81  
         }
 82  0
         return total;
 83  
     }
 84  
 /*
 85  
 20081216195754=FTPSource=CrossWire|ftp.crosswire.org|/pub/sword/raw
 86  
 20090224125400=FTPSource=CrossWire Beta|ftp.crosswire.org|/pub/sword/betaraw
 87  
 20090514005700=FTPSource=Bible.org|ftp.bible.org|/sword
 88  
 20090514005900=FTPSource=Xiphos|ftp.xiphos.org|.
 89  
 20120224005000=FTPSource=CrossWire av11n|ftp.crosswire.org|/pub/sword/avraw
 90  
 20120224005100=FTPSource=CrossWire Attic|ftp.crosswire.org|/pub/sword/atticraw
 91  
 20120224005200=FTPSource=IBT|ftp.ibt.org.ru|/pub/modsword/raw
 92  
 20120711005000=FTPSource=CrossWire Wycliffe|ftp.crosswire.org|/pub/sword/wyclifferaw
 93  
 20120711005100=FTPSource=CrossWire av11n Attic|ftp.crosswire.org|/pub/sword/avatticraw
 94  
 
 95  
 OutputStream output = new FileOutputStream(local);
 96  
 ftp.retrieveFile(remote, output);
 97  
 output.close();
 98  
  */
 99  
     public static void main(String[] args) {
 100  0
         String server = "ftp.crosswire.org";
 101  0
         int port = 21;
 102  0
         String user = "anonymous";
 103  0
         String pass = "jsword@crosswire.org";
 104  0
         String dirToList = "/pub/sword/avraw";
 105  0
         String confPath = "mods.d/azeri.conf";
 106  0
         String dataPath = "modules/texts/ztext/azeri";
 107  
 
 108  0
         FTPClient ftpClient = new FTPClient();
 109  
         try {
 110  0
             ftpClient.connect(server, port);
 111  0
             int replyCode = ftpClient.getReplyCode();
 112  0
             if (!FTPReply.isPositiveCompletion(replyCode)) {
 113  0
                 System.out.println("Connect failed");
 114  
                 return;
 115  
             }
 116  0
             boolean success = ftpClient.login(user, pass);
 117  0
             if (!success) {
 118  0
                 System.out.println("Could not login to the server");
 119  
                 return;
 120  
             }
 121  0
             ftpClient.setUseEPSVwithIPv4(true);
 122  0
             copyFile(ftpClient, dirToList, confPath, "/Users/DM/Library/Application Support/Sword");
 123  0
             System.out.println("Size is " + getSize(ftpClient, dirToList, confPath));
 124  0
             copyDirectory(ftpClient, dirToList, dataPath, "/Users/DM/Library/Application Support/Sword", "");
 125  0
             System.out.println("Size is " + getSize(ftpClient, dirToList, dataPath));
 126  0
         } catch (IOException e) {
 127  0
             System.out.println("Oops! Something wrong happened");
 128  0
             e.printStackTrace();
 129  
         } finally {
 130  
             // logs out and disconnects from server
 131  0
             try {
 132  0
                 if (ftpClient.isConnected()) {
 133  0
                     ftpClient.logout();
 134  0
                     ftpClient.disconnect();
 135  
                 }
 136  0
             } catch (IOException ex) {
 137  0
                 System.out.println("Oops! Something wrong happened");
 138  0
                 ex.printStackTrace();
 139  0
             }
 140  0
         }
 141  0
     }
 142  
 }