diff options
Diffstat (limited to 'dozentenmodul/src/main/java/org/openslx/dozmod/filetransfer')
-rw-r--r-- | dozentenmodul/src/main/java/org/openslx/dozmod/filetransfer/DownloadTask.java | 73 | ||||
-rw-r--r-- | dozentenmodul/src/main/java/org/openslx/dozmod/filetransfer/UploadTask.java | 72 |
2 files changed, 127 insertions, 18 deletions
diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/filetransfer/DownloadTask.java b/dozentenmodul/src/main/java/org/openslx/dozmod/filetransfer/DownloadTask.java index 3b57222f..3951d630 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/filetransfer/DownloadTask.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/filetransfer/DownloadTask.java @@ -6,8 +6,11 @@ import java.io.RandomAccessFile; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; +import javax.net.ssl.SSLContext; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import org.openslx.bwlp.thrift.iface.TransferInformation; import org.openslx.bwlp.thrift.iface.TransferState; import org.openslx.dozmod.Config; import org.openslx.filetransfer.DataReceivedCallback; @@ -33,19 +36,23 @@ public class DownloadTask extends TransferTask { private static final AtomicInteger THREAD_ID = new AtomicInteger(); private final String host; - private final int port; + private final int portPlain; + private final int portSsl; + private final SSLContext sslCtx; private final String downloadToken; private final RandomAccessFile fileHandle; private final ChunkList chunks; private final long startTime; private boolean fileWritable = true; - public DownloadTask(String host, int port, String downloadToken, File destinationFile, long fileSize, + public DownloadTask(String host, TransferInformation ti, SSLContext ctx, File destinationFile, long fileSize, List<byte[]> sha1Sums) throws FileNotFoundException { super(destinationFile, fileSize); this.host = host; - this.port = port; - this.downloadToken = downloadToken; + this.portPlain = ti.plainPort; + this.portSsl = ti.sslPort; + this.downloadToken = ti.token; + this.sslCtx = ctx; this.fileHandle = new RandomAccessFile(destinationFile, "rw"); this.chunks = new ChunkList(fileSize, sha1Sums); this.startTime = System.currentTimeMillis(); @@ -129,17 +136,65 @@ public class DownloadTask extends TransferTask { public DownloadThread() { super("UpConn#" + THREAD_ID.incrementAndGet()); } + + private Exception initPlain(Exception ex) { + if (portPlain <= 0 || portPlain > 65535) + return ex; + LOGGER.info("Establishing plain download connection to " + host + ":" + portPlain); + try { + downloader = new Downloader(host, portPlain, Config.TRANSFER_TIMEOUT, null, downloadToken); + } catch (Exception e) { + LOGGER.info("Connection failed"); + return e; + } + return null; + } + + private Exception initSsl(Exception ex) { + if (portSsl <= 0 || portSsl > 65535 || sslCtx == null) + return ex; + LOGGER.info("Establishing SSL download connection to " + host + ":" + portSsl); + try { + downloader = new Downloader(host, portSsl, Config.TRANSFER_TIMEOUT, sslCtx, downloadToken); + } catch (Exception e) { + LOGGER.info("Connection failed"); + return e; + } + return null; + } @Override public void run() { - try { - downloader = new Downloader(host, port, Config.TRANSFER_TIMEOUT, null, downloadToken); - } catch (Exception e) { - LOGGER.warn("Could not initialize new uploader", e); + Exception ex = null; + switch (Config.getFileTransferMode()) { + case SSL: + ex = initSsl(ex); + if (downloader == null) { + ex = initPlain(ex); + } + break; + case SSL_ONLY: + ex = initSsl(ex); + break; + case PLAIN: + default: + ex = initPlain(ex); + if (downloader == null) { + ex = initSsl(ex); + } + break; + } + if (downloader == null) { + if (ex == null) { + LOGGER.warn("Could not initialize new downloader because neither plain" + + " nor SSL transfer data is given"); + } else { + LOGGER.warn("Could not initialize new downloader, all connection methods failed", ex); + } consecutiveInitFails.incrementAndGet(); connectFailed(this); return; - } // TODO: SSL + } connectSucceeded(this); boolean ret = downloader.download(cb, cb); diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/filetransfer/UploadTask.java b/dozentenmodul/src/main/java/org/openslx/dozmod/filetransfer/UploadTask.java index fc13f113..06736619 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/filetransfer/UploadTask.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/filetransfer/UploadTask.java @@ -4,9 +4,12 @@ import java.io.File; import java.io.FileNotFoundException; import java.util.concurrent.atomic.AtomicInteger; +import javax.net.ssl.SSLContext; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.openslx.bwlp.thrift.iface.TInvalidTokenException; +import org.openslx.bwlp.thrift.iface.TransferInformation; import org.openslx.bwlp.thrift.iface.TransferState; import org.openslx.bwlp.thrift.iface.TransferStatus; import org.openslx.dozmod.Config; @@ -35,8 +38,10 @@ public class UploadTask extends TransferTask { private static final int THRIFT_INTERVAL_MS = (int) (THRIFT_INTERVAL_SECONDS * 1000); private final String host; - private final int port; private final String uploadToken; + private final int portPlain; + private final int portSsl; + private final SSLContext sslCtx; private final long startTime; private String transferConnectionError = null; @@ -54,15 +59,16 @@ public class UploadTask extends TransferTask { return numConnections.get(); } - public UploadTask(String host, int port, String uploadToken, File uploadFile) + public UploadTask(String host, TransferInformation ti, SSLContext ctx, File uploadFile) throws FileNotFoundException { super(uploadFile, uploadFile.length()); if (!uploadFile.canRead()) throw new FileNotFoundException(); - // TODO: SSL + this.sslCtx = ctx; this.host = host; - this.port = port; - this.uploadToken = uploadToken; + this.portPlain = ti.plainPort; + this.portSsl = ti.sslPort; + this.uploadToken = ti.token; this.startTime = System.currentTimeMillis(); } @@ -86,15 +92,63 @@ public class UploadTask extends TransferTask { } } - public void run2() { + private Exception initPlain(Exception ex) { + if (portPlain <= 0 || portPlain > 65535) + return ex; + LOGGER.info("Establishing plain upload connection to " + host + ":" + portPlain); + try { + uploader = new Uploader(host, portPlain, Config.TRANSFER_TIMEOUT, null, uploadToken); + } catch (Exception e) { + LOGGER.info("Connection failed"); + return e; + } + return null; + } + + private Exception initSsl(Exception ex) { + if (portSsl <= 0 || portSsl > 65535 || sslCtx == null) + return ex; + LOGGER.info("Establishing SSL upload connection to " + host + ":" + portSsl); try { - uploader = new Uploader(host, port, Config.TRANSFER_TIMEOUT, null, uploadToken); + uploader = new Uploader(host, portSsl, Config.TRANSFER_TIMEOUT, sslCtx, uploadToken); } catch (Exception e) { - LOGGER.warn("Could not initialize new uploader", e); + LOGGER.info("Connection failed"); + return e; + } + return null; + } + + public void run2() { + Exception ex = null; + switch (Config.getFileTransferMode()) { + case SSL: + ex = initSsl(ex); + if (uploader == null) { + ex = initPlain(ex); + } + break; + case SSL_ONLY: + ex = initSsl(ex); + break; + case PLAIN: + default: + ex = initPlain(ex); + if (uploader == null) { + ex = initSsl(ex); + } + break; + } + if (uploader == null) { + if (ex == null) { + LOGGER.warn("Could not initialize new uploader because neither plain" + + " nor SSL transfer data is given"); + } else { + LOGGER.warn("Could not initialize new uploader, all connection methods failed", ex); + } consecutiveInitFails.incrementAndGet(); connectFailed(this); return; - } // TODO: SSL + } connectSucceeded(this); final UploadThread thread = this; |