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";
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()
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);
}
/**
* 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
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);
{
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;
+ }
+}