1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-11-30 08:11:17 +00:00

Fixed inotify events not passing parameters to Syncthing (ref #465).

The parameters set in RestApi#onFolderFileChanged() were not actually
used by PostTask#doInBackground().
This commit is contained in:
Felix Ableitner 2015-10-15 21:58:17 +02:00
parent 76f90f184c
commit dc31cd1cb6
3 changed files with 97 additions and 19 deletions

View file

@ -14,41 +14,37 @@ import org.apache.http.protocol.HTTP;
import java.io.IOException; import java.io.IOException;
/** /**
* Performs a POST request with no parameters to the URL in uri[0] with the path in uri[1]. * Sends a new config to {@link #URI_CONFIG}.
*/ */
public class PostTask extends AsyncTask<String, Void, Boolean> { public class PostConfigTask extends AsyncTask<String, Void, Boolean> {
private static final String TAG = "PostTask"; private static final String TAG = "PostConfigTask";
public static final String URI_CONFIG = "/rest/system/config"; public static final String URI_CONFIG = "/rest/system/config";
public static final String URI_SCAN = "/rest/db/scan";
private String mHttpsCertPath; private String mHttpsCertPath;
public PostTask(String httpsCertPath) { public PostConfigTask(String httpsCertPath) {
mHttpsCertPath = httpsCertPath; mHttpsCertPath = httpsCertPath;
} }
/** /**
* params[0] Syncthing hostname * params[0] Syncthing hostname
* params[1] URI to call * params[1] Syncthing API key
* params[2] Syncthing API key * params[2] The new config
* params[3] The request content (optional)
*/ */
@Override @Override
protected Boolean doInBackground(String... params) { protected Boolean doInBackground(String... params) {
String fullUri = params[0] + params[1]; String fullUri = params[0] + URI_CONFIG;
Log.v(TAG, "Calling Rest API at " + fullUri); Log.v(TAG, "Calling Rest API at " + fullUri);
HttpClient httpclient = Https.createHttpsClient(mHttpsCertPath); HttpClient httpclient = Https.createHttpsClient(mHttpsCertPath);
HttpPost post = new HttpPost(fullUri); HttpPost post = new HttpPost(fullUri);
post.addHeader(new BasicHeader(RestApi.HEADER_API_KEY, params[2])); post.addHeader(new BasicHeader(RestApi.HEADER_API_KEY, params[1]));
try { try {
if (params.length > 3) {
post.setEntity(new StringEntity(params[3], HTTP.UTF_8)); post.setEntity(new StringEntity(params[3], HTTP.UTF_8));
Log.v(TAG, "API call parameters: " + params[3]); Log.v(TAG, "API call parameters: " + params[3]);
}
httpclient.execute(post); httpclient.execute(post);
} catch (IOException|IllegalArgumentException e) { } catch (IOException|IllegalArgumentException e) {
Log.w(TAG, "Failed to call Rest API at " + fullUri, e); Log.w(TAG, "Failed to call Rest API at " + fullUri, e);

View file

@ -0,0 +1,83 @@
package com.nutomic.syncthingandroid.syncthing;
import android.os.AsyncTask;
import android.util.Log;
import com.nutomic.syncthingandroid.util.Https;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.LinkedList;
/**
* Performs a POST request to {@link #URI_SCAN} to notify Syncthing of a changed file or folder.
*/
public class PostScanTask extends AsyncTask<String, Void, Void> {
private static final String TAG = "PostScanTask";
public static final String URI_SCAN = "/rest/db/scan";
private String mHttpsCertPath;
public PostScanTask(String httpsCertPath) {
mHttpsCertPath = httpsCertPath;
}
/**
* params[0] Syncthing hostname
* params[1] Syncthing API key
* params[2] folder parameter (the Syncthing folder to update)
* params[3] sub parameter (the subfolder to update
*/
@Override
protected Void doInBackground(String... params) {
String fullUri = params[0] + URI_SCAN;
LinkedList<NameValuePair> urlParams = new LinkedList<>();
urlParams.add(new BasicNameValuePair("folder", params[2]));
urlParams.add(new BasicNameValuePair("sub", params[3]));
fullUri += "?" + URLEncodedUtils.format(urlParams, HTTP.UTF_8);
Log.v(TAG, "Calling Rest API at " + fullUri);
// Retry at most 10 times before failing
for (int i = 0; i < 10; i++) {
HttpClient httpclient = Https.createHttpsClient(mHttpsCertPath);
HttpPost post = new HttpPost(fullUri);
post.addHeader(new BasicHeader(RestApi.HEADER_API_KEY, params[1]));
if (isCancelled())
return null;
try {
HttpResponse response = httpclient.execute(post);
if (response.getEntity() != null)
return null;
} catch (IOException | IllegalArgumentException e) {
Log.w(TAG, "Failed to call Rest API at " + fullUri, e);
}
try {
// Don't push the API too hard
Thread.sleep(500 * i);
} catch (InterruptedException e) {
Log.w(TAG, e);
}
Log.w(TAG, "Retrying GetTask Rest API call ("+(i+1)+"/10)");
}
return null;
}
}

View file

@ -347,10 +347,10 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener,
*/ */
public void requireRestart(Activity activity, boolean updateConfig) { public void requireRestart(Activity activity, boolean updateConfig) {
if (updateConfig) { if (updateConfig) {
new PostTask(mHttpsCertPath) new PostConfigTask(mHttpsCertPath)
.execute(mUrl, PostTask.URI_CONFIG, mApiKey, mConfig.toString()); .execute(mUrl, mApiKey, mConfig.toString());
} }
// TODO Should wait until PostTask is completed, see #398 // TODO Should wait until PostConfigTask is completed, see #398
if (mRestartPostponed) if (mRestartPostponed)
return; return;
@ -941,8 +941,7 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener,
*/ */
@Override @Override
public void onFolderFileChange(String folderId, String relativePath) { public void onFolderFileChange(String folderId, String relativePath) {
new PostTask(mHttpsCertPath).execute(mUrl, PostTask.URI_SCAN, mApiKey, "folder", folderId, "sub", new PostScanTask(mHttpsCertPath).execute(mUrl, mApiKey, folderId, relativePath);
relativePath);
} }
/** /**