Update formatting in `UserAgentTagUtil`

This commit is contained in:
Jon Chambers 2023-07-10 11:59:32 -04:00 committed by Jon Chambers
parent f592201e4c
commit c315b34395
1 changed files with 52 additions and 52 deletions

View File

@ -13,74 +13,74 @@ import org.whispersystems.textsecuregcm.util.ua.UnrecognizedUserAgentException;
import org.whispersystems.textsecuregcm.util.ua.UserAgent; import org.whispersystems.textsecuregcm.util.ua.UserAgent;
import org.whispersystems.textsecuregcm.util.ua.UserAgentUtil; import org.whispersystems.textsecuregcm.util.ua.UserAgentUtil;
import java.util.EnumMap; import java.util.*;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/** /**
* Utility class for extracting platform/version metrics tags from User-Agent strings. * Utility class for extracting platform/version metrics tags from User-Agent strings.
*/ */
public class UserAgentTagUtil { public class UserAgentTagUtil {
public static final String PLATFORM_TAG = "platform"; public static final String PLATFORM_TAG = "platform";
public static final String VERSION_TAG = "clientVersion"; public static final String VERSION_TAG = "clientVersion";
static final List<Tag> OVERFLOW_TAGS = List.of(Tag.of(PLATFORM_TAG, "overflow"), Tag.of(VERSION_TAG, "overflow")); static final List<Tag> OVERFLOW_TAGS = List.of(Tag.of(PLATFORM_TAG, "overflow"), Tag.of(VERSION_TAG, "overflow"));
static final List<Tag> UNRECOGNIZED_TAGS = List.of(Tag.of(PLATFORM_TAG, "unrecognized"), Tag.of(VERSION_TAG, "unrecognized")); static final List<Tag> UNRECOGNIZED_TAGS = List.of(Tag.of(PLATFORM_TAG, "unrecognized"),
Tag.of(VERSION_TAG, "unrecognized"));
private static final Map<ClientPlatform, Semver> MINIMUM_VERSION_BY_PLATFORM = new EnumMap<>(ClientPlatform.class); private static final Map<ClientPlatform, Semver> MINIMUM_VERSION_BY_PLATFORM = new EnumMap<>(ClientPlatform.class);
static { static {
MINIMUM_VERSION_BY_PLATFORM.put(ClientPlatform.ANDROID, new Semver("4.0.0")); MINIMUM_VERSION_BY_PLATFORM.put(ClientPlatform.ANDROID, new Semver("4.0.0"));
MINIMUM_VERSION_BY_PLATFORM.put(ClientPlatform.DESKTOP, new Semver("1.0.0")); MINIMUM_VERSION_BY_PLATFORM.put(ClientPlatform.DESKTOP, new Semver("1.0.0"));
MINIMUM_VERSION_BY_PLATFORM.put(ClientPlatform.IOS, new Semver("3.0.0")); MINIMUM_VERSION_BY_PLATFORM.put(ClientPlatform.IOS, new Semver("3.0.0"));
} }
static final int MAX_VERSIONS = 1_000; static final int MAX_VERSIONS = 1_000;
private static final Set<Pair<ClientPlatform, Semver>> SEEN_VERSIONS = new HashSet<>(); private static final Set<Pair<ClientPlatform, Semver>> SEEN_VERSIONS = new HashSet<>();
private UserAgentTagUtil() { private UserAgentTagUtil() {
} }
public static List<Tag> getUserAgentTags(final String userAgentString) { public static List<Tag> getUserAgentTags(final String userAgentString) {
try { try {
final UserAgent userAgent = UserAgentUtil.parseUserAgentString(userAgentString); final UserAgent userAgent = UserAgentUtil.parseUserAgentString(userAgentString);
final List<Tag> tags; final List<Tag> tags;
if (userAgent.getVersion().isStable() && userAgent.getVersion().isGreaterThanOrEqualTo(MINIMUM_VERSION_BY_PLATFORM.get(userAgent.getPlatform()))) { if (userAgent.getVersion().isStable() && userAgent.getVersion()
if (allowVersion(userAgent.getPlatform(), userAgent.getVersion())) { .isGreaterThanOrEqualTo(MINIMUM_VERSION_BY_PLATFORM.get(userAgent.getPlatform()))) {
tags = List.of(Tag.of(PLATFORM_TAG, userAgent.getPlatform().name().toLowerCase()), Tag.of(VERSION_TAG, userAgent.getVersion().toString())); if (allowVersion(userAgent.getPlatform(), userAgent.getVersion())) {
} else { tags = List.of(Tag.of(PLATFORM_TAG, userAgent.getPlatform().name().toLowerCase()),
tags = OVERFLOW_TAGS; Tag.of(VERSION_TAG, userAgent.getVersion().toString()));
} } else {
} else { tags = OVERFLOW_TAGS;
tags = UNRECOGNIZED_TAGS;
}
return tags;
} catch (final UnrecognizedUserAgentException e) {
return UNRECOGNIZED_TAGS;
} }
} else {
tags = UNRECOGNIZED_TAGS;
}
return tags;
} catch (final UnrecognizedUserAgentException e) {
return UNRECOGNIZED_TAGS;
}
}
public static Tag getPlatformTag(final String userAgentString) {
String platform;
try {
platform = UserAgentUtil.parseUserAgentString(userAgentString).getPlatform().name().toLowerCase();
} catch (final UnrecognizedUserAgentException e) {
platform = "unrecognized";
} }
public static Tag getPlatformTag(final String userAgentString) { return Tag.of(PLATFORM_TAG, platform);
String platform; }
try { private static boolean allowVersion(final ClientPlatform platform, final Semver version) {
platform = UserAgentUtil.parseUserAgentString(userAgentString).getPlatform().name().toLowerCase(); final Pair<ClientPlatform, Semver> platformAndVersion = new Pair<>(platform, version);
} catch (final UnrecognizedUserAgentException e) {
platform = "unrecognized";
}
return Tag.of(PLATFORM_TAG, platform); synchronized (SEEN_VERSIONS) {
} return SEEN_VERSIONS.contains(platformAndVersion) || (SEEN_VERSIONS.size() < MAX_VERSIONS && SEEN_VERSIONS.add(
platformAndVersion));
private static boolean allowVersion(final ClientPlatform platform, final Semver version) {
final Pair<ClientPlatform, Semver> platformAndVersion = new Pair<>(platform, version);
synchronized (SEEN_VERSIONS) {
return SEEN_VERSIONS.contains(platformAndVersion) || (SEEN_VERSIONS.size() < MAX_VERSIONS && SEEN_VERSIONS.add(platformAndVersion));
}
} }
}
} }