2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.analytics;
23 import java.io.BufferedReader;
24 import java.io.IOException;
25 import java.io.InputStreamReader;
26 import java.io.OutputStream;
27 import java.io.UnsupportedEncodingException;
28 import java.lang.invoke.MethodHandles;
29 import java.net.HttpURLConnection;
30 import java.net.MalformedURLException;
32 import java.net.URLConnection;
33 import java.net.URLEncoder;
34 import java.nio.charset.StandardCharsets;
35 import java.util.AbstractMap;
36 import java.util.ArrayList;
37 import java.util.Collections;
38 import java.util.HashMap;
39 import java.util.Iterator;
40 import java.util.List;
42 import java.util.Random;
44 import jalview.bin.Cache;
45 import jalview.bin.Console;
46 import jalview.util.ChannelProperties;
47 import jalview.util.HttpUtils;
49 public class Plausible
51 private static final String USER_AGENT;
53 private static final String JALVIEW_ID = "Jalview Desktop";
55 private static final String DOMAIN = "jalview.org";
57 private static final String CONFIG_API_BASE_URL = "https://www.jalview.org/services/config/analytics/url";
59 private static final String DEFAULT_API_BASE_URL = "https://analytics.jalview.org/api/event";
61 private static final String API_BASE_URL;
63 private static final String clientId;
65 public static final String APPLICATION_BASE_URL = "desktop://localhost";
67 private List<Map.Entry<String, String>> queryStringValues;
69 private List<Map.Entry<String, Object>> jsonObject;
71 private List<Map.Entry<String, String>> cookieValues;
73 private static boolean ENABLED = false;
75 private static boolean DEBUG = true;
77 private static Plausible instance = null;
79 private static final Map<String, String> defaultProps;
83 defaultProps = new HashMap<>();
84 defaultProps.put("app_name",
85 ChannelProperties.getProperty("app_name") + " Desktop");
86 defaultProps.put("version", Cache.getProperty("VERSION"));
87 defaultProps.put("build_date",
88 Cache.getDefault("BUILD_DATE", "unknown"));
89 defaultProps.put("java_version", System.getProperty("java.version"));
90 String val = System.getProperty("sys.install4jVersion");
93 defaultProps.put("install4j_version", val);
95 val = System.getProperty("installer_template_version");
98 defaultProps.put("install4j_template_version", val);
100 val = System.getProperty("launcher_version");
103 defaultProps.put("launcher_version", val);
105 defaultProps.put("java_arch",
106 System.getProperty("os.arch") + " "
107 + System.getProperty("os.name") + " "
108 + System.getProperty("os.version"));
109 defaultProps.put("os", System.getProperty("os.name"));
110 defaultProps.put("os_version", System.getProperty("os.version"));
111 defaultProps.put("os_arch", System.getProperty("os.arch"));
112 String installation = Cache.applicationProperties
113 .getProperty("INSTALLATION");
114 if (installation != null)
116 defaultProps.put("installation", installation);
119 // ascertain the API_BASE_URL
120 API_BASE_URL = getAPIBaseURL();
122 // random clientId to make User-Agent unique (to register analytic)
123 clientId = String.format("%08x", new Random().nextInt());
125 USER_AGENT = HttpUtils.getUserAgent(
126 MethodHandles.lookup().lookupClass().getCanonicalName() + " "
135 public static void setEnabled(boolean b)
140 public void sendEvent(String eventName, String urlString,
141 String... propsStrings)
143 sendEvent(eventName, urlString, false, propsStrings);
147 * The simplest way to send an analytic event.
150 * The event name. To emulate a webpage view use "pageview" and set a
151 * "url" key/value. See https://plausible.io/docs/events-api
152 * @param sendDefaultProps
153 * Flag whether to add the default props about the application.
154 * @param propsStrings
155 * Optional multiple Strings in key, value pairs (there should be an
156 * even number of propsStrings) to be set as property of the event.
157 * To emulate a webpage view set "url" as the URL in a "pageview"
160 public void sendEvent(String eventName, String urlString,
161 boolean sendDefaultProps, String... propsStrings)
163 // clear out old lists
168 Console.debug("Plausible not enabled.");
171 Map<String, String> props = new HashMap<>();
173 // add these to all events from this application instance
174 if (sendDefaultProps)
176 props.putAll(defaultProps);
179 // add (and overwrite with) the passed in props
180 if (propsStrings != null && propsStrings.length > 0)
182 if (propsStrings.length % 2 != 0)
185 "Cannot addEvent with odd number of propsStrings. Ignoring the last one.");
187 for (int i = 0; i < propsStrings.length - 1; i += 2)
189 String key = propsStrings[i];
190 String value = propsStrings[i + 1];
191 props.put(key, value);
195 addJsonValue("domain", DOMAIN);
196 addJsonValue("name", eventName);
197 StringBuilder eventUrlSb = new StringBuilder(APPLICATION_BASE_URL);
198 if (!APPLICATION_BASE_URL.endsWith("/") && !urlString.startsWith("/"))
200 eventUrlSb.append("/");
202 eventUrlSb.append(urlString);
203 addJsonValue("url", eventUrlSb.toString());
204 addJsonObject("props", props);
205 StringBuilder urlSb = new StringBuilder();
206 urlSb.append(API_BASE_URL);
207 String qs = buildQueryString();
208 if (qs != null && qs.length() > 0)
215 URL url = new URL(urlSb.toString());
216 URLConnection urlConnection = url.openConnection();
217 HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
218 httpURLConnection.setRequestMethod("POST");
219 httpURLConnection.setDoOutput(true);
221 String jsonString = buildJson();
224 "Plausible: HTTP Request is: '" + urlSb.toString() + "'");
227 Console.debug("Plausible: User-Agent is: '" + USER_AGENT + "'");
229 Console.debug("Plausible: POSTed JSON is:\n" + jsonString);
231 byte[] jsonBytes = jsonString.getBytes(StandardCharsets.UTF_8);
232 int jsonLength = jsonBytes.length;
234 httpURLConnection.setFixedLengthStreamingMode(jsonLength);
235 httpURLConnection.setRequestProperty("Content-Type",
237 httpURLConnection.setRequestProperty("User-Agent", USER_AGENT);
238 httpURLConnection.connect();
239 try (OutputStream os = httpURLConnection.getOutputStream())
243 int responseCode = httpURLConnection.getResponseCode();
244 String responseMessage = httpURLConnection.getResponseMessage();
246 if (responseCode < 200 || responseCode > 299)
248 Console.warn("Plausible connection failed: '" + responseCode + " "
249 + responseMessage + "'");
253 Console.debug("Plausible connection succeeded: '" + responseCode
254 + " " + responseMessage + "'");
259 BufferedReader br = new BufferedReader(new InputStreamReader(
260 (httpURLConnection.getInputStream())));
261 StringBuilder sb = new StringBuilder();
263 while ((response = br.readLine()) != null)
267 String body = sb.toString();
268 Console.debug("Plausible response content:\n" + body);
270 } catch (MalformedURLException e)
273 "Somehow the Plausible BASE_URL and queryString is malformed: '"
274 + urlSb.toString() + "'",
277 } catch (IOException e)
279 Console.debug("Connection to Plausible BASE_URL '" + API_BASE_URL
281 } catch (ClassCastException e)
284 "Couldn't cast URLConnection to HttpURLConnection in Plausible.",
289 private void addJsonObject(String key, Map<String, String> map)
291 List<Map.Entry<String, ? extends Object>> list = new ArrayList<>();
292 for (String k : map.keySet())
294 list.add(stringEntry(k, map.get(k)));
296 addJsonObject(key, list);
300 private void addJsonObject(String key,
301 List<Map.Entry<String, ? extends Object>> object)
303 jsonObject.add(objectEntry(key, object));
306 private void addJsonValues(String key, List<Object> values)
308 jsonObject.add(objectEntry(key, values));
311 private void addJsonValue(String key, String value)
313 jsonObject.add(objectEntry(key, value));
316 private void addJsonValue(String key, int value)
318 jsonObject.add(objectEntry(key, Integer.valueOf(value)));
321 private void addJsonValue(String key, boolean value)
323 jsonObject.add(objectEntry(key, Boolean.valueOf(value)));
326 private void addQueryStringValue(String key, String value)
328 queryStringValues.add(stringEntry(key, value));
331 private void addCookieValue(String key, String value)
333 cookieValues.add(stringEntry(key, value));
336 private void resetLists()
338 jsonObject = new ArrayList<>();
339 queryStringValues = new ArrayList<>();
340 cookieValues = new ArrayList<>();
343 public static Plausible getInstance()
345 if (instance == null)
347 instance = new Plausible();
352 public static void reset()
354 getInstance().resetLists();
357 private String buildQueryString()
359 StringBuilder sb = new StringBuilder();
360 for (Map.Entry<String, String> entry : queryStringValues)
368 sb.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
369 } catch (UnsupportedEncodingException e)
371 sb.append(entry.getKey());
376 sb.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
377 } catch (UnsupportedEncodingException e)
379 sb.append(entry.getValue());
382 return sb.toString();
385 private void buildCookieHeaders()
387 // TODO not needed yet
390 private String buildJson()
392 StringBuilder sb = new StringBuilder();
393 addJsonObject(sb, 0, jsonObject);
394 return sb.toString();
397 private void addJsonObject(StringBuilder sb, int indent,
398 List<Map.Entry<String, Object>> entries)
403 Iterator<Map.Entry<String, Object>> entriesI = entries.iterator();
404 while (entriesI.hasNext())
406 Map.Entry<String, Object> entry = entriesI.next();
407 String key = entry.getKey();
408 // TODO sensibly escape " characters in key
409 Object value = entry.getValue();
410 indent(sb, indent + 1);
411 sb.append('"').append(quoteEscape(key)).append('"').append(':');
413 if (value != null && value instanceof List)
417 addJsonValue(sb, indent + 2, value);
418 if (entriesI.hasNext())
428 private void addJsonValue(StringBuilder sb, int indent, Object value)
436 if (value instanceof Map.Entry)
438 Map.Entry<String, Object> entry = (Map.Entry<String, Object>) value;
439 List<Map.Entry<String, Object>> object = new ArrayList<>();
441 addJsonObject(sb, indent, object);
443 else if (value instanceof List)
445 // list of Map.Entries or list of values?
446 List<Object> valueList = (List<Object>) value;
447 if (valueList.size() > 0 && valueList.get(0) instanceof Map.Entry)
450 // indent(sb, indent);
451 List<Map.Entry<String, Object>> entryList = (List<Map.Entry<String, Object>>) value;
452 addJsonObject(sb, indent, entryList);
460 Iterator<Object> valueListI = valueList.iterator();
461 while (valueListI.hasNext())
463 Object v = valueListI.next();
464 addJsonValue(sb, indent + 1, v);
465 if (valueListI.hasNext())
475 else if (value instanceof String)
477 sb.append('"').append(quoteEscape((String) value)).append('"');
479 else if (value instanceof Integer)
481 sb.append(((Integer) value).toString());
483 else if (value instanceof Boolean)
485 sb.append('"').append(((Boolean) value).toString()).append('"');
487 } catch (ClassCastException e)
490 "Could not deal with type of json Object " + value.toString(),
495 private static String quoteEscape(String s)
501 // this escapes quotation marks (") that aren't already escaped (in the
502 // string) ready to go into a quoted JSON string value
503 return s.replaceAll("((?<!\\\\)(?:\\\\{2})*)\"", "$1\\\\\"");
506 private static void prettyWhitespace(StringBuilder sb, String whitespace,
509 // only add whitespace if we're in DEBUG mode
510 if (!Console.getLogger().isDebugEnabled())
514 if (repeat >= 0 && whitespace != null)
516 // sb.append(whitespace.repeat(repeat));
517 sb.append(String.join("", Collections.nCopies(repeat, whitespace)));
522 sb.append(whitespace);
526 private static void indent(StringBuilder sb, int indent)
528 prettyWhitespace(sb, " ", indent);
531 private static void newline(StringBuilder sb)
533 prettyWhitespace(sb, "\n", -1);
536 private static void space(StringBuilder sb)
538 prettyWhitespace(sb, " ", -1);
541 protected static Map.Entry<String, Object> objectEntry(String s, Object o)
543 return new AbstractMap.SimpleEntry<String, Object>(s, o);
546 protected static Map.Entry<String, String> stringEntry(String s, String v)
548 return new AbstractMap.SimpleEntry<String, String>(s, v);
551 private static String getAPIBaseURL()
555 URL url = new URL(CONFIG_API_BASE_URL);
556 URLConnection urlConnection = url.openConnection();
557 HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
558 httpURLConnection.setRequestMethod("GET");
559 httpURLConnection.setRequestProperty("User-Agent", USER_AGENT);
560 httpURLConnection.setConnectTimeout(5000);
561 httpURLConnection.setReadTimeout(3000);
562 httpURLConnection.connect();
563 int responseCode = httpURLConnection.getResponseCode();
564 String responseMessage = httpURLConnection.getResponseMessage();
566 if (responseCode < 200 || responseCode > 299)
568 Console.warn("Config URL connection to '" + CONFIG_API_BASE_URL
569 + "' failed: '" + responseCode + " " + responseMessage
573 BufferedReader br = new BufferedReader(
574 new InputStreamReader((httpURLConnection.getInputStream())));
575 StringBuilder sb = new StringBuilder();
577 while ((response = br.readLine()) != null)
581 if (sb.length() > 7 && sb.substring(0, 5).equals("https"))
583 return sb.toString();
586 } catch (MalformedURLException e)
588 Console.debug("Somehow the config URL is malformed: '"
589 + CONFIG_API_BASE_URL + "'", e);
590 } catch (IOException e)
592 Console.debug("Connection to Plausible BASE_URL '" + API_BASE_URL
594 } catch (ClassCastException e)
597 "Couldn't cast URLConnection to HttpURLConnection in Plausible.",
600 return DEFAULT_API_BASE_URL;