Browse Source

Replace return code int flag with DaemonException for 403 detection

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

64
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,30 +94,36 @@ 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 (is_v2) { if (apiVersion < 0) {
// Preemptive assumption, for authentication if (is_v2) {
apiVersion = (float) 2.3; // Preemptive assumption, for authentication
apiVersion = (float) 2.3;
// Authenticate, and try v2 again
try { // Authenticate, and try v2 again
ensureAuthenticated(log); try {
String apiVerText = makeRequest(log, "/api/v2/app/webapiVersion"); ensureAuthenticated(log);
apiVersion = Float.parseFloat(apiVerText.trim()); String apiVerText = makeRequest(log, "/api/v2/app/webapiVersion");
} catch (DaemonException | NumberFormatException e) { apiVersion = Float.parseFloat(apiVerText.trim());
apiVersion = (float) 2.3; // assume this is new API since we are forbidden to access API } catch (DaemonException | NumberFormatException e) {
} apiVersion = (float) 2.3; // assume this is new API since we are forbidden to access API
} else { }
// Fall back to old api } else {
try { // Fall back to old api
String apiVerText = makeRequest(log, "/version/api"); try {
apiVersion = Float.parseFloat(apiVerText.trim()); String apiVerText = makeRequest(log, "/version/api");
} catch (DaemonException | NumberFormatException e) { apiVersion = Float.parseFloat(apiVerText.trim());
apiVersion = 1; } catch (DaemonException | NumberFormatException e) {
apiVersion = 1;
}
} }
} }
@ -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,7 +602,13 @@ 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());
throw new DaemonException(ExceptionType.ConnectionError, e.toString());
if (e instanceof DaemonException) {
throw (DaemonException) e;
}
else {
throw new DaemonException(ExceptionType.ConnectionError, e.toString());
}
} }
} }

Loading…
Cancel
Save