mirror of
https://github.com/syncthing/syncthing-android.git
synced 2024-11-30 00:01:19 +00:00
Simplified RestTask classes
This commit is contained in:
parent
d222cd7a76
commit
fa2879c9f0
4 changed files with 65 additions and 45 deletions
|
@ -3,18 +3,17 @@ package com.nutomic.syncthingandroid.http;
|
|||
import android.util.Log;
|
||||
import android.util.Pair;
|
||||
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.URL;
|
||||
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
|
||||
/**
|
||||
* 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
|
||||
* 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";
|
||||
|
||||
|
@ -28,7 +27,7 @@ public class GetTask extends RestTask<String, Void, String> {
|
|||
public static final String URI_EVENTS = "/rest/events";
|
||||
|
||||
public GetTask(URL url, String path, String httpsCertPath, String apiKey,
|
||||
OnSuccessListener<String> listener) {
|
||||
OnSuccessListener listener) {
|
||||
super(url, path, httpsCertPath, apiKey, listener);
|
||||
}
|
||||
|
||||
|
@ -40,16 +39,7 @@ public class GetTask extends RestTask<String, Void, String> {
|
|||
try {
|
||||
HttpsURLConnection connection = openConnection(params);
|
||||
Log.v(TAG, "Calling Rest API at " + connection.getURL());
|
||||
connection.connect();
|
||||
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);
|
||||
return connect(connection);
|
||||
} catch (IOException e) {
|
||||
Log.w(TAG, "Failed to call rest api", e);
|
||||
return new Pair<>(false, null);
|
||||
|
|
|
@ -4,14 +4,15 @@ package com.nutomic.syncthingandroid.http;
|
|||
import android.util.Log;
|
||||
import android.util.Pair;
|
||||
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
|
||||
/**
|
||||
* 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";
|
||||
|
||||
|
@ -22,12 +23,12 @@ public class PollWebGuiAvailableTask extends RestTask<Void, Void, Void> {
|
|||
private static final long WEB_GUI_POLL_INTERVAL = 100;
|
||||
|
||||
public PollWebGuiAvailableTask(URL url, String httpsCertPath, String apiKey,
|
||||
OnSuccessListener<Void> listener) {
|
||||
OnSuccessListener listener) {
|
||||
super(url, "", httpsCertPath, apiKey, listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Pair<Boolean, Void> doInBackground(Void... aVoid) {
|
||||
protected Pair<Boolean, String> doInBackground(Void... aVoid) {
|
||||
int status = 0;
|
||||
do {
|
||||
try {
|
||||
|
|
|
@ -3,17 +3,16 @@ package com.nutomic.syncthingandroid.http;
|
|||
import android.util.Log;
|
||||
import android.util.Pair;
|
||||
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.net.URL;
|
||||
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
|
||||
/**
|
||||
* 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";
|
||||
|
||||
|
@ -21,7 +20,7 @@ public class PostTask extends RestTask<String, Void, Boolean> {
|
|||
public static final String URI_SCAN = "/rest/db/scan";
|
||||
|
||||
public PostTask(URL url, String path, String httpsCertPath, String apiKey,
|
||||
OnSuccessListener<Boolean> listener) {
|
||||
OnSuccessListener 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.
|
||||
*/
|
||||
@Override
|
||||
protected Pair<Boolean, Boolean> doInBackground(String... params) {
|
||||
protected Pair<Boolean, String> doInBackground(String... params) {
|
||||
try {
|
||||
HttpsURLConnection connection = (mPath.equals(URI_SCAN))
|
||||
? openConnection("folder", params[0], "sub", params[1])
|
||||
|
@ -43,16 +42,7 @@ public class PostTask extends RestTask<String, Void, Boolean> {
|
|||
OutputStream os = connection.getOutputStream();
|
||||
os.write(params[0].getBytes("UTF-8"));
|
||||
}
|
||||
connection.connect();
|
||||
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);
|
||||
return connect(connection);
|
||||
} catch (IOException e) {
|
||||
Log.w(TAG, "Failed to call rest api", e);
|
||||
return new Pair<>(false, null);
|
||||
|
|
|
@ -7,36 +7,49 @@ import android.os.AsyncTask;
|
|||
import android.util.Log;
|
||||
import android.util.Pair;
|
||||
|
||||
import com.google.common.base.Charsets;
|
||||
import com.google.common.io.ByteSource;
|
||||
import com.nutomic.syncthingandroid.syncthing.RestApi;
|
||||
|
||||
import javax.net.ssl.*;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
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.CertificateFactory;
|
||||
import java.security.cert.X509Certificate;
|
||||
|
||||
public abstract class RestTask<Params, Progress, Result> extends
|
||||
AsyncTask<Params, Progress, Pair<Boolean, Result>> {
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
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";
|
||||
|
||||
public interface OnSuccessListener<Result> {
|
||||
public void onSuccess(Result result);
|
||||
public interface OnSuccessListener {
|
||||
public void onSuccess(String result);
|
||||
}
|
||||
|
||||
private final URL mUrl;
|
||||
protected final String mPath;
|
||||
private final String mHttpsCertPath;
|
||||
private final String mApiKey;
|
||||
private final OnSuccessListener<Result> mListener;
|
||||
private final OnSuccessListener mListener;
|
||||
|
||||
public RestTask(URL url, String path, String httpsCertPath, String apiKey,
|
||||
OnSuccessListener<Result> listener) {
|
||||
OnSuccessListener listener) {
|
||||
mUrl = url;
|
||||
mPath = path;
|
||||
mHttpsCertPath = httpsCertPath;
|
||||
|
@ -60,8 +73,34 @@ public abstract class RestTask<Params, Progress, Result> extends
|
|||
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;
|
||||
|
||||
mListener.onSuccess(result.second);
|
||||
|
|
Loading…
Reference in a new issue