From e91e8a30b1f4a806d77d74871df5af6cdb87ca0e Mon Sep 17 00:00:00 2001 From: steckbrief Date: Tue, 23 Aug 2016 09:49:06 +0200 Subject: services.filetransfer.httpupload moved to new namespace services.filetransfer.http.upload; delete parts of services.filetransfer.http moved to .delete package --- .../ConversationsPlusApplication.java | 2 +- .../services/filetransfer/FileTransferManager.java | 2 +- .../filetransfer/http/DeleteRemoteFile.java | 22 --- .../filetransfer/http/DeleteRemoteFileService.java | 30 ----- .../filetransfer/http/DeleteTokenReceived.java | 83 ------------ .../filetransfer/http/delete/DeleteRemoteFile.java | 22 +++ .../http/delete/DeleteRemoteFileService.java | 30 +++++ .../http/delete/DeleteTokenReceived.java | 83 ++++++++++++ .../http/upload/HttpFileTransferEntity.java | 87 ++++++++++++ .../filetransfer/http/upload/HttpFileUploader.java | 149 +++++++++++++++++++++ .../http/upload/HttpUploadFileTransferService.java | 91 +++++++++++++ .../http/upload/HttpUploadSlotRequestReceived.java | 35 +++++ .../HttpUploadedFileEncryptionUiCallback.java | 34 +++++ .../httpupload/HttpFileTransferEntity.java | 87 ------------ .../filetransfer/httpupload/HttpFileUploader.java | 149 --------------------- .../httpupload/HttpUploadFileTransferService.java | 91 ------------- .../httpupload/HttpUploadSlotRequestReceived.java | 35 ----- .../HttpUploadedFileEncryptionUiCallback.java | 34 ----- .../conversationsplus/ui/ConversationFragment.java | 2 +- 19 files changed, 534 insertions(+), 534 deletions(-) delete mode 100644 src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/DeleteRemoteFile.java delete mode 100644 src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/DeleteRemoteFileService.java delete mode 100644 src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/DeleteTokenReceived.java create mode 100644 src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/delete/DeleteRemoteFile.java create mode 100644 src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/delete/DeleteRemoteFileService.java create mode 100644 src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/delete/DeleteTokenReceived.java create mode 100644 src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/upload/HttpFileTransferEntity.java create mode 100644 src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/upload/HttpFileUploader.java create mode 100644 src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/upload/HttpUploadFileTransferService.java create mode 100644 src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/upload/HttpUploadSlotRequestReceived.java create mode 100644 src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/upload/HttpUploadedFileEncryptionUiCallback.java delete mode 100644 src/main/java/de/thedevstack/conversationsplus/services/filetransfer/httpupload/HttpFileTransferEntity.java delete mode 100644 src/main/java/de/thedevstack/conversationsplus/services/filetransfer/httpupload/HttpFileUploader.java delete mode 100644 src/main/java/de/thedevstack/conversationsplus/services/filetransfer/httpupload/HttpUploadFileTransferService.java delete mode 100644 src/main/java/de/thedevstack/conversationsplus/services/filetransfer/httpupload/HttpUploadSlotRequestReceived.java delete mode 100644 src/main/java/de/thedevstack/conversationsplus/services/filetransfer/httpupload/HttpUploadedFileEncryptionUiCallback.java (limited to 'src/main/java/de/thedevstack') diff --git a/src/main/java/de/thedevstack/conversationsplus/ConversationsPlusApplication.java b/src/main/java/de/thedevstack/conversationsplus/ConversationsPlusApplication.java index 8f970a09..d4e3ebc2 100644 --- a/src/main/java/de/thedevstack/conversationsplus/ConversationsPlusApplication.java +++ b/src/main/java/de/thedevstack/conversationsplus/ConversationsPlusApplication.java @@ -14,7 +14,7 @@ import de.thedevstack.conversationsplus.http.HttpClient; import de.thedevstack.conversationsplus.http.HttpConnectionManager; import de.thedevstack.conversationsplus.persistance.FileBackend; import de.thedevstack.conversationsplus.services.filetransfer.FileTransferManager; -import de.thedevstack.conversationsplus.services.filetransfer.httpupload.HttpUploadFileTransferService; +import de.thedevstack.conversationsplus.services.filetransfer.http.upload.HttpUploadFileTransferService; import de.thedevstack.conversationsplus.services.filetransfer.jingle.JingleFileTransferService; import de.thedevstack.conversationsplus.utils.ImageUtil; import de.thedevstack.conversationsplus.utils.PRNGFixes; diff --git a/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/FileTransferManager.java b/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/FileTransferManager.java index 017b88ea..2f9a819b 100644 --- a/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/FileTransferManager.java +++ b/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/FileTransferManager.java @@ -9,7 +9,7 @@ import de.thedevstack.android.logcat.Logging; import de.thedevstack.conversationsplus.Config; import de.thedevstack.conversationsplus.entities.Message; import de.thedevstack.conversationsplus.services.FileTransferService; -import de.thedevstack.conversationsplus.services.filetransfer.httpupload.HttpFileTransferEntity; +import de.thedevstack.conversationsplus.services.filetransfer.http.upload.HttpFileTransferEntity; import de.thedevstack.conversationsplus.utils.MessageUtil; /** diff --git a/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/DeleteRemoteFile.java b/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/DeleteRemoteFile.java deleted file mode 100644 index 9f6b3bbd..00000000 --- a/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/DeleteRemoteFile.java +++ /dev/null @@ -1,22 +0,0 @@ -package de.thedevstack.conversationsplus.services.filetransfer.http; - -import android.support.annotation.NonNull; - -import de.thedevstack.conversationsplus.entities.Message; -import de.thedevstack.conversationsplus.entities.RemoteFile; - -/** - * Created by steckbrief on 22.08.2016. - */ -public class DeleteRemoteFile extends RemoteFile { - private final Message message; - - public DeleteRemoteFile(@NonNull String path, @NonNull Message message) { - super(path); - this.message = message; - } - - public Message getMessage() { - return message; - } -} diff --git a/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/DeleteRemoteFileService.java b/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/DeleteRemoteFileService.java deleted file mode 100644 index 565143be..00000000 --- a/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/DeleteRemoteFileService.java +++ /dev/null @@ -1,30 +0,0 @@ -package de.thedevstack.conversationsplus.services.filetransfer.http; - -import de.thedevstack.conversationsplus.entities.Account; -import de.thedevstack.conversationsplus.entities.Message; -import de.thedevstack.conversationsplus.utils.XmppSendUtil; -import de.thedevstack.conversationsplus.xmpp.filetransfer.http.FileTransferHttp; -import de.thedevstack.conversationsplus.xmpp.filetransfer.http.delete.FileTransferHttpDeleteSlotRequestPacketGenerator; -import de.thedevstack.conversationsplus.xmpp.jid.Jid; -import de.thedevstack.conversationsplus.xmpp.stanzas.IqPacket; - -/** - * Created by steckbrief on 21.08.2016. - */ -public class DeleteRemoteFileService { - public void deleteRemoteFile(Message message) { - if (message.isHttpUploaded()) { - String path = message.getBody(); - if (message.hasFileOnRemoteHost()) { - path = message.getFileParams().url.toString(); - } - - DeleteRemoteFile remoteFile = new DeleteRemoteFile(path, message); - Account account = message.getConversation().getAccount(); - Jid host = account.getXmppConnection().findDiscoItemByFeature(FileTransferHttp.NAMESPACE); - IqPacket request = FileTransferHttpDeleteSlotRequestPacketGenerator.generate(host, path); - - XmppSendUtil.sendIqPacket(account, request, new DeleteTokenReceived(remoteFile)); - } - } -} diff --git a/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/DeleteTokenReceived.java b/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/DeleteTokenReceived.java deleted file mode 100644 index cce0b713..00000000 --- a/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/DeleteTokenReceived.java +++ /dev/null @@ -1,83 +0,0 @@ -package de.thedevstack.conversationsplus.services.filetransfer.http; - -import org.json.JSONException; -import org.json.JSONObject; - -import java.io.IOException; - -import de.thedevstack.android.logcat.Logging; -import de.thedevstack.conversationsplus.entities.Account; -import de.thedevstack.conversationsplus.entities.Message; -import de.thedevstack.conversationsplus.http.HttpClient; -import de.thedevstack.conversationsplus.utils.MessageUtil; -import de.thedevstack.conversationsplus.xmpp.OnIqPacketReceived; -import de.thedevstack.conversationsplus.xmpp.exceptions.XmppException; -import de.thedevstack.conversationsplus.xmpp.filetransfer.http.delete.DeleteSlotPacketParser; -import de.thedevstack.conversationsplus.xmpp.stanzas.IqPacket; -import okhttp3.Call; -import okhttp3.Callback; -import okhttp3.OkHttpClient; -import okhttp3.Request; -import okhttp3.Response; - -/** - * Created by steckbrief on 21.08.2016. - */ -public class DeleteTokenReceived implements OnIqPacketReceived { - private static final String HEADER_NAME_DELETE_TOKEN = "X-FILETRANSFER-HTTP-DELETE-TOKEN"; - private final DeleteRemoteFile remoteFile; - - public DeleteTokenReceived(DeleteRemoteFile remoteFile) { - this.remoteFile = remoteFile; - } - - @Override - public void onIqPacketReceived(Account account, IqPacket packet) { - try { - String url = this.remoteFile.getPath(); - String deleteToken = DeleteSlotPacketParser.parseDeleteToken(packet); - Logging.d("filetransfer.http.delete", "Got delete token '" + deleteToken + "' for remote file '" + remoteFile.getPath() + "'"); - OkHttpClient client = HttpClient.getClient(true); - Request request = new Request.Builder() - .url(url) - .addHeader(HEADER_NAME_DELETE_TOKEN, deleteToken) - .delete() - .build(); - client.newCall(request).enqueue(new Callback() { - @Override - public void onFailure(Call call, IOException e) { - Logging.e("filetransfer.http.delete", "Error while connecting to '" + call.request().url() + "': " + e.getMessage()); - MessageUtil.markMessage(remoteFile.getMessage(), Message.STATUS_REMOTE_FILE_DELETE_FAILED); - } - - @Override - public void onResponse(Call call, Response response) throws IOException { - if (response.isSuccessful()) { - MessageUtil.markMessage(remoteFile.getMessage(), Message.STATUS_REMOTE_FILE_DELETED); - Logging.i("filetransfer.http.delete", "Remote file successfully deleted '" + remoteFile.getPath() + "'"); - } else { - String detailedMessage = response.body().string(); - switch (response.code()) { - case 403: - case 404: - case 500: - try { - JSONObject jsonObject = new JSONObject(detailedMessage); - detailedMessage = jsonObject.getString("msg"); - } catch (JSONException e) { - Logging.e("filetransfer.http.delete", "Failed to get error message from expected json response: " + detailedMessage); - } - break; - } - Logging.e("filetransfer.http.delete", "Could not delete remote file '" + remoteFile.getPath() + "'. Response Code: " + response.code() + ", details: " + detailedMessage); - MessageUtil.markMessage(remoteFile.getMessage(), Message.STATUS_REMOTE_FILE_DELETE_FAILED); - } - } - }); - - } catch (XmppException e) { - Logging.e("filetransfer.http.delete", "Error while trying to get the delete token: " + e.getMessage()); - MessageUtil.markMessage(remoteFile.getMessage(), Message.STATUS_REMOTE_FILE_DELETE_FAILED); - } - } -} diff --git a/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/delete/DeleteRemoteFile.java b/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/delete/DeleteRemoteFile.java new file mode 100644 index 00000000..39a369ef --- /dev/null +++ b/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/delete/DeleteRemoteFile.java @@ -0,0 +1,22 @@ +package de.thedevstack.conversationsplus.services.filetransfer.http.delete; + +import android.support.annotation.NonNull; + +import de.thedevstack.conversationsplus.entities.Message; +import de.thedevstack.conversationsplus.entities.RemoteFile; + +/** + * Created by steckbrief on 22.08.2016. + */ +public class DeleteRemoteFile extends RemoteFile { + private final Message message; + + public DeleteRemoteFile(@NonNull String path, @NonNull Message message) { + super(path); + this.message = message; + } + + public Message getMessage() { + return message; + } +} diff --git a/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/delete/DeleteRemoteFileService.java b/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/delete/DeleteRemoteFileService.java new file mode 100644 index 00000000..42410ed4 --- /dev/null +++ b/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/delete/DeleteRemoteFileService.java @@ -0,0 +1,30 @@ +package de.thedevstack.conversationsplus.services.filetransfer.http.delete; + +import de.thedevstack.conversationsplus.entities.Account; +import de.thedevstack.conversationsplus.entities.Message; +import de.thedevstack.conversationsplus.utils.XmppSendUtil; +import de.thedevstack.conversationsplus.xmpp.filetransfer.http.FileTransferHttp; +import de.thedevstack.conversationsplus.xmpp.filetransfer.http.delete.FileTransferHttpDeleteSlotRequestPacketGenerator; +import de.thedevstack.conversationsplus.xmpp.jid.Jid; +import de.thedevstack.conversationsplus.xmpp.stanzas.IqPacket; + +/** + * Created by steckbrief on 21.08.2016. + */ +public class DeleteRemoteFileService { + public void deleteRemoteFile(Message message) { + if (message.isHttpUploaded()) { + String path = message.getBody(); + if (message.hasFileOnRemoteHost()) { + path = message.getFileParams().url.toString(); + } + + DeleteRemoteFile remoteFile = new DeleteRemoteFile(path, message); + Account account = message.getConversation().getAccount(); + Jid host = account.getXmppConnection().findDiscoItemByFeature(FileTransferHttp.NAMESPACE); + IqPacket request = FileTransferHttpDeleteSlotRequestPacketGenerator.generate(host, path); + + XmppSendUtil.sendIqPacket(account, request, new DeleteTokenReceived(remoteFile)); + } + } +} diff --git a/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/delete/DeleteTokenReceived.java b/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/delete/DeleteTokenReceived.java new file mode 100644 index 00000000..a8bef0ed --- /dev/null +++ b/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/delete/DeleteTokenReceived.java @@ -0,0 +1,83 @@ +package de.thedevstack.conversationsplus.services.filetransfer.http.delete; + +import org.json.JSONException; +import org.json.JSONObject; + +import java.io.IOException; + +import de.thedevstack.android.logcat.Logging; +import de.thedevstack.conversationsplus.entities.Account; +import de.thedevstack.conversationsplus.entities.Message; +import de.thedevstack.conversationsplus.http.HttpClient; +import de.thedevstack.conversationsplus.utils.MessageUtil; +import de.thedevstack.conversationsplus.xmpp.OnIqPacketReceived; +import de.thedevstack.conversationsplus.xmpp.exceptions.XmppException; +import de.thedevstack.conversationsplus.xmpp.filetransfer.http.delete.DeleteSlotPacketParser; +import de.thedevstack.conversationsplus.xmpp.stanzas.IqPacket; +import okhttp3.Call; +import okhttp3.Callback; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; + +/** + * Created by steckbrief on 21.08.2016. + */ +public class DeleteTokenReceived implements OnIqPacketReceived { + private static final String HEADER_NAME_DELETE_TOKEN = "X-FILETRANSFER-HTTP-DELETE-TOKEN"; + private final DeleteRemoteFile remoteFile; + + public DeleteTokenReceived(DeleteRemoteFile remoteFile) { + this.remoteFile = remoteFile; + } + + @Override + public void onIqPacketReceived(Account account, IqPacket packet) { + try { + String url = this.remoteFile.getPath(); + String deleteToken = DeleteSlotPacketParser.parseDeleteToken(packet); + Logging.d("filetransfer.http.delete", "Got delete token '" + deleteToken + "' for remote file '" + remoteFile.getPath() + "'"); + OkHttpClient client = HttpClient.getClient(true); + Request request = new Request.Builder() + .url(url) + .addHeader(HEADER_NAME_DELETE_TOKEN, deleteToken) + .delete() + .build(); + client.newCall(request).enqueue(new Callback() { + @Override + public void onFailure(Call call, IOException e) { + Logging.e("filetransfer.http.delete", "Error while connecting to '" + call.request().url() + "': " + e.getMessage()); + MessageUtil.markMessage(remoteFile.getMessage(), Message.STATUS_REMOTE_FILE_DELETE_FAILED); + } + + @Override + public void onResponse(Call call, Response response) throws IOException { + if (response.isSuccessful()) { + MessageUtil.markMessage(remoteFile.getMessage(), Message.STATUS_REMOTE_FILE_DELETED); + Logging.i("filetransfer.http.delete", "Remote file successfully deleted '" + remoteFile.getPath() + "'"); + } else { + String detailedMessage = response.body().string(); + switch (response.code()) { + case 403: + case 404: + case 500: + try { + JSONObject jsonObject = new JSONObject(detailedMessage); + detailedMessage = jsonObject.getString("msg"); + } catch (JSONException e) { + Logging.e("filetransfer.http.delete", "Failed to get error message from expected json response: " + detailedMessage); + } + break; + } + Logging.e("filetransfer.http.delete", "Could not delete remote file '" + remoteFile.getPath() + "'. Response Code: " + response.code() + ", details: " + detailedMessage); + MessageUtil.markMessage(remoteFile.getMessage(), Message.STATUS_REMOTE_FILE_DELETE_FAILED); + } + } + }); + + } catch (XmppException e) { + Logging.e("filetransfer.http.delete", "Error while trying to get the delete token: " + e.getMessage()); + MessageUtil.markMessage(remoteFile.getMessage(), Message.STATUS_REMOTE_FILE_DELETE_FAILED); + } + } +} diff --git a/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/upload/HttpFileTransferEntity.java b/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/upload/HttpFileTransferEntity.java new file mode 100644 index 00000000..fbe7186b --- /dev/null +++ b/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/upload/HttpFileTransferEntity.java @@ -0,0 +1,87 @@ +package de.thedevstack.conversationsplus.services.filetransfer.http.upload; + +import java.net.MalformedURLException; +import java.net.URL; + +import de.thedevstack.android.logcat.Logging; +import de.thedevstack.conversationsplus.Config; +import de.thedevstack.conversationsplus.ConversationsPlusApplication; +import de.thedevstack.conversationsplus.entities.Message; +import de.thedevstack.conversationsplus.services.filetransfer.FileTransferEntity; +import de.thedevstack.conversationsplus.services.filetransfer.FileTransferFailureReason; +import de.thedevstack.conversationsplus.utils.CryptoHelper; +import de.thedevstack.conversationsplus.utils.MessageUtil; +import de.thedevstack.conversationsplus.xmpp.filetransfer.http.upload.HttpUploadSlot; + +/** + * + */ +public class HttpFileTransferEntity extends FileTransferEntity { + private HttpUploadSlot slot; + private final byte[] key; + private final boolean delayed; + + public HttpFileTransferEntity(Message message, boolean delayed) { + super(message); + this.getMessage().setHttpUploaded(true); + this.getMessage().setNoDownloadable(); + if (Config.ENCRYPT_ON_HTTP_UPLOADED + || message.getEncryption() == Message.ENCRYPTION_AXOLOTL + || message.getEncryption() == Message.ENCRYPTION_OTR) { + this.key = new byte[48]; + ConversationsPlusApplication.getSecureRandom().nextBytes(this.key); + this.getFile().setKeyAndIv(this.key); + } else { + this.key = null; + } + this.delayed = delayed; + } + + public void setSlot(HttpUploadSlot slot) { + this.slot = slot; + } + + public String getGetUrl() { + return this.slot.getGetUrl(); + } + + public String getPutUrl() { + return this.slot.getPutUrl(); + } + + public byte[] getKey() { + return key; + } + + public boolean isDelayed() { + return this.delayed; + } + + @Override + public void fail(FileTransferFailureReason failureReason) { + this.getMessage().setHttpUploaded(false); + super.fail(failureReason); + } + + @Override + public void cancel() { + this.getMessage().setHttpUploaded(false); + super.cancel(); + } + + @Override + public void transferred() { + try { + URL getUrl = new URL(this.getGetUrl()); + if (this.getKey() != null) { + getUrl = new URL(getUrl.toString() + "#" + CryptoHelper.bytesToHex(this.getKey())); + } + + MessageUtil.updateFileParams(this.getMessage(), getUrl); + } catch (MalformedURLException e) { + Logging.e("httpupload", "Not a valid get url"); + } + + super.transferred(); + } +} diff --git a/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/upload/HttpFileUploader.java b/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/upload/HttpFileUploader.java new file mode 100644 index 00000000..318a8b8f --- /dev/null +++ b/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/upload/HttpFileUploader.java @@ -0,0 +1,149 @@ +package de.thedevstack.conversationsplus.services.filetransfer.http.upload; + +import android.os.PowerManager; + +import java.io.IOException; +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; + +import de.thedevstack.android.logcat.Logging; +import de.thedevstack.conversationsplus.ConversationsPlusApplication; +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.StreamUtil; +import de.thedevstack.conversationsplus.utils.UiUpdateHelper; +import de.thedevstack.conversationsplus.utils.XmppConnectionServiceAccessor; +import de.thedevstack.conversationsplus.xmpp.filetransfer.http.upload.HttpUpload; + +public class HttpFileUploader implements Runnable { + private static final String HTTP_METHOD = "PUT"; + private static final String MIME_REQUEST_PROPERTY_NAME = "Content-Type"; + private static final String USER_AGENT_REQUEST_PROPERTY_NAME = "User-Agent"; + private static final int BUFFER_LENGTH = 4096; + private final HttpFileTransferEntity entity; + + public HttpFileUploader(HttpFileTransferEntity entity) { + this.entity = entity; + } + + @Override + public void run() { + this.upload(); + } + + private void upload() { + OutputStream os = 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("httpupload", "uploading file to " + this.entity.getPutUrl()); + URL putUrl = new URL(this.entity.getPutUrl()); + fileInputStream = this.entity.getFileInputStream(); + connection = (HttpURLConnection) putUrl.openConnection(); + + if (connection instanceof HttpsURLConnection) { + HttpConnectionManager.setupTrustManager((HttpsURLConnection) connection, true); + } + connection.setRequestMethod(HTTP_METHOD); + connection.setFixedLengthStreamingMode((int) file.getExpectedSize()); + 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); + connection.connect(); + os = connection.getOutputStream(); + int count = -1; + byte[] buffer = new byte[BUFFER_LENGTH]; + while (((count = fileInputStream.read(buffer)) != -1) && !this.entity.isCanceled()) { + this.entity.updateProgress(count); + os.write(buffer, 0, count); + UiUpdateHelper.updateConversationUi(); + } + os.flush(); + os.close(); + fileInputStream.close(); + int code = connection.getResponseCode(); + if (code == 200 || code == 201) { + Logging.d("httpupload", "finished uploading file"); + this.entity.transferred(); + + FileBackend.updateMediaScanner(file, XmppConnectionServiceAccessor.xmppConnectionService); // Why??? + + // Send the URL to the counterpart + Message message = this.entity.getMessage(); + message.setCounterpart(message.getConversation().getJid().toBareJid()); + if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) { + XmppConnectionServiceAccessor.xmppConnectionService.getPgpEngine().encrypt(message, new HttpUploadedFileEncryptionUiCallback(this.entity)); + } else { + XmppConnectionServiceAccessor.xmppConnectionService.resendMessage(message, this.entity.isDelayed()); + } + } else { + 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); + } + 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()); + // TODO: Differentiate IOException while internet connection wasn't available and others + this.entity.fail(FileTransferFailureReason.createRecoverableFailureReason(e, httpResponseMessage)); + } finally { + StreamUtil.close(os); + StreamUtil.close(fileInputStream); + if (connection != null) { + connection.disconnect(); + } + 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 diff --git a/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/upload/HttpUploadFileTransferService.java b/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/upload/HttpUploadFileTransferService.java new file mode 100644 index 00000000..f08e27f5 --- /dev/null +++ b/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/upload/HttpUploadFileTransferService.java @@ -0,0 +1,91 @@ +package de.thedevstack.conversationsplus.services.filetransfer.http.upload; + +import android.util.Pair; + +import java.io.FileNotFoundException; +import java.io.InputStream; + +import de.thedevstack.android.logcat.Logging; +import de.thedevstack.conversationsplus.entities.Account; +import de.thedevstack.conversationsplus.entities.DownloadableFile; +import de.thedevstack.conversationsplus.entities.Message; +import de.thedevstack.conversationsplus.persistance.FileBackend; +import de.thedevstack.conversationsplus.services.AbstractConnectionManager; +import de.thedevstack.conversationsplus.services.FileTransferService; +import de.thedevstack.conversationsplus.services.filetransfer.AbstractFileTransferService; +import de.thedevstack.conversationsplus.utils.MessageUtil; +import de.thedevstack.conversationsplus.utils.XmppSendUtil; +import de.thedevstack.conversationsplus.xmpp.filetransfer.http.upload.HttpUpload; +import de.thedevstack.conversationsplus.xmpp.filetransfer.http.upload.HttpUploadRequestSlotPacketGenerator; +import de.thedevstack.conversationsplus.xmpp.jid.Jid; +import de.thedevstack.conversationsplus.xmpp.stanzas.IqPacket; + +/** + * + */ +public class HttpUploadFileTransferService extends AbstractFileTransferService implements FileTransferService { + + public HttpUploadFileTransferService() { + } + + /** + * Transfers a file for the corresponding message. + * + * @param message the message containing the file to transfer + * @return true if the file transfer was started successfully, false otherwise + */ + @Override + public boolean transferFile(Message message) { + return this.transferFile(message, false); + } + + /** + * Transfers a file for the corresponding message. + * + * @param message the message containing the file to transfer + * @param delay whether the message is delayed or not + * @return true if the file transfer was started successfully, false otherwise + */ + @Override + public boolean transferFile(Message message, boolean delay) { + Logging.d("httpupload", "Starting to upload file"); + boolean started = false; + try { + final HttpFileTransferEntity entity = new HttpFileTransferEntity(message, delay); + this.addStatusListenerToEntity(entity); + entity.startAttempt(); + Account account = message.getConversation().getAccount(); + DownloadableFile file = entity.getFile(); + Pair inputStreamAndExpectedSize = AbstractConnectionManager.createInputStream(file, true); + + entity.setFileInputStream(inputStreamAndExpectedSize.first); + file.setExpectedSize(inputStreamAndExpectedSize.second); + + Logging.d("httpupload", "Requesting upload slot for file upload"); + Jid host = account.getXmppConnection().findDiscoItemByFeature(HttpUpload.NAMESPACE); + IqPacket request = HttpUploadRequestSlotPacketGenerator.generate(host, file.getName(), file.getSize(), file.getMimeType()); + XmppSendUtil.sendIqPacket(account, request, new HttpUploadSlotRequestReceived(entity)); + MessageUtil.markMessage(message, Message.STATUS_UNSEND); + + Logging.d("httpupload", "Upload slot for file upload requested"); + started = true; + } catch (FileNotFoundException e) { + Logging.e("httpupload", "Could not find file, exception message: " + e.getMessage()); + } + return started; + } + + /** + * Checks whether a message can be sent using this service or not. + * + * @param message the message to be checked + * @return true if the message can be processed, false otherwise + */ + @Override + public boolean accept(Message message) { + return null != message + && null != message.getConversation() + && null != message.getConversation().getAccount() + && message.getConversation().getAccount().httpUploadAvailable(FileBackend.getFile(message, false).getSize()); + } +} diff --git a/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/upload/HttpUploadSlotRequestReceived.java b/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/upload/HttpUploadSlotRequestReceived.java new file mode 100644 index 00000000..2687878d --- /dev/null +++ b/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/upload/HttpUploadSlotRequestReceived.java @@ -0,0 +1,35 @@ +package de.thedevstack.conversationsplus.services.filetransfer.http.upload; + +import de.thedevstack.android.logcat.Logging; +import de.thedevstack.conversationsplus.entities.Account; +import de.thedevstack.conversationsplus.services.filetransfer.FileTransferFailureReason; +import de.thedevstack.conversationsplus.xmpp.OnIqPacketReceived; +import de.thedevstack.conversationsplus.xmpp.exceptions.XmppException; +import de.thedevstack.conversationsplus.xmpp.filetransfer.http.upload.HttpUploadSlot; +import de.thedevstack.conversationsplus.xmpp.filetransfer.http.upload.SlotPacketParser; +import de.thedevstack.conversationsplus.xmpp.stanzas.IqPacket; + +/** + * + */ +public class HttpUploadSlotRequestReceived implements OnIqPacketReceived { + private final HttpFileTransferEntity entity; + + public HttpUploadSlotRequestReceived(HttpFileTransferEntity entity) { + this.entity = entity; + } + + @Override + public void onIqPacketReceived(Account account, IqPacket packet) { + try { + HttpUploadSlot slot = SlotPacketParser.parseGetAndPutUrl(packet); + this.entity.setSlot(slot); + if (!this.entity.isCanceled()) { + new Thread(new HttpFileUploader(this.entity)).start(); + } + } catch (XmppException e) { + Logging.e("httpupload", e.getMessage()); + this.entity.fail(FileTransferFailureReason.createNonRecoverableFailureReason(e)); + } + } +} diff --git a/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/upload/HttpUploadedFileEncryptionUiCallback.java b/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/upload/HttpUploadedFileEncryptionUiCallback.java new file mode 100644 index 00000000..f363a675 --- /dev/null +++ b/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/upload/HttpUploadedFileEncryptionUiCallback.java @@ -0,0 +1,34 @@ +package de.thedevstack.conversationsplus.services.filetransfer.http.upload; + +import android.app.PendingIntent; + +import de.thedevstack.conversationsplus.entities.Message; +import de.thedevstack.conversationsplus.services.filetransfer.FileTransferFailureReason; +import de.thedevstack.conversationsplus.ui.UiCallback; +import de.thedevstack.conversationsplus.utils.XmppConnectionServiceAccessor; + +/** + * + */ +public class HttpUploadedFileEncryptionUiCallback implements UiCallback { + private final HttpFileTransferEntity entity; + + public HttpUploadedFileEncryptionUiCallback(HttpFileTransferEntity entity) { + this.entity = entity; + } + + @Override + public void success(Message message) { + XmppConnectionServiceAccessor.xmppConnectionService.resendMessage(message, this.entity.isDelayed()); + } + + @Override + public void error(int errorCode, Message object) { + this.entity.fail(FileTransferFailureReason.createLimitedRecoverableFailureReason("Failed to encrypt")); + } + + @Override + public void userInputRequried(PendingIntent pi, Message object) { + this.entity.fail(FileTransferFailureReason.createLimitedRecoverableFailureReason("Failed to encrypt, user input would have been required")); + } +} diff --git a/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/httpupload/HttpFileTransferEntity.java b/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/httpupload/HttpFileTransferEntity.java deleted file mode 100644 index f6596b0c..00000000 --- a/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/httpupload/HttpFileTransferEntity.java +++ /dev/null @@ -1,87 +0,0 @@ -package de.thedevstack.conversationsplus.services.filetransfer.httpupload; - -import java.net.MalformedURLException; -import java.net.URL; - -import de.thedevstack.android.logcat.Logging; -import de.thedevstack.conversationsplus.Config; -import de.thedevstack.conversationsplus.ConversationsPlusApplication; -import de.thedevstack.conversationsplus.entities.Message; -import de.thedevstack.conversationsplus.services.filetransfer.FileTransferEntity; -import de.thedevstack.conversationsplus.services.filetransfer.FileTransferFailureReason; -import de.thedevstack.conversationsplus.utils.CryptoHelper; -import de.thedevstack.conversationsplus.utils.MessageUtil; -import de.thedevstack.conversationsplus.xmpp.filetransfer.http.upload.HttpUploadSlot; - -/** - * - */ -public class HttpFileTransferEntity extends FileTransferEntity { - private HttpUploadSlot slot; - private final byte[] key; - private final boolean delayed; - - public HttpFileTransferEntity(Message message, boolean delayed) { - super(message); - this.getMessage().setHttpUploaded(true); - this.getMessage().setNoDownloadable(); - if (Config.ENCRYPT_ON_HTTP_UPLOADED - || message.getEncryption() == Message.ENCRYPTION_AXOLOTL - || message.getEncryption() == Message.ENCRYPTION_OTR) { - this.key = new byte[48]; - ConversationsPlusApplication.getSecureRandom().nextBytes(this.key); - this.getFile().setKeyAndIv(this.key); - } else { - this.key = null; - } - this.delayed = delayed; - } - - public void setSlot(HttpUploadSlot slot) { - this.slot = slot; - } - - public String getGetUrl() { - return this.slot.getGetUrl(); - } - - public String getPutUrl() { - return this.slot.getPutUrl(); - } - - public byte[] getKey() { - return key; - } - - public boolean isDelayed() { - return this.delayed; - } - - @Override - public void fail(FileTransferFailureReason failureReason) { - this.getMessage().setHttpUploaded(false); - super.fail(failureReason); - } - - @Override - public void cancel() { - this.getMessage().setHttpUploaded(false); - super.cancel(); - } - - @Override - public void transferred() { - try { - URL getUrl = new URL(this.getGetUrl()); - if (this.getKey() != null) { - getUrl = new URL(getUrl.toString() + "#" + CryptoHelper.bytesToHex(this.getKey())); - } - - MessageUtil.updateFileParams(this.getMessage(), getUrl); - } catch (MalformedURLException e) { - Logging.e("httpupload", "Not a valid get url"); - } - - super.transferred(); - } -} 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 deleted file mode 100644 index 705dd23c..00000000 --- a/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/httpupload/HttpFileUploader.java +++ /dev/null @@ -1,149 +0,0 @@ -package de.thedevstack.conversationsplus.services.filetransfer.httpupload; - -import android.os.PowerManager; - -import java.io.IOException; -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; - -import de.thedevstack.android.logcat.Logging; -import de.thedevstack.conversationsplus.ConversationsPlusApplication; -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.StreamUtil; -import de.thedevstack.conversationsplus.utils.UiUpdateHelper; -import de.thedevstack.conversationsplus.utils.XmppConnectionServiceAccessor; -import de.thedevstack.conversationsplus.xmpp.filetransfer.http.upload.HttpUpload; - -public class HttpFileUploader implements Runnable { - private static final String HTTP_METHOD = "PUT"; - private static final String MIME_REQUEST_PROPERTY_NAME = "Content-Type"; - private static final String USER_AGENT_REQUEST_PROPERTY_NAME = "User-Agent"; - private static final int BUFFER_LENGTH = 4096; - private final HttpFileTransferEntity entity; - - public HttpFileUploader(HttpFileTransferEntity entity) { - this.entity = entity; - } - - @Override - public void run() { - this.upload(); - } - - private void upload() { - OutputStream os = 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("httpupload", "uploading file to " + this.entity.getPutUrl()); - URL putUrl = new URL(this.entity.getPutUrl()); - fileInputStream = this.entity.getFileInputStream(); - connection = (HttpURLConnection) putUrl.openConnection(); - - if (connection instanceof HttpsURLConnection) { - HttpConnectionManager.setupTrustManager((HttpsURLConnection) connection, true); - } - connection.setRequestMethod(HTTP_METHOD); - connection.setFixedLengthStreamingMode((int) file.getExpectedSize()); - 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); - connection.connect(); - os = connection.getOutputStream(); - int count = -1; - byte[] buffer = new byte[BUFFER_LENGTH]; - while (((count = fileInputStream.read(buffer)) != -1) && !this.entity.isCanceled()) { - this.entity.updateProgress(count); - os.write(buffer, 0, count); - UiUpdateHelper.updateConversationUi(); - } - os.flush(); - os.close(); - fileInputStream.close(); - int code = connection.getResponseCode(); - if (code == 200 || code == 201) { - Logging.d("httpupload", "finished uploading file"); - this.entity.transferred(); - - FileBackend.updateMediaScanner(file, XmppConnectionServiceAccessor.xmppConnectionService); // Why??? - - // Send the URL to the counterpart - Message message = this.entity.getMessage(); - message.setCounterpart(message.getConversation().getJid().toBareJid()); - if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) { - XmppConnectionServiceAccessor.xmppConnectionService.getPgpEngine().encrypt(message, new HttpUploadedFileEncryptionUiCallback(this.entity)); - } else { - XmppConnectionServiceAccessor.xmppConnectionService.resendMessage(message, this.entity.isDelayed()); - } - } else { - 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); - } - 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()); - // TODO: Differentiate IOException while internet connection wasn't available and others - this.entity.fail(FileTransferFailureReason.createRecoverableFailureReason(e, httpResponseMessage)); - } finally { - StreamUtil.close(os); - StreamUtil.close(fileInputStream); - if (connection != null) { - connection.disconnect(); - } - 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 diff --git a/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/httpupload/HttpUploadFileTransferService.java b/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/httpupload/HttpUploadFileTransferService.java deleted file mode 100644 index 6fd458a9..00000000 --- a/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/httpupload/HttpUploadFileTransferService.java +++ /dev/null @@ -1,91 +0,0 @@ -package de.thedevstack.conversationsplus.services.filetransfer.httpupload; - -import android.util.Pair; - -import java.io.FileNotFoundException; -import java.io.InputStream; - -import de.thedevstack.android.logcat.Logging; -import de.thedevstack.conversationsplus.entities.Account; -import de.thedevstack.conversationsplus.entities.DownloadableFile; -import de.thedevstack.conversationsplus.entities.Message; -import de.thedevstack.conversationsplus.persistance.FileBackend; -import de.thedevstack.conversationsplus.services.AbstractConnectionManager; -import de.thedevstack.conversationsplus.services.FileTransferService; -import de.thedevstack.conversationsplus.services.filetransfer.AbstractFileTransferService; -import de.thedevstack.conversationsplus.utils.MessageUtil; -import de.thedevstack.conversationsplus.utils.XmppSendUtil; -import de.thedevstack.conversationsplus.xmpp.filetransfer.http.upload.HttpUpload; -import de.thedevstack.conversationsplus.xmpp.filetransfer.http.upload.HttpUploadRequestSlotPacketGenerator; -import de.thedevstack.conversationsplus.xmpp.jid.Jid; -import de.thedevstack.conversationsplus.xmpp.stanzas.IqPacket; - -/** - * - */ -public class HttpUploadFileTransferService extends AbstractFileTransferService implements FileTransferService { - - public HttpUploadFileTransferService() { - } - - /** - * Transfers a file for the corresponding message. - * - * @param message the message containing the file to transfer - * @return true if the file transfer was started successfully, false otherwise - */ - @Override - public boolean transferFile(Message message) { - return this.transferFile(message, false); - } - - /** - * Transfers a file for the corresponding message. - * - * @param message the message containing the file to transfer - * @param delay whether the message is delayed or not - * @return true if the file transfer was started successfully, false otherwise - */ - @Override - public boolean transferFile(Message message, boolean delay) { - Logging.d("httpupload", "Starting to upload file"); - boolean started = false; - try { - final HttpFileTransferEntity entity = new HttpFileTransferEntity(message, delay); - this.addStatusListenerToEntity(entity); - entity.startAttempt(); - Account account = message.getConversation().getAccount(); - DownloadableFile file = entity.getFile(); - Pair inputStreamAndExpectedSize = AbstractConnectionManager.createInputStream(file, true); - - entity.setFileInputStream(inputStreamAndExpectedSize.first); - file.setExpectedSize(inputStreamAndExpectedSize.second); - - Logging.d("httpupload", "Requesting upload slot for file upload"); - Jid host = account.getXmppConnection().findDiscoItemByFeature(HttpUpload.NAMESPACE); - IqPacket request = HttpUploadRequestSlotPacketGenerator.generate(host, file.getName(), file.getSize(), file.getMimeType()); - XmppSendUtil.sendIqPacket(account, request, new HttpUploadSlotRequestReceived(entity)); - MessageUtil.markMessage(message, Message.STATUS_UNSEND); - - Logging.d("httpupload", "Upload slot for file upload requested"); - started = true; - } catch (FileNotFoundException e) { - Logging.e("httpupload", "Could not find file, exception message: " + e.getMessage()); - } - return started; - } - - /** - * Checks whether a message can be sent using this service or not. - * - * @param message the message to be checked - * @return true if the message can be processed, false otherwise - */ - @Override - public boolean accept(Message message) { - return null != message - && null != message.getConversation() - && null != message.getConversation().getAccount() - && message.getConversation().getAccount().httpUploadAvailable(FileBackend.getFile(message, false).getSize()); - } -} diff --git a/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/httpupload/HttpUploadSlotRequestReceived.java b/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/httpupload/HttpUploadSlotRequestReceived.java deleted file mode 100644 index 75e793f1..00000000 --- a/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/httpupload/HttpUploadSlotRequestReceived.java +++ /dev/null @@ -1,35 +0,0 @@ -package de.thedevstack.conversationsplus.services.filetransfer.httpupload; - -import de.thedevstack.android.logcat.Logging; -import de.thedevstack.conversationsplus.entities.Account; -import de.thedevstack.conversationsplus.services.filetransfer.FileTransferFailureReason; -import de.thedevstack.conversationsplus.xmpp.OnIqPacketReceived; -import de.thedevstack.conversationsplus.xmpp.exceptions.XmppException; -import de.thedevstack.conversationsplus.xmpp.filetransfer.http.upload.HttpUploadSlot; -import de.thedevstack.conversationsplus.xmpp.filetransfer.http.upload.SlotPacketParser; -import de.thedevstack.conversationsplus.xmpp.stanzas.IqPacket; - -/** - * - */ -public class HttpUploadSlotRequestReceived implements OnIqPacketReceived { - private final HttpFileTransferEntity entity; - - public HttpUploadSlotRequestReceived(HttpFileTransferEntity entity) { - this.entity = entity; - } - - @Override - public void onIqPacketReceived(Account account, IqPacket packet) { - try { - HttpUploadSlot slot = SlotPacketParser.parseGetAndPutUrl(packet); - this.entity.setSlot(slot); - if (!this.entity.isCanceled()) { - new Thread(new HttpFileUploader(this.entity)).start(); - } - } catch (XmppException e) { - Logging.e("httpupload", e.getMessage()); - this.entity.fail(FileTransferFailureReason.createNonRecoverableFailureReason(e)); - } - } -} diff --git a/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/httpupload/HttpUploadedFileEncryptionUiCallback.java b/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/httpupload/HttpUploadedFileEncryptionUiCallback.java deleted file mode 100644 index e3935252..00000000 --- a/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/httpupload/HttpUploadedFileEncryptionUiCallback.java +++ /dev/null @@ -1,34 +0,0 @@ -package de.thedevstack.conversationsplus.services.filetransfer.httpupload; - -import android.app.PendingIntent; - -import de.thedevstack.conversationsplus.entities.Message; -import de.thedevstack.conversationsplus.services.filetransfer.FileTransferFailureReason; -import de.thedevstack.conversationsplus.ui.UiCallback; -import de.thedevstack.conversationsplus.utils.XmppConnectionServiceAccessor; - -/** - * - */ -public class HttpUploadedFileEncryptionUiCallback implements UiCallback { - private final HttpFileTransferEntity entity; - - public HttpUploadedFileEncryptionUiCallback(HttpFileTransferEntity entity) { - this.entity = entity; - } - - @Override - public void success(Message message) { - XmppConnectionServiceAccessor.xmppConnectionService.resendMessage(message, this.entity.isDelayed()); - } - - @Override - public void error(int errorCode, Message object) { - this.entity.fail(FileTransferFailureReason.createLimitedRecoverableFailureReason("Failed to encrypt")); - } - - @Override - public void userInputRequried(PendingIntent pi, Message object) { - this.entity.fail(FileTransferFailureReason.createLimitedRecoverableFailureReason("Failed to encrypt, user input would have been required")); - } -} diff --git a/src/main/java/de/thedevstack/conversationsplus/ui/ConversationFragment.java b/src/main/java/de/thedevstack/conversationsplus/ui/ConversationFragment.java index 934f16b6..c3d7c962 100644 --- a/src/main/java/de/thedevstack/conversationsplus/ui/ConversationFragment.java +++ b/src/main/java/de/thedevstack/conversationsplus/ui/ConversationFragment.java @@ -45,7 +45,7 @@ import java.util.List; import de.thedevstack.conversationsplus.ConversationsPlusPreferences; import de.thedevstack.conversationsplus.http.HttpConnectionManager; import de.thedevstack.conversationsplus.http.HttpDownloadConnection; -import de.thedevstack.conversationsplus.services.filetransfer.http.DeleteRemoteFileService; +import de.thedevstack.conversationsplus.services.filetransfer.http.delete.DeleteRemoteFileService; import de.thedevstack.conversationsplus.ui.dialogs.MessageDetailsDialog; import de.thedevstack.conversationsplus.Config; import de.thedevstack.conversationsplus.R; -- cgit v1.2.3