JAL-4001 Dynamic API_BASE_URL lookup at https://www.jalview.org/config/analytics...
authorBen Soares <b.soares@dundee.ac.uk>
Thu, 22 Jun 2023 15:05:25 +0000 (16:05 +0100)
committerBen Soares <b.soares@dundee.ac.uk>
Thu, 22 Jun 2023 15:05:25 +0000 (16:05 +0100)
src/jalview/analytics/Plausible.java

index 4dd0d35..0c085d0 100644 (file)
@@ -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<String, String>(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;
+  }
+}