From 0de9c2aa23990749360ee0e9f509e13a54b14137 Mon Sep 17 00:00:00 2001 From: lookshe Date: Wed, 16 Mar 2016 18:45:18 +0100 Subject: partially implements FS#161 do not show download button on 404 --- .../exceptions/RemoteFileNotFoundException.java | 20 ++++++++++ .../http/HttpDownloadConnection.java | 46 ++++++++++++++-------- 2 files changed, 49 insertions(+), 17 deletions(-) create mode 100644 src/main/java/de/thedevstack/conversationsplus/exceptions/RemoteFileNotFoundException.java (limited to 'src') diff --git a/src/main/java/de/thedevstack/conversationsplus/exceptions/RemoteFileNotFoundException.java b/src/main/java/de/thedevstack/conversationsplus/exceptions/RemoteFileNotFoundException.java new file mode 100644 index 00000000..41a548cb --- /dev/null +++ b/src/main/java/de/thedevstack/conversationsplus/exceptions/RemoteFileNotFoundException.java @@ -0,0 +1,20 @@ +package de.thedevstack.conversationsplus.exceptions; + +import java.io.IOException; + +/** + * Created by lookshe on 15.03.16. + * + * Exception class if HTTP status code 404 occured + */ +public class RemoteFileNotFoundException extends IOException { + private static final long serialVersionUID = -1010013599132881427L; + + public RemoteFileNotFoundException() { + super(); + } + + public RemoteFileNotFoundException(Throwable e) { + super(e); + } +} diff --git a/src/main/java/de/thedevstack/conversationsplus/http/HttpDownloadConnection.java b/src/main/java/de/thedevstack/conversationsplus/http/HttpDownloadConnection.java index bbd47089..0e50dd4d 100644 --- a/src/main/java/de/thedevstack/conversationsplus/http/HttpDownloadConnection.java +++ b/src/main/java/de/thedevstack/conversationsplus/http/HttpDownloadConnection.java @@ -24,6 +24,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 +182,9 @@ public class HttpDownloadConnection implements Transferable { HttpDownloadConnection.this.acceptedAutomatically = false; HttpDownloadConnection.this.mXmppConnectionService.getNotificationService().push(message); return; + } catch (RemoteFileNotFoundException e) { + cancel(); + return; } catch (IOException e) { Log.d(Config.LOGTAG, "io exception in http file size checker: " + e.getMessage()); if (interactive) { @@ -205,23 +209,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) { -- cgit v1.2.3