1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-11-26 22:31:16 +00:00

Simplified RestTask classes

This commit is contained in:
Felix Ableitner 2016-10-16 12:59:23 +09:00
parent d222cd7a76
commit fa2879c9f0
4 changed files with 65 additions and 45 deletions

View file

@ -3,18 +3,17 @@ package com.nutomic.syncthingandroid.http;
import android.util.Log; import android.util.Log;
import android.util.Pair; import android.util.Pair;
import javax.net.ssl.HttpsURLConnection;
import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL; import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
/** /**
* Performs a GET request to the Syncthing API * Performs a GET request to the Syncthing API
* Performs a GET request with no parameters to the URL in uri[0] with the path in uri[1] and * Performs a GET request with no parameters to the URL in uri[0] with the path in uri[1] and
* returns the result as a String. * returns the result as a String.
*/ */
public class GetTask extends RestTask<String, Void, String> { public class GetTask extends RestTask<String, Void> {
private static final String TAG = "GetTask"; private static final String TAG = "GetTask";
@ -28,7 +27,7 @@ public class GetTask extends RestTask<String, Void, String> {
public static final String URI_EVENTS = "/rest/events"; public static final String URI_EVENTS = "/rest/events";
public GetTask(URL url, String path, String httpsCertPath, String apiKey, public GetTask(URL url, String path, String httpsCertPath, String apiKey,
OnSuccessListener<String> listener) { OnSuccessListener listener) {
super(url, path, httpsCertPath, apiKey, listener); super(url, path, httpsCertPath, apiKey, listener);
} }
@ -40,16 +39,7 @@ public class GetTask extends RestTask<String, Void, String> {
try { try {
HttpsURLConnection connection = openConnection(params); HttpsURLConnection connection = openConnection(params);
Log.v(TAG, "Calling Rest API at " + connection.getURL()); Log.v(TAG, "Calling Rest API at " + connection.getURL());
connection.connect(); return connect(connection);
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
String line;
String result = "";
while ((line = br.readLine()) != null) {
result += line;
}
br.close();
Log.v(TAG, "API call result: " + result);
return new Pair<>(true, result);
} catch (IOException e) { } catch (IOException e) {
Log.w(TAG, "Failed to call rest api", e); Log.w(TAG, "Failed to call rest api", e);
return new Pair<>(false, null); return new Pair<>(false, null);

View file

@ -4,14 +4,15 @@ package com.nutomic.syncthingandroid.http;
import android.util.Log; import android.util.Log;
import android.util.Pair; import android.util.Pair;
import javax.net.ssl.HttpsURLConnection;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
/** /**
* Polls to load the web interface, until we receive http status 200. * Polls to load the web interface, until we receive http status 200.
*/ */
public class PollWebGuiAvailableTask extends RestTask<Void, Void, Void> { public class PollWebGuiAvailableTask extends RestTask<Void, Void> {
private static final String TAG = "PollWebGuiAvailableTask"; private static final String TAG = "PollWebGuiAvailableTask";
@ -22,12 +23,12 @@ public class PollWebGuiAvailableTask extends RestTask<Void, Void, Void> {
private static final long WEB_GUI_POLL_INTERVAL = 100; private static final long WEB_GUI_POLL_INTERVAL = 100;
public PollWebGuiAvailableTask(URL url, String httpsCertPath, String apiKey, public PollWebGuiAvailableTask(URL url, String httpsCertPath, String apiKey,
OnSuccessListener<Void> listener) { OnSuccessListener listener) {
super(url, "", httpsCertPath, apiKey, listener); super(url, "", httpsCertPath, apiKey, listener);
} }
@Override @Override
protected Pair<Boolean, Void> doInBackground(Void... aVoid) { protected Pair<Boolean, String> doInBackground(Void... aVoid) {
int status = 0; int status = 0;
do { do {
try { try {

View file

@ -3,17 +3,16 @@ package com.nutomic.syncthingandroid.http;
import android.util.Log; import android.util.Log;
import android.util.Pair; import android.util.Pair;
import javax.net.ssl.HttpsURLConnection;
import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.URL; import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
/** /**
* Sends a POST request to the Syncthing API. * Sends a POST request to the Syncthing API.
*/ */
public class PostTask extends RestTask<String, Void, Boolean> { public class PostTask extends RestTask<String, Void> {
private static final String TAG = "PostTask"; private static final String TAG = "PostTask";
@ -21,7 +20,7 @@ public class PostTask extends RestTask<String, Void, Boolean> {
public static final String URI_SCAN = "/rest/db/scan"; public static final String URI_SCAN = "/rest/db/scan";
public PostTask(URL url, String path, String httpsCertPath, String apiKey, public PostTask(URL url, String path, String httpsCertPath, String apiKey,
OnSuccessListener<Boolean> listener) { OnSuccessListener listener) {
super(url, path, httpsCertPath, apiKey, listener); super(url, path, httpsCertPath, apiKey, listener);
} }
@ -31,7 +30,7 @@ public class PostTask extends RestTask<String, Void, Boolean> {
* For {@link #URI_SCAN}, params[0] must contain the folder, and params[1] the subfolder. * For {@link #URI_SCAN}, params[0] must contain the folder, and params[1] the subfolder.
*/ */
@Override @Override
protected Pair<Boolean, Boolean> doInBackground(String... params) { protected Pair<Boolean, String> doInBackground(String... params) {
try { try {
HttpsURLConnection connection = (mPath.equals(URI_SCAN)) HttpsURLConnection connection = (mPath.equals(URI_SCAN))
? openConnection("folder", params[0], "sub", params[1]) ? openConnection("folder", params[0], "sub", params[1])
@ -43,16 +42,7 @@ public class PostTask extends RestTask<String, Void, Boolean> {
OutputStream os = connection.getOutputStream(); OutputStream os = connection.getOutputStream();
os.write(params[0].getBytes("UTF-8")); os.write(params[0].getBytes("UTF-8"));
} }
connection.connect(); return connect(connection);
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
String line;
String result = "";
while ((line = br.readLine()) != null) {
result += line;
}
br.close();
Log.v(TAG, "API call result: " + result);
return new Pair<>(true, true);
} catch (IOException e) { } catch (IOException e) {
Log.w(TAG, "Failed to call rest api", e); Log.w(TAG, "Failed to call rest api", e);
return new Pair<>(false, null); return new Pair<>(false, null);

View file

@ -7,36 +7,49 @@ import android.os.AsyncTask;
import android.util.Log; import android.util.Log;
import android.util.Pair; import android.util.Pair;
import com.google.common.base.Charsets;
import com.google.common.io.ByteSource;
import com.nutomic.syncthingandroid.syncthing.RestApi; import com.nutomic.syncthingandroid.syncthing.RestApi;
import javax.net.ssl.*;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.security.*; import java.security.InvalidKeyException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.SecureRandom;
import java.security.SignatureException;
import java.security.cert.CertificateException; import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory; import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate; import java.security.cert.X509Certificate;
public abstract class RestTask<Params, Progress, Result> extends import javax.net.ssl.HttpsURLConnection;
AsyncTask<Params, Progress, Pair<Boolean, Result>> { import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
public abstract class RestTask<Params, Progress> extends
AsyncTask<Params, Progress, Pair<Boolean, String>> {
private static final String TAG = "RestTask"; private static final String TAG = "RestTask";
public interface OnSuccessListener<Result> { public interface OnSuccessListener {
public void onSuccess(Result result); public void onSuccess(String result);
} }
private final URL mUrl; private final URL mUrl;
protected final String mPath; protected final String mPath;
private final String mHttpsCertPath; private final String mHttpsCertPath;
private final String mApiKey; private final String mApiKey;
private final OnSuccessListener<Result> mListener; private final OnSuccessListener mListener;
public RestTask(URL url, String path, String httpsCertPath, String apiKey, public RestTask(URL url, String path, String httpsCertPath, String apiKey,
OnSuccessListener<Result> listener) { OnSuccessListener listener) {
mUrl = url; mUrl = url;
mPath = path; mPath = path;
mHttpsCertPath = httpsCertPath; mHttpsCertPath = httpsCertPath;
@ -60,8 +73,34 @@ public abstract class RestTask<Params, Progress, Result> extends
return connection; return connection;
} }
protected void onPostExecute(Pair<Boolean, Result> result) { /**
if (mListener == null) * Opens the connection, then returns success status and response string.
*/
protected Pair<Boolean, String> connect(HttpsURLConnection connection) throws IOException {
connection.connect();
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
int responseCode = connection.getResponseCode();
String responseMessage = connection.getResponseMessage();
Log.i(TAG, "Request to " + connection.getURL() + " failed, code: " + responseCode +
", message: " + responseMessage);
return new Pair<>(false, streamToString(connection.getErrorStream()));
}
return new Pair<>(true, streamToString(connection.getInputStream()));
}
private String streamToString(InputStream is) throws IOException {
ByteSource byteSource = new ByteSource() {
@Override
public InputStream openStream() throws IOException {
return is;
}
};
return byteSource.asCharSource(Charsets.UTF_8).read();
}
protected void onPostExecute(Pair<Boolean, String> result) {
if (mListener == null || !result.first)
return; return;
mListener.onSuccess(result.second); mListener.onSuccess(result.second);