| 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 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
public final class FTPManager { |
| 38 | |
private final FTPClient ftp; |
| 39 | |
private final FTPConnectionInfos infos; |
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 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 | |
|
| 56 | |
|
| 57 | |
|
| 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 | |
|
| 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 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 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 | |
|
| 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 | |
} |