aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/httpupload/HttpFileUploader.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/de/thedevstack/conversationsplus/services/filetransfer/httpupload/HttpFileUploader.java')
-rw-r--r--src/main/java/de/thedevstack/conversationsplus/services/filetransfer/httpupload/HttpFileUploader.java74
1 files changed, 53 insertions, 21 deletions
diff --git a/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/httpupload/HttpFileUploader.java b/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/httpupload/HttpFileUploader.java
index 6352c7a7..8edc5be7 100644
--- a/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/httpupload/HttpFileUploader.java
+++ b/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/httpupload/HttpFileUploader.java
@@ -7,6 +7,7 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
+import java.net.UnknownHostException;
import java.util.Scanner;
import javax.net.ssl.HttpsURLConnection;
@@ -18,6 +19,7 @@ import de.thedevstack.conversationsplus.entities.DownloadableFile;
import de.thedevstack.conversationsplus.entities.Message;
import de.thedevstack.conversationsplus.http.HttpConnectionManager;
import de.thedevstack.conversationsplus.persistance.FileBackend;
+import de.thedevstack.conversationsplus.services.filetransfer.FileTransferFailureReason;
import de.thedevstack.conversationsplus.utils.CryptoHelper;
import de.thedevstack.conversationsplus.utils.MessageUtil;
import de.thedevstack.conversationsplus.utils.StreamUtil;
@@ -43,14 +45,14 @@ public class HttpFileUploader implements Runnable {
private void upload() {
OutputStream os = null;
- InputStream errorStream = null;
+
HttpURLConnection connection = null;
InputStream fileInputStream = null;
DownloadableFile file = this.entity.getFile();
PowerManager.WakeLock wakeLock = ConversationsPlusApplication.createPartialWakeLock("http_upload_" + entity.getMessage().getUuid());
try {
wakeLock.acquire();
- Logging.d(Config.LOGTAG, "uploading to " + this.entity.getPutUrl());
+ Logging.d("httpupload", "uploading file to " + this.entity.getPutUrl());
URL putUrl = new URL(this.entity.getPutUrl());
fileInputStream = this.entity.getFileInputStream();
connection = (HttpURLConnection) putUrl.openConnection();
@@ -60,7 +62,7 @@ public class HttpFileUploader implements Runnable {
}
connection.setRequestMethod(HTTP_METHOD);
connection.setFixedLengthStreamingMode((int) file.getExpectedSize());
- String mime = this.entity.getFile().getMimeType();
+ String mime = file.getMimeType();
connection.setRequestProperty(MIME_REQUEST_PROPERTY_NAME, mime == null ? HttpUpload.DEFAULT_MIME_TYPE : mime);
connection.setRequestProperty(USER_AGENT_REQUEST_PROPERTY_NAME, ConversationsPlusApplication.getNameAndVersion());
connection.setDoOutput(true);
@@ -78,16 +80,13 @@ public class HttpFileUploader implements Runnable {
fileInputStream.close();
int code = connection.getResponseCode();
if (code == 200 || code == 201) {
- Logging.d(Config.LOGTAG, "finished uploading file");
+ Logging.d("httpupload", "finished uploading file");
this.entity.transferred();
- URL getUrl = new URL(this.entity.getGetUrl());
- if (this.entity.getKey() != null) {
- getUrl = new URL(getUrl.toString() + "#" + CryptoHelper.bytesToHex(this.entity.getKey()));
- }
+
+ FileBackend.updateMediaScanner(file, XmppConnectionServiceAccessor.xmppConnectionService); // Why???
+
+ // Send the URL to the counterpart
Message message = this.entity.getMessage();
- MessageUtil.updateFileParams(message, getUrl);
- FileBackend.updateMediaScanner(file, XmppConnectionServiceAccessor.xmppConnectionService);
- message.setTransferable(null);
message.setCounterpart(message.getConversation().getJid().toBareJid());
if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
XmppConnectionServiceAccessor.xmppConnectionService.getPgpEngine().encrypt(message, new HttpUploadedFileEncryptionUiCallback(this.entity));
@@ -95,21 +94,37 @@ public class HttpFileUploader implements Runnable {
XmppConnectionServiceAccessor.xmppConnectionService.resendMessage(message, this.entity.isDelayed());
}
} else {
- errorStream = connection.getErrorStream();
- Logging.e("httpupload", "file upload failed: http code (" + code + ") " + new Scanner(errorStream).useDelimiter("\\A").next());
- this.entity.fail();
+ String httpResponseMessage = this.getErrorStreamContent(connection);
+ String errorMessage = "file upload failed: http code (" + code + ") " + httpResponseMessage;
+ Logging.e("httpupload", errorMessage);
+ FileTransferFailureReason failureReason = null;
+ switch (code) {
+ case 403:
+ failureReason = FileTransferFailureReason.createLimitedRecoverableFailureReason(errorMessage);
+ break;
+ case 404:
+ failureReason = FileTransferFailureReason.createNonRecoverableFailureReason("Upload URL not found");
+ break;
+ case 500:
+ failureReason = FileTransferFailureReason.createNonRecoverableFailureReason("Internal Server Error");
+ break;
+ default:
+ failureReason = FileTransferFailureReason.createRecoverableFailureReason(errorMessage);
}
- } catch (IOException e) {
- errorStream = (null != connection) ? connection.getErrorStream() : null;
- String httpResponseMessage = null;
- if (null != errorStream) {
- httpResponseMessage = new Scanner(errorStream).useDelimiter("\\A").next();
+ this.entity.fail(failureReason);
}
+ } catch (UnknownHostException e) {
+ Logging.e("httpupload", "File upload failed due to unknown host. " + e.getMessage());
+ //if (!HAS_INTERNET_CONNECTION) {
+ this.entity.fail(FileTransferFailureReason.createRecoverableFailureReason(e, "Missing internet connection"));
+ //}
+ } catch (IOException e) {
+ String httpResponseMessage = this.getErrorStreamContent(connection);
Logging.e("httpupload", ((null != httpResponseMessage) ? ("http response: " + httpResponseMessage + ", ") : "") + "exception message: " + e.getMessage());
- this.entity.fail();
+ // TODO: Differentiate IOException while internet connection wasn't available and others
+ this.entity.fail(FileTransferFailureReason.createRecoverableFailureReason(e, httpResponseMessage));
} finally {
StreamUtil.close(os);
- StreamUtil.close(errorStream);
StreamUtil.close(fileInputStream);
if (connection != null) {
connection.disconnect();
@@ -117,4 +132,21 @@ public class HttpFileUploader implements Runnable {
wakeLock.release();
}
}
+
+ private String getErrorStreamContent(HttpURLConnection connection) {
+ InputStream errorStream = null;
+ String httpResponseMessage = null;
+ try {
+ errorStream = connection.getErrorStream();
+ if (null != errorStream) {
+ Scanner scanner = new Scanner(errorStream).useDelimiter("\\A");
+ if (scanner.hasNext()) {
+ httpResponseMessage = scanner.next();
+ }
+ }
+ } finally {
+ StreamUtil.close(errorStream);
+ }
+ return httpResponseMessage;
+ }
} \ No newline at end of file