Browse Source

Replace return code int flag with DaemonException for 403 detection

pull/523/head
Phillip Dykman 5 years ago
parent
commit
2cb1b09858
  1. 24
      app/src/main/java/org/transdroid/daemon/Qbittorrent/QbittorrentAdapter.java

24
app/src/main/java/org/transdroid/daemon/Qbittorrent/QbittorrentAdapter.java

@ -73,7 +73,6 @@ public class QbittorrentAdapter implements IDaemonAdapter {
private DefaultHttpClient httpclient; private DefaultHttpClient httpclient;
private int version = -1; private int version = -1;
private float apiVersion = -1; // starting from 2.3 old API is dropped so we are going to use float private float apiVersion = -1; // starting from 2.3 old API is dropped so we are going to use float
private int http_response_code = -1;
public QbittorrentAdapter(DaemonSettings settings) { public QbittorrentAdapter(DaemonSettings settings) {
this.settings = settings; this.settings = settings;
@ -95,11 +94,16 @@ public class QbittorrentAdapter implements IDaemonAdapter {
try { try {
String apiVerText = makeRequest(log, "/api/v2/app/webapiVersion"); String apiVerText = makeRequest(log, "/api/v2/app/webapiVersion");
apiVersion = Float.parseFloat(apiVerText.trim()); apiVersion = Float.parseFloat(apiVerText.trim());
} catch (DaemonException | NumberFormatException e) { } catch (DaemonException e) {
is_v2 = http_response_code == 403; // 403 Forbidden - endpoint exists. Keep trying v2
is_v2 = e.getType() == ExceptionType.AuthenticationFailure;
} catch (NumberFormatException e) {
// Assume endpoint exists and is reachable, set lowest possible version and stop trying
apiVersion = (float) 2.3;
} }
// Keep trying // Keep trying
if (apiVersion < 0) {
if (is_v2) { if (is_v2) {
// Preemptive assumption, for authentication // Preemptive assumption, for authentication
apiVersion = (float) 2.3; apiVersion = (float) 2.3;
@ -121,6 +125,7 @@ public class QbittorrentAdapter implements IDaemonAdapter {
apiVersion = 1; apiVersion = 1;
} }
} }
}
log.d(LOG_NAME, "qBittorrent API version is " + apiVersion); log.d(LOG_NAME, "qBittorrent API version is " + apiVersion);
@ -571,8 +576,11 @@ public class QbittorrentAdapter implements IDaemonAdapter {
// Execute // Execute
HttpResponse response = httpclient.execute(httpmethod); HttpResponse response = httpclient.execute(httpmethod);
http_response_code = response.getStatusLine().getStatusCode();
log.d(LOG_NAME, "Response code is: " + http_response_code); // Throw exception on 403
if (response.getStatusLine().getStatusCode() == 403) {
throw new DaemonException(ExceptionType.AuthenticationFailure, "Response code 403");
}
HttpEntity entity = response.getEntity(); HttpEntity entity = response.getEntity();
if (entity != null) { if (entity != null) {
@ -594,8 +602,14 @@ public class QbittorrentAdapter implements IDaemonAdapter {
} catch (Exception e) { } catch (Exception e) {
log.d(LOG_NAME, "Error: " + e.toString()); log.d(LOG_NAME, "Error: " + e.toString());
if (e instanceof DaemonException) {
throw (DaemonException) e;
}
else {
throw new DaemonException(ExceptionType.ConnectionError, e.toString()); throw new DaemonException(ExceptionType.ConnectionError, e.toString());
} }
}
} }

Loading…
Cancel
Save