summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/ftp
diff options
context:
space:
mode:
authorSimon Rettberg2015-05-29 17:56:36 +0200
committerSimon Rettberg2015-05-29 17:56:36 +0200
commit605857a1ef614964d73ea83e0561d1bfb06e611b (patch)
treeae968412f038b1a53d07bf83ab8f7fdf808750d1 /dozentenmodul/src/main/java/ftp
parent[server] Added some TODOs (diff)
downloadtutor-module-605857a1ef614964d73ea83e0561d1bfb06e611b.tar.gz
tutor-module-605857a1ef614964d73ea83e0561d1bfb06e611b.tar.xz
tutor-module-605857a1ef614964d73ea83e0561d1bfb06e611b.zip
[client] Compiles again, but is broken....
Diffstat (limited to 'dozentenmodul/src/main/java/ftp')
-rw-r--r--dozentenmodul/src/main/java/ftp/DownloadTask.java246
-rw-r--r--dozentenmodul/src/main/java/ftp/FTPException.java8
-rw-r--r--dozentenmodul/src/main/java/ftp/FTPUtility.java197
-rw-r--r--dozentenmodul/src/main/java/ftp/UploadTask.java143
4 files changed, 210 insertions, 384 deletions
diff --git a/dozentenmodul/src/main/java/ftp/DownloadTask.java b/dozentenmodul/src/main/java/ftp/DownloadTask.java
index ef95b8da..95e74169 100644
--- a/dozentenmodul/src/main/java/ftp/DownloadTask.java
+++ b/dozentenmodul/src/main/java/ftp/DownloadTask.java
@@ -4,10 +4,9 @@ import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
import java.io.IOException;
-import java.io.InputStream;
import java.io.InputStreamReader;
+import java.io.RandomAccessFile;
import java.nio.charset.StandardCharsets;
import java.util.Map;
@@ -20,6 +19,10 @@ import models.SessionData;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.apache.thrift.TException;
+import org.openslx.filetransfer.DataReceivedCallback;
+import org.openslx.filetransfer.Downloader;
+import org.openslx.filetransfer.FileRange;
+import org.openslx.filetransfer.WantRangeCallback;
import org.openslx.thrifthelper.ThriftManager;
import util.ResourceLoader;
@@ -37,27 +40,78 @@ public class DownloadTask extends SwingWorker<Void, Void> {
*/
private final static Logger LOGGER = Logger.getLogger(DownloadTask.class);
- private static final int BUFFER_SIZE = 8 * 1024 * 1024;
private static final double UPDATE_INTERVAL_SECONDS = 0.6;
private static final double UPDATE_INTERVAL_MS = UPDATE_INTERVAL_SECONDS * 1000;
private static final double BYTES_PER_MIB = 1024 * 1024;
+ private static final long CHUNK_SIZE = 16 * 1024 * 1024;
- private String host;
- private int port;
- private String username;
- private String password;
- private String downloadPath;
- private String saveDir;
- private int percentCompleted;
+ private final String host;
+ private final int port;
+ private final String downloadToken;
+ private final String saveDir;
+ private final long fileSize;
+ private boolean success = false;
- public DownloadTask(String host, int port, String username, String password,
- String downloadPath, String saveDir) {
+ public DownloadTask(String host, int port, String downloadToken, String saveDir, long fileSize) {
this.host = host;
this.port = port;
- this.username = username;
- this.password = password;
- this.downloadPath = downloadPath;
+ this.downloadToken = downloadToken;
this.saveDir = saveDir;
+ this.fileSize = fileSize;
+ }
+
+ class Callbacks implements WantRangeCallback, DataReceivedCallback {
+ // initialize the counters needed for speed calculations
+ private long currentRequestedOffset = -1;
+ private long totalBytesRead = 0;
+ private long lastUpdate = 0;
+ private long lastBytes = 0;
+ private long currentBytes = 0;
+ private final RandomAccessFile file;
+
+ public Callbacks(RandomAccessFile file) {
+ this.file = file;
+ }
+
+ @Override
+ public FileRange get() {
+ if (currentRequestedOffset == -1)
+ currentRequestedOffset = 0;
+ else
+ currentRequestedOffset += CHUNK_SIZE;
+ if (currentRequestedOffset >= fileSize)
+ return null;
+ long end = currentRequestedOffset + CHUNK_SIZE;
+ if (end > fileSize)
+ end = fileSize;
+ return new FileRange(currentRequestedOffset, end);
+ }
+
+ @Override
+ public boolean dataReceived(final long fileOffset, final int dataLength, final byte[] data) {
+ try {
+ file.seek(fileOffset);
+ file.write(data, 0, dataLength);
+ } catch (Exception e) {
+ LOGGER.error("Could not write to file at offset " + fileOffset, e);
+ return false;
+ }
+ currentBytes += dataLength;
+ totalBytesRead += dataLength;
+ final long now = System.currentTimeMillis();
+ if (lastUpdate + UPDATE_INTERVAL_MS < now) {
+ final int percentCompleted = (int) ((totalBytesRead * 100) / fileSize);
+ setProgress(percentCompleted);
+ lastBytes = (lastBytes * 2 + currentBytes) / 3;
+ final double speed = lastBytes / UPDATE_INTERVAL_SECONDS;
+ firePropertyChange("speed", 0, speed / BYTES_PER_MIB);
+ firePropertyChange("bytesread", 0, totalBytesRead);
+ lastUpdate = now;
+ currentBytes = 0;
+ }
+ return true;
+ }
+
}
/**
@@ -65,61 +119,45 @@ public class DownloadTask extends SwingWorker<Void, Void> {
*/
@Override
protected Void doInBackground() throws Exception {
- FTPUtility util = new FTPUtility(host, port, username, password);
- try {
- util.connect();
+ boolean ret = false;
+ // show filesize in the GUI
+ firePropertyChange("filesize", 0, fileSize);
- // show filesize in the GUI
- long fileSize = util.getFileSize(downloadPath);
- firePropertyChange("filesize", 0, fileSize);
- util.downloadFile(downloadPath);
+ Downloader download = null;
+ RandomAccessFile file = null;
+ try {
+ download = new Downloader(host, port, null, downloadToken); // TODO: SSL
+ try {
+ file = new RandomAccessFile(new File(saveDir), "rw");
+ } catch (Exception e2) {
+ JOptionPane.showMessageDialog(null, "Could not open destination file:\n" + saveDir + "\n"
+ + e2.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
+ e2.printStackTrace();
+ setProgress(0);
+ return null;
+ }
- // prepare the input/output streams
- String fileName = new File(downloadPath).getName();
- File downloadFile = new File(saveDir + File.separator + fileName);
- FileOutputStream outputStream = new FileOutputStream(downloadFile);
- InputStream inputStream = util.getInputStream();
+ Callbacks cb = new Callbacks(file);
- // initialize the counters needed for speed calculations
- percentCompleted = 0;
- byte[] buffer = new byte[BUFFER_SIZE];
- int bytesRead = -1;
- long totalBytesRead = 0;
- long lastUpdate = 0;
- long lastBytes = 0;
- long currentBytes = 0;
- while ((bytesRead = inputStream.read(buffer)) != -1 && !isCancelled()) {
- outputStream.write(buffer, 0, bytesRead);
- currentBytes += bytesRead;
- totalBytesRead += bytesRead;
- long now = System.currentTimeMillis();
- if (lastUpdate + UPDATE_INTERVAL_MS < now) {
- percentCompleted = (int) ((totalBytesRead * 100) / fileSize);
- setProgress(percentCompleted);
- lastBytes = (lastBytes * 2 + currentBytes) / 3;
- final double speed = lastBytes / UPDATE_INTERVAL_SECONDS;
- firePropertyChange("speed", 0, speed / BYTES_PER_MIB);
- firePropertyChange("bytesread", 0, totalBytesRead);
- lastUpdate = now;
- currentBytes = 0;
+ ret = download.download(cb, cb);
+ } finally {
+ if (file != null) {
+ try {
+ file.close();
+ } catch (Exception e) {
}
}
- // finalize the download by updating the progress bar one last time
- // (in case we didn't get to do it because of the time interval)
- percentCompleted = (int) ((totalBytesRead * 100) / fileSize);
- setProgress(percentCompleted);
- firePropertyChange("bytesread", 0, totalBytesRead);
- outputStream.close();
- util.finish();
- } catch (FTPException ex) {
- JOptionPane.showMessageDialog(null,
- "Error downloading file: " + ex.getMessage(), "Error",
- JOptionPane.ERROR_MESSAGE);
- ex.printStackTrace();
- setProgress(0);
- cancel(true);
- } finally {
- util.disconnect();
+ if (download != null)
+ download.close(null);
+ }
+
+ // if the download succeeded, set the progress to 100% manually again here to make
+ // sure the GUI knows about it.
+ if (ret) {
+ setProgress(100);
+ firePropertyChange("bytesread", 0, fileSize);
+ firePropertyChange("success", false, true);
+ success = true;
}
return null;
@@ -130,24 +168,26 @@ public class DownloadTask extends SwingWorker<Void, Void> {
*/
@Override
protected void done() {
- if (!isCancelled() && percentCompleted == 100) {
+ if (isCancelled())
+ return;
+ if (success) {
LOGGER.info("Datei erfolgreich heruntergeladen.");
String vmxResult = "";
- vmxResult = generateVmx() ? "Passende VMX generiert."
- : "Keine passende VMX generiert!";
- JOptionPane.showMessageDialog(null, "Datei erfolgreich heruntergeladen. "
- + vmxResult, "Message", JOptionPane.INFORMATION_MESSAGE);
- } else if (!isCancelled() && percentCompleted != 100) {
+ vmxResult = generateVmx() ? "Passende VMX generiert." : "Keine passende VMX generiert!";
+ JOptionPane.showMessageDialog(null, "Datei erfolgreich heruntergeladen. " + vmxResult, "Message",
+ JOptionPane.INFORMATION_MESSAGE);
+ } else {
LOGGER.error("Datei wurde unvollständig heruntergeladen.");
JOptionPane.showMessageDialog(null,
- "Datei wurde unvollständig heruntergeladen. Bitte wiederholen.",
- "Message", JOptionPane.INFORMATION_MESSAGE);
+ "Datei wurde unvollständig heruntergeladen. Bitte wiederholen.", "Message",
+ JOptionPane.INFORMATION_MESSAGE);
}
}
/**
* Helper to generate the vmx for the downloaded image
- *
+ * TODO: Not really related to DownloadTask...
+ *
* @return true|false indicating the success of the file creation
*/
private boolean generateVmx() {
@@ -159,8 +199,7 @@ public class DownloadTask extends SwingWorker<Void, Void> {
LOGGER.debug("Image's ID: " + Image.ImageId);
Map<String, String> imageData = null;
try {
- imageData = ThriftManager.getSatClient().getImageData(
- Image.ImageId, Image.Version,
+ imageData = ThriftManager.getSatClient().getImageData(Image.ImageId, Image.Version,
SessionData.authToken);
} catch (TException e) {
LOGGER.error("Thrift exception during transfer, see trace: ", e);
@@ -177,37 +216,29 @@ public class DownloadTask extends SwingWorker<Void, Void> {
int hardwareVersion = extractHardwareVersion(saveDir + File.separator
+ imageData.get("path").replaceFirst("^prod/", ""));
if (hardwareVersion == 0) {
- LOGGER
- .error("'extractHardwareVersion' returned 0 indicating some problem. See logs.");
+ LOGGER.error("'extractHardwareVersion' returned 0 indicating some problem. See logs.");
LOGGER.error("Falling back to default hardware version of '10'.");
hardwareVersion = 10;
}
// TODO: sanity checks on the content of imageData would be good here...
// use the information we received about the image
- vmxTemplate = vmxTemplate.replace("%VM_DISPLAY_NAME%",
- imageData.get("name"));
+ vmxTemplate = vmxTemplate.replace("%VM_DISPLAY_NAME%", imageData.get("name"));
vmxTemplate = vmxTemplate.replace("%VM_GUEST_OS%", imageData.get("os"));
vmxTemplate = vmxTemplate.replace("%VM_CPU_COUNT%", imageData.get("cpu"));
vmxTemplate = vmxTemplate.replace("%VM_RAM_SIZE%",
String.valueOf(Integer.valueOf(imageData.get("ram")) * 1024));
- vmxTemplate = vmxTemplate.replace("%VM_DISK_PATH%", imageData.get("path")
- .replaceFirst("^prod/", ""));
- vmxTemplate = vmxTemplate.replace("%VM_HW_VERSION%",
- String.valueOf(hardwareVersion));
+ vmxTemplate = vmxTemplate.replace("%VM_DISK_PATH%", imageData.get("path").replaceFirst("^prod/", ""));
+ vmxTemplate = vmxTemplate.replace("%VM_HW_VERSION%", String.valueOf(hardwareVersion));
// build filename for the vmx, basicly the same as the path of the vmdk
// just without the leading "prod/" and "vmx" instead of "vmdk" at the end.
- String targetFilename = saveDir
- + File.separator
- + imageData.get("path").replaceFirst("^prod/", "")
- .replaceFirst("\\.vmdk$", "") + ".vmx";
+ String targetFilename = saveDir + File.separator
+ + imageData.get("path").replaceFirst("^prod/", "").replaceFirst("\\.vmdk$", "") + ".vmx";
try {
// try to write it to file
- FileUtils.writeStringToFile(new File(targetFilename), vmxTemplate,
- StandardCharsets.UTF_8);
+ FileUtils.writeStringToFile(new File(targetFilename), vmxTemplate, StandardCharsets.UTF_8);
} catch (IOException e) {
- LOGGER.error("Could not write vmx-template to '" + targetFilename
- + "'. See trace: ", e);
+ LOGGER.error("Could not write vmx-template to '" + targetFilename + "'. See trace: ", e);
return false;
}
return true;
@@ -216,44 +247,41 @@ public class DownloadTask extends SwingWorker<Void, Void> {
/**
* Helper to extract the hardware version of the VMDK file by inspecting its
* content.
- *
- * @return value of hardware version as integer. A return value of 0 indicates
+ *
+ * @return value of hardware version as integer. A return value of 0
+ * indicates
* an error.
*/
private int extractHardwareVersion(String path) {
BufferedReader br = null;
try {
try {
- br = new BufferedReader(
- new InputStreamReader(new FileInputStream(path)));
+ br = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
String line;
// first 4 characters of a VMDK file start with 'KDMV'
// first lets check if this is the case
line = br.readLine();
if (!line.subSequence(0, 4).equals("KDMV")) {
- LOGGER
- .error("Did not see 'KDMV' as first chars of the VMDK! Returning 0.");
+ LOGGER.error("Did not see 'KDMV' as first chars of the VMDK! Returning 0.");
LOGGER.debug("First line was: " + line);
LOGGER.debug("First 4 characters of it: " + line.subSequence(0, 4));
return 0;
}
- // only read a maximum of 20 lines, just in case...
+ // only read a maximum of 25 lines, just in case...
int round = 0;
- while ((line = br.readLine()) != null && round < 20) {
- if (line.matches("^ddb\\.virtualHWVersion.*")) {
+ while ((line = br.readLine()) != null && round < 25) {
+ if (line.startsWith("ddb.virtualHWVersion")) {
String[] tmp = line.split("=");
// we should get 2 strings only after the split, lets be sure
if (tmp.length != 2) {
- LOGGER
- .debug("Splitting returned more than 2 parts, this should not happen!");
+ LOGGER.debug("Splitting returned more than 2 parts, this should not happen!");
return 0;
}
int candidate = Integer.parseInt(tmp[1].trim().replace("\"", ""));
LOGGER.debug("Considering hardware version: " + candidate);
if (candidate > 0) {
- LOGGER
- .debug("Valid value of the candidate. Using hardware version of: "
- + candidate);
+ LOGGER.debug("Valid value of the candidate. Using hardware version of: "
+ + candidate);
return candidate;
} else {
LOGGER.error("Candidate is not > 0! Returning 0.");
@@ -262,8 +290,10 @@ public class DownloadTask extends SwingWorker<Void, Void> {
}
round++;
}
- LOGGER.error("Failed to find hardware version. Tried " + round
- + " rounds.");
+ LOGGER.error("Failed to find hardware version. Tried " + round + " rounds.");
+ } catch (NumberFormatException e) {
+ // Not a number?
+ return 0;
} finally {
br.close();
}
diff --git a/dozentenmodul/src/main/java/ftp/FTPException.java b/dozentenmodul/src/main/java/ftp/FTPException.java
deleted file mode 100644
index bc3193e0..00000000
--- a/dozentenmodul/src/main/java/ftp/FTPException.java
+++ /dev/null
@@ -1,8 +0,0 @@
-package ftp;
-
-@SuppressWarnings("serial")
-public class FTPException extends Exception {
- public FTPException(String message) {
- super(message);
- }
-} \ No newline at end of file
diff --git a/dozentenmodul/src/main/java/ftp/FTPUtility.java b/dozentenmodul/src/main/java/ftp/FTPUtility.java
deleted file mode 100644
index a7f48e4d..00000000
--- a/dozentenmodul/src/main/java/ftp/FTPUtility.java
+++ /dev/null
@@ -1,197 +0,0 @@
-package ftp;
-
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.security.NoSuchAlgorithmException;
-
-import models.Image;
-
-import org.apache.commons.net.ftp.FTP;
-import org.apache.commons.net.ftp.FTPFile;
-import org.apache.commons.net.ftp.FTPReply;
-import org.apache.commons.net.ftp.FTPSClient;
-
-/**
- * A utility class that provides functionality for downloading files from a FTP
- * server.
- *
- * @author www.codejava.net
- *
- */
-public class FTPUtility {
-
- // FTP server information
- private String host;
- private int port;
- private String username;
- private String password;
-
- private FTPSClient ftpClient = new FTPSClient();
- private int replyCode;
-
- private InputStream inputStream;
- private OutputStream outputStream;
-
- public FTPUtility(String host, int port, String user, String pass) {
- this.host = host;
- this.port = port;
- this.username = user;
- this.password = pass;
- this.ftpClient.setBufferSize(0);
- }
-
- /**
- * Connect and login to the server.
- *
- * @throws FTPException
- * @throws NoSuchAlgorithmException
- */
- public void connect() throws FTPException, NoSuchAlgorithmException {
- try {
- ftpClient.connect(host, port);
- replyCode = ftpClient.getReplyCode();
- if (!FTPReply.isPositiveCompletion(replyCode)) {
- throw new FTPException("FTP server refused connection.");
- }
-
- boolean logged = ftpClient.login(username, password);
- if (!logged) {
- // failed to login
- ftpClient.execPROT("P");
- ftpClient.disconnect();
- throw new FTPException("Could not login to the server.");
- }
-
- ftpClient.enterLocalPassiveMode();
-
- } catch (IOException ex) {
- throw new FTPException("I/O error: " + ex.getMessage());
- }
- }
-
- /**
- * Gets size (in bytes) of the file on the server.
- *
- * @param filePath
- * Path of the file on server
- * @return file size in bytes
- * @throws FTPException
- */
- public long getFileSize(String filePath) throws FTPException {
- try {
- FTPFile file = ftpClient.mlistFile(filePath);
- if (file == null) {
- throw new FTPException("The file may not exist on the server!");
- }
- return file.getSize();
- } catch (IOException ex) {
- throw new FTPException("Could not determine size of the file: "
- + ex.getMessage());
- }
- }
-
- /**
- * Start downloading a file from the server
- *
- * @param downloadPath
- * Full path of the file on the server
- * @throws FTPException
- * if client-server communication error occurred
- */
- public void downloadFile(String downloadPath) throws FTPException {
- try {
-
- boolean success = ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
- if (!success) {
- throw new FTPException("Could not set binary file type.");
- }
-
- inputStream = ftpClient.retrieveFileStream(downloadPath);
-
- if (inputStream == null) {
- throw new FTPException(
- "Could not open input stream. The file may not exist on the server.");
- }
- } catch (IOException ex) {
- throw new FTPException("Error downloading file: " + ex.getMessage());
- }
- }
-
- /**
- * Start uploading a file to the server
- *
- * @param uploadFile
- * the file to be uploaded
- * @param destDir
- * destination directory on the server where the file is stored
- * @throws FTPException
- * if client-server communication error occurred
- */
- public void uploadFile(File uploadFile, String destDir) throws FTPException {
- try {
- boolean success = ftpClient.changeWorkingDirectory(destDir);
- if (!success) {
- throw new FTPException("Could not change working directory to "
- + destDir + ". The directory may not exist.");
- }
-
- success = ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
- if (!success) {
- throw new FTPException("Could not set binary file type.");
- }
-
- outputStream = ftpClient.storeFileStream(Image.NewName);
-
- // ftpClient.rename(uploadFile.getName(), );
- } catch (IOException ex) {
- throw new FTPException("Error uploading file: " + ex.getMessage());
- }
- }
-
- /**
- * Write an array of bytes to the output stream.
- */
- public void writeFileBytes(byte[] bytes, int offset, int length)
- throws IOException {
- outputStream.write(bytes, offset, length);
- }
-
- /**
- * Complete the download operation.
- */
- public void finish() throws IOException {
- inputStream.close();
- ftpClient.completePendingCommand();
- }
-
- /**
- * Log out and disconnect from the server
- */
- public void disconnect() throws FTPException {
- if (ftpClient.isConnected()) {
- try {
- if (!ftpClient.logout()) {
- throw new FTPException("Could not log out from the server");
- }
- ftpClient.disconnect();
- } catch (IOException ex) {
- throw new FTPException("Error disconnect from the server: "
- + ex.getMessage());
- }
- }
- }
-
- /**
- * Return InputStream of the remote file on the server.
- */
- public InputStream getInputStream() {
- return inputStream;
- }
-
- public OutputStream getOutputStream() {
- return outputStream;
- }
-
-} \ No newline at end of file
diff --git a/dozentenmodul/src/main/java/ftp/UploadTask.java b/dozentenmodul/src/main/java/ftp/UploadTask.java
index 61f81c3b..7069db29 100644
--- a/dozentenmodul/src/main/java/ftp/UploadTask.java
+++ b/dozentenmodul/src/main/java/ftp/UploadTask.java
@@ -1,15 +1,17 @@
package ftp;
import java.io.File;
-import java.io.FileInputStream;
+import java.io.IOException;
import javax.swing.JOptionPane;
import javax.swing.SwingWorker;
-import org.apache.log4j.Logger;
-
import models.Image;
+import org.apache.log4j.Logger;
+import org.openslx.filetransfer.UploadStatusCallback;
+import org.openslx.filetransfer.Uploader;
+
/**
* Executes the file upload in a background thread and updates progress to
* listeners that implement the java.beans.PropertyChangeListener interface.
@@ -24,26 +26,21 @@ public class UploadTask extends SwingWorker<Void, Void> {
*/
private final static Logger LOGGER = Logger.getLogger(UploadTask.class);
- private static final int BUFFER_SIZE = 1024 * 1024;
private static final double UPDATE_INTERVAL_SECONDS = 0.6;
private static final double UPDATE_INTERVAL_MS = UPDATE_INTERVAL_SECONDS * 1000;
private static final double BYTES_PER_MIB = 1024 * 1024;
- private String host;
- private int port;
- private String username;
- private String password;
- private String destDir;
- private File uploadFile;
- private int percentCompleted;
+ private final String host;
+ private final int port;
+ private final String uploadToken;
+ private final File uploadFile;
+ private boolean success = false;
- public UploadTask(String host, int port, String username, String password,
- String destDir, File uploadFile) {
+ public UploadTask(String host, int port, String uploadToken, File uploadFile) {
+ // TODO: SSL
this.host = host;
this.port = port;
- this.username = username;
- this.password = password;
- this.destDir = destDir;
+ this.uploadToken = uploadToken;
this.uploadFile = uploadFile;
}
@@ -52,60 +49,63 @@ public class UploadTask extends SwingWorker<Void, Void> {
* Executed in background thread
*/
@Override
- protected Void doInBackground() throws Exception {
- FTPUtility util = new FTPUtility(host, port, username, password);
+ protected Void doInBackground() {
+ Uploader upload = null;
try {
- util.connect();
- util.uploadFile(uploadFile, destDir);
-
- // show filesize in the GUI
- long fileSize = uploadFile.length();
- Image.Filesize = fileSize;
- firePropertyChange("filesize", 0, fileSize);
-
- // prepare input stream
- FileInputStream inputStream = new FileInputStream(uploadFile);
-
- // initialize the counters needed for speed calculations
- percentCompleted = 0;
- byte[] buffer = new byte[BUFFER_SIZE];
- int bytesRead = -1;
- long totalBytesRead = 0;
- long lastUpdate = 0;
- long lastBytes = 0;
- long currentBytes = 0;
- while ((bytesRead = inputStream.read(buffer)) != -1 && !isCancelled()) {
- util.writeFileBytes(buffer, 0, bytesRead);
- currentBytes += bytesRead;
- totalBytesRead += bytesRead;
- long now = System.currentTimeMillis();
- if (lastUpdate + UPDATE_INTERVAL_MS < now) {
- percentCompleted = (int) ((totalBytesRead * 100) / fileSize);
- setProgress(percentCompleted);
- lastBytes = (lastBytes * 2 + currentBytes) / 3;
- final double speed = lastBytes / UPDATE_INTERVAL_SECONDS;
- firePropertyChange("speed", 0, speed / BYTES_PER_MIB);
- firePropertyChange("bytesread", 0, totalBytesRead);
- lastUpdate = now;
- currentBytes = 0;
+ upload = new Uploader(host, port, null, uploadToken); // TODO: SSL
+
+ final Uploader ul = upload;
+ final long fileSize = Image.Filesize = uploadFile.length();
+ firePropertyChange("filesize", 0, fileSize); // Updates GUI
+
+ final boolean ret = upload.upload(uploadFile.getAbsolutePath(), new UploadStatusCallback() {
+ // progress counter
+ private long totalBytesRead = 0;
+ // initialize the counters needed for speed calculations
+ private long lastUpdate = 0;
+ private long lastBytes = 0;
+ private long currentBytes = 0;
+
+ @Override
+ public void uploadProgress(long bytesSent) {
+ totalBytesRead += bytesSent;
+ currentBytes += bytesSent;
+ final long now = System.currentTimeMillis();
+ if (lastUpdate + UPDATE_INTERVAL_MS < now) {
+ final int percentCompleted = (int) ((totalBytesRead * 100) / fileSize);
+ setProgress(percentCompleted);
+ lastBytes = (lastBytes * 2 + currentBytes) / 3;
+ final double speed = lastBytes / UPDATE_INTERVAL_SECONDS;
+ firePropertyChange("speed", 0, speed / BYTES_PER_MIB);
+ firePropertyChange("bytesread", 0, totalBytesRead);
+ lastUpdate = now;
+ currentBytes = 0;
+ }
}
+
+ @Override
+ public void uploadError(String message) {
+ JOptionPane.showMessageDialog(null, "Error uploading file: " + message, "Error",
+ JOptionPane.ERROR_MESSAGE);
+ ul.close(null);
+ }
+ });
+
+ // if the upload succeeded, set the progress to 100% manually again here to make
+ // sure the GUI knows about it.
+ if (ret) {
+ setProgress(100);
+ firePropertyChange("bytesread", 0, uploadFile.length());
+ firePropertyChange("success", false, true);
+ success = true;
}
- // finalize the upload by updating the progress bar one last time
- // (in case we didn't get to do it because of the time interval)
- percentCompleted = (int) ((totalBytesRead * 100) / fileSize);
- setProgress(percentCompleted);
- firePropertyChange("bytesread", 0, totalBytesRead);
- inputStream.close();
- util.finish();
- } catch (FTPException ex) {
- JOptionPane.showMessageDialog(null,
- "Error uploading file: " + ex.getMessage(), "Error",
+ } catch (IOException e) {
+ JOptionPane.showMessageDialog(null, "Error uploading file: " + e.getMessage(), "Error",
JOptionPane.ERROR_MESSAGE);
- ex.printStackTrace();
- setProgress(0);
- cancel(true);
+ LOGGER.error("Upload of " + uploadFile.getAbsolutePath() + " failed!", e);
} finally {
- util.disconnect();
+ if (upload != null)
+ upload.close(null);
}
return null;
@@ -116,14 +116,15 @@ public class UploadTask extends SwingWorker<Void, Void> {
*/
@Override
protected void done() {
- if (!isCancelled() && percentCompleted == 100) {
+ if (isCancelled())
+ return;
+ if (success) {
LOGGER.info("Datei erfolgreich hochgeladen.");
- JOptionPane.showMessageDialog(null, "Datei erfolgreich hochgeladen.",
- "Message", JOptionPane.INFORMATION_MESSAGE);
- } else if (!isCancelled() && percentCompleted != 100) {
+ JOptionPane.showMessageDialog(null, "Datei erfolgreich hochgeladen.", "Message",
+ JOptionPane.INFORMATION_MESSAGE);
+ } else {
LOGGER.error("Datei wurde unvollständig hochgeladen.");
- JOptionPane.showMessageDialog(null,
- "Datei wurde unvollständig hochgeladen. Bitte wiederholen.",
+ JOptionPane.showMessageDialog(null, "Datei wurde unvollständig hochgeladen. Bitte wiederholen.",
"Message", JOptionPane.INFORMATION_MESSAGE);
}
}