com.amazonaws.eclipse.core.mobileanalytics.context.ClientContextConfig.java Source code

Java tutorial

Introduction

Here is the source code for com.amazonaws.eclipse.core.mobileanalytics.context.ClientContextConfig.java

Source

/*
 * Copyright 2015 Amazon Technologies, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *    http://aws.amazon.com/apache2.0
 *
 * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
 * OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and
 * limitations under the License.
 */
package com.amazonaws.eclipse.core.mobileanalytics.context;

import static com.amazonaws.eclipse.core.mobileanalytics.internal.Constants.MOBILE_ANALYTICS_CLIENT_ID_PREF_STORE_KEY;
import static com.amazonaws.eclipse.core.mobileanalytics.internal.Constants.MOBILE_ANALYTICS_APP_ID_PROD;
import static com.amazonaws.eclipse.core.mobileanalytics.internal.Constants.MOBILE_ANALYTICS_APP_ID_TEST;
import static com.amazonaws.eclipse.core.mobileanalytics.internal.Constants.MOBILE_ANALYTICS_APP_TITLE_PROD;
import static com.amazonaws.eclipse.core.mobileanalytics.internal.Constants.MOBILE_ANALYTICS_APP_TITLE_TEST;

import java.util.Locale;
import java.util.UUID;

import org.eclipse.jface.preference.IPreferenceStore;

import com.amazonaws.annotation.Immutable;
import com.amazonaws.eclipse.core.AwsToolkitCore;
import com.amazonaws.eclipse.core.mobileanalytics.internal.Constants;

/**
 * Container for all the data required in the x-amz-client-context header.
 *
 * @see http://docs.aws.amazon.com/mobileanalytics/latest/ug/PutEvents.html#putEvents-request-client-context-header
 */
@Immutable
public class ClientContextConfig {

    private final String appTitle;
    private final String appId;
    private final String envPlatformName;
    private final String envPlatformVersion;
    private final String envLocale;
    // unique per installation; persisted in preference store
    private final String clientId;

    public static final ClientContextConfig PROD_CONFIG = new ClientContextConfig(MOBILE_ANALYTICS_APP_TITLE_PROD,
            MOBILE_ANALYTICS_APP_ID_PROD, _getSystemOsName(), _getSystemOsVersion(), _getSystemLocaleCountry(),
            _getOrGenerateClientId());

    public static final ClientContextConfig TEST_CONFIG = new ClientContextConfig(MOBILE_ANALYTICS_APP_TITLE_TEST,
            MOBILE_ANALYTICS_APP_ID_TEST, _getSystemOsName(), _getSystemOsVersion(), _getSystemLocaleCountry(),
            _getOrGenerateClientId());

    private ClientContextConfig(String appTitle, String appId, String envPlatformName, String envPlatformVersion,
            String envLocale, String clientId) {
        this.appTitle = appTitle;
        this.appId = appId;
        this.envPlatformName = envPlatformName;
        this.envPlatformVersion = envPlatformVersion;
        this.envLocale = envLocale;
        this.clientId = clientId;
    }

    public String getAppTitle() {
        return appTitle;
    }

    public String getAppId() {
        return appId;
    }

    public String getEnvPlatformName() {
        return envPlatformName;
    }

    public String getEnvPlatformVersion() {
        return envPlatformVersion;
    }

    public String getEnvLocale() {
        return envLocale;
    }

    public String getClientId() {
        return clientId;
    }

    private static String _getSystemOsName() {
        try {
            String osName = System.getProperty("os.name");
            if (osName == null) {
                return null;
            }

            osName = osName.toLowerCase();

            if (osName.startsWith("windows")) {
                return Constants.CLIENT_CONTEXT_ENV_PLATFORM_WINDOWS;
            }
            if (osName.startsWith("mac")) {
                return Constants.CLIENT_CONTEXT_ENV_PLATFORM_MACOS;
            }
            if (osName.startsWith("linux")) {
                return Constants.CLIENT_CONTEXT_ENV_PLATFORM_LINUX;
            }

            AwsToolkitCore.getDefault().logInfo("Unknown OS name: " + osName);
            return null;

        } catch (Exception e) {
            return null;
        }
    }

    private static String _getSystemOsVersion() {
        try {
            return System.getProperty("os.version");
        } catch (Exception e) {
            return null;
        }
    }

    private static String _getSystemLocaleCountry() {
        try {
            return Locale.getDefault().getDisplayCountry(Locale.US);
        } catch (Exception e) {
            return null;
        }
    }

    private static String _getOrGenerateClientId() {
        IPreferenceStore store = AwsToolkitCore.getDefault().getPreferenceStore();

        String clientId = store.getString(MOBILE_ANALYTICS_CLIENT_ID_PREF_STORE_KEY);
        if (clientId != null && !clientId.isEmpty()) {
            return clientId;
        }

        // Generate a GUID as the client id and persist it in the preference store
        String newClientId = UUID.randomUUID().toString();
        store.setValue(MOBILE_ANALYTICS_CLIENT_ID_PREF_STORE_KEY, newClientId);
        return newClientId;
    }

}