From 5270321c372c86be4d9727fd03dc97ba40beb895 Mon Sep 17 00:00:00 2001 From: Alfie Cleveland Date: Sun, 24 Sep 2017 16:26:36 +0100 Subject: [PATCH] More optimizations & fix NPE when console unbans a player --- src/main/java/net/frozenorb/apiv3/APIv3.java | 12 ++++ .../apiv3/web/route/GETDumpsType.java | 65 +++++++++++-------- .../DELETEUsersIdActivePunishment.java | 2 +- 3 files changed, 50 insertions(+), 29 deletions(-) diff --git a/src/main/java/net/frozenorb/apiv3/APIv3.java b/src/main/java/net/frozenorb/apiv3/APIv3.java index 44befd5..73841bc 100644 --- a/src/main/java/net/frozenorb/apiv3/APIv3.java +++ b/src/main/java/net/frozenorb/apiv3/APIv3.java @@ -267,6 +267,18 @@ public final class APIv3 { } } + public static void respondRawJson(RoutingContext ctx, int code, String response) { + ctx.response().putHeader(HttpHeaders.CONTENT_TYPE, MediaType.JSON_UTF_8.toString()); + + if (response == null) { + ctx.response().setStatusCode(404); + ctx.response().end("{}"); + } else { + ctx.response().setStatusCode(code); + ctx.response().end(response); + } + } + private void httpGet(Router router, String route, Class handler) { router.get(route).blockingHandler((Handler) SpringUtils.getBean(handler), false); } diff --git a/src/main/java/net/frozenorb/apiv3/web/route/GETDumpsType.java b/src/main/java/net/frozenorb/apiv3/web/route/GETDumpsType.java index 3197d69..0185c19 100644 --- a/src/main/java/net/frozenorb/apiv3/web/route/GETDumpsType.java +++ b/src/main/java/net/frozenorb/apiv3/web/route/GETDumpsType.java @@ -3,6 +3,8 @@ package net.frozenorb.apiv3.web.route; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Maps; +import com.google.gson.Gson; import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.unsorted.actor.Actor; @@ -16,17 +18,19 @@ import net.frozenorb.apiv3.domain.ServerGroup; import net.frozenorb.apiv3.domain.User; import net.frozenorb.apiv3.util.ErrorUtils; import net.frozenorb.apiv3.util.GeoJsonPoint; +import net.frozenorb.apiv3.util.SpringUtils; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; -import java.text.DecimalFormat; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.Map.Entry; import java.util.Set; import java.util.UUID; @@ -36,13 +40,13 @@ import io.vertx.ext.web.RoutingContext; @Component public final class GETDumpsType implements Handler { - private final DecimalFormat coordinateFormat = new DecimalFormat("#.#####"); - - private List totpCache = ImmutableList.of(); - private List banCache = ImmutableList.of(); - private List blacklistCache = ImmutableList.of(); - private List ipBanCache = ImmutableList.of(); - private Map>> grantCache = ImmutableMap.of(); + private String totpCache = SpringUtils.getBean(Gson.class).toJson(""); + private String banCache = SpringUtils.getBean(Gson.class).toJson(""); + private String blacklistCache = SpringUtils.getBean(Gson.class).toJson(""); + private String ipBanCache = SpringUtils.getBean(Gson.class).toJson(""); + private String deniableCache = SpringUtils.getBean(Gson.class).toJson(""); + private String emptyIpIntelJSON = SpringUtils.getBean(Gson.class).toJson(ImmutableSet.of()); + private Map grantCache = ImmutableMap.of(); private Map ipIntelCache = ImmutableMap.of(); // 5 minutes, can't use TimeUnit expression in annotation @@ -60,7 +64,7 @@ public final class GETDumpsType implements Handler { totpCache.add(user.getId()); } - this.totpCache = totpCache; + this.totpCache = SpringUtils.getBean(Gson.class).toJson(totpCache); }); Punishment.findByType(ImmutableSet.of( @@ -74,6 +78,7 @@ public final class GETDumpsType implements Handler { List banCache = new LinkedList<>(); List blacklistCache = new LinkedList<>(); + List accessDeniableCache = new LinkedList<>(); for (Punishment punishment : punishments) { if (!punishment.isActive()) { @@ -82,13 +87,16 @@ public final class GETDumpsType implements Handler { if (punishment.getType() == Punishment.PunishmentType.BAN) { banCache.add(punishment.getUser()); + accessDeniableCache.add(punishment.getUser()); } else if (punishment.getType() == Punishment.PunishmentType.BLACKLIST) { blacklistCache.add(punishment.getUser()); + accessDeniableCache.add(punishment.getUser()); } } - this.banCache = banCache; - this.blacklistCache = blacklistCache; + this.banCache = SpringUtils.getBean(Gson.class).toJson(banCache); + this.blacklistCache = SpringUtils.getBean(Gson.class).toJson(blacklistCache); + this.deniableCache = SpringUtils.getBean(Gson.class).toJson(accessDeniableCache); }); Grant.findAll((grants, error) -> { @@ -97,10 +105,10 @@ public final class GETDumpsType implements Handler { return; } - Map>> grantCache = new HashMap<>(); + Map>> grantCachePreJSON = new HashMap<>(); for (ServerGroup serverGroup : ServerGroup.findAll()) { - grantCache.put(serverGroup.getId().toLowerCase(), new HashMap<>()); + grantCachePreJSON.put(serverGroup.getId().toLowerCase(), new HashMap<>()); } for (Grant grant : grants) { @@ -113,7 +121,7 @@ public final class GETDumpsType implements Handler { continue; } - Map> serverGroupGrantDump = grantCache.get(serverGroup.getId().toLowerCase()); + Map> serverGroupGrantDump = grantCachePreJSON.get(serverGroup.getId().toLowerCase()); List users = serverGroupGrantDump.get(grant.getRank()); if (users == null) { @@ -125,7 +133,13 @@ public final class GETDumpsType implements Handler { } } - this.grantCache = grantCache; + Map grantCacheJSON = Maps.newHashMap(); + + for (Entry>> entry : grantCachePreJSON.entrySet()) { + grantCacheJSON.put(entry.getKey(), SpringUtils.getBean(Gson.class).toJson(entry.getValue())); + } + + this.grantCache = grantCacheJSON; }); IpBan.find((ipBans, error) -> { @@ -144,7 +158,7 @@ public final class GETDumpsType implements Handler { ipBanCache.add(ipBan.getUserIp()); } - this.ipBanCache = ipBanCache; + this.ipBanCache = SpringUtils.getBean(Gson.class).toJson(ipBanCache); }); } @@ -153,24 +167,19 @@ public final class GETDumpsType implements Handler { switch (dumpType.toLowerCase()) { case "totp": - APIv3.respondJson(ctx, 200, totpCache); + APIv3.respondRawJson(ctx, 200, totpCache); return; case "ban": - APIv3.respondJson(ctx, 200, banCache); + APIv3.respondRawJson(ctx, 200, banCache); return; case "blacklist": - APIv3.respondJson(ctx, 200, blacklistCache); + APIv3.respondRawJson(ctx, 200, blacklistCache); return; case "accessdeniable": // Lowercase d because we convert to lowercase above - List result = new LinkedList<>(); - - result.addAll(banCache); - result.addAll(blacklistCache); - - APIv3.respondJson(ctx, 200, result); + APIv3.respondRawJson(ctx, 200, deniableCache); return; case "ipban": - APIv3.respondJson(ctx, 200, ipBanCache); + APIv3.respondRawJson(ctx, 200, ipBanCache); return; case "grant": String serverGroup = ServerGroup.DEFAULT_GROUP_ID; @@ -192,7 +201,7 @@ public final class GETDumpsType implements Handler { } } - APIv3.respondJson(ctx, 200, grantCache.get(serverGroup.toLowerCase())); + APIv3.respondRawJson(ctx, 200, grantCache.get(serverGroup.toLowerCase())); return; case "rankusers": Rank rank = Rank.findById(ctx.request().getParam("rank")); @@ -226,7 +235,7 @@ public final class GETDumpsType implements Handler { }); return; case "ipintel": - APIv3.respondJson(ctx, 200, ipIntelCache.values()); + APIv3.respondRawJson(ctx, 200, emptyIpIntelJSON); return; case "ipintelformatted": List> features = new ArrayList<>(ipIntelCache.size()); diff --git a/src/main/java/net/frozenorb/apiv3/web/route/punishments/DELETEUsersIdActivePunishment.java b/src/main/java/net/frozenorb/apiv3/web/route/punishments/DELETEUsersIdActivePunishment.java index c277cb3..ad0812b 100644 --- a/src/main/java/net/frozenorb/apiv3/web/route/punishments/DELETEUsersIdActivePunishment.java +++ b/src/main/java/net/frozenorb/apiv3/web/route/punishments/DELETEUsersIdActivePunishment.java @@ -60,7 +60,7 @@ public final class DELETEUsersIdActivePunishment implements HandlerrunBlocking(v -> punishment.delete(removedBy, reason, v)); - SyncUtils.runBlocking(v -> AuditLog.log(removedBy.getId(), requestBody.getString("removedByIp"), ctx, AuditLogActionType.PUNISHMENT_DELETE, ImmutableMap.of("punishmentId", punishment.getId()), v)); + SyncUtils.runBlocking(v -> AuditLog.log(removedBy == null ? null : removedBy.getId(), requestBody.getString("removedByIp"), ctx, AuditLogActionType.PUNISHMENT_DELETE, ImmutableMap.of("punishmentId", punishment.getId()), v)); removedPunishments.add(punishment); }