From 72b15d38937e98a52bdacc9b0a566f6c4781c1f1 Mon Sep 17 00:00:00 2001 From: stom79 Date: Fri, 19 Jan 2018 18:59:30 +0100 Subject: [PATCH] Prepares proxy --- .../mastodon/activities/BaseMainActivity.java | 2 +- .../activities/InstanceHealthActivity.java | 2 +- .../mastodon/activities/LoginActivity.java | 6 +- .../activities/RemoteFollowActivity.java | 2 +- .../activities/WebviewConnectActivity.java | 2 +- .../asynctasks/RetrieveMetaDataAsyncTask.java | 9 +- .../fr/gouv/etalab/mastodon/client/API.java | 72 ++++++++-------- .../mastodon/client/HttpsConnection.java | 83 ++++++++++++++++--- .../gouv/etalab/mastodon/helper/Helper.java | 1 + 9 files changed, 120 insertions(+), 59 deletions(-) diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/activities/BaseMainActivity.java b/app/src/main/java/fr/gouv/etalab/mastodon/activities/BaseMainActivity.java index 059ea5013..8e09188a6 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/activities/BaseMainActivity.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/activities/BaseMainActivity.java @@ -1096,7 +1096,7 @@ public abstract class BaseMainActivity extends BaseActivity if(matchStart < matchEnd && sharedText.length() >= matchEnd) sharedText = sharedText.substring(matchStart, matchEnd); } - new RetrieveMetaDataAsyncTask(sharedText, BaseMainActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + new RetrieveMetaDataAsyncTask(BaseMainActivity.this, sharedText, BaseMainActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); Intent intentToot = new Intent(getApplicationContext(), TootActivity.class); Bundle b = new Bundle(); b.putString("sharedSubject", sharedSubject); diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/activities/InstanceHealthActivity.java b/app/src/main/java/fr/gouv/etalab/mastodon/activities/InstanceHealthActivity.java index b0e0db36d..f09c67bb8 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/activities/InstanceHealthActivity.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/activities/InstanceHealthActivity.java @@ -140,7 +140,7 @@ public class InstanceHealthActivity extends BaseActivity { try { HashMap parameters = new HashMap<>(); parameters.put("name", instance.trim()); - final String response = new HttpsConnection().get("https://instances.social/api/1.0/instances/show", 30, parameters, Helper.THEKINRAR_SECRET_TOKEN ); + final String response = new HttpsConnection(InstanceHealthActivity.this).get("https://instances.social/api/1.0/instances/show", 30, parameters, Helper.THEKINRAR_SECRET_TOKEN ); if( response != null) instanceSocial = API.parseInstanceSocialResponse(getApplicationContext(), new JSONObject(response)); runOnUiThread(new Runnable() { diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/activities/LoginActivity.java b/app/src/main/java/fr/gouv/etalab/mastodon/activities/LoginActivity.java index a36d88533..45b317cba 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/activities/LoginActivity.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/activities/LoginActivity.java @@ -125,7 +125,7 @@ public class LoginActivity extends BaseActivity { @Override public void run() { try { - final String response = new HttpsConnection().get("https://instances.social/api/1.0" + action, 30, parameters, Helper.THEKINRAR_SECRET_TOKEN ); + final String response = new HttpsConnection(LoginActivity.this).get("https://instances.social/api/1.0" + action, 30, parameters, Helper.THEKINRAR_SECRET_TOKEN ); runOnUiThread(new Runnable() { public void run() { isLoadingInstance = false; @@ -252,7 +252,7 @@ public class LoginActivity extends BaseActivity { @Override public void run() { try { - final String response = new HttpsConnection().post("https://" + instance + action, 30, parameters, null ); + final String response = new HttpsConnection(LoginActivity.this).post("https://" + instance + action, 30, parameters, null ); runOnUiThread(new Runnable() { public void run() { JSONObject resobj; @@ -326,7 +326,7 @@ public class LoginActivity extends BaseActivity { @Override public void run() { try { - final String response = new HttpsConnection().post("https://" + instance + "/oauth/token", 30, parameters, null ); + final String response = new HttpsConnection(LoginActivity.this).post("https://" + instance + "/oauth/token", 30, parameters, null ); runOnUiThread(new Runnable() { public void run() { JSONObject resobj; diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/activities/RemoteFollowActivity.java b/app/src/main/java/fr/gouv/etalab/mastodon/activities/RemoteFollowActivity.java index 546fcbe3d..6bb270488 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/activities/RemoteFollowActivity.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/activities/RemoteFollowActivity.java @@ -184,7 +184,7 @@ public class RemoteFollowActivity extends BaseActivity implements OnRetrieveRemo @Override public void run() { try { - final String response = new HttpsConnection().get("https://instances.social/api/1.0" + action, 30, parameters, Helper.THEKINRAR_SECRET_TOKEN ); + final String response = new HttpsConnection(RemoteFollowActivity.this).get("https://instances.social/api/1.0" + action, 30, parameters, Helper.THEKINRAR_SECRET_TOKEN ); runOnUiThread(new Runnable() { public void run() { isLoadingInstance = false; diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/activities/WebviewConnectActivity.java b/app/src/main/java/fr/gouv/etalab/mastodon/activities/WebviewConnectActivity.java index 6bc6963c7..276c147a7 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/activities/WebviewConnectActivity.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/activities/WebviewConnectActivity.java @@ -119,7 +119,7 @@ public class WebviewConnectActivity extends BaseActivity { @Override public void run() { try { - final String response = new HttpsConnection().post("https://" + instance + action, 30, parameters, null); + final String response = new HttpsConnection(WebviewConnectActivity.this).post("https://" + instance + action, 30, parameters, null); JSONObject resobj; try { resobj = new JSONObject(response); diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/RetrieveMetaDataAsyncTask.java b/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/RetrieveMetaDataAsyncTask.java index 04ca619d6..5d29b7267 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/RetrieveMetaDataAsyncTask.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/RetrieveMetaDataAsyncTask.java @@ -14,16 +14,17 @@ * see . */ package fr.gouv.etalab.mastodon.asynctasks; +import android.content.Context; import android.os.AsyncTask; import android.os.Build; import android.util.Patterns; import java.io.IOException; +import java.lang.ref.WeakReference; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; import java.util.regex.Matcher; import java.util.regex.Pattern; - import fr.gouv.etalab.mastodon.client.HttpsConnection; import fr.gouv.etalab.mastodon.helper.Helper; import fr.gouv.etalab.mastodon.interfaces.OnRetrieveMetaDataInterface; @@ -40,10 +41,12 @@ public class RetrieveMetaDataAsyncTask extends AsyncTask { private String url; private boolean error = false; private String image, title, description; + private WeakReference contextWeakReference; - public RetrieveMetaDataAsyncTask(String url, OnRetrieveMetaDataInterface onRetrieveRemoteAccountInterface){ + public RetrieveMetaDataAsyncTask(Context context, String url, OnRetrieveMetaDataInterface onRetrieveRemoteAccountInterface){ this.url = url; this.listener = onRetrieveRemoteAccountInterface; + this.contextWeakReference = new WeakReference<>(context); } @Override @@ -70,7 +73,7 @@ public class RetrieveMetaDataAsyncTask extends AsyncTask { Pattern descriptionPattern = Pattern.compile("meta\\s+property=[\"']og:description[\"']\\s+content=[\"'](.*)[\"']"); Pattern imagePattern = Pattern.compile("meta\\s+property=[\"']og:image[\"']\\s+content=[\"'](.*)[\"']"); try { - String response = new HttpsConnection().get(potentialUrl); + String response = new HttpsConnection(this.contextWeakReference.get()).get(potentialUrl); Matcher matcherTitle = titlePattern.matcher(response); Matcher matcherDescription = descriptionPattern.matcher(response); Matcher matcherImage = imagePattern.matcher(response); diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java b/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java index 4371f00b1..54c9a3ea3 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java @@ -123,7 +123,7 @@ public class API { */ public APIResponse getInstance() { try { - String response = new HttpsConnection().get(getAbsoluteUrl("/instance"), 30, null, prefKeyOauthTokenT); + String response = new HttpsConnection(context).get(getAbsoluteUrl("/instance"), 30, null, prefKeyOauthTokenT); Instance instanceEntity = parseInstance(new JSONObject(response)); apiResponse.setInstance(instanceEntity); } catch (HttpsConnection.HttpsConnectionException e) { @@ -166,7 +166,7 @@ public class API { requestParams.put("header",header); } try { - new HttpsConnection().patch(getAbsoluteUrl("/accounts/update_credentials"), 60, requestParams, prefKeyOauthTokenT); + new HttpsConnection(context).patch(getAbsoluteUrl("/accounts/update_credentials"), 60, requestParams, prefKeyOauthTokenT); } catch (HttpsConnection.HttpsConnectionException e) { e.printStackTrace(); setError(e.getStatusCode(), e); @@ -185,7 +185,7 @@ public class API { public Account verifyCredentials() { account = new Account(); try { - String response = new HttpsConnection().get(getAbsoluteUrl("/accounts/verify_credentials"), 60, null, prefKeyOauthTokenT); + String response = new HttpsConnection(context).get(getAbsoluteUrl("/accounts/verify_credentials"), 60, null, prefKeyOauthTokenT); account = parseAccountResponse(context, new JSONObject(response)); } catch (HttpsConnection.HttpsConnectionException e) { setError(e.getStatusCode(), e); @@ -204,7 +204,7 @@ public class API { account = new Account(); try { - String response = new HttpsConnection().get(getAbsoluteUrl(String.format("/accounts/%s",accountId)), 60, null, prefKeyOauthTokenT); + String response = new HttpsConnection(context).get(getAbsoluteUrl(String.format("/accounts/%s",accountId)), 60, null, prefKeyOauthTokenT); account = parseAccountResponse(context, new JSONObject(response)); } catch (HttpsConnection.HttpsConnectionException e) { setError(e.getStatusCode(), e); @@ -227,7 +227,7 @@ public class API { HashMap params = new HashMap<>(); params.put("id",accountId); try { - String response = new HttpsConnection().get(getAbsoluteUrl("/accounts/relationships"), 60, params, prefKeyOauthTokenT); + String response = new HttpsConnection(context).get(getAbsoluteUrl("/accounts/relationships"), 60, params, prefKeyOauthTokenT); relationships = parseRelationshipResponse(new JSONArray(response)); if( relationships != null && relationships.size() > 0) relationship = relationships.get(0); @@ -259,7 +259,7 @@ public class API { } List relationships = new ArrayList<>(); try { - HttpsConnection httpsConnection = new HttpsConnection(); + HttpsConnection httpsConnection = new HttpsConnection(context); String response = httpsConnection.get(getAbsoluteUrl("/accounts/relationships"), 60, params, prefKeyOauthTokenT); relationships = parseRelationshipResponse(new JSONArray(response)); apiResponse.setSince_id(httpsConnection.getSince_id()); @@ -348,7 +348,7 @@ public class API { statuses = new ArrayList<>(); try { - HttpsConnection httpsConnection = new HttpsConnection(); + HttpsConnection httpsConnection = new HttpsConnection(context); String response = httpsConnection.get(getAbsoluteUrl(String.format("/accounts/%s/statuses", accountId)), 60, params, prefKeyOauthTokenT); statuses = parseStatuses(new JSONArray(response)); apiResponse.setSince_id(httpsConnection.getSince_id()); @@ -375,7 +375,7 @@ public class API { statuses = new ArrayList<>(); try { - HttpsConnection httpsConnection = new HttpsConnection(); + HttpsConnection httpsConnection = new HttpsConnection(context); String response = httpsConnection.get(getAbsoluteUrl(String.format("/statuses/%s", statusId)), 60, null, prefKeyOauthTokenT); Status status = parseStatuses(context, new JSONObject(response)); statuses.add(status); @@ -397,7 +397,7 @@ public class API { public fr.gouv.etalab.mastodon.client.Entities.Context getStatusContext(String statusId) { fr.gouv.etalab.mastodon.client.Entities.Context statusContext = new fr.gouv.etalab.mastodon.client.Entities.Context(); try { - HttpsConnection httpsConnection = new HttpsConnection(); + HttpsConnection httpsConnection = new HttpsConnection(context); String response = httpsConnection.get(getAbsoluteUrl(String.format("/statuses/%s/context", statusId)), 60, null, prefKeyOauthTokenT); statusContext = parseContext(new JSONObject(response)); } catch (HttpsConnection.HttpsConnectionException e) { @@ -448,7 +448,7 @@ public class API { params.put("limit",String.valueOf(limit)); statuses = new ArrayList<>(); try { - HttpsConnection httpsConnection = new HttpsConnection(); + HttpsConnection httpsConnection = new HttpsConnection(context); String response = httpsConnection.get(getAbsoluteUrl("/timelines/home"), 60, params, prefKeyOauthTokenT); apiResponse.setSince_id(httpsConnection.getSince_id()); apiResponse.setMax_id(httpsConnection.getMax_id()); @@ -507,7 +507,7 @@ public class API { params.put("limit",String.valueOf(limit)); statuses = new ArrayList<>(); try { - HttpsConnection httpsConnection = new HttpsConnection(); + HttpsConnection httpsConnection = new HttpsConnection(context); String response = httpsConnection.get(getAbsoluteUrl("/timelines/public"), 60, params, prefKeyOauthTokenT); apiResponse.setSince_id(httpsConnection.getSince_id()); apiResponse.setMax_id(httpsConnection.getMax_id()); @@ -556,7 +556,7 @@ public class API { params.put("limit",String.valueOf(limit)); statuses = new ArrayList<>(); try { - HttpsConnection httpsConnection = new HttpsConnection(); + HttpsConnection httpsConnection = new HttpsConnection(context); String response = httpsConnection.get(getAbsoluteUrl(String.format("/timelines/tag/%s",tag.trim())), 60, params, prefKeyOauthTokenT); apiResponse.setSince_id(httpsConnection.getSince_id()); apiResponse.setMax_id(httpsConnection.getMax_id()); @@ -630,7 +630,7 @@ public class API { params.put("limit",String.valueOf(limit)); accounts = new ArrayList<>(); try { - HttpsConnection httpsConnection = new HttpsConnection(); + HttpsConnection httpsConnection = new HttpsConnection(context); String response = httpsConnection.get(getAbsoluteUrl(action), 60, params, prefKeyOauthTokenT); apiResponse.setSince_id(httpsConnection.getSince_id()); apiResponse.setMax_id(httpsConnection.getMax_id()); @@ -673,7 +673,7 @@ public class API { params.put("limit",String.valueOf(limit)); accounts = new ArrayList<>(); try { - HttpsConnection httpsConnection = new HttpsConnection(); + HttpsConnection httpsConnection = new HttpsConnection(context); String response = httpsConnection.get(getAbsoluteUrl("/follow_requests"), 60, params, prefKeyOauthTokenT); apiResponse.setSince_id(httpsConnection.getSince_id()); apiResponse.setMax_id(httpsConnection.getMax_id()); @@ -716,7 +716,7 @@ public class API { params.put("limit",String.valueOf(limit)); statuses = new ArrayList<>(); try { - HttpsConnection httpsConnection = new HttpsConnection(); + HttpsConnection httpsConnection = new HttpsConnection(context); String response = httpsConnection.get(getAbsoluteUrl("/favourites"), 60, params, prefKeyOauthTokenT); apiResponse.setSince_id(httpsConnection.getSince_id()); apiResponse.setMax_id(httpsConnection.getMax_id()); @@ -752,7 +752,7 @@ public class API { HashMap params = new HashMap<>(); params.put("notifications", Boolean.toString(muteNotifications)); try { - HttpsConnection httpsConnection = new HttpsConnection(); + HttpsConnection httpsConnection = new HttpsConnection(context); httpsConnection.post(getAbsoluteUrl(String.format("/accounts/%s/mute", targetedId)), 60, params, prefKeyOauthTokenT); actionCode = httpsConnection.getActionCode(); } catch (HttpsConnection.HttpsConnectionException e) { @@ -882,7 +882,7 @@ public class API { if(statusAction != StatusAction.UNSTATUS ) { try { - HttpsConnection httpsConnection = new HttpsConnection(); + HttpsConnection httpsConnection = new HttpsConnection(context); httpsConnection.post(getAbsoluteUrl(action), 60, params, prefKeyOauthTokenT); actionCode = httpsConnection.getActionCode(); } catch (HttpsConnection.HttpsConnectionException e) { @@ -892,7 +892,7 @@ public class API { } }else{ try { - HttpsConnection httpsConnection = new HttpsConnection(); + HttpsConnection httpsConnection = new HttpsConnection(context); httpsConnection.delete(getAbsoluteUrl(action), 60, null, prefKeyOauthTokenT); actionCode = httpsConnection.getActionCode(); } catch (HttpsConnection.HttpsConnectionException e) { @@ -939,7 +939,7 @@ public class API { statuses = new ArrayList<>(); try { - HttpsConnection httpsConnection = new HttpsConnection(); + HttpsConnection httpsConnection = new HttpsConnection(context); String response = httpsConnection.post(getAbsoluteUrl("/statuses"), 60, params, prefKeyOauthTokenT); apiResponse.setSince_id(httpsConnection.getSince_id()); apiResponse.setMax_id(httpsConnection.getMax_id()); @@ -971,7 +971,7 @@ public class API { action = "/notifications/dismiss"; } try { - new HttpsConnection().post(getAbsoluteUrl(action), 60, params, prefKeyOauthTokenT); + new HttpsConnection(context).post(getAbsoluteUrl(action), 60, params, prefKeyOauthTokenT); } catch (HttpsConnection.HttpsConnectionException e) { setError(e.getStatusCode(), e); }catch (Exception e) { @@ -1060,7 +1060,7 @@ public class API { List notifications = new ArrayList<>(); try { - HttpsConnection httpsConnection = new HttpsConnection(); + HttpsConnection httpsConnection = new HttpsConnection(context); String response = httpsConnection.get(getAbsoluteUrl("/notifications"), 60, params, prefKeyOauthTokenT); apiResponse.setSince_id(httpsConnection.getSince_id()); apiResponse.setMax_id(httpsConnection.getMax_id()); @@ -1092,7 +1092,7 @@ public class API { params.put("description", description); } try { - HttpsConnection httpsConnection = new HttpsConnection(); + HttpsConnection httpsConnection = new HttpsConnection(context); String response = httpsConnection.put(getAbsoluteUrl(String.format("/media/%s", mediaId)), 240, params, prefKeyOauthTokenT); attachment = parseAttachmentResponse(new JSONObject(response)); } catch (HttpsConnection.HttpsConnectionException e) { @@ -1114,7 +1114,7 @@ public class API { HashMap params = new HashMap<>(); params.put("q", query); try { - HttpsConnection httpsConnection = new HttpsConnection(); + HttpsConnection httpsConnection = new HttpsConnection(context); String response = httpsConnection.get(getAbsoluteUrl("/search"), 60, params, prefKeyOauthTokenT); results = parseResultsResponse(new JSONObject(response)); } catch (HttpsConnection.HttpsConnectionException e) { @@ -1158,7 +1158,7 @@ public class API { params.put("limit", String.valueOf(count)); try { - HttpsConnection httpsConnection = new HttpsConnection(); + HttpsConnection httpsConnection = new HttpsConnection(context); String response = httpsConnection.get(getAbsoluteUrl("/accounts/search"), 60, params, prefKeyOauthTokenT); accounts = parseAccountResponse(new JSONArray(response)); apiResponse.setSince_id(httpsConnection.getSince_id()); @@ -1184,7 +1184,7 @@ public class API { public APIResponse getCustomEmoji() { List emojis = new ArrayList<>(); try { - HttpsConnection httpsConnection = new HttpsConnection(); + HttpsConnection httpsConnection = new HttpsConnection(context); String response = httpsConnection.get(getAbsoluteUrl("/custom_emojis"), 60, null, prefKeyOauthTokenT); emojis = parseEmojis(new JSONArray(response)); apiResponse.setSince_id(httpsConnection.getSince_id()); @@ -1209,7 +1209,7 @@ public class API { List lists = new ArrayList<>(); try { - String response = new HttpsConnection().get(getAbsoluteUrl("/lists"), 60, null, prefKeyOauthTokenT); + String response = new HttpsConnection(context).get(getAbsoluteUrl("/lists"), 60, null, prefKeyOauthTokenT); lists = parseLists(new JSONArray(response)); } catch (HttpsConnection.HttpsConnectionException e) { setError(e.getStatusCode(), e); @@ -1229,7 +1229,7 @@ public class API { List lists = new ArrayList<>(); fr.gouv.etalab.mastodon.client.Entities.List list; try { - String response = new HttpsConnection().get(getAbsoluteUrl(String.format("/accounts/%s/lists", userId)), 60, null, prefKeyOauthTokenT); + String response = new HttpsConnection(context).get(getAbsoluteUrl(String.format("/accounts/%s/lists", userId)), 60, null, prefKeyOauthTokenT); list = parseList(new JSONObject(response)); lists.add(list); } catch (HttpsConnection.HttpsConnectionException e) { @@ -1261,7 +1261,7 @@ public class API { params.put("limit",String.valueOf(limit)); statuses = new ArrayList<>(); try { - HttpsConnection httpsConnection = new HttpsConnection(); + HttpsConnection httpsConnection = new HttpsConnection(context); String response = httpsConnection.get(getAbsoluteUrl(String.format("/timelines/list/%s",list_id)), 60, params, prefKeyOauthTokenT); apiResponse.setSince_id(httpsConnection.getSince_id()); apiResponse.setMax_id(httpsConnection.getMax_id()); @@ -1295,7 +1295,7 @@ public class API { limit = 50; params.put("limit",String.valueOf(limit)); try { - HttpsConnection httpsConnection = new HttpsConnection(); + HttpsConnection httpsConnection = new HttpsConnection(context); String response = httpsConnection.get(getAbsoluteUrl(String.format("/lists/%s/accounts", listId)), 60, params, prefKeyOauthTokenT); accounts = parseAccountResponse(new JSONArray(response)); apiResponse.setSince_id(httpsConnection.getSince_id()); @@ -1321,7 +1321,7 @@ public class API { List lists = new ArrayList<>(); fr.gouv.etalab.mastodon.client.Entities.List list; try { - String response = new HttpsConnection().get(getAbsoluteUrl(String.format("/lists/%s",id)), 60, null, prefKeyOauthTokenT); + String response = new HttpsConnection(context).get(getAbsoluteUrl(String.format("/lists/%s",id)), 60, null, prefKeyOauthTokenT); list = parseList(new JSONObject(response)); lists.add(list); } catch (HttpsConnection.HttpsConnectionException e) { @@ -1355,7 +1355,7 @@ public class API { List lists = new ArrayList<>(); fr.gouv.etalab.mastodon.client.Entities.List list; try { - new HttpsConnection().post(getAbsoluteUrl(String.format("/lists/%s/accounts", id)), 60, params, prefKeyOauthTokenT); + new HttpsConnection(context).post(getAbsoluteUrl(String.format("/lists/%s/accounts", id)), 60, params, prefKeyOauthTokenT); } catch (HttpsConnection.HttpsConnectionException e) { setError(e.getStatusCode(), e); }catch (Exception e) { @@ -1372,7 +1372,7 @@ public class API { */ public int deleteAccountFromList(String id, String[] account_ids){ try { - HttpsConnection httpsConnection = new HttpsConnection(); + HttpsConnection httpsConnection = new HttpsConnection(context); StringBuilder parameters = new StringBuilder(); HashMap params = new HashMap<>(); for(String val: account_ids) @@ -1405,7 +1405,7 @@ public class API { List lists = new ArrayList<>(); fr.gouv.etalab.mastodon.client.Entities.List list; try { - String response = new HttpsConnection().post(getAbsoluteUrl("/lists"), 60, params, prefKeyOauthTokenT); + String response = new HttpsConnection(context).post(getAbsoluteUrl("/lists"), 60, params, prefKeyOauthTokenT); list = parseList(new JSONObject(response)); lists.add(list); } catch (HttpsConnection.HttpsConnectionException e) { @@ -1427,7 +1427,7 @@ public class API { Card card = null; try { - String response = new HttpsConnection().get(getAbsoluteUrl(String.format("/statuses/%s/card", statusId)), 60, null, prefKeyOauthTokenT); + String response = new HttpsConnection(context).get(getAbsoluteUrl(String.format("/statuses/%s/card", statusId)), 60, null, prefKeyOauthTokenT); card = parseCardResponse(new JSONObject(response)); }catch (Exception ignored) {ignored.printStackTrace();} return card; @@ -1446,7 +1446,7 @@ public class API { List lists = new ArrayList<>(); fr.gouv.etalab.mastodon.client.Entities.List list; try { - String response = new HttpsConnection().put(getAbsoluteUrl(String.format("/lists/%s", id)), 60, params, prefKeyOauthTokenT); + String response = new HttpsConnection(context).put(getAbsoluteUrl(String.format("/lists/%s", id)), 60, params, prefKeyOauthTokenT); list = parseList(new JSONObject(response)); lists.add(list); } catch (HttpsConnection.HttpsConnectionException e) { @@ -1466,7 +1466,7 @@ public class API { */ public int deleteList(String id){ try { - HttpsConnection httpsConnection = new HttpsConnection(); + HttpsConnection httpsConnection = new HttpsConnection(context); httpsConnection.delete(getAbsoluteUrl(String.format("/lists/%s", id)), 60, null, prefKeyOauthTokenT); actionCode = httpsConnection.getActionCode(); } catch (HttpsConnection.HttpsConnectionException e) { diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/client/HttpsConnection.java b/app/src/main/java/fr/gouv/etalab/mastodon/client/HttpsConnection.java index 802e6dc32..e3a377f93 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/client/HttpsConnection.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/client/HttpsConnection.java @@ -31,7 +31,11 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.net.Authenticator; import java.net.HttpURLConnection; +import java.net.InetSocketAddress; +import java.net.PasswordAuthentication; +import java.net.Proxy; import java.net.URL; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; @@ -66,11 +70,32 @@ public class HttpsConnection { private String since_id, max_id; private Context context; private int CHUNK_SIZE = 4096; + private SharedPreferences sharedpreferences; + private Proxy proxy; - public HttpsConnection(){} public HttpsConnection(Context context){ this.context = context; + sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); + boolean proxyEnabled = sharedpreferences.getBoolean(Helper.SET_PROXY_ENABLED, false); + proxy = null; + if( proxyEnabled ){ + String host = sharedpreferences.getString(Helper.SET_PROXY_HOST, "127.0.0.1"); + int port = sharedpreferences.getInt(Helper.SET_PROXY_PORT, 8118); + proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(host, port)); + final String login = sharedpreferences.getString(Helper.SET_PROXY_LOGIN, null); + final String pwd = sharedpreferences.getString(Helper.SET_PROXY_PASSWORD, null); + if( login != null) { + Authenticator authenticator = new Authenticator() { + public PasswordAuthentication getPasswordAuthentication() { + assert pwd != null; + return (new PasswordAuthentication(login, + pwd.toCharArray())); + } + }; + Authenticator.setDefault(authenticator); + } + } } @@ -95,7 +120,10 @@ public class HttpsConnection { postData.append(String.valueOf(param.getValue())); } URL url = new URL(urlConnection + "?" + postData); - httpsURLConnection = (HttpsURLConnection)url.openConnection(); + if( proxy !=null ) + httpsURLConnection = (HttpsURLConnection)url.openConnection(proxy); + else + httpsURLConnection = (HttpsURLConnection)url.openConnection(); httpsURLConnection.setConnectTimeout(timeout * 1000); httpsURLConnection.setRequestProperty("http.keepAlive", "false"); httpsURLConnection.setRequestProperty("User-Agent", Helper.USER_AGENT); @@ -124,7 +152,10 @@ public class HttpsConnection { HttpURLConnection httpURLConnection; if( urlConnection.startsWith("https://")) { URL url = new URL(urlConnection); - httpsURLConnection = (HttpsURLConnection) url.openConnection(); + if( proxy !=null ) + httpsURLConnection = (HttpsURLConnection)url.openConnection(proxy); + else + httpsURLConnection = (HttpsURLConnection)url.openConnection(); httpsURLConnection.setConnectTimeout(30 * 1000); httpsURLConnection.setRequestProperty("http.keepAlive", "false"); httpsURLConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36"); @@ -145,7 +176,10 @@ public class HttpsConnection { return response; }else{ URL url = new URL(urlConnection); - httpURLConnection = (HttpURLConnection) url.openConnection(); + if( proxy !=null ) + httpURLConnection = (HttpURLConnection)url.openConnection(proxy); + else + httpURLConnection = (HttpURLConnection)url.openConnection(); httpURLConnection.setConnectTimeout(30 * 1000); httpURLConnection.setRequestProperty("http.keepAlive", "false"); httpURLConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36"); @@ -188,7 +222,10 @@ public class HttpsConnection { } byte[] postDataBytes = postData.toString().getBytes("UTF-8"); - httpsURLConnection = (HttpsURLConnection)url.openConnection(); + if( proxy !=null ) + httpsURLConnection = (HttpsURLConnection)url.openConnection(proxy); + else + httpsURLConnection = (HttpsURLConnection)url.openConnection(); httpsURLConnection.setRequestProperty("User-Agent", Helper.USER_AGENT); httpsURLConnection.setConnectTimeout(timeout * 1000); httpsURLConnection.setDoOutput(true); @@ -231,7 +268,10 @@ public class HttpsConnection { if (downloadUrl.startsWith("https://")) { try { url = new URL(downloadUrl); - httpsURLConnection = (HttpsURLConnection) url.openConnection(); + if( proxy !=null ) + httpsURLConnection = (HttpsURLConnection)url.openConnection(proxy); + else + httpsURLConnection = (HttpsURLConnection)url.openConnection(); httpsURLConnection.setRequestProperty("User-Agent", Helper.USER_AGENT); int responseCode = httpsURLConnection.getResponseCode(); @@ -318,7 +358,10 @@ public class HttpsConnection { } else { try { url = new URL(downloadUrl); - httpURLConnection = (HttpURLConnection) url.openConnection(); + if( proxy !=null ) + httpURLConnection = (HttpURLConnection)url.openConnection(proxy); + else + httpURLConnection = (HttpURLConnection)url.openConnection(); httpURLConnection.setRequestProperty("User-Agent", Helper.USER_AGENT); int responseCode = httpURLConnection.getResponseCode(); @@ -412,7 +455,10 @@ public class HttpsConnection { public InputStream getPicture(final String downloadUrl) { try { URL url = new URL(downloadUrl); - httpsURLConnection = (HttpsURLConnection) url.openConnection(); + if( proxy !=null ) + httpsURLConnection = (HttpsURLConnection)url.openConnection(proxy); + else + httpsURLConnection = (HttpsURLConnection)url.openConnection(); httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory()); httpsURLConnection.setRequestProperty("User-Agent", Helper.USER_AGENT); int responseCode = httpsURLConnection.getResponseCode(); @@ -449,7 +495,6 @@ public class HttpsConnection { String boundary = "*****" + Long.toString(System.currentTimeMillis()) + "*****"; String lineEnd = "\r\n"; - SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); String token = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null); final URL url = new URL("https://"+Helper.getLiveInstance(context)+"/api/v1/media"); ByteArrayOutputStream ous = null; @@ -475,7 +520,10 @@ public class HttpsConnection { lengthSent += ("Content-Disposition: form-data; name=\"file\";filename=\"picture.png\"" + lineEnd).getBytes().length; lengthSent += 2 * (lineEnd).getBytes().length; - httpsURLConnection = (HttpsURLConnection) url.openConnection(); + if( proxy !=null ) + httpsURLConnection = (HttpsURLConnection)url.openConnection(proxy); + else + httpsURLConnection = (HttpsURLConnection)url.openConnection(); httpsURLConnection.setFixedLengthStreamingMode(lengthSent); httpsURLConnection.setRequestProperty("User-Agent", Helper.USER_AGENT); @@ -599,7 +647,10 @@ public class HttpsConnection { } byte[] postDataBytes = postData.toString().getBytes("UTF-8"); - httpsURLConnection = (HttpsURLConnection)url.openConnection(); + if( proxy !=null ) + httpsURLConnection = (HttpsURLConnection)url.openConnection(proxy); + else + httpsURLConnection = (HttpsURLConnection)url.openConnection(); httpsURLConnection.setRequestProperty("User-Agent", Helper.USER_AGENT); httpsURLConnection.setConnectTimeout(timeout * 1000); httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory()); @@ -650,7 +701,10 @@ public class HttpsConnection { } byte[] postDataBytes = postData.toString().getBytes("UTF-8"); - httpsURLConnection = (HttpsURLConnection)url.openConnection(); + if( proxy !=null ) + httpsURLConnection = (HttpsURLConnection)url.openConnection(proxy); + else + httpsURLConnection = (HttpsURLConnection)url.openConnection(); httpsURLConnection.setRequestProperty("User-Agent", Helper.USER_AGENT); httpsURLConnection.setConnectTimeout(timeout * 1000); httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory()); @@ -699,7 +753,10 @@ public class HttpsConnection { } byte[] postDataBytes = postData.toString().getBytes("UTF-8"); - httpsURLConnection = (HttpsURLConnection)url.openConnection(); + if( proxy !=null ) + httpsURLConnection = (HttpsURLConnection)url.openConnection(proxy); + else + httpsURLConnection = (HttpsURLConnection)url.openConnection(); httpsURLConnection.setRequestProperty("User-Agent", Helper.USER_AGENT); httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory()); if( token != null) diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java b/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java index 6cbee447b..f5fa46ee2 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java @@ -270,6 +270,7 @@ public class Helper { //Proxy + public static final String SET_PROXY_ENABLED = "set_proxy_enabled"; public static final String SET_PROXY_HOST = "set_proxy_host"; public static final String SET_PROXY_PORT = "set_proxy_port"; public static final String SET_PROXY_LOGIN = "set_proxy_login";