aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/thedevstack/conversationsplus/http/HttpUploadConnection.java
diff options
context:
space:
mode:
authorlookshe <github@lookshe.org>2015-08-11 20:30:11 +0200
committerlookshe <github@lookshe.org>2015-08-11 20:30:11 +0200
commit639babfdb5289a035e0a22bf607c068caefa5c99 (patch)
tree6a11aa6a189f3a5988d5d1e1c733d2027bc3494f /src/main/java/de/thedevstack/conversationsplus/http/HttpUploadConnection.java
parent1b5966ae3b1108c88a810d7d32a0aefa8812d11f (diff)
parent8fd688ca96005152be754eeba1be72c7c0aab9ad (diff)
Merge branch 'trz/rebase' into trz/rename
Conflicts: build.gradle src/main/java/de/thedevstack/conversationsplus/crypto/OtrEngine.java src/main/java/de/thedevstack/conversationsplus/entities/Account.java src/main/java/de/thedevstack/conversationsplus/entities/Contact.java src/main/java/de/thedevstack/conversationsplus/entities/Downloadable.java src/main/java/de/thedevstack/conversationsplus/entities/DownloadableFile.java src/main/java/de/thedevstack/conversationsplus/entities/DownloadablePlaceholder.java src/main/java/de/thedevstack/conversationsplus/entities/Message.java src/main/java/de/thedevstack/conversationsplus/generator/IqGenerator.java src/main/java/de/thedevstack/conversationsplus/http/HttpConnection.java src/main/java/de/thedevstack/conversationsplus/http/HttpConnectionManager.java src/main/java/de/thedevstack/conversationsplus/parser/AbstractParser.java src/main/java/de/thedevstack/conversationsplus/parser/MessageParser.java src/main/java/de/thedevstack/conversationsplus/parser/PresenceParser.java src/main/java/de/thedevstack/conversationsplus/persistance/DatabaseBackend.java src/main/java/de/thedevstack/conversationsplus/persistance/FileBackend.java src/main/java/de/thedevstack/conversationsplus/services/NotificationService.java src/main/java/de/thedevstack/conversationsplus/services/XmppConnectionService.java src/main/java/de/thedevstack/conversationsplus/ui/ConversationActivity.java src/main/java/de/thedevstack/conversationsplus/ui/ConversationFragment.java src/main/java/de/thedevstack/conversationsplus/ui/SettingsActivity.java src/main/java/de/thedevstack/conversationsplus/ui/StartConversationActivity.java src/main/java/de/thedevstack/conversationsplus/ui/adapter/AccountAdapter.java src/main/java/de/thedevstack/conversationsplus/ui/adapter/ConversationAdapter.java src/main/java/de/thedevstack/conversationsplus/ui/adapter/ListItemAdapter.java src/main/java/de/thedevstack/conversationsplus/ui/adapter/MessageAdapter.java src/main/java/de/thedevstack/conversationsplus/utils/UIHelper.java src/main/java/de/thedevstack/conversationsplus/xmpp/jingle/JingleConnection.java src/main/java/de/thedevstack/conversationsplus/xmpp/jingle/JingleConnectionManager.java src/main/java/de/thedevstack/conversationsplus/xmpp/jingle/JingleInbandTransport.java src/main/java/de/thedevstack/conversationsplus/xmpp/jingle/JingleSocks5Transport.java src/main/java/de/thedevstack/conversationsplus/xmpp/stanzas/MessagePacket.java src/main/java/eu/siacs/conversations/crypto/OtrEngine.java src/main/java/eu/siacs/conversations/crypto/OtrService.java src/main/java/eu/siacs/conversations/entities/DownloadablePlaceholder.java src/main/java/eu/siacs/conversations/entities/TransferablePlaceholder.java src/main/java/eu/siacs/conversations/http/HttpConnection.java src/main/java/eu/siacs/conversations/http/HttpDownloadConnection.java src/main/res/layout/activity_about.xml
Diffstat (limited to 'src/main/java/de/thedevstack/conversationsplus/http/HttpUploadConnection.java')
-rw-r--r--src/main/java/de/thedevstack/conversationsplus/http/HttpUploadConnection.java204
1 files changed, 204 insertions, 0 deletions
diff --git a/src/main/java/de/thedevstack/conversationsplus/http/HttpUploadConnection.java b/src/main/java/de/thedevstack/conversationsplus/http/HttpUploadConnection.java
new file mode 100644
index 00000000..a9682718
--- /dev/null
+++ b/src/main/java/de/thedevstack/conversationsplus/http/HttpUploadConnection.java
@@ -0,0 +1,204 @@
+package de.thedevstack.conversationsplus.http;
+
+import android.app.PendingIntent;
+import android.util.Log;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
+import java.net.URL;
+
+import javax.net.ssl.HttpsURLConnection;
+
+import de.thedevstack.conversationsplus.Config;
+import de.thedevstack.conversationsplus.entities.Account;
+import de.thedevstack.conversationsplus.entities.Transferable;
+import de.thedevstack.conversationsplus.entities.DownloadableFile;
+import de.thedevstack.conversationsplus.entities.Message;
+import de.thedevstack.conversationsplus.persistance.FileBackend;
+import de.thedevstack.conversationsplus.services.XmppConnectionService;
+import de.thedevstack.conversationsplus.ui.UiCallback;
+import de.thedevstack.conversationsplus.utils.CryptoHelper;
+import de.thedevstack.conversationsplus.utils.Xmlns;
+import de.thedevstack.conversationsplus.xml.Element;
+import de.thedevstack.conversationsplus.xmpp.OnIqPacketReceived;
+import de.thedevstack.conversationsplus.xmpp.jid.Jid;
+import de.thedevstack.conversationsplus.xmpp.stanzas.IqPacket;
+
+public class HttpUploadConnection implements Transferable {
+
+ private HttpConnectionManager mHttpConnectionManager;
+ private XmppConnectionService mXmppConnectionService;
+
+ private boolean canceled = false;
+ private Account account;
+ private DownloadableFile file;
+ private Message message;
+ private URL mGetUrl;
+ private URL mPutUrl;
+
+ private byte[] key = null;
+
+ private long transmitted = 0;
+ private long expected = 1;
+
+ public HttpUploadConnection(HttpConnectionManager httpConnectionManager) {
+ this.mHttpConnectionManager = httpConnectionManager;
+ this.mXmppConnectionService = httpConnectionManager.getXmppConnectionService();
+ }
+
+ @Override
+ public boolean start() {
+ return false;
+ }
+
+ @Override
+ public int getStatus() {
+ return STATUS_UPLOADING;
+ }
+
+ @Override
+ public long getFileSize() {
+ return this.file.getExpectedSize();
+ }
+
+ @Override
+ public int getProgress() {
+ return (int) ((((double) transmitted) / expected) * 100);
+ }
+
+ @Override
+ public void cancel() {
+ this.canceled = true;
+ }
+
+ private void fail() {
+ mHttpConnectionManager.finishUploadConnection(this);
+ message.setTransferable(null);
+ mXmppConnectionService.markMessage(message,Message.STATUS_SEND_FAILED);
+ }
+
+ public void init(Message message) {
+ this.message = message;
+ message.setTransferable(this);
+ mXmppConnectionService.markMessage(message,Message.STATUS_UNSEND);
+ this.account = message.getConversation().getAccount();
+ this.file = mXmppConnectionService.getFileBackend().getFile(message, false);
+ this.file.setExpectedSize(this.file.getSize());
+
+ if (Config.ENCRYPT_ON_HTTP_UPLOADED) {
+ this.key = new byte[48];
+ mXmppConnectionService.getRNG().nextBytes(this.key);
+ this.file.setKey(this.key);
+ }
+
+ Jid host = account.getXmppConnection().findDiscoItemByFeature(Xmlns.HTTP_UPLOAD);
+ IqPacket request = mXmppConnectionService.getIqGenerator().requestHttpUploadSlot(host,file);
+ mXmppConnectionService.sendIqPacket(account, request, new OnIqPacketReceived() {
+ @Override
+ public void onIqPacketReceived(Account account, IqPacket packet) {
+ if (packet.getType() == IqPacket.TYPE.RESULT) {
+ Element slot = packet.findChild("slot",Xmlns.HTTP_UPLOAD);
+ if (slot != null) {
+ try {
+ mGetUrl = new URL(slot.findChildContent("get"));
+ mPutUrl = new URL(slot.findChildContent("put"));
+ if (!canceled) {
+ new Thread(new FileUploader()).start();
+ }
+ } catch (MalformedURLException e) {
+ fail();
+ }
+ } else {
+ fail();
+ }
+ } else {
+ fail();
+ }
+ }
+ });
+ }
+
+ private class FileUploader implements Runnable {
+
+ @Override
+ public void run() {
+ this.upload();
+ }
+
+ private void upload() {
+ OutputStream os = null;
+ InputStream is = null;
+ HttpURLConnection connection = null;
+ try {
+ Log.d(Config.LOGTAG, "uploading to " + mPutUrl.toString());
+ connection = (HttpURLConnection) mPutUrl.openConnection();
+ if (connection instanceof HttpsURLConnection) {
+ mHttpConnectionManager.setupTrustManager((HttpsURLConnection) connection, true);
+ }
+ connection.setRequestMethod("PUT");
+ connection.setFixedLengthStreamingMode((int) file.getExpectedSize());
+ connection.setDoOutput(true);
+ connection.connect();
+ os = connection.getOutputStream();
+ is = file.createInputStream();
+ transmitted = 0;
+ expected = file.getExpectedSize();
+ int count = -1;
+ byte[] buffer = new byte[4096];
+ while (((count = is.read(buffer)) != -1) && !canceled) {
+ transmitted += count;
+ os.write(buffer, 0, count);
+ mXmppConnectionService.updateConversationUi();
+ }
+ os.flush();
+ os.close();
+ is.close();
+ int code = connection.getResponseCode();
+ if (code == 200 || code == 201) {
+ Log.d(Config.LOGTAG, "finished uploading file");
+ Message.FileParams params = message.getFileParams();
+ if (key != null) {
+ mGetUrl = new URL(mGetUrl.toString() + "#" + CryptoHelper.bytesToHex(key));
+ }
+ mXmppConnectionService.getFileBackend().updateFileParams(message, mGetUrl);
+ message.setTransferable(null);
+ message.setCounterpart(message.getConversation().getJid().toBareJid());
+ if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
+ mXmppConnectionService.getPgpEngine().encrypt(message, new UiCallback<Message>() {
+ @Override
+ public void success(Message message) {
+ mXmppConnectionService.resendMessage(message);
+ }
+
+ @Override
+ public void error(int errorCode, Message object) {
+ fail();
+ }
+
+ @Override
+ public void userInputRequried(PendingIntent pi, Message object) {
+ fail();
+ }
+ });
+ } else {
+ mXmppConnectionService.resendMessage(message);
+ }
+ } else {
+ fail();
+ }
+ } catch (IOException e) {
+ Log.d(Config.LOGTAG, e.getMessage());
+ fail();
+ } finally {
+ FileBackend.close(is);
+ FileBackend.close(os);
+ if (connection != null) {
+ connection.disconnect();
+ }
+ }
+ }
+ }
+}