Coverage Report - org.jtheque.films.services.impl.utils.file.FTPManager
 
Classes in this File Line Coverage Branch Coverage Complexity
FTPManager
0 %
0/49
0 %
0/12
3.2
 
 1  
 package org.jtheque.films.services.impl.utils.file;
 2  
 
 3  
 import org.apache.commons.net.ftp.FTP;
 4  
 import org.apache.commons.net.ftp.FTPClient;
 5  
 import org.apache.commons.net.ftp.FTPConnectionClosedException;
 6  
 import org.apache.commons.net.ftp.FTPReply;
 7  
 import org.jtheque.core.managers.Managers;
 8  
 import org.jtheque.core.managers.log.ILoggingManager;
 9  
 import org.jtheque.core.utils.Response;
 10  
 import org.jtheque.utils.io.FileUtils;
 11  
 
 12  
 import java.io.FileInputStream;
 13  
 import java.io.IOException;
 14  
 import java.io.InputStream;
 15  
 
 16  
 /*
 17  
  * This file is part of JTheque.
 18  
  *
 19  
  * JTheque is free software: you can redistribute it and/or modify
 20  
  * it under the terms of the GNU General Public License as published by
 21  
  * the Free Software Foundation, either version 3 of the License.
 22  
  *
 23  
  * JTheque is distributed in the hope that it will be useful,
 24  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 25  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 26  
  * GNU General Public License for more details.
 27  
  *
 28  
  * You should have received a copy of the GNU General Public License
 29  
  * along with JTheque.  If not, see <http://www.gnu.org/licenses/>.
 30  
  */
 31  
 
 32  
 /**
 33  
  * A manager to connect, store file in, disconnect to a FTP server.
 34  
  *
 35  
  * @author Baptiste Wicht
 36  
  */
 37  
 public final class FTPManager {
 38  
     private final FTPClient ftp;
 39  
     private final FTPConnectionInfos infos;
 40  
 
 41  
     /**
 42  
      * Construct a new FTP Manager.
 43  
      *
 44  
      * @param infos The connection infos.
 45  
      */
 46  
     public FTPManager(FTPConnectionInfos infos) {
 47  0
         super();
 48  
 
 49  0
         this.infos = infos;
 50  
 
 51  0
         ftp = new FTPClient();
 52  0
     }
 53  
 
 54  
     /**
 55  
      * Connect to the server.
 56  
      *
 57  
      * @return The result of the connection.
 58  
      */
 59  
     public Response connect() {
 60  
         Response response;
 61  
 
 62  
         try {
 63  0
             ftp.connect(infos.getHost());
 64  
 
 65  0
             int reply = ftp.getReplyCode();
 66  
 
 67  0
             if (FTPReply.isPositiveCompletion(reply)) {
 68  0
                 if (ftp.login(infos.getUser(), infos.getPassword())) {
 69  0
                     response = new Response(true, "");
 70  
 
 71  0
                     ftp.setFileType(FTP.ASCII_FILE_TYPE);
 72  
 
 73  0
                     if (infos.isPassive()) {
 74  0
                         ftp.enterLocalPassiveMode();
 75  
                     }
 76  
                 } else {
 77  0
                     ftp.logout();
 78  
 
 79  0
                     response = new Response(false, "ftp.errors.login.impossible");
 80  
                 }
 81  
             } else {
 82  0
                 ftp.disconnect();
 83  
 
 84  0
                 response = new Response(false, "ftp.errors.connection.refused");
 85  
             }
 86  0
         } catch (IOException e) {
 87  0
             disconnectSimply();
 88  
 
 89  0
             Managers.getManager(ILoggingManager.class).getLogger(getClass()).error(e);
 90  
 
 91  0
             response = new Response(false, "ftp.errors.connection.impossible");
 92  0
         }
 93  
 
 94  0
         return response;
 95  
     }
 96  
 
 97  
     /**
 98  
      * Simply disconnect without logout.
 99  
      */
 100  
     private void disconnectSimply() {
 101  0
         if (ftp.isConnected()) {
 102  
             try {
 103  0
                 ftp.disconnect();
 104  0
             } catch (IOException f) {
 105  0
                 Managers.getManager(ILoggingManager.class).getLogger(getClass()).error(f);
 106  0
             }
 107  
         }
 108  0
     }
 109  
 
 110  
     /**
 111  
      * Upload a file to the server.
 112  
      *
 113  
      * @param local  The local file.
 114  
      * @param remote The remote file.
 115  
      * @return The response of the store.
 116  
      */
 117  
     public Response upload(String local, String remote) {
 118  0
         Response response = null;
 119  
 
 120  0
         InputStream input = null;
 121  
         try {
 122  0
             input = new FileInputStream(local);
 123  
 
 124  0
             boolean stored = ftp.storeFile(remote, input);
 125  
 
 126  0
             response = stored ? new Response(true, "ftp.messages.write.stored") : new Response(false, "ftp.errors.write.notstored");
 127  0
         } catch (FTPConnectionClosedException e) {
 128  0
             Managers.getManager(ILoggingManager.class).getLogger(getClass()).error(e);
 129  
 
 130  0
             response = new Response(false, "ftp.errors.write.close");
 131  0
         } catch (IOException e) {
 132  0
             Managers.getManager(ILoggingManager.class).getLogger(getClass()).error(e);
 133  
 
 134  0
             response = new Response(false, "ftp.errors.write.errors");
 135  
         } finally {
 136  0
             FileUtils.close(input);
 137  0
         }
 138  
 
 139  0
         return response;
 140  
     }
 141  
 
 142  
     /**
 143  
      * Disconnect from the server.
 144  
      */
 145  
     public void disconnect() {
 146  
         try {
 147  0
             ftp.logout();
 148  
 
 149  0
             if (ftp.isConnected()) {
 150  0
                 ftp.disconnect();
 151  
             }
 152  0
         } catch (IOException f) {
 153  0
             Managers.getManager(ILoggingManager.class).getLogger(getClass()).error(f);
 154  0
         }
 155  0
     }
 156  
 }