diff options
author | steckbrief <steckbrief@chefmail.de> | 2016-03-18 17:13:36 +0100 |
---|---|---|
committer | steckbrief <steckbrief@chefmail.de> | 2016-03-18 17:13:36 +0100 |
commit | f5b684ab6b061cd558bd161c90bcea2b679eb798 (patch) | |
tree | 0abf4656e534f8fcf2b2c9b3b8d65b6943bf0c23 /src/main/java/de/thedevstack/conversationsplus/http/HttpDownloadConnection.java | |
parent | 74582979b3c97117fd736422f07e007b1373b9a6 (diff) | |
parent | 67586766919f5d531d610a24331c0302311f8911 (diff) |
Merge branch 'master' of git.fucktheforce.de:conversations-plus
Diffstat (limited to 'src/main/java/de/thedevstack/conversationsplus/http/HttpDownloadConnection.java')
-rw-r--r-- | src/main/java/de/thedevstack/conversationsplus/http/HttpDownloadConnection.java | 51 |
1 files changed, 30 insertions, 21 deletions
diff --git a/src/main/java/de/thedevstack/conversationsplus/http/HttpDownloadConnection.java b/src/main/java/de/thedevstack/conversationsplus/http/HttpDownloadConnection.java index bbd47089..1517e5d1 100644 --- a/src/main/java/de/thedevstack/conversationsplus/http/HttpDownloadConnection.java +++ b/src/main/java/de/thedevstack/conversationsplus/http/HttpDownloadConnection.java @@ -10,12 +10,8 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; -import java.net.InetAddress; -import java.net.InetSocketAddress; import java.net.MalformedURLException; -import java.net.Proxy; import java.net.URL; -import java.util.Arrays; import java.util.concurrent.CancellationException; import javax.net.ssl.HttpsURLConnection; @@ -24,6 +20,7 @@ import javax.net.ssl.SSLHandshakeException; import de.thedevstack.android.logcat.Logging; import de.thedevstack.conversationsplus.ConversationsPlusApplication; import de.thedevstack.conversationsplus.ConversationsPlusPreferences; +import de.thedevstack.conversationsplus.exceptions.RemoteFileNotFoundException; import de.thedevstack.conversationsplus.utils.MessageUtil; import de.thedevstack.conversationsplus.utils.StreamUtil; import de.thedevstack.conversationsplus.Config; @@ -181,6 +178,10 @@ public class HttpDownloadConnection implements Transferable { HttpDownloadConnection.this.acceptedAutomatically = false; HttpDownloadConnection.this.mXmppConnectionService.getNotificationService().push(message); return; + } catch (RemoteFileNotFoundException e) { + message.setNoDownloadable(); + cancel(); + return; } catch (IOException e) { Log.d(Config.LOGTAG, "io exception in http file size checker: " + e.getMessage()); if (interactive) { @@ -205,23 +206,31 @@ public class HttpDownloadConnection implements Transferable { private long retrieveFileSize() throws IOException { try { - Logging.d(Config.LOGTAG, "retrieve file size. interactive:" + String.valueOf(interactive)); - changeStatus(STATUS_CHECKING); - HttpURLConnection connection = (HttpURLConnection) mUrl.openConnection(); - connection.setRequestMethod("HEAD"); - Logging.d(Config.LOGTAG, "url: "+connection.getURL().toString()); - Logging.d(Config.LOGTAG, "connection: "+connection.toString()); - connection.setRequestProperty("User-Agent", ConversationsPlusApplication.getNameAndVersion()); - if (connection instanceof HttpsURLConnection) { - mHttpConnectionManager.setupTrustManager((HttpsURLConnection) connection, interactive); - } - connection.connect(); - String contentLength = connection.getHeaderField("Content-Length"); - connection.disconnect(); - if (contentLength == null) { - return -1; - } - return Long.parseLong(contentLength, 10); + Logging.d(Config.LOGTAG, "retrieve file size. interactive:" + String.valueOf(interactive)); + changeStatus(STATUS_CHECKING); + HttpURLConnection connection = (HttpURLConnection) mUrl.openConnection(); + connection.setRequestMethod("HEAD"); + Logging.d(Config.LOGTAG, "url: " + connection.getURL().toString()); + Logging.d(Config.LOGTAG, "connection: " + connection.toString()); + connection.setRequestProperty("User-Agent", ConversationsPlusApplication.getNameAndVersion()); + // https://code.google.com/p/android/issues/detail?id=24672 + connection.setRequestProperty("Accept-Encoding", ""); + if (connection instanceof HttpsURLConnection) { + mHttpConnectionManager.setupTrustManager((HttpsURLConnection) connection, interactive); + } + connection.connect(); + if (connection.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) { + Logging.d(Config.LOGTAG, "remote file not found"); + throw new RemoteFileNotFoundException(); + } + String contentLength = connection.getHeaderField("Content-Length"); + connection.disconnect(); + if (contentLength == null) { + return -1; + } + return Long.parseLong(contentLength, 10); + } catch (RemoteFileNotFoundException e) { + throw e; } catch (IOException e) { return -1; } catch (NumberFormatException e) { |