JAL-4117 JAL-1551 source licensing
[jalview.git] / src / jalview / analytics / Plausible.java
index 4dd0d35..4df12b4 100644 (file)
@@ -1,3 +1,23 @@
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ * 
+ * This file is part of Jalview.
+ * 
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License 
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *  
+ * Jalview is distributed in the hope that it will be useful, but 
+ * WITHOUT ANY WARRANTY; without even the implied warranty 
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
+ * PURPOSE.  See the GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
 package jalview.analytics;
 
 import java.io.BufferedReader;
@@ -19,23 +39,28 @@ import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.Random;
 
 import jalview.bin.Cache;
 import jalview.bin.Console;
-import jalview.bin.Jalview;
 import jalview.util.ChannelProperties;
 import jalview.util.HttpUtils;
 
 public class Plausible
 {
-  private static final String USER_AGENT = HttpUtils.getUserAgent(
-          MethodHandles.lookup().lookupClass().getCanonicalName());
+  private static final String USER_AGENT;
 
   private static final String JALVIEW_ID = "Jalview Desktop";
 
   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/services/config/analytics/url";
+
+  private static final String DEFAULT_API_BASE_URL = "https://analytics.jalview.org/api/event";
+
+  private static final String API_BASE_URL;
+
+  private static final String clientId;
 
   public static final String APPLICATION_BASE_URL = "desktop://localhost";
 
@@ -81,6 +106,25 @@ public class Plausible
             System.getProperty("os.arch") + " "
                     + System.getProperty("os.name") + " "
                     + System.getProperty("os.version"));
+    defaultProps.put("os", System.getProperty("os.name"));
+    defaultProps.put("os_version", System.getProperty("os.version"));
+    defaultProps.put("os_arch", System.getProperty("os.arch"));
+    String installation = Cache.applicationProperties
+            .getProperty("INSTALLATION");
+    if (installation != null)
+    {
+      defaultProps.put("installation", installation);
+    }
+
+    // ascertain the API_BASE_URL
+    API_BASE_URL = getAPIBaseURL();
+
+    // random clientId to make User-Agent unique (to register analytic)
+    clientId = String.format("%08x", new Random().nextInt());
+
+    USER_AGENT = HttpUtils.getUserAgent(
+            MethodHandles.lookup().lookupClass().getCanonicalName() + " "
+                    + clientId);
   }
 
   private Plausible()
@@ -93,10 +137,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 +153,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
@@ -130,10 +174,6 @@ public class Plausible
     if (sendDefaultProps)
     {
       props.putAll(defaultProps);
-      if (Jalview.isHeadlessMode())
-      {
-        props.put("headless", "true");
-      }
     }
 
     // add (and overwrite with) the passed in props
@@ -154,13 +194,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 +547,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;
+  }
+}