From 74d777aaa2dd40376096064df2cb23a87efd47ad Mon Sep 17 00:00:00 2001 From: Ben Soares Date: Thu, 22 Jun 2023 16:05:25 +0100 Subject: [PATCH] JAL-4001 Dynamic API_BASE_URL lookup at https://www.jalview.org/config/analytics/url. Change more terminology. Add 'installation' property. --- src/jalview/analytics/Plausible.java | 89 +++++++++++++++++++++++++++++----- 1 file changed, 77 insertions(+), 12 deletions(-) diff --git a/src/jalview/analytics/Plausible.java b/src/jalview/analytics/Plausible.java index 4dd0d35..0c085d0 100644 --- a/src/jalview/analytics/Plausible.java +++ b/src/jalview/analytics/Plausible.java @@ -35,7 +35,11 @@ public class Plausible private static final String DOMAIN = "jalview.org"; - private static final String API_BASE_URL = "https://plausible.io/api/event"; + private static final String CONFIG_API_BASE_URL = "https://www.jalview.org/config/analytics/url"; + + private static final String DEFAULT_API_BASE_URL = "https://DEFAULT.plausible.io/api/event"; + + private static final String API_BASE_URL; public static final String APPLICATION_BASE_URL = "desktop://localhost"; @@ -81,6 +85,15 @@ public class Plausible System.getProperty("os.arch") + " " + System.getProperty("os.name") + " " + System.getProperty("os.version")); + String installation = Cache.applicationProperties + .getProperty("INSTALLATION"); + if (installation != null) + { + defaultProps.put("installation", installation); + } + + // ascertain the API_BASE_URL + API_BASE_URL = getAPIBaseURL(); } private Plausible() @@ -93,10 +106,10 @@ public class Plausible ENABLED = b; } - public void sendEvent(String eventName, String path, + public void sendEvent(String eventName, String urlString, String... propsStrings) { - sendEvent(eventName, path, false, propsStrings); + sendEvent(eventName, urlString, false, propsStrings); } /** @@ -109,11 +122,11 @@ public class Plausible * Flag whether to add the default props about the application. * @param propsStrings * Optional multiple Strings in key, value pairs (there should be an - * even number of propsStrings) to be set as properties of the event. - * To emulate a webpage view use "url" as the URL in a "pageview" + * even number of propsStrings) to be set as property of the event. + * To emulate a webpage view set "url" as the URL in a "pageview" * event. */ - public void sendEvent(String eventName, String path, + public void sendEvent(String eventName, String urlString, boolean sendDefaultProps, String... propsStrings) { // clear out old lists @@ -154,13 +167,13 @@ public class Plausible addJsonValue("domain", DOMAIN); addJsonValue("name", eventName); - StringBuilder recordedUrlSb = new StringBuilder(APPLICATION_BASE_URL); - if (!APPLICATION_BASE_URL.endsWith("/") && !path.startsWith("/")) + StringBuilder eventUrlSb = new StringBuilder(APPLICATION_BASE_URL); + if (!APPLICATION_BASE_URL.endsWith("/") && !urlString.startsWith("/")) { - recordedUrlSb.append("/"); + eventUrlSb.append("/"); } - recordedUrlSb.append(path); - addJsonValue("url", recordedUrlSb.toString()); + eventUrlSb.append(urlString); + addJsonValue("url", eventUrlSb.toString()); addJsonObject("props", props); StringBuilder urlSb = new StringBuilder(); urlSb.append(API_BASE_URL); @@ -507,4 +520,56 @@ public class Plausible { return new AbstractMap.SimpleEntry(s, v); } -} \ No newline at end of file + + private static String getAPIBaseURL() + { + try + { + URL url = new URL(CONFIG_API_BASE_URL); + URLConnection urlConnection = url.openConnection(); + HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection; + httpURLConnection.setRequestMethod("GET"); + httpURLConnection.setRequestProperty("User-Agent", USER_AGENT); + httpURLConnection.setConnectTimeout(5000); + httpURLConnection.setReadTimeout(3000); + httpURLConnection.connect(); + int responseCode = httpURLConnection.getResponseCode(); + String responseMessage = httpURLConnection.getResponseMessage(); + + if (responseCode < 200 || responseCode > 299) + { + Console.warn("Config URL connection to '" + CONFIG_API_BASE_URL + + "' failed: '" + responseCode + " " + responseMessage + + "'"); + } + + BufferedReader br = new BufferedReader( + new InputStreamReader((httpURLConnection.getInputStream()))); + StringBuilder sb = new StringBuilder(); + String response; + while ((response = br.readLine()) != null) + { + sb.append(response); + } + if (sb.length() > 7 && sb.substring(0, 5).equals("https")) + { + return sb.toString(); + } + + } catch (MalformedURLException e) + { + Console.debug("Somehow the config URL is malformed: '" + + CONFIG_API_BASE_URL + "'", e); + } catch (IOException e) + { + Console.debug("Connection to Plausible BASE_URL '" + API_BASE_URL + + "' failed.", e); + } catch (ClassCastException e) + { + Console.debug( + "Couldn't cast URLConnection to HttpURLConnection in Plausible.", + e); + } + return DEFAULT_API_BASE_URL; + } +} -- 1.7.10.2