summaryrefslogtreecommitdiffstats
path: root/mobile/android/base/java/org/mozilla/gecko/telemetry/pingbuilders/TelemetryPingBuilder.java
blob: 57fa0fd8bf5a0004335c9d3bae85dc36c7ca52df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

package org.mozilla.gecko.telemetry.pingbuilders;

import org.mozilla.gecko.AppConstants;
import org.mozilla.gecko.sync.ExtendedJSONObject;
import org.mozilla.gecko.telemetry.TelemetryPing;

import java.util.Set;
import java.util.UUID;

/**
 * A generic Builder for {@link TelemetryPing} instances. Each overriding class is
 * expected to create a specific type of ping (e.g. "core").
 *
 * This base class handles the common ping operations under the hood:
 *   * Validating mandatory fields
 *   * Forming the server url
 */
abstract class TelemetryPingBuilder {
    // In the server url, the initial path directly after the "scheme://host:port/"
    private static final String SERVER_INITIAL_PATH = "submit/telemetry";

    private final String serverPath;
    protected final ExtendedJSONObject payload;
    private final String docID;

    public TelemetryPingBuilder() {
        docID = UUID.randomUUID().toString();
        serverPath = getTelemetryServerPath(getDocType(), docID);
        payload = new ExtendedJSONObject();
    }

    /**
     * @return the name of the ping (e.g. "core")
     */
    public abstract String getDocType();

    /**
     * @return the fields that are mandatory for the resultant ping to be uploaded to
     *         the server. These will be validated before the ping is built.
     */
    public abstract String[] getMandatoryFields();

    public TelemetryPing build() {
        validatePayload();
        return new TelemetryPing(serverPath, payload, docID);
    }

    private void validatePayload() {
        final Set<String> keySet = payload.keySet();
        for (final String mandatoryField : getMandatoryFields()) {
            if (!keySet.contains(mandatoryField)) {
                throw new IllegalArgumentException("Builder does not contain mandatory field: " +
                        mandatoryField);
            }
        }
    }

    /**
     * Returns a url of the format:
     *   http://hostname/submit/telemetry/docId/docType/appName/appVersion/appUpdateChannel/appBuildID
     *
     * @param docType The name of the ping (e.g. "main")
     * @return a url at which to POST the telemetry data to
     */
    private static String getTelemetryServerPath(final String docType, final String docID) {
        final String appName = AppConstants.MOZ_APP_BASENAME;
        final String appVersion = AppConstants.MOZ_APP_VERSION;
        final String appUpdateChannel = AppConstants.MOZ_UPDATE_CHANNEL;
        final String appBuildId = AppConstants.MOZ_APP_BUILDID;

        // The compiler will optimize a single String concatenation into a StringBuilder statement.
        // If you change this `return`, be sure to keep it as a single statement to keep it optimized!
        return SERVER_INITIAL_PATH + '/' +
                docID + '/' +
                docType + '/' +
                appName + '/' +
                appVersion + '/' +
                appUpdateChannel + '/' +
                appBuildId;
    }
}