aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/thedevstack/conversationsplus/http
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/de/thedevstack/conversationsplus/http')
-rw-r--r--src/main/java/de/thedevstack/conversationsplus/http/HttpConnectionManager.java74
-rw-r--r--src/main/java/de/thedevstack/conversationsplus/http/HttpDownloadConnection.java (renamed from src/main/java/de/thedevstack/conversationsplus/http/HttpConnection.java)135
-rw-r--r--src/main/java/de/thedevstack/conversationsplus/http/HttpUploadConnection.java204
3 files changed, 319 insertions, 94 deletions
diff --git a/src/main/java/de/thedevstack/conversationsplus/http/HttpConnectionManager.java b/src/main/java/de/thedevstack/conversationsplus/http/HttpConnectionManager.java
index b41f0793..484e77a3 100644
--- a/src/main/java/de/thedevstack/conversationsplus/http/HttpConnectionManager.java
+++ b/src/main/java/de/thedevstack/conversationsplus/http/HttpConnectionManager.java
@@ -1,11 +1,22 @@
package de.thedevstack.conversationsplus.http;
+import org.apache.http.conn.ssl.StrictHostnameVerifier;
+
+import java.security.KeyManagementException;
+import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
+import javax.net.ssl.HostnameVerifier;
+import javax.net.ssl.HttpsURLConnection;
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.SSLSocketFactory;
+import javax.net.ssl.X509TrustManager;
+
import de.thedevstack.conversationsplus.entities.Message;
import de.thedevstack.conversationsplus.services.AbstractConnectionManager;
import de.thedevstack.conversationsplus.services.XmppConnectionService;
+import de.thedevstack.conversationsplus.utils.CryptoHelper;
public class HttpConnectionManager extends AbstractConnectionManager {
@@ -13,16 +24,67 @@ public class HttpConnectionManager extends AbstractConnectionManager {
super(service);
}
- private List<HttpConnection> connections = new CopyOnWriteArrayList<HttpConnection>();
+ private List<HttpDownloadConnection> downloadConnections = new CopyOnWriteArrayList<>();
+ private List<HttpUploadConnection> uploadConnections = new CopyOnWriteArrayList<>();
+
+ public HttpDownloadConnection createNewDownloadConnection(Message message) {
+ return this.createNewDownloadConnection(message, false);
+ }
+
+ public HttpDownloadConnection createNewDownloadConnection(Message message, boolean interactive) {
+ HttpDownloadConnection connection = new HttpDownloadConnection(this);
+ connection.init(message,interactive);
+ this.downloadConnections.add(connection);
+ return connection;
+ }
- public HttpConnection createNewConnection(Message message) {
- HttpConnection connection = new HttpConnection(this);
+ public HttpUploadConnection createNewUploadConnection(Message message) {
+ HttpUploadConnection connection = new HttpUploadConnection(this);
connection.init(message);
- this.connections.add(connection);
+ this.uploadConnections.add(connection);
return connection;
}
- public void finishConnection(HttpConnection connection) {
- this.connections.remove(connection);
+ public void finishConnection(HttpDownloadConnection connection) {
+ this.downloadConnections.remove(connection);
+ }
+
+ public void finishUploadConnection(HttpUploadConnection httpUploadConnection) {
+ this.uploadConnections.remove(httpUploadConnection);
+ }
+
+ public void setupTrustManager(final HttpsURLConnection connection, final boolean interactive) {
+ final X509TrustManager trustManager;
+ final HostnameVerifier hostnameVerifier;
+ if (interactive) {
+ trustManager = mXmppConnectionService.getMemorizingTrustManager();
+ hostnameVerifier = mXmppConnectionService
+ .getMemorizingTrustManager().wrapHostnameVerifier(
+ new StrictHostnameVerifier());
+ } else {
+ trustManager = mXmppConnectionService.getMemorizingTrustManager()
+ .getNonInteractive();
+ hostnameVerifier = mXmppConnectionService
+ .getMemorizingTrustManager()
+ .wrapHostnameVerifierNonInteractive(
+ new StrictHostnameVerifier());
+ }
+ try {
+ final SSLContext sc = SSLContext.getInstance("TLS");
+ sc.init(null, new X509TrustManager[]{trustManager},
+ mXmppConnectionService.getRNG());
+
+ final SSLSocketFactory sf = sc.getSocketFactory();
+ final String[] cipherSuites = CryptoHelper.getOrderedCipherSuites(
+ sf.getSupportedCipherSuites());
+ if (cipherSuites.length > 0) {
+ sc.getDefaultSSLParameters().setCipherSuites(cipherSuites);
+
+ }
+
+ connection.setSSLSocketFactory(sf);
+ connection.setHostnameVerifier(hostnameVerifier);
+ } catch (final KeyManagementException | NoSuchAlgorithmException ignored) {
+ }
}
}
diff --git a/src/main/java/de/thedevstack/conversationsplus/http/HttpConnection.java b/src/main/java/de/thedevstack/conversationsplus/http/HttpDownloadConnection.java
index 96b64854..3f66be21 100644
--- a/src/main/java/de/thedevstack/conversationsplus/http/HttpConnection.java
+++ b/src/main/java/de/thedevstack/conversationsplus/http/HttpDownloadConnection.java
@@ -3,8 +3,7 @@ package de.thedevstack.conversationsplus.http;
import android.content.Intent;
import android.net.Uri;
import android.os.SystemClock;
-
-import org.apache.http.conn.ssl.StrictHostnameVerifier;
+import android.util.Log;
import java.io.BufferedInputStream;
import java.io.IOException;
@@ -12,25 +11,20 @@ import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
-import java.security.KeyManagementException;
-import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
-import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
-import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLHandshakeException;
-import javax.net.ssl.SSLSocketFactory;
-import javax.net.ssl.X509TrustManager;
import de.thedevstack.conversationsplus.Config;
-import de.thedevstack.conversationsplus.entities.Downloadable;
+import de.thedevstack.conversationsplus.R;
+import de.thedevstack.conversationsplus.entities.Transferable;
import de.thedevstack.conversationsplus.entities.DownloadableFile;
import de.thedevstack.conversationsplus.entities.Message;
import de.thedevstack.conversationsplus.services.XmppConnectionService;
import de.thedevstack.conversationsplus.utils.CryptoHelper;
-public class HttpConnection implements Downloadable {
+public class HttpDownloadConnection implements Transferable {
private HttpConnectionManager mHttpConnectionManager;
private XmppConnectionService mXmppConnectionService;
@@ -38,12 +32,12 @@ public class HttpConnection implements Downloadable {
private URL mUrl;
private Message message;
private DownloadableFile file;
- private int mStatus = Downloadable.STATUS_UNKNOWN;
+ private int mStatus = Transferable.STATUS_UNKNOWN;
private boolean acceptedAutomatically = false;
private int mProgress = 0;
private long mLastGuiRefresh = 0;
- public HttpConnection(HttpConnectionManager manager) {
+ public HttpDownloadConnection(HttpConnectionManager manager) {
this.mHttpConnectionManager = manager;
this.mXmppConnectionService = manager.getXmppConnectionService();
}
@@ -63,8 +57,12 @@ public class HttpConnection implements Downloadable {
}
public void init(Message message) {
+ init(message, false);
+ }
+
+ public void init(Message message, boolean interactive) {
this.message = message;
- this.message.setDownloadable(this);
+ this.message.setTransferable(this);
try {
mUrl = new URL(message.getBody());
String[] parts = mUrl.getPath().toLowerCase().split("\\.");
@@ -92,7 +90,7 @@ public class HttpConnection implements Downloadable {
&& this.file.getKey() == null) {
this.message.setEncryption(Message.ENCRYPTION_NONE);
}
- checkFileSize(false);
+ checkFileSize(interactive);
} catch (MalformedURLException e) {
this.cancel();
}
@@ -104,7 +102,7 @@ public class HttpConnection implements Downloadable {
public void cancel() {
mHttpConnectionManager.finishConnection(this);
- message.setDownloadable(null);
+ message.setTransferable(null);
mXmppConnectionService.updateConversationUi();
}
@@ -112,7 +110,7 @@ public class HttpConnection implements Downloadable {
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(file));
mXmppConnectionService.sendBroadcast(intent);
- message.setDownloadable(null);
+ message.setTransferable(null);
mHttpConnectionManager.finishConnection(this);
mXmppConnectionService.updateConversationUi();
if (acceptedAutomatically) {
@@ -125,42 +123,6 @@ public class HttpConnection implements Downloadable {
mXmppConnectionService.updateConversationUi();
}
- private void setupTrustManager(final HttpsURLConnection connection,
- final boolean interactive) {
- final X509TrustManager trustManager;
- final HostnameVerifier hostnameVerifier;
- if (interactive) {
- trustManager = mXmppConnectionService.getMemorizingTrustManager();
- hostnameVerifier = mXmppConnectionService
- .getMemorizingTrustManager().wrapHostnameVerifier(
- new StrictHostnameVerifier());
- } else {
- trustManager = mXmppConnectionService.getMemorizingTrustManager()
- .getNonInteractive();
- hostnameVerifier = mXmppConnectionService
- .getMemorizingTrustManager()
- .wrapHostnameVerifierNonInteractive(
- new StrictHostnameVerifier());
- }
- try {
- final SSLContext sc = SSLContext.getInstance("TLS");
- sc.init(null, new X509TrustManager[]{trustManager},
- mXmppConnectionService.getRNG());
-
- final SSLSocketFactory sf = sc.getSocketFactory();
- final String[] cipherSuites = CryptoHelper.getOrderedCipherSuites(
- sf.getSupportedCipherSuites());
- if (cipherSuites.length > 0) {
- sc.getDefaultSSLParameters().setCipherSuites(cipherSuites);
-
- }
-
- connection.setSSLSocketFactory(sf);
- connection.setHostnameVerifier(hostnameVerifier);
- } catch (final KeyManagementException | NoSuchAlgorithmException ignored) {
- }
- }
-
private class FileSizeChecker implements Runnable {
private boolean interactive = false;
@@ -176,43 +138,46 @@ public class HttpConnection implements Downloadable {
size = retrieveFileSize();
} catch (SSLHandshakeException e) {
changeStatus(STATUS_OFFER_CHECK_FILESIZE);
- HttpConnection.this.acceptedAutomatically = false;
- HttpConnection.this.mXmppConnectionService.getNotificationService().push(message);
+ HttpDownloadConnection.this.acceptedAutomatically = false;
+ HttpDownloadConnection.this.mXmppConnectionService.getNotificationService().push(message);
return;
} catch (IOException e) {
+ Log.d(Config.LOGTAG, "io exception in http file size checker: " + e.getMessage());
+ if (interactive) {
+ mXmppConnectionService.showErrorToastInUi(R.string.file_not_found_on_remote_host);
+ }
cancel();
return;
}
file.setExpectedSize(size);
if (size <= mHttpConnectionManager.getAutoAcceptFileSize()) {
- HttpConnection.this.acceptedAutomatically = true;
+ HttpDownloadConnection.this.acceptedAutomatically = true;
new Thread(new FileDownloader(interactive)).start();
} else {
changeStatus(STATUS_OFFER);
- HttpConnection.this.acceptedAutomatically = false;
- HttpConnection.this.mXmppConnectionService.getNotificationService().push(message);
+ HttpDownloadConnection.this.acceptedAutomatically = false;
+ HttpDownloadConnection.this.mXmppConnectionService.getNotificationService().push(message);
}
}
- private long retrieveFileSize() throws IOException,
- SSLHandshakeException {
- changeStatus(STATUS_CHECKING);
- HttpURLConnection connection = (HttpURLConnection) mUrl
- .openConnection();
- connection.setRequestMethod("HEAD");
- if (connection instanceof HttpsURLConnection) {
- setupTrustManager((HttpsURLConnection) connection, interactive);
- }
- connection.connect();
- String contentLength = connection.getHeaderField("Content-Length");
- if (contentLength == null) {
- throw new IOException();
- }
- try {
- return Long.parseLong(contentLength, 10);
- } catch (NumberFormatException e) {
- throw new IOException();
- }
+ private long retrieveFileSize() throws IOException {
+ Log.d(Config.LOGTAG,"retrieve file size. interactive:"+String.valueOf(interactive));
+ changeStatus(STATUS_CHECKING);
+ HttpURLConnection connection = (HttpURLConnection) mUrl.openConnection();
+ connection.setRequestMethod("HEAD");
+ if (connection instanceof HttpsURLConnection) {
+ mHttpConnectionManager.setupTrustManager((HttpsURLConnection) connection, interactive);
+ }
+ connection.connect();
+ String contentLength = connection.getHeaderField("Content-Length");
+ if (contentLength == null) {
+ throw new IOException();
+ }
+ try {
+ return Long.parseLong(contentLength, 10);
+ } catch (NumberFormatException e) {
+ throw new IOException();
+ }
}
}
@@ -235,19 +200,18 @@ public class HttpConnection implements Downloadable {
} catch (SSLHandshakeException e) {
changeStatus(STATUS_OFFER);
} catch (IOException e) {
+ mXmppConnectionService.showErrorToastInUi(R.string.file_not_found_on_remote_host);
cancel();
}
}
private void download() throws SSLHandshakeException, IOException {
- HttpURLConnection connection = (HttpURLConnection) mUrl
- .openConnection();
+ HttpURLConnection connection = (HttpURLConnection) mUrl.openConnection();
if (connection instanceof HttpsURLConnection) {
- setupTrustManager((HttpsURLConnection) connection, interactive);
+ mHttpConnectionManager.setupTrustManager((HttpsURLConnection) connection, interactive);
}
connection.connect();
- BufferedInputStream is = new BufferedInputStream(
- connection.getInputStream());
+ BufferedInputStream is = new BufferedInputStream(connection.getInputStream());
file.getParentFile().mkdirs();
file.createNewFile();
OutputStream os = file.createOutputStream();
@@ -269,8 +233,8 @@ public class HttpConnection implements Downloadable {
}
private void updateImageBounds() {
- message.setType(Message.TYPE_IMAGE);
- mXmppConnectionService.getFileBackend().updateFileParams(message,mUrl);
+ message.setType(Message.TYPE_FILE);
+ mXmppConnectionService.getFileBackend().updateFileParams(message, mUrl);
mXmppConnectionService.updateMessage(message);
}
@@ -302,9 +266,4 @@ public class HttpConnection implements Downloadable {
public int getProgress() {
return this.mProgress;
}
-
- @Override
- public String getMimeType() {
- return "";
- }
}
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();
+ }
+ }
+ }
+ }
+}