aboutsummaryrefslogtreecommitdiffstats
path: root/src/eu/siacs/conversations/http
diff options
context:
space:
mode:
authoriNPUTmice <daniel@gultsch.de>2014-10-14 01:06:45 +0200
committeriNPUTmice <daniel@gultsch.de>2014-10-14 01:06:45 +0200
commitbbb0693f4a9a62a872b61b12a5118b01c7ca9450 (patch)
tree7db16c0083e42461921936344fba1f9af7c58f97 /src/eu/siacs/conversations/http
parent7e373bc89fd11cf214fb435f25063d77cc2c48ae (diff)
basic image over http downloading
Diffstat (limited to 'src/eu/siacs/conversations/http')
-rw-r--r--src/eu/siacs/conversations/http/HttpConnection.java129
-rw-r--r--src/eu/siacs/conversations/http/HttpConnectionManager.java27
2 files changed, 156 insertions, 0 deletions
diff --git a/src/eu/siacs/conversations/http/HttpConnection.java b/src/eu/siacs/conversations/http/HttpConnection.java
new file mode 100644
index 00000000..d3b1700b
--- /dev/null
+++ b/src/eu/siacs/conversations/http/HttpConnection.java
@@ -0,0 +1,129 @@
+package eu.siacs.conversations.http;
+
+import java.io.BufferedInputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
+import java.net.URL;
+
+import javax.net.ssl.HttpsURLConnection;
+
+import android.util.Log;
+
+import eu.siacs.conversations.Config;
+import eu.siacs.conversations.DownloadableFile;
+import eu.siacs.conversations.entities.Downloadable;
+import eu.siacs.conversations.entities.Message;
+import eu.siacs.conversations.services.XmppConnectionService;
+
+public class HttpConnection implements Downloadable {
+
+ private HttpConnectionManager mHttpConnectionManager;
+ private XmppConnectionService mXmppConnectionService;
+
+ private URL mUrl;
+ private Message message;
+ private DownloadableFile file;
+
+ public HttpConnection(HttpConnectionManager manager) {
+ this.mHttpConnectionManager = manager;
+ this.mXmppConnectionService = manager.getXmppConnectionService();
+ }
+
+ @Override
+ public void start() {
+ new Thread(new FileDownloader()).start();
+ }
+
+ public void init(Message message) {
+ this.message = message;
+ this.message.setDownloadable(this);
+ try {
+ mUrl = new URL(message.getBody());
+ this.file = mXmppConnectionService.getFileBackend().getConversationsFile(message,false);
+ message.setType(Message.TYPE_IMAGE);
+ mXmppConnectionService.markMessage(message, Message.STATUS_RECEIVED_OFFER);
+ checkFileSize();
+ } catch (MalformedURLException e) {
+ this.cancel();
+ }
+ }
+
+ private void checkFileSize() {
+ new Thread(new FileSizeChecker()).start();
+ }
+
+ public void cancel() {
+ mXmppConnectionService.markMessage(message, Message.STATUS_RECEPTION_FAILED);
+ Log.d(Config.LOGTAG,"canceled download");
+ }
+
+ private class FileSizeChecker implements Runnable {
+
+ @Override
+ public void run() {
+ try {
+ long size = retrieveFileSize();
+ file.setExpectedSize(size);
+ if (size <= mHttpConnectionManager.getAutoAcceptFileSize()) {
+ start();
+ }
+ Log.d(Config.LOGTAG,"file size: "+size);
+ } catch (IOException e) {
+ cancel();
+ }
+ }
+
+ private long retrieveFileSize() throws IOException {
+ HttpURLConnection connection = (HttpURLConnection) mUrl.openConnection();
+ connection.setRequestMethod("HEAD");
+ if (connection instanceof HttpsURLConnection) {
+
+ }
+ 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 class FileDownloader implements Runnable {
+
+ @Override
+ public void run() {
+ try {
+ mXmppConnectionService.markMessage(message, Message.STATUS_RECEIVING);
+ download();
+ mXmppConnectionService.markMessage(message, Message.STATUS_RECEIVED);
+ } catch (IOException e) {
+ cancel();
+ }
+ }
+
+ private void download() throws IOException {
+ HttpURLConnection connection = (HttpURLConnection) mUrl.openConnection();
+ if (connection instanceof HttpsURLConnection) {
+
+ }
+ BufferedInputStream is = new BufferedInputStream(connection.getInputStream());
+ OutputStream os = file.createOutputStream();
+ int count = -1;
+ byte[] buffer = new byte[1024];
+ while ((count = is.read(buffer)) != -1) {
+ os.write(buffer, 0, count);
+ }
+ os.flush();
+ os.close();
+ is.close();
+ Log.d(Config.LOGTAG,"finished downloading "+file.getAbsolutePath().toString());
+ }
+
+ }
+} \ No newline at end of file
diff --git a/src/eu/siacs/conversations/http/HttpConnectionManager.java b/src/eu/siacs/conversations/http/HttpConnectionManager.java
new file mode 100644
index 00000000..ee50ef7e
--- /dev/null
+++ b/src/eu/siacs/conversations/http/HttpConnectionManager.java
@@ -0,0 +1,27 @@
+package eu.siacs.conversations.http;
+
+import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
+
+import eu.siacs.conversations.AbstractConnectionManager;
+import eu.siacs.conversations.entities.Message;
+import eu.siacs.conversations.services.XmppConnectionService;
+
+public class HttpConnectionManager extends AbstractConnectionManager {
+
+ public HttpConnectionManager(XmppConnectionService service) {
+ super(service);
+ }
+
+ private XmppConnectionService mXmppConnectionService;
+
+ private List<HttpConnection> connections = new CopyOnWriteArrayList<HttpConnection>();
+
+
+ public HttpConnection createNewConnection(Message message) {
+ HttpConnection connection = new HttpConnection(this);
+ connection.init(message);
+ this.connections.add(connection);
+ return connection;
+ }
+}