Refactoring

pull/214/head
M66B 1 year ago
parent 8346ded5c9
commit 3a5c8d6415

@ -23,10 +23,11 @@ import android.content.Context;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import java.io.IOException; import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import javax.net.ssl.HttpsURLConnection;
public class Avatar { public class Avatar {
static final String GRAVATAR_PRIVACY_URI = "https://automattic.com/privacy/"; static final String GRAVATAR_PRIVACY_URI = "https://automattic.com/privacy/";
static final String LIBRAVATAR_PRIVACY_URI = "https://www.libravatar.org/privacy/"; static final String LIBRAVATAR_PRIVACY_URI = "https://www.libravatar.org/privacy/";
@ -47,7 +48,7 @@ public class Avatar {
URL url = new URL(GRAVATAR_URI + hash + "?d=404"); URL url = new URL(GRAVATAR_URI + hash + "?d=404");
Log.i("Gravatar key=" + email + " url=" + url); Log.i("Gravatar key=" + email + " url=" + url);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET"); urlConnection.setRequestMethod("GET");
urlConnection.setReadTimeout(GRAVATAR_READ_TIMEOUT); urlConnection.setReadTimeout(GRAVATAR_READ_TIMEOUT);
urlConnection.setConnectTimeout(GRAVATAR_CONNECT_TIMEOUT); urlConnection.setConnectTimeout(GRAVATAR_CONNECT_TIMEOUT);
@ -56,11 +57,11 @@ public class Avatar {
try { try {
int status = urlConnection.getResponseCode(); int status = urlConnection.getResponseCode();
if (status == HttpURLConnection.HTTP_OK) { if (status == HttpsURLConnection.HTTP_OK) {
// Positive reply // Positive reply
Bitmap bitmap = ImageHelper.getScaledBitmap(urlConnection.getInputStream(), url.toString(), null, scaleToPixels); Bitmap bitmap = ImageHelper.getScaledBitmap(urlConnection.getInputStream(), url.toString(), null, scaleToPixels);
return (bitmap == null ? null : new ContactInfo.Favicon(bitmap, "gravatar", false)); return (bitmap == null ? null : new ContactInfo.Favicon(bitmap, "gravatar", false));
} else if (status == HttpURLConnection.HTTP_NOT_FOUND) { } else if (status == HttpsURLConnection.HTTP_NOT_FOUND) {
// Negative reply // Negative reply
return null; return null;
} else } else
@ -93,7 +94,7 @@ public class Avatar {
URL url = new URL(baseUrl + hash + "?d=404"); URL url = new URL(baseUrl + hash + "?d=404");
Log.i("Libravatar key=" + email + " url=" + url); Log.i("Libravatar key=" + email + " url=" + url);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET"); urlConnection.setRequestMethod("GET");
urlConnection.setReadTimeout(LIBRAVATAR_READ_TIMEOUT); urlConnection.setReadTimeout(LIBRAVATAR_READ_TIMEOUT);
urlConnection.setConnectTimeout(LIBRAVATAR_CONNECT_TIMEOUT); urlConnection.setConnectTimeout(LIBRAVATAR_CONNECT_TIMEOUT);
@ -102,11 +103,11 @@ public class Avatar {
try { try {
int status = urlConnection.getResponseCode(); int status = urlConnection.getResponseCode();
if (status == HttpURLConnection.HTTP_OK) { if (status == HttpsURLConnection.HTTP_OK) {
// Positive reply // Positive reply
Bitmap bitmap = ImageHelper.getScaledBitmap(urlConnection.getInputStream(), url.toString(), null, scaleToPixels); Bitmap bitmap = ImageHelper.getScaledBitmap(urlConnection.getInputStream(), url.toString(), null, scaleToPixels);
return (bitmap == null ? null : new ContactInfo.Favicon(bitmap, "libravatar", false)); return (bitmap == null ? null : new ContactInfo.Favicon(bitmap, "libravatar", false));
} else if (status == HttpURLConnection.HTTP_NOT_FOUND) { } else if (status == HttpsURLConnection.HTTP_NOT_FOUND) {
// Negative reply // Negative reply
return null; return null;
} else } else

@ -50,7 +50,6 @@ import java.io.ByteArrayInputStream;
import java.io.File; import java.io.File;
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.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.text.DateFormat; import java.text.DateFormat;
@ -75,6 +74,7 @@ import javax.mail.Part;
import javax.mail.internet.AddressException; import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress; import javax.mail.internet.InternetAddress;
import javax.mail.internet.InternetHeaders; import javax.mail.internet.InternetHeaders;
import javax.net.ssl.HttpsURLConnection;
@Entity( @Entity(
tableName = EntityRule.TABLE_NAME, tableName = EntityRule.TABLE_NAME,
@ -1447,9 +1447,9 @@ public class EntityRule {
Log.i("GET " + url); Log.i("GET " + url);
HttpURLConnection connection = null; HttpsURLConnection connection = null;
try { try {
connection = (HttpURLConnection) new URL(url).openConnection(); connection = (HttpsURLConnection) new URL(url).openConnection();
connection.setRequestMethod(method); connection.setRequestMethod(method);
connection.setDoOutput(body != null); connection.setDoOutput(body != null);
connection.setReadTimeout(URL_TIMEOUT); connection.setReadTimeout(URL_TIMEOUT);

@ -56,10 +56,11 @@ import org.jsoup.nodes.Element;
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.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import javax.net.ssl.HttpsURLConnection;
public class FragmentDialogInsertLink extends FragmentDialogBase { public class FragmentDialogInsertLink extends FragmentDialogBase {
private EditText etLink; private EditText etLink;
private CheckBox cbImage; private CheckBox cbImage;
@ -132,7 +133,7 @@ public class FragmentDialogInsertLink extends FragmentDialogBase {
tvInsecure.setVisibility( tvInsecure.setVisibility(
!UriHelper.isHyperLink(uri) || UriHelper.isSecure(uri) !UriHelper.isHyperLink(uri) || UriHelper.isSecure(uri)
? View.GONE : View.VISIBLE); ? View.GONE : View.VISIBLE);
btnMetadata.setEnabled(UriHelper.isHyperLink(uri)); btnMetadata.setEnabled(UriHelper.isSecure(uri));
} }
}); });
@ -162,7 +163,7 @@ public class FragmentDialogInsertLink extends FragmentDialogBase {
OpenGraph og = new OpenGraph(); OpenGraph og = new OpenGraph();
HttpURLConnection connection = (HttpURLConnection) base.openConnection(); HttpsURLConnection connection = (HttpsURLConnection) base.openConnection();
connection.setRequestMethod("GET"); connection.setRequestMethod("GET");
connection.setReadTimeout(METADATA_READ_TIMEOUT); connection.setReadTimeout(METADATA_READ_TIMEOUT);
connection.setConnectTimeout(METADATA_CONNECT_TIMEOUT); connection.setConnectTimeout(METADATA_CONNECT_TIMEOUT);
@ -172,7 +173,7 @@ public class FragmentDialogInsertLink extends FragmentDialogBase {
try { try {
int status = connection.getResponseCode(); int status = connection.getResponseCode();
if (status != HttpURLConnection.HTTP_OK) { if (status != HttpsURLConnection.HTTP_OK) {
String error = "Error " + status + ": " + connection.getResponseMessage(); String error = "Error " + status + ": " + connection.getResponseMessage();
try { try {
InputStream is = connection.getErrorStream(); InputStream is = connection.getErrorStream();

@ -42,7 +42,6 @@ import org.json.JSONObject;
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.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -50,6 +49,8 @@ import java.util.Locale;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import javax.net.ssl.HttpsURLConnection;
public class LanguageTool { public class LanguageTool {
static final String LT_URI = "https://api.languagetool.org/v2/"; static final String LT_URI = "https://api.languagetool.org/v2/";
static final String LT_URI_PLUS = "https://api.languagetoolplus.com/v2/"; static final String LT_URI_PLUS = "https://api.languagetoolplus.com/v2/";
@ -87,7 +88,7 @@ public class LanguageTool {
Log.i("LT uri=" + uri); Log.i("LT uri=" + uri);
URL url = new URL(uri.toString()); URL url = new URL(uri.toString());
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("GET"); connection.setRequestMethod("GET");
connection.setDoOutput(false); connection.setDoOutput(false);
connection.setReadTimeout(LT_TIMEOUT * 1000); connection.setReadTimeout(LT_TIMEOUT * 1000);
@ -248,7 +249,7 @@ public class LanguageTool {
Log.i("LT uri=" + uri + " request=" + request); Log.i("LT uri=" + uri + " request=" + request);
URL url = new URL(uri.toString()); URL url = new URL(uri.toString());
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("POST"); connection.setRequestMethod("POST");
connection.setDoOutput(true); connection.setDoOutput(true);
connection.setReadTimeout(LT_TIMEOUT * 1000); connection.setReadTimeout(LT_TIMEOUT * 1000);
@ -327,7 +328,7 @@ public class LanguageTool {
Log.i("LT uri=" + uri + " request=" + request); Log.i("LT uri=" + uri + " request=" + request);
URL url = new URL(uri.toString()); URL url = new URL(uri.toString());
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("POST"); connection.setRequestMethod("POST");
connection.setDoOutput(true); connection.setDoOutput(true);
connection.setReadTimeout(LT_TIMEOUT * 1000); connection.setReadTimeout(LT_TIMEOUT * 1000);
@ -378,7 +379,7 @@ public class LanguageTool {
Log.i("LT uri=" + uri); Log.i("LT uri=" + uri);
URL url = new URL(uri.toString()); URL url = new URL(uri.toString());
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("GET"); connection.setRequestMethod("GET");
connection.setDoOutput(false); connection.setDoOutput(false);
connection.setReadTimeout(LT_TIMEOUT * 1000); connection.setReadTimeout(LT_TIMEOUT * 1000);
@ -460,9 +461,9 @@ public class LanguageTool {
return (!TextUtils.isEmpty(lt_user) && !TextUtils.isEmpty(lt_key)); return (!TextUtils.isEmpty(lt_user) && !TextUtils.isEmpty(lt_key));
} }
private static void checkStatus(HttpURLConnection connection) throws IOException { private static void checkStatus(HttpsURLConnection connection) throws IOException {
int status = connection.getResponseCode(); int status = connection.getResponseCode();
if (status != HttpURLConnection.HTTP_OK) { if (status != HttpsURLConnection.HTTP_OK) {
String error = "Error " + status + ": " + connection.getResponseMessage(); String error = "Error " + status + ": " + connection.getResponseMessage();
try { try {
InputStream is = connection.getErrorStream(); InputStream is = connection.getErrorStream();

@ -37,13 +37,13 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.util.Date; import java.util.Date;
import java.util.Objects; import java.util.Objects;
import javax.mail.MessagingException; import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMessage;
import javax.net.ssl.HttpsURLConnection;
public class MicrosoftGraph { public class MicrosoftGraph {
static final int GRAPH_TIMEOUT = 20; // seconds static final int GRAPH_TIMEOUT = 20; // seconds
@ -68,7 +68,7 @@ public class MicrosoftGraph {
// https://learn.microsoft.com/en-us/graph/api/user-sendmail?view=graph-rest-1.0 // https://learn.microsoft.com/en-us/graph/api/user-sendmail?view=graph-rest-1.0
URL url = new URL(MicrosoftGraph.GRAPH_ENDPOINT + "sendMail"); URL url = new URL(MicrosoftGraph.GRAPH_ENDPOINT + "sendMail");
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("POST"); connection.setRequestMethod("POST");
connection.setDoOutput(true); connection.setDoOutput(true);
connection.setReadTimeout(MicrosoftGraph.GRAPH_TIMEOUT * 1000); connection.setReadTimeout(MicrosoftGraph.GRAPH_TIMEOUT * 1000);
@ -90,7 +90,7 @@ public class MicrosoftGraph {
long end = new Date().getTime(); long end = new Date().getTime();
int status = connection.getResponseCode(); int status = connection.getResponseCode();
if (status == HttpURLConnection.HTTP_ACCEPTED) { if (status == HttpsURLConnection.HTTP_ACCEPTED) {
EntityLog.log(context, "Sent via Graph " + ident.user + " elapse=" + (end - start) + " ms"); EntityLog.log(context, "Sent via Graph " + ident.user + " elapse=" + (end - start) + " ms");
boolean log = prefs.getBoolean("protocol", false); boolean log = prefs.getBoolean("protocol", false);
if (log || BuildConfig.DEBUG) if (log || BuildConfig.DEBUG)
@ -136,7 +136,7 @@ public class MicrosoftGraph {
// https://learn.microsoft.com/en-us/graph/api/user-list-contacts?view=graph-rest-1.0&tabs=http // https://learn.microsoft.com/en-us/graph/api/user-list-contacts?view=graph-rest-1.0&tabs=http
URL url = new URL(MicrosoftGraph.GRAPH_ENDPOINT + "contacts"); URL url = new URL(MicrosoftGraph.GRAPH_ENDPOINT + "contacts");
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("GET"); connection.setRequestMethod("GET");
connection.setReadTimeout(GRAPH_TIMEOUT * 1000); connection.setReadTimeout(GRAPH_TIMEOUT * 1000);
connection.setConnectTimeout(GRAPH_TIMEOUT * 1000); connection.setConnectTimeout(GRAPH_TIMEOUT * 1000);
@ -146,7 +146,7 @@ public class MicrosoftGraph {
try { try {
int status = connection.getResponseCode(); int status = connection.getResponseCode();
if (status == HttpURLConnection.HTTP_OK) { if (status == HttpsURLConnection.HTTP_OK) {
String response = Helper.readStream(connection.getInputStream()); String response = Helper.readStream(connection.getInputStream());
JSONObject jroot = new JSONObject(response); JSONObject jroot = new JSONObject(response);
JSONArray jvalue = jroot.getJSONArray("value"); JSONArray jvalue = jroot.getJSONArray("value");

@ -33,7 +33,6 @@ import org.json.JSONObject;
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.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
@ -41,6 +40,8 @@ import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import javax.net.ssl.HttpsURLConnection;
public class OpenAI { public class OpenAI {
private static final int MAX_OPENAI_LEN = 1000; // characters private static final int MAX_OPENAI_LEN = 1000; // characters
private static final int TIMEOUT = 45; // seconds private static final int TIMEOUT = 45; // seconds
@ -146,7 +147,8 @@ public class OpenAI {
long start = new Date().getTime(); long start = new Date().getTime();
URL url = new URL(uri.toString()); URL url = new URL(uri.toString());
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod(method); connection.setRequestMethod(method);
connection.setDoOutput(args != null); connection.setDoOutput(args != null);
connection.setDoInput(true); connection.setDoInput(true);
@ -166,7 +168,7 @@ public class OpenAI {
} }
int status = connection.getResponseCode(); int status = connection.getResponseCode();
if (status != HttpURLConnection.HTTP_OK) { if (status != HttpsURLConnection.HTTP_OK) {
// https://platform.openai.com/docs/guides/error-codes/api-errors // https://platform.openai.com/docs/guides/error-codes/api-errors
String error = "Error " + status + ": " + connection.getResponseMessage(); String error = "Error " + status + ": " + connection.getResponseMessage();
try { try {

Loading…
Cancel
Save