From 453a3264bea535775ac4b81c349461cf0494b39d Mon Sep 17 00:00:00 2001 From: Colin McDonald Date: Thu, 16 Jun 2016 12:25:22 -0400 Subject: [PATCH 01/24] Swap Date to Instant --- src/main/java/net/frozenorb/apiv3/APIv3.java | 10 +++++++--- .../dataImport/converters/GrantConverter.java | 6 +++--- .../dataImport/converters/IpLogConverter.java | 5 ++--- .../converters/PunishmentConverter.java | 7 +++---- .../dataImport/converters/UserConverter.java | 6 +++--- .../apiv3/handler/ActorAttributeHandler.java | 1 - .../frozenorb/apiv3/model/AuditLogEntry.java | 10 +++++++--- .../java/net/frozenorb/apiv3/model/Grant.java | 15 ++++++++------- .../java/net/frozenorb/apiv3/model/IpBan.java | 19 ++++++++++--------- .../net/frozenorb/apiv3/model/IpLogEntry.java | 12 ++++++------ .../net/frozenorb/apiv3/model/Punishment.java | 17 +++++++++-------- .../net/frozenorb/apiv3/model/Server.java | 7 ++++--- .../java/net/frozenorb/apiv3/model/User.java | 19 ++++++++++--------- .../apiv3/route/grants/POSTUserGrant.java | 10 +++++----- .../apiv3/route/ipBans/POSTIpIpBan.java | 10 +++++----- .../route/punishments/POSTUserPunish.java | 10 +++++----- .../route/servers/POSTServerHeartbeat.java | 8 ++++++-- .../route/users/POSTUserConfirmRegister.java | 2 +- .../apiv3/route/users/POSTUserRegister.java | 6 +++--- ...peAdapter.java => InstantTimeAdapter.java} | 10 +++++----- .../net/frozenorb/apiv3/util/TimeUtils.java | 6 +++--- 21 files changed, 105 insertions(+), 91 deletions(-) rename src/main/java/net/frozenorb/apiv3/serialization/gson/{DateTypeAdapter.java => InstantTimeAdapter.java} (64%) diff --git a/src/main/java/net/frozenorb/apiv3/APIv3.java b/src/main/java/net/frozenorb/apiv3/APIv3.java index 8d393ea..80e73ab 100644 --- a/src/main/java/net/frozenorb/apiv3/APIv3.java +++ b/src/main/java/net/frozenorb/apiv3/APIv3.java @@ -62,8 +62,8 @@ import net.frozenorb.apiv3.route.serverGroups.GETServerGroups; import net.frozenorb.apiv3.route.serverGroups.POSTServerGroup; import net.frozenorb.apiv3.route.servers.*; import net.frozenorb.apiv3.route.users.*; -import net.frozenorb.apiv3.serialization.gson.DateTypeAdapter; import net.frozenorb.apiv3.serialization.gson.FollowAnnotationExclusionStrategy; +import net.frozenorb.apiv3.serialization.gson.InstantTimeAdapter; import net.frozenorb.apiv3.serialization.jackson.UUIDJsonDeserializer; import net.frozenorb.apiv3.serialization.jackson.UUIDJsonSerializer; import net.frozenorb.apiv3.serialization.mongodb.UUIDCodecProvider; @@ -79,7 +79,11 @@ import org.bson.codecs.configuration.CodecRegistry; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; -import java.util.*; +import java.time.Instant; +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; +import java.util.UUID; @Slf4j public final class APIv3 extends AbstractVerticle { @@ -89,7 +93,7 @@ public final class APIv3 extends AbstractVerticle { @Getter private static Properties config = new Properties(); @Getter private static RedisClient redisClient; private static final Gson gson = new GsonBuilder() - .registerTypeAdapter(Date.class, new DateTypeAdapter()) + .registerTypeAdapter(Instant.class, new InstantTimeAdapter()) .setExclusionStrategies(new FollowAnnotationExclusionStrategy()) .create(); diff --git a/src/main/java/net/frozenorb/apiv3/dataImport/converters/GrantConverter.java b/src/main/java/net/frozenorb/apiv3/dataImport/converters/GrantConverter.java index 9f1bb24..f2de982 100644 --- a/src/main/java/net/frozenorb/apiv3/dataImport/converters/GrantConverter.java +++ b/src/main/java/net/frozenorb/apiv3/dataImport/converters/GrantConverter.java @@ -7,8 +7,8 @@ import net.frozenorb.apiv3.model.Grant; import org.bson.Document; import org.bson.types.ObjectId; +import java.time.Instant; import java.util.Collection; -import java.util.Date; import java.util.Map; import java.util.UUID; @@ -45,9 +45,9 @@ public final class GrantConverter implements Block { grant.containsKey("comment") ? grant.getString("comment") : "", grant.containsKey("scope") ? ImmutableSet.copyOf((Collection) grant.get("scope")) : ImmutableSet.of(), rank, - grant.getDate("expires"), + grant.containsKey("expires") ? grant.getDate("expires").toInstant() : null, grant.containsKey("addedBy") ? oidToUniqueId.get(((Map) grant.get("addedBy")).get("$id")) : null, - grant.containsKey("created") ? grant.getDate("created") : new Date(), + grant.containsKey("created") ? grant.getDate("created").toInstant() : Instant.now(), null, null, null diff --git a/src/main/java/net/frozenorb/apiv3/dataImport/converters/IpLogConverter.java b/src/main/java/net/frozenorb/apiv3/dataImport/converters/IpLogConverter.java index dcf648f..6d6c3d7 100644 --- a/src/main/java/net/frozenorb/apiv3/dataImport/converters/IpLogConverter.java +++ b/src/main/java/net/frozenorb/apiv3/dataImport/converters/IpLogConverter.java @@ -44,10 +44,9 @@ public final class IpLogConverter implements Block { new ObjectId().toString(), user, ip, - lastSeen, - lastSeen, + lastSeen.toInstant(), + lastSeen.toInstant(), ((Number) ipLogEntry.get("uses")).intValue() - ); created.insert(); diff --git a/src/main/java/net/frozenorb/apiv3/dataImport/converters/PunishmentConverter.java b/src/main/java/net/frozenorb/apiv3/dataImport/converters/PunishmentConverter.java index e260107..1274c6a 100644 --- a/src/main/java/net/frozenorb/apiv3/dataImport/converters/PunishmentConverter.java +++ b/src/main/java/net/frozenorb/apiv3/dataImport/converters/PunishmentConverter.java @@ -8,7 +8,6 @@ import net.frozenorb.apiv3.model.Punishment; import org.bson.Document; import org.bson.types.ObjectId; -import java.util.Date; import java.util.List; import java.util.Map; import java.util.UUID; @@ -40,15 +39,15 @@ public final class PunishmentConverter implements Block { target, punishment.getString("reason").toString(), Punishment.PunishmentType.valueOf(punishment.getString("type").toUpperCase()), - punishment.getDate("expires"), + punishment.containsKey("expires") ? punishment.getDate("expires").toInstant() : null, punishment.containsKey("meta") ? (punishment.get("meta") instanceof List ? ImmutableMap.of() : (Document) punishment.get("meta")) : ImmutableMap.of(), null, punishment.containsKey("addedBy") ? oidToUniqueId.get(((Map) punishment.get("addedBy")).get("$id")) : null, - (Date) punishment.getDate("created").clone(), + punishment.getDate("created").toInstant(), punishment.containsKey("createdOn") ? String.valueOf(((Map) punishment.get("createdOn")).get("$id")) : "Website", punishment.containsKey("createdOn") ? ActorType.SERVER : ActorType.WEBSITE, punishment.containsKey("removedBy") ? (((Map) punishment.get("removedBy")).get("$ref").equals("user") ? oidToUniqueId.get(((Map) punishment.get("removedBy")).get("$id")) : null) : null, - punishment.containsKey("removedBy") ? (punishment.containsKey("removedAt") ? punishment.getDate("removedAt") : punishment.getDate("created")) : null, + punishment.containsKey("removedBy") ? (punishment.containsKey("removedAt") ? punishment.getDate("removedAt") : punishment.getDate("created")).toInstant() : null, punishment.containsKey("removedBy") ? punishment.getString("removalReason").toString() : null ); diff --git a/src/main/java/net/frozenorb/apiv3/dataImport/converters/UserConverter.java b/src/main/java/net/frozenorb/apiv3/dataImport/converters/UserConverter.java index 9b1a47e..246a4b7 100644 --- a/src/main/java/net/frozenorb/apiv3/dataImport/converters/UserConverter.java +++ b/src/main/java/net/frozenorb/apiv3/dataImport/converters/UserConverter.java @@ -39,7 +39,7 @@ public final class UserConverter implements Block { User created = new User( uuid, user.get("name").toString(), - ImmutableMap.of(user.get("name").toString(), user.getDate("joined")), + ImmutableMap.of(user.get("name").toString(), user.getDate("joined").toInstant()), null, null, null, @@ -47,8 +47,8 @@ public final class UserConverter implements Block { user.getString("email"), user.getString("phone"), "INVALID", - user.getDate("joined"), - user.getDate("joined"), + user.getDate("joined").toInstant(), + user.getDate("joined").toInstant(), false ); diff --git a/src/main/java/net/frozenorb/apiv3/handler/ActorAttributeHandler.java b/src/main/java/net/frozenorb/apiv3/handler/ActorAttributeHandler.java index 21dd9f3..37c456d 100644 --- a/src/main/java/net/frozenorb/apiv3/handler/ActorAttributeHandler.java +++ b/src/main/java/net/frozenorb/apiv3/handler/ActorAttributeHandler.java @@ -9,7 +9,6 @@ import net.frozenorb.apiv3.model.Server; import net.frozenorb.apiv3.model.User; import net.frozenorb.apiv3.util.ErrorUtils; -import java.nio.charset.Charset; import java.util.Base64; public final class ActorAttributeHandler implements Handler { diff --git a/src/main/java/net/frozenorb/apiv3/model/AuditLogEntry.java b/src/main/java/net/frozenorb/apiv3/model/AuditLogEntry.java index 1161e3e..388389f 100644 --- a/src/main/java/net/frozenorb/apiv3/model/AuditLogEntry.java +++ b/src/main/java/net/frozenorb/apiv3/model/AuditLogEntry.java @@ -14,7 +14,11 @@ import net.frozenorb.apiv3.util.SyncUtils; import org.bson.Document; import org.bson.types.ObjectId; -import java.util.*; +import java.time.Instant; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.UUID; @Entity public final class AuditLogEntry { @@ -24,7 +28,7 @@ public final class AuditLogEntry { @Getter @Id private String id; @Getter private UUID user; @Getter private String userIp; - @Getter private Date performedAt; + @Getter private Instant performedAt; @Getter private String actorName; @Getter private ActorType actorType; @Getter private AuditLogActionType type; @@ -76,7 +80,7 @@ public final class AuditLogEntry { this.id = new ObjectId().toString(); this.user = user.getId(); this.userIp = userIp; - this.performedAt = new Date(); + this.performedAt = Instant.now(); this.actorName = actor.getName(); this.actorType = actor.getType(); this.type = type; diff --git a/src/main/java/net/frozenorb/apiv3/model/Grant.java b/src/main/java/net/frozenorb/apiv3/model/Grant.java index bb5e882..726a992 100644 --- a/src/main/java/net/frozenorb/apiv3/model/Grant.java +++ b/src/main/java/net/frozenorb/apiv3/model/Grant.java @@ -14,6 +14,7 @@ import net.frozenorb.apiv3.util.SyncUtils; import org.bson.Document; import org.bson.types.ObjectId; +import java.time.Instant; import java.util.*; import java.util.stream.Collectors; @@ -28,13 +29,13 @@ public final class Grant { @Getter private String reason; @Getter private Set scopes = new HashSet<>(); // So on things w/o scopes we still load properly (Morphia drops empty sets) @Getter private String rank; - @Getter private Date expiresAt; + @Getter private Instant expiresAt; @Getter private UUID addedBy; - @Getter private Date addedAt; + @Getter private Instant addedAt; @Getter private UUID removedBy; - @Getter private Date removedAt; + @Getter private Instant removedAt; @Getter private String removalReason; public static List findAllSync() { @@ -109,7 +110,7 @@ public final class Grant { public Grant() {} // For Morphia - public Grant(User user, String reason, Set scopes, Rank rank, Date expiresAt, User addedBy) { + public Grant(User user, String reason, Set scopes, Rank rank, Instant expiresAt, User addedBy) { this.id = new ObjectId().toString(); this.user = user.getId(); this.reason = reason; @@ -117,7 +118,7 @@ public final class Grant { this.rank = rank.getId(); this.expiresAt = expiresAt; this.addedBy = addedBy == null ? null : addedBy.getId(); - this.addedAt = new Date(); + this.addedAt = Instant.now(); } public boolean isActive() { @@ -128,7 +129,7 @@ public final class Grant { if (expiresAt == null) { return false; // Never expires } else { - return expiresAt.before(new Date()); + return expiresAt.isBefore(Instant.now()); } } @@ -152,7 +153,7 @@ public final class Grant { public void delete(User removedBy, String reason) { this.removedBy = removedBy.getId(); - this.removedAt = new Date(); + this.removedAt = Instant.now(); this.removalReason = reason; BlockingCallback callback = new BlockingCallback<>(); diff --git a/src/main/java/net/frozenorb/apiv3/model/IpBan.java b/src/main/java/net/frozenorb/apiv3/model/IpBan.java index cf71079..dbff14a 100644 --- a/src/main/java/net/frozenorb/apiv3/model/IpBan.java +++ b/src/main/java/net/frozenorb/apiv3/model/IpBan.java @@ -16,6 +16,7 @@ import net.frozenorb.apiv3.util.TimeUtils; import org.bson.Document; import org.bson.types.ObjectId; +import java.time.Instant; import java.util.*; @Entity @@ -27,15 +28,15 @@ public final class IpBan { @Getter @Id private String id; @Getter private String userIp; @Getter private String reason; - @Getter private Date expiresAt; + @Getter private Instant expiresAt; @Getter private UUID addedBy; - @Getter private Date addedAt; + @Getter private Instant addedAt; @Getter private String actorName; @Getter private ActorType actorType; @Getter private UUID removedBy; - @Getter private Date removedAt; + @Getter private Instant removedAt; @Getter private String removalReason; public static List findAllSync() { @@ -98,18 +99,18 @@ public final class IpBan { this.reason = linked.getReason(); this.expiresAt = linked.getExpiresAt(); this.addedBy = linked.getAddedBy(); - this.addedAt = new Date(); + this.addedAt = Instant.now(); this.actorName = linked.getActorName(); this.actorType = linked.getActorType(); } - public IpBan(String userIp, String reason, Date expiresAt, User addedBy, Actor actor) { + public IpBan(String userIp, String reason, Instant expiresAt, User addedBy, Actor actor) { this.id = new ObjectId().toString(); this.userIp = userIp; this.reason = reason; this.expiresAt = expiresAt; this.addedBy = addedBy == null ? null : addedBy.getId(); - this.addedAt = new Date(); + this.addedAt = Instant.now(); this.actorName = actor.getName(); this.actorType = actor.getType(); } @@ -122,7 +123,7 @@ public final class IpBan { if (expiresAt == null) { return false; // Never expires } else { - return expiresAt.before(new Date()); + return expiresAt.isBefore(Instant.now()); } } @@ -134,7 +135,7 @@ public final class IpBan { String accessDenialReason = "Your ip address has been suspended from the MineHQ Network. \n\n"; if (getExpiresAt() != null) { - accessDenialReason += "Expires in " + TimeUtils.formatIntoDetailedString(TimeUtils.getSecondsBetween(getExpiresAt(), new Date())); + accessDenialReason += "Expires in " + TimeUtils.formatIntoDetailedString(TimeUtils.getSecondsBetween(getExpiresAt(), Instant.now())); } else { accessDenialReason += "Appeal at MineHQ.com/appeal"; } @@ -150,7 +151,7 @@ public final class IpBan { public void delete(User removedBy, String reason) { this.removedBy = removedBy.getId(); - this.removedAt = new Date(); + this.removedAt = Instant.now(); this.removalReason = reason; BlockingCallback callback = new BlockingCallback<>(); diff --git a/src/main/java/net/frozenorb/apiv3/model/IpLogEntry.java b/src/main/java/net/frozenorb/apiv3/model/IpLogEntry.java index 94f805c..15fbfe6 100644 --- a/src/main/java/net/frozenorb/apiv3/model/IpLogEntry.java +++ b/src/main/java/net/frozenorb/apiv3/model/IpLogEntry.java @@ -13,8 +13,8 @@ import net.frozenorb.apiv3.util.SyncUtils; import org.bson.Document; import org.bson.types.ObjectId; +import java.time.Instant; import java.util.ArrayList; -import java.util.Date; import java.util.List; import java.util.UUID; @@ -27,8 +27,8 @@ public final class IpLogEntry { @Getter @Id private String id; @Getter private UUID user; @Getter private String userIp; - @Getter private Date firstSeenAt; - @Getter private Date lastSeenAt; + @Getter private Instant firstSeenAt; + @Getter private Instant lastSeenAt; @Getter private int uses; public static List findAllSync() { @@ -85,13 +85,13 @@ public final class IpLogEntry { this.id = new ObjectId().toString(); this.user = user.getId(); this.userIp = userIp; - this.firstSeenAt = new Date(); - this.lastSeenAt = new Date(); + this.firstSeenAt = Instant.now(); + this.lastSeenAt = Instant.now(); this.uses = 0; } public void used() { - this.lastSeenAt = new Date(); + this.lastSeenAt = Instant.now(); this.uses++; } diff --git a/src/main/java/net/frozenorb/apiv3/model/Punishment.java b/src/main/java/net/frozenorb/apiv3/model/Punishment.java index 4b80d74..ef62520 100644 --- a/src/main/java/net/frozenorb/apiv3/model/Punishment.java +++ b/src/main/java/net/frozenorb/apiv3/model/Punishment.java @@ -16,6 +16,7 @@ import net.frozenorb.apiv3.util.TimeUtils; import org.bson.Document; import org.bson.types.ObjectId; +import java.time.Instant; import java.util.*; import java.util.stream.Collectors; @@ -29,17 +30,17 @@ public final class Punishment { @Getter private UUID user; @Getter private String reason; @Getter private PunishmentType type; - @Getter private Date expiresAt; + @Getter private Instant expiresAt; @Getter private Map metadata; @Getter private String linkedIpBanId; @Getter private UUID addedBy; - @Getter private Date addedAt; + @Getter private Instant addedAt; @Getter private String actorName; @Getter private ActorType actorType; @Getter private UUID removedBy; - @Getter private Date removedAt; + @Getter private Instant removedAt; @Getter private String removalReason; public static List findAllSync() { @@ -132,14 +133,14 @@ public final class Punishment { public Punishment() {} // For Morphia - public Punishment(User user, String reason, PunishmentType type, Date expiresAt, User addedBy, Actor actor, Map metadata) { + public Punishment(User user, String reason, PunishmentType type, Instant expiresAt, User addedBy, Actor actor, Map metadata) { this.id = new ObjectId().toString(); this.user = user.getId(); this.reason = reason; this.type = type; this.expiresAt = expiresAt; this.addedBy = addedBy == null ? null : addedBy.getId(); - this.addedAt = new Date(); + this.addedAt = Instant.now(); this.actorName = actor.getName(); this.actorType = actor.getType(); this.metadata = metadata; @@ -153,7 +154,7 @@ public final class Punishment { if (expiresAt == null) { return false; // Never expires } else { - return expiresAt.before(new Date()); + return expiresAt.isBefore(Instant.now()); } } @@ -169,7 +170,7 @@ public final class Punishment { String accessDenialReason = "Your account has been suspended from the MineHQ Network. \n\n"; if (getExpiresAt() != null) { - accessDenialReason += "Expires in " + TimeUtils.formatIntoDetailedString(TimeUtils.getSecondsBetween(getExpiresAt(), new Date())); + accessDenialReason += "Expires in " + TimeUtils.formatIntoDetailedString(TimeUtils.getSecondsBetween(getExpiresAt(), Instant.now())); } else { accessDenialReason += "Appeal at MineHQ.com/appeal"; } @@ -192,7 +193,7 @@ public final class Punishment { public void delete(User removedBy, String reason) { this.removedBy = removedBy.getId(); - this.removedAt = new Date(); + this.removedAt = Instant.now(); this.removalReason = reason; if (linkedIpBanId != null) { diff --git a/src/main/java/net/frozenorb/apiv3/model/Server.java b/src/main/java/net/frozenorb/apiv3/model/Server.java index 4cc11f3..94c7ca3 100644 --- a/src/main/java/net/frozenorb/apiv3/model/Server.java +++ b/src/main/java/net/frozenorb/apiv3/model/Server.java @@ -14,6 +14,7 @@ import net.frozenorb.apiv3.unsorted.BlockingCallback; import net.frozenorb.apiv3.util.SyncUtils; import org.bson.Document; +import java.time.Instant; import java.util.*; import java.util.concurrent.TimeUnit; @@ -31,7 +32,7 @@ public final class Server { @Getter @ExcludeFromReplies String apiKey; @Getter private String serverGroup; @Getter private String serverIp; - @Getter @Setter private Date lastUpdatedAt; + @Getter @Setter private Instant lastUpdatedAt; @Getter @Setter private double lastTps; @Getter @Setter @ExcludeFromReplies private Set players; @@ -53,7 +54,7 @@ public final class Server { this.apiKey = apiKey; this.serverGroup = serverGroup.getId(); this.serverIp = serverIp; - this.lastUpdatedAt = new Date(); + this.lastUpdatedAt = Instant.now(); this.lastTps = 0; this.players = new HashSet<>(); } @@ -75,7 +76,7 @@ public final class Server { } public void receivedHeartbeat(double tps, Iterable players) { - this.lastUpdatedAt = new Date(); + this.lastUpdatedAt = Instant.now(); this.lastTps = tps; this.players = ImmutableSet.copyOf(players); } diff --git a/src/main/java/net/frozenorb/apiv3/model/User.java b/src/main/java/net/frozenorb/apiv3/model/User.java index 9a105a6..7a47dc1 100644 --- a/src/main/java/net/frozenorb/apiv3/model/User.java +++ b/src/main/java/net/frozenorb/apiv3/model/User.java @@ -27,6 +27,7 @@ import net.frozenorb.apiv3.util.SyncUtils; import net.frozenorb.apiv3.util.UUIDUtils; import org.bson.Document; +import java.time.Instant; import java.util.*; @Entity @@ -37,16 +38,16 @@ public final class User { @Getter @Id @JsonSerialize(using=UUIDJsonSerializer.class) @JsonDeserialize(using=UUIDJsonDeserializer.class) private UUID id; @Getter private String lastUsername; - @Getter @ExcludeFromReplies private Map aliases = new HashMap<>(); + @Getter @ExcludeFromReplies private Map aliases = new HashMap<>(); @Getter @ExcludeFromReplies @Setter private String totpSecret; @Getter @ExcludeFromReplies @Setter private String emailToken; - @Getter @ExcludeFromReplies @Setter private Date emailTokenSetAt; + @Getter @ExcludeFromReplies @Setter private Instant emailTokenSetAt; @Getter @ExcludeFromReplies private String password; @Getter @Setter private String email; @Getter private String phoneNumber; @Getter private String lastSeenOn; - @Getter private Date lastSeenAt; - @Getter private Date firstSeenAt; + @Getter private Instant lastSeenAt; + @Getter private Instant firstSeenAt; @Getter private boolean online; public static List findAllSync() { @@ -141,8 +142,8 @@ public final class User { this.email = null; this.phoneNumber = null; this.lastSeenOn = null; - this.lastSeenAt = new Date(); - this.firstSeenAt = new Date(); + this.lastSeenAt = Instant.now(); + this.firstSeenAt = Instant.now(); // TODO: MAKE THIS ASYNC? SOMEHOW? BlockingCallback blockingCallback = new BlockingCallback<>(); @@ -175,7 +176,7 @@ public final class User { this.lastSeenOn = server.getId(); if (!online) { - this.lastSeenAt = new Date(); + this.lastSeenAt = Instant.now(); } this.online = true; @@ -183,12 +184,12 @@ public final class User { } public void leftServer() { - this.lastSeenAt = new Date(); + this.lastSeenAt = Instant.now(); this.online = false; } public void updateUsername(String newUsername, SingleResultCallback callback) { - this.aliases.put(newUsername, new Date()); + this.aliases.put(newUsername, Instant.now()); if (newUsername.equalsIgnoreCase(lastUsername)) { callback.onResult(null, null); diff --git a/src/main/java/net/frozenorb/apiv3/route/grants/POSTUserGrant.java b/src/main/java/net/frozenorb/apiv3/route/grants/POSTUserGrant.java index f55e72f..3254651 100644 --- a/src/main/java/net/frozenorb/apiv3/route/grants/POSTUserGrant.java +++ b/src/main/java/net/frozenorb/apiv3/route/grants/POSTUserGrant.java @@ -9,7 +9,7 @@ import net.frozenorb.apiv3.model.ServerGroup; import net.frozenorb.apiv3.model.User; import net.frozenorb.apiv3.util.ErrorUtils; -import java.util.Date; +import java.time.Instant; import java.util.HashSet; import java.util.Set; @@ -53,16 +53,16 @@ public final class POSTUserGrant implements Handler { return; } - Date expiresAt; + Instant expiresAt; try { - expiresAt = new Date(Long.parseLong(ctx.request().getParam("expiresAt"))); + expiresAt = Instant.ofEpochMilli(Long.parseLong(ctx.request().getParam("expiresAt"))); } catch (NumberFormatException ex) { expiresAt = null; } - if (expiresAt != null && expiresAt.before(new Date())) { - ErrorUtils.respondInvalidInput(ctx, "Expiration date cannot be in the past."); + if (expiresAt != null && expiresAt.isBefore(Instant.now())) { + ErrorUtils.respondInvalidInput(ctx, "Expiration time cannot be in the past."); return; } diff --git a/src/main/java/net/frozenorb/apiv3/route/ipBans/POSTIpIpBan.java b/src/main/java/net/frozenorb/apiv3/route/ipBans/POSTIpIpBan.java index d8d9105..81fb1db 100644 --- a/src/main/java/net/frozenorb/apiv3/route/ipBans/POSTIpIpBan.java +++ b/src/main/java/net/frozenorb/apiv3/route/ipBans/POSTIpIpBan.java @@ -8,7 +8,7 @@ import net.frozenorb.apiv3.model.User; import net.frozenorb.apiv3.util.ErrorUtils; import net.frozenorb.apiv3.util.IpUtils; -import java.util.Date; +import java.time.Instant; public final class POSTIpIpBan implements Handler { @@ -27,16 +27,16 @@ public final class POSTIpIpBan implements Handler { return; } - Date expiresAt; + Instant expiresAt; try { - expiresAt = new Date(Long.parseLong(ctx.request().getParam("expiresAt"))); + expiresAt = Instant.ofEpochMilli(Long.parseLong(ctx.request().getParam("expiresAt"))); } catch (NumberFormatException ex) { expiresAt = null; } - if (expiresAt != null && expiresAt.before(new Date())) { - ErrorUtils.respondInvalidInput(ctx, "Expiration date cannot be in the past."); + if (expiresAt != null && expiresAt.isBefore(Instant.now())) { + ErrorUtils.respondInvalidInput(ctx, "Expiration time cannot be in the past."); return; } diff --git a/src/main/java/net/frozenorb/apiv3/route/punishments/POSTUserPunish.java b/src/main/java/net/frozenorb/apiv3/route/punishments/POSTUserPunish.java index 9e5354f..0fd7298 100644 --- a/src/main/java/net/frozenorb/apiv3/route/punishments/POSTUserPunish.java +++ b/src/main/java/net/frozenorb/apiv3/route/punishments/POSTUserPunish.java @@ -12,7 +12,7 @@ import net.frozenorb.apiv3.unsorted.Permissions; import net.frozenorb.apiv3.util.ErrorUtils; import org.bson.Document; -import java.util.Date; +import java.time.Instant; import java.util.Map; public final class POSTUserPunish implements Handler { @@ -43,16 +43,16 @@ public final class POSTUserPunish implements Handler { } } - Date expiresAt; + Instant expiresAt; try { - expiresAt = new Date(Long.parseLong(ctx.request().getParam("expiresAt"))); + expiresAt = Instant.ofEpochMilli(Long.parseLong(ctx.request().getParam("expiresAt"))); } catch (NumberFormatException ex) { expiresAt = null; } - if (expiresAt != null && expiresAt.before(new Date())) { - ErrorUtils.respondInvalidInput(ctx, "Expiration date cannot be in the past."); + if (expiresAt != null && expiresAt.isBefore(Instant.now())) { + ErrorUtils.respondInvalidInput(ctx, "Expiration time cannot be in the past."); return; } diff --git a/src/main/java/net/frozenorb/apiv3/route/servers/POSTServerHeartbeat.java b/src/main/java/net/frozenorb/apiv3/route/servers/POSTServerHeartbeat.java index 0ad2b64..b0c3a36 100644 --- a/src/main/java/net/frozenorb/apiv3/route/servers/POSTServerHeartbeat.java +++ b/src/main/java/net/frozenorb/apiv3/route/servers/POSTServerHeartbeat.java @@ -16,7 +16,11 @@ import net.frozenorb.apiv3.util.PermissionUtils; import net.frozenorb.apiv3.util.UUIDUtils; import org.bson.Document; -import java.util.*; +import java.time.Instant; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; @Slf4j public final class POSTServerHeartbeat implements Handler { @@ -60,7 +64,7 @@ public final class POSTServerHeartbeat implements Handler { server.setPlayers(playerNames.keySet()); server.setLastTps(tps); - server.setLastUpdatedAt(new Date()); + server.setLastUpdatedAt(Instant.now()); server.save(); callback.complete(); diff --git a/src/main/java/net/frozenorb/apiv3/route/users/POSTUserConfirmRegister.java b/src/main/java/net/frozenorb/apiv3/route/users/POSTUserConfirmRegister.java index c71ec5f..366340f 100644 --- a/src/main/java/net/frozenorb/apiv3/route/users/POSTUserConfirmRegister.java +++ b/src/main/java/net/frozenorb/apiv3/route/users/POSTUserConfirmRegister.java @@ -36,7 +36,7 @@ public final class POSTUserConfirmRegister implements Handler { return; } - if ((System.currentTimeMillis() - user.getEmailTokenSetAt().getTime()) > TimeUnit.DAYS.toMillis(2)) { + if ((System.currentTimeMillis() - user.getEmailTokenSetAt().toEpochMilli()) > TimeUnit.DAYS.toMillis(2)) { ErrorUtils.respondGeneric(ctx, 200, "Email token is expired"); return; } diff --git a/src/main/java/net/frozenorb/apiv3/route/users/POSTUserRegister.java b/src/main/java/net/frozenorb/apiv3/route/users/POSTUserRegister.java index 811cf77..93c50b6 100644 --- a/src/main/java/net/frozenorb/apiv3/route/users/POSTUserRegister.java +++ b/src/main/java/net/frozenorb/apiv3/route/users/POSTUserRegister.java @@ -10,7 +10,7 @@ import net.frozenorb.apiv3.unsorted.Notification; import net.frozenorb.apiv3.util.ErrorUtils; import java.math.BigInteger; -import java.util.Date; +import java.time.Instant; import java.util.Map; import java.util.Random; import java.util.concurrent.TimeUnit; @@ -43,14 +43,14 @@ public final class POSTUserRegister implements Handler { return; } - if (user.getEmailToken() != null && (System.currentTimeMillis() - user.getEmailTokenSetAt().getTime()) < TimeUnit.DAYS.toMillis(2)) { + if (user.getEmailToken() != null && (System.currentTimeMillis() - user.getEmailTokenSetAt().toEpochMilli()) < TimeUnit.DAYS.toMillis(2)) { ErrorUtils.respondGeneric(ctx, 200, "We just recently sent you a confirmation email. Please wait before trying to register again."); return; } user.setEmail(email); user.setEmailToken(new BigInteger(130, new Random()).toString(32)); - user.setEmailTokenSetAt(new Date()); + user.setEmailTokenSetAt(Instant.now()); user.save(); Map replacements = ImmutableMap.of( diff --git a/src/main/java/net/frozenorb/apiv3/serialization/gson/DateTypeAdapter.java b/src/main/java/net/frozenorb/apiv3/serialization/gson/InstantTimeAdapter.java similarity index 64% rename from src/main/java/net/frozenorb/apiv3/serialization/gson/DateTypeAdapter.java rename to src/main/java/net/frozenorb/apiv3/serialization/gson/InstantTimeAdapter.java index 813a274..e6263f2 100644 --- a/src/main/java/net/frozenorb/apiv3/serialization/gson/DateTypeAdapter.java +++ b/src/main/java/net/frozenorb/apiv3/serialization/gson/InstantTimeAdapter.java @@ -5,22 +5,22 @@ import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonWriter; import java.io.IOException; -import java.util.Date; +import java.time.Instant; -public final class DateTypeAdapter extends TypeAdapter { +public final class InstantTimeAdapter extends TypeAdapter { - public void write(JsonWriter writer, Date write) throws IOException { + public void write(JsonWriter writer, Instant write) throws IOException { if (write == null) { writer.nullValue(); } else { - writer.value(write.getTime()); + writer.value(write.toEpochMilli()); } } // This is used with Gson, which is only used // to serialize outgoing responses, thus we // don't need to have a read method. - public Date read(JsonReader reader) { + public Instant read(JsonReader reader) { throw new IllegalArgumentException(); } diff --git a/src/main/java/net/frozenorb/apiv3/util/TimeUtils.java b/src/main/java/net/frozenorb/apiv3/util/TimeUtils.java index 2d4228e..0ce40de 100644 --- a/src/main/java/net/frozenorb/apiv3/util/TimeUtils.java +++ b/src/main/java/net/frozenorb/apiv3/util/TimeUtils.java @@ -2,7 +2,7 @@ package net.frozenorb.apiv3.util; import lombok.experimental.UtilityClass; -import java.util.Date; +import java.time.Instant; @UtilityClass public class TimeUtils { @@ -27,8 +27,8 @@ public class TimeUtils { return (fDays + fHours + fMinutes + fSeconds).trim(); } - public static int getSecondsBetween(Date a, Date b) { - return (Math.abs((int) (a.getTime() - b.getTime()) / 1000)); + public static int getSecondsBetween(Instant a, Instant b) { + return (Math.abs((int) (a.toEpochMilli() - b.toEpochMilli()) / 1000)); } } \ No newline at end of file From 63b1e7b2c3802fe4d55e382382a088a9a97ceb02 Mon Sep 17 00:00:00 2001 From: Colin McDonald Date: Fri, 17 Jun 2016 01:07:20 -0400 Subject: [PATCH 02/24] Add MaxMind files --- apiv3.properties | 2 ++ .../frozenorb/apiv3/maxmind/MaxMindCity.java | 4 +++ .../apiv3/maxmind/MaxMindContinent.java | 4 +++ .../apiv3/maxmind/MaxMindCountry.java | 4 +++ .../apiv3/maxmind/MaxMindLocation.java | 4 +++ .../apiv3/maxmind/MaxMindPostal.java | 4 +++ .../maxmind/MaxMindRegisteredCountry.java | 4 +++ .../apiv3/maxmind/MaxMindResult.java | 20 ++++++++++++ .../apiv3/maxmind/MaxMindSubdivision.java | 4 +++ .../apiv3/maxmind/MaxMindTraits.java | 4 +++ .../frozenorb/apiv3/util/MaxMindUtils.java | 31 +++++++++++++++++++ 11 files changed, 85 insertions(+) create mode 100644 src/main/java/net/frozenorb/apiv3/maxmind/MaxMindCity.java create mode 100644 src/main/java/net/frozenorb/apiv3/maxmind/MaxMindContinent.java create mode 100644 src/main/java/net/frozenorb/apiv3/maxmind/MaxMindCountry.java create mode 100644 src/main/java/net/frozenorb/apiv3/maxmind/MaxMindLocation.java create mode 100644 src/main/java/net/frozenorb/apiv3/maxmind/MaxMindPostal.java create mode 100644 src/main/java/net/frozenorb/apiv3/maxmind/MaxMindRegisteredCountry.java create mode 100644 src/main/java/net/frozenorb/apiv3/maxmind/MaxMindResult.java create mode 100644 src/main/java/net/frozenorb/apiv3/maxmind/MaxMindSubdivision.java create mode 100644 src/main/java/net/frozenorb/apiv3/maxmind/MaxMindTraits.java create mode 100644 src/main/java/net/frozenorb/apiv3/util/MaxMindUtils.java diff --git a/apiv3.properties b/apiv3.properties index 436214b..dcb7dee 100644 --- a/apiv3.properties +++ b/apiv3.properties @@ -9,5 +9,7 @@ redis.port=6379 http.port=80 mandrill.apiKey=0OYtwymqJP6oqvszeJu0vQ bugsnag.apiKey=0e47fba8b825416b7cbc839066184509 +maxMind.userId=66817 +maxMind.maxMindLicenseKey=8Aw9NsOUeOp7 auth.websiteApiKey=RVbp4hY6sCFVaf auth.bungeeApiKey=6d9cf76dc9f0d23 \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindCity.java b/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindCity.java new file mode 100644 index 0000000..83f3afe --- /dev/null +++ b/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindCity.java @@ -0,0 +1,4 @@ +package net.frozenorb.apiv3.maxmind; + +public class MaxMindCity { +} diff --git a/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindContinent.java b/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindContinent.java new file mode 100644 index 0000000..48f1312 --- /dev/null +++ b/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindContinent.java @@ -0,0 +1,4 @@ +package net.frozenorb.apiv3.maxmind; + +public class MaxMindContinent { +} diff --git a/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindCountry.java b/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindCountry.java new file mode 100644 index 0000000..b6d82c3 --- /dev/null +++ b/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindCountry.java @@ -0,0 +1,4 @@ +package net.frozenorb.apiv3.maxmind; + +public class MaxMindCountry { +} diff --git a/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindLocation.java b/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindLocation.java new file mode 100644 index 0000000..1fc0675 --- /dev/null +++ b/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindLocation.java @@ -0,0 +1,4 @@ +package net.frozenorb.apiv3.maxmind; + +public class MaxMindLocation { +} diff --git a/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindPostal.java b/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindPostal.java new file mode 100644 index 0000000..d098f9f --- /dev/null +++ b/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindPostal.java @@ -0,0 +1,4 @@ +package net.frozenorb.apiv3.maxmind; + +public class MaxMindPostal { +} diff --git a/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindRegisteredCountry.java b/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindRegisteredCountry.java new file mode 100644 index 0000000..db69f89 --- /dev/null +++ b/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindRegisteredCountry.java @@ -0,0 +1,4 @@ +package net.frozenorb.apiv3.maxmind; + +public class MaxMindRegisteredCountry { +} diff --git a/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindResult.java b/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindResult.java new file mode 100644 index 0000000..c5ebce2 --- /dev/null +++ b/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindResult.java @@ -0,0 +1,20 @@ +package net.frozenorb.apiv3.maxmind; + +import java.time.Instant; + +public final class MaxMindResult { + + private String ip; + private Instant lastUpdatedAt; + private MaxMindContinent continent; + private MaxMindCity city; + private MaxMindPostal postal; + private MaxMindTraits traits; + private MaxMindLocation location; + private MaxMindSubdivision[] subdivisions; + private MaxMindCountry country; + private MaxMindRegisteredCountry registeredCountry; + + public MaxMindResult() {} + +} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindSubdivision.java b/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindSubdivision.java new file mode 100644 index 0000000..7576f98 --- /dev/null +++ b/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindSubdivision.java @@ -0,0 +1,4 @@ +package net.frozenorb.apiv3.maxmind; + +public class MaxMindSubdivision { +} diff --git a/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindTraits.java b/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindTraits.java new file mode 100644 index 0000000..986b8f2 --- /dev/null +++ b/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindTraits.java @@ -0,0 +1,4 @@ +package net.frozenorb.apiv3.maxmind; + +public class MaxMindTraits { +} diff --git a/src/main/java/net/frozenorb/apiv3/util/MaxMindUtils.java b/src/main/java/net/frozenorb/apiv3/util/MaxMindUtils.java new file mode 100644 index 0000000..c96e877 --- /dev/null +++ b/src/main/java/net/frozenorb/apiv3/util/MaxMindUtils.java @@ -0,0 +1,31 @@ +package net.frozenorb.apiv3.util; + +import com.google.common.base.Charsets; +import com.mongodb.async.SingleResultCallback; +import lombok.experimental.UtilityClass; +import net.frozenorb.apiv3.APIv3; +import org.bson.Document; + +import java.util.Base64; + +@UtilityClass +public class MaxMindUtils { + + private static final String maxMindUserId = APIv3.getConfig().getProperty("maxMind.userId"); + private static final String maxMindLicenseKey = APIv3.getConfig().getProperty("maxMind.maxMindLicenseKey"); + + public static void getInsights(String ip, SingleResultCallback callback) { + // We have to specifically use getHttpSClient(), vertx's http client is dumb. + APIv3.getHttpsClient().get(443, "geoip.maxmind.com", "/geoip/v2.1/insights/" + ip, (response) -> { + response.bodyHandler((body) -> { + Document resJson = Document.parse(body.toString()); + callback.onResult(resJson, null); + }); + + response.exceptionHandler((error) -> { + callback.onResult(null, error); + }); + }).putHeader("Authorization", "Basic " + Base64.getEncoder().encodeToString((maxMindUserId + ":" + maxMindLicenseKey).getBytes(Charsets.UTF_8))).end(); + } + +} \ No newline at end of file From a92dee1e6fbab69ba4d64bd4869f869eab56bb56 Mon Sep 17 00:00:00 2001 From: Colin McDonald Date: Fri, 17 Jun 2016 01:09:28 -0400 Subject: [PATCH 03/24] Rename some classes to fit Google's Java standards --- .../apiv3/dataImport/converters/UserConverter.java | 4 ++-- .../apiv3/serialization/jackson/UUIDJsonDeserializer.java | 2 +- .../apiv3/serialization/jackson/UUIDJsonSerializer.java | 2 +- .../frozenorb/apiv3/serialization/mongodb/UUIDCodec.java | 2 +- .../apiv3/serialization/mongodb/UUIDCodecProvider.java | 4 ++-- .../net/frozenorb/apiv3/unsorted/BugsnagSLF4JLogger.java | 2 +- src/main/java/net/frozenorb/apiv3/util/TOTPUtils.java | 6 +++--- src/main/java/net/frozenorb/apiv3/util/UUIDUtils.java | 4 ++-- 8 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/main/java/net/frozenorb/apiv3/dataImport/converters/UserConverter.java b/src/main/java/net/frozenorb/apiv3/dataImport/converters/UserConverter.java index 246a4b7..b8effbf 100644 --- a/src/main/java/net/frozenorb/apiv3/dataImport/converters/UserConverter.java +++ b/src/main/java/net/frozenorb/apiv3/dataImport/converters/UserConverter.java @@ -4,7 +4,7 @@ import com.google.common.collect.ImmutableMap; import com.mongodb.Block; import lombok.extern.slf4j.Slf4j; import net.frozenorb.apiv3.model.User; -import net.frozenorb.apiv3.util.UUIDUtils; +import net.frozenorb.apiv3.util.UuidUtils; import org.bson.Document; import org.bson.types.ObjectId; @@ -30,7 +30,7 @@ public final class UserConverter implements Block { UUID uuid = UUID.fromString(uuidString.replaceFirst("([0-9a-fA-F]{8})([0-9a-fA-F]{4})([0-9a-fA-F]{4})([0-9a-fA-F]{4})([0-9a-fA-F]+)", "$1-$2-$3-$4-$5")); - if (!UUIDUtils.isAcceptableUUID(uuid)) { + if (!UuidUtils.isAcceptableUuid(uuid)) { return; } diff --git a/src/main/java/net/frozenorb/apiv3/serialization/jackson/UUIDJsonDeserializer.java b/src/main/java/net/frozenorb/apiv3/serialization/jackson/UUIDJsonDeserializer.java index 35c1af5..f086f8f 100644 --- a/src/main/java/net/frozenorb/apiv3/serialization/jackson/UUIDJsonDeserializer.java +++ b/src/main/java/net/frozenorb/apiv3/serialization/jackson/UUIDJsonDeserializer.java @@ -8,7 +8,7 @@ import com.fasterxml.jackson.databind.JsonDeserializer; import java.io.IOException; import java.util.UUID; -public class UUIDJsonDeserializer extends JsonDeserializer { +public final class UuidJsonDeserializer extends JsonDeserializer { @Override public UUID deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { diff --git a/src/main/java/net/frozenorb/apiv3/serialization/jackson/UUIDJsonSerializer.java b/src/main/java/net/frozenorb/apiv3/serialization/jackson/UUIDJsonSerializer.java index c81e401..6818f95 100644 --- a/src/main/java/net/frozenorb/apiv3/serialization/jackson/UUIDJsonSerializer.java +++ b/src/main/java/net/frozenorb/apiv3/serialization/jackson/UUIDJsonSerializer.java @@ -8,7 +8,7 @@ import com.fasterxml.jackson.databind.SerializerProvider; import java.io.IOException; import java.util.UUID; -public final class UUIDJsonSerializer extends JsonSerializer { +public final class UuidJsonSerializer extends JsonSerializer { @Override public void serialize(UUID uuid, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException { diff --git a/src/main/java/net/frozenorb/apiv3/serialization/mongodb/UUIDCodec.java b/src/main/java/net/frozenorb/apiv3/serialization/mongodb/UUIDCodec.java index 211956b..2c3d949 100644 --- a/src/main/java/net/frozenorb/apiv3/serialization/mongodb/UUIDCodec.java +++ b/src/main/java/net/frozenorb/apiv3/serialization/mongodb/UUIDCodec.java @@ -8,7 +8,7 @@ import org.bson.codecs.EncoderContext; import java.util.UUID; -public final class UUIDCodec implements Codec { +public final class UuidCodec implements Codec { @Override public UUID decode(BsonReader bsonReader, DecoderContext decoderContext) { diff --git a/src/main/java/net/frozenorb/apiv3/serialization/mongodb/UUIDCodecProvider.java b/src/main/java/net/frozenorb/apiv3/serialization/mongodb/UUIDCodecProvider.java index b5bd0b8..afd0e9a 100644 --- a/src/main/java/net/frozenorb/apiv3/serialization/mongodb/UUIDCodecProvider.java +++ b/src/main/java/net/frozenorb/apiv3/serialization/mongodb/UUIDCodecProvider.java @@ -6,11 +6,11 @@ import org.bson.codecs.configuration.CodecRegistry; import java.util.UUID; -public final class UUIDCodecProvider implements CodecProvider { +public final class UuidCodecProvider implements CodecProvider { public Codec get(Class clazz, CodecRegistry codecRegistry) { if (clazz == UUID.class) { - return (Codec) new UUIDCodec(); + return (Codec) new UuidCodec(); } else { return null; } diff --git a/src/main/java/net/frozenorb/apiv3/unsorted/BugsnagSLF4JLogger.java b/src/main/java/net/frozenorb/apiv3/unsorted/BugsnagSLF4JLogger.java index f1574c6..2aba341 100644 --- a/src/main/java/net/frozenorb/apiv3/unsorted/BugsnagSLF4JLogger.java +++ b/src/main/java/net/frozenorb/apiv3/unsorted/BugsnagSLF4JLogger.java @@ -4,7 +4,7 @@ import com.bugsnag.Logger; import lombok.extern.slf4j.Slf4j; @Slf4j -public class BugsnagSLF4JLogger extends Logger { +public class BugsnagSlf4jLogger extends Logger { public void debug(String message) { log.debug(message); diff --git a/src/main/java/net/frozenorb/apiv3/util/TOTPUtils.java b/src/main/java/net/frozenorb/apiv3/util/TOTPUtils.java index 9c29844..42c81ac 100644 --- a/src/main/java/net/frozenorb/apiv3/util/TOTPUtils.java +++ b/src/main/java/net/frozenorb/apiv3/util/TOTPUtils.java @@ -12,11 +12,11 @@ import net.frozenorb.apiv3.model.User; import java.util.concurrent.TimeUnit; @UtilityClass -public class TOTPUtils { +public class TotpUtils { private static GoogleAuthenticator googleAuthenticator = new GoogleAuthenticator(new GoogleAuthenticatorConfig.GoogleAuthenticatorConfigBuilder().setWindowSize(10).build()); - public static GoogleAuthenticatorKey generateTOTPSecret() { + public static GoogleAuthenticatorKey generateTotpSecret() { return googleAuthenticator.createCredentials(); } @@ -24,7 +24,7 @@ public class TOTPUtils { return googleAuthenticator.authorize(user.getTotpSecret(), code); } - public static String getQRCodeURL(User user, GoogleAuthenticatorKey secret) { + public static String getQrCodeUrl(User user, GoogleAuthenticatorKey secret) { return GoogleAuthenticatorQRGenerator.getOtpAuthURL( "MineHQ Network", user.getLastUsername(), diff --git a/src/main/java/net/frozenorb/apiv3/util/UUIDUtils.java b/src/main/java/net/frozenorb/apiv3/util/UUIDUtils.java index 01529ef..ce0af79 100644 --- a/src/main/java/net/frozenorb/apiv3/util/UUIDUtils.java +++ b/src/main/java/net/frozenorb/apiv3/util/UUIDUtils.java @@ -5,9 +5,9 @@ import lombok.experimental.UtilityClass; import java.util.UUID; @UtilityClass -public class UUIDUtils { +public class UuidUtils { - public static boolean isAcceptableUUID(UUID uuid) { + public static boolean isAcceptableUuid(UUID uuid) { return uuid.version() == 4; } From de933bc5719fc46c3b2a056936ae08716e0c3638 Mon Sep 17 00:00:00 2001 From: Colin McDonald Date: Fri, 17 Jun 2016 01:10:09 -0400 Subject: [PATCH 04/24] Convert some basic read-only routes to async --- src/main/java/net/frozenorb/apiv3/APIv3.java | 142 ++++++++++-------- .../route/announcements/GETAnnouncements.java | 5 +- .../route/announcements/PUTAnnouncements.java | 26 ++-- .../apiv3/route/auditLog/GETAuditLog.java | 8 +- .../route/auditLog/POSTUserAuditLogEntry.java | 50 +++--- .../apiv3/route/grants/GETGrant.java | 9 +- .../apiv3/route/grants/GETGrants.java | 8 +- .../apiv3/route/grants/GETUserGrants.java | 23 ++- .../apiv3/route/ipBans/GETIpBan.java | 10 +- .../apiv3/route/ipBans/GETIpBans.java | 9 +- .../apiv3/route/ipBans/GETIpIpBans.java | 10 +- .../apiv3/route/ipLog/GETUserIpLog.java | 25 ++- .../GETNotificationTemplate.java | 10 +- .../GETNotificationTemplates.java | 10 +- .../route/punishments/GETPunishment.java | 10 +- .../route/punishments/GETPunishments.java | 9 +- .../route/punishments/GETUserPunishments.java | 24 ++- .../route/punishments/POSTUserPunish.java | 2 +- .../route/servers/POSTServerHeartbeat.java | 4 +- .../frozenorb/apiv3/route/users/GETUser.java | 10 +- .../route/users/GETUserRequiresTOTP.java | 4 +- .../apiv3/route/users/POSTUserLogin.java | 71 ++++----- .../apiv3/route/users/POSTUserSetupTOTP.java | 6 +- .../apiv3/route/users/POSTUserVerifyTOTP.java | 10 +- 24 files changed, 304 insertions(+), 191 deletions(-) diff --git a/src/main/java/net/frozenorb/apiv3/APIv3.java b/src/main/java/net/frozenorb/apiv3/APIv3.java index 80e73ab..e8bfda5 100644 --- a/src/main/java/net/frozenorb/apiv3/APIv3.java +++ b/src/main/java/net/frozenorb/apiv3/APIv3.java @@ -21,10 +21,7 @@ import com.mongodb.connection.ClusterSettings; import fr.javatic.mongo.jacksonCodec.JacksonCodecProvider; import fr.javatic.mongo.jacksonCodec.ObjectMapperFactory; import io.vertx.core.AbstractVerticle; -import io.vertx.core.http.HttpClient; -import io.vertx.core.http.HttpHeaders; -import io.vertx.core.http.HttpMethod; -import io.vertx.core.http.HttpServer; +import io.vertx.core.http.*; import io.vertx.ext.web.Router; import io.vertx.ext.web.RoutingContext; import io.vertx.ext.web.handler.BodyHandler; @@ -63,11 +60,13 @@ import net.frozenorb.apiv3.route.serverGroups.POSTServerGroup; import net.frozenorb.apiv3.route.servers.*; import net.frozenorb.apiv3.route.users.*; import net.frozenorb.apiv3.serialization.gson.FollowAnnotationExclusionStrategy; -import net.frozenorb.apiv3.serialization.gson.InstantTimeAdapter; -import net.frozenorb.apiv3.serialization.jackson.UUIDJsonDeserializer; -import net.frozenorb.apiv3.serialization.jackson.UUIDJsonSerializer; -import net.frozenorb.apiv3.serialization.mongodb.UUIDCodecProvider; -import net.frozenorb.apiv3.unsorted.BugsnagSLF4JLogger; +import net.frozenorb.apiv3.serialization.gson.InstantTypeAdapter; +import net.frozenorb.apiv3.serialization.jackson.InstantJsonDeserializer; +import net.frozenorb.apiv3.serialization.jackson.InstantJsonSerializer; +import net.frozenorb.apiv3.serialization.jackson.UuidJsonDeserializer; +import net.frozenorb.apiv3.serialization.jackson.UuidJsonSerializer; +import net.frozenorb.apiv3.serialization.mongodb.UuidCodecProvider; +import net.frozenorb.apiv3.unsorted.BugsnagSlf4jLogger; import org.bson.Document; import org.bson.codecs.BsonValueCodecProvider; import org.bson.codecs.DocumentCodecProvider; @@ -89,11 +88,12 @@ import java.util.UUID; public final class APIv3 extends AbstractVerticle { @Getter private static HttpClient httpClient; + @Getter private static HttpClient httpsClient; @Getter private static MongoDatabase database; @Getter private static Properties config = new Properties(); @Getter private static RedisClient redisClient; private static final Gson gson = new GsonBuilder() - .registerTypeAdapter(Instant.class, new InstantTimeAdapter()) + .registerTypeAdapter(Instant.class, new InstantTypeAdapter()) .setExclusionStrategies(new FollowAnnotationExclusionStrategy()) .create(); @@ -164,11 +164,15 @@ public final class APIv3 extends AbstractVerticle { new IndexModel(new Document("user", 1)), new IndexModel(new Document("user", 1).append("userIp", 1)) ), (a, b) -> {}); + database.getCollection("ipBans").createIndexes(ImmutableList.of( + new IndexModel(new Document("userIp", 1)) + ), (a, b) -> {}); database.getCollection("punishments").createIndexes(ImmutableList.of( new IndexModel(new Document("user", 1)), new IndexModel(new Document("type", 1)), new IndexModel(new Document("addedAt", 1)), - new IndexModel(new Document("addedBy", 1)) + new IndexModel(new Document("addedBy", 1)), + new IndexModel(new Document("linkedIpBanId", 1)) ), (a, b) -> {}); database.getCollection("users").createIndexes(ImmutableList.of( new IndexModel(new Document("lastUsername", 1)), @@ -183,8 +187,10 @@ public final class APIv3 extends AbstractVerticle { ObjectMapper objectMapper = ObjectMapperFactory.createObjectMapper(); SimpleModule simpleModule = new SimpleModule(); - simpleModule.addSerializer(UUID.class, new UUIDJsonSerializer()); - simpleModule.addDeserializer(UUID.class, new UUIDJsonDeserializer()); + simpleModule.addSerializer(Instant.class, new InstantJsonSerializer()); + simpleModule.addDeserializer(Instant.class, new InstantJsonDeserializer()); + simpleModule.addSerializer(UUID.class, new UuidJsonSerializer()); + simpleModule.addDeserializer(UUID.class, new UuidJsonDeserializer()); objectMapper.registerModule(simpleModule); objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE); @@ -194,7 +200,7 @@ public final class APIv3 extends AbstractVerticle { List providers = new ArrayList<>(); // Our fixed uuid codec - providers.add(new UUIDCodecProvider()); + providers.add(new UuidCodecProvider()); // Normal providers providers.add(new ValueCodecProvider()); @@ -220,7 +226,7 @@ public final class APIv3 extends AbstractVerticle { Client bugsnag = new Client(config.getProperty("bugsnag.apiKey")); bugsnag.setReleaseStage(config.getProperty("general.releaseStage")); bugsnag.setProjectPackages("net.frozenorb.apiv3"); - bugsnag.setLogger(new BugsnagSLF4JLogger()); + bugsnag.setLogger(new BugsnagSlf4jLogger()); } // TODO: blockingHandler -> handler @@ -235,79 +241,80 @@ public final class APIv3 extends AbstractVerticle { // TODO: The commented out routes - mainRouter.get("/announcements/:id").blockingHandler(new GETAnnouncements()); - mainRouter.put("/announcements/:id").blockingHandler(new PUTAnnouncements()); + mainRouter.get("/announcements/:id").handler(new GETAnnouncements()); + mainRouter.put("/announcements/:id").handler(new PUTAnnouncements()); - mainRouter.get("/auditLog").blockingHandler(new GETAuditLog()); - mainRouter.post("/user/:id/auditLogEntry").blockingHandler(new POSTUserAuditLogEntry()); + mainRouter.get("/auditLog").handler(new GETAuditLog()); + mainRouter.post("/user/:id/auditLogEntry").handler(new POSTUserAuditLogEntry()); mainRouter.get("/chatFilterList").handler(new GETChatFilterList()); - mainRouter.get("/grant/:id").blockingHandler(new GETGrant()); - mainRouter.get("/grants").blockingHandler(new GETGrants()); - mainRouter.get("/user/:id/grants").blockingHandler(new GETUserGrants()); - mainRouter.post("/user/:id/grant").blockingHandler(new POSTUserGrant()); - mainRouter.delete("/grant/:id").blockingHandler(new DELETEGrant()); + mainRouter.get("/grant/:id").handler(new GETGrant()); + mainRouter.get("/grants").handler(new GETGrants()); + mainRouter.get("/user/:id/grants").handler(new GETUserGrants()); + mainRouter.post("/user/:id/grant").blockingHandler(new POSTUserGrant(), false); + mainRouter.delete("/grant/:id").blockingHandler(new DELETEGrant(), false); - mainRouter.get("/ipBan/:id").blockingHandler(new GETIpBan()); - mainRouter.get("/ipBans").blockingHandler(new GETIpBans()); - mainRouter.get("/ip/:id/ipBans").blockingHandler(new GETIpIpBans()); - mainRouter.post("/ip/:id/ipBan").blockingHandler(new POSTIpIpBan()); - mainRouter.delete("/ipBan/:id").blockingHandler(new DELETEIpBan()); + mainRouter.get("/ipBan/:id").handler(new GETIpBan()); + mainRouter.get("/ipBans").handler(new GETIpBans()); + mainRouter.get("/ip/:id/ipBans").handler(new GETIpIpBans()); + mainRouter.post("/ip/:id/ipBan").blockingHandler(new POSTIpIpBan(), false); + mainRouter.delete("/ipBan/:id").blockingHandler(new DELETEIpBan(), false); - mainRouter.get("/user/:id/ipLog").blockingHandler(new GETUserIpLog()); + mainRouter.get("/user/:id/ipLog").handler(new GETUserIpLog()); - mainRouter.get("/notificationTemplate/:id").blockingHandler(new GETNotificationTemplate()); - mainRouter.get("/notificationTemplates").blockingHandler(new GETNotificationTemplates()); - mainRouter.post("/notificationTemplate").blockingHandler(new POSTNotificationTemplate()); - //mainRouter.put("/notificationTemplate/:id").blockingHandler(new PUTNotificationTemplate()); - mainRouter.delete("/notificationTemplate/:id").blockingHandler(new DELETENotificationTemplate()); + mainRouter.get("/notificationTemplate/:id").handler(new GETNotificationTemplate()); + mainRouter.get("/notificationTemplates").handler(new GETNotificationTemplates()); + mainRouter.post("/notificationTemplate").blockingHandler(new POSTNotificationTemplate(), false); + //mainRouter.put("/notificationTemplate/:id").blockingHandler(new PUTNotificationTemplate(), false); + mainRouter.delete("/notificationTemplate/:id").blockingHandler(new DELETENotificationTemplate(), false); - mainRouter.get("/punishment/:id").blockingHandler(new GETPunishment()); - mainRouter.get("/punishments").blockingHandler(new GETPunishments()); - mainRouter.get("/user/:id/punishments").blockingHandler(new GETUserPunishments()); - mainRouter.post("/user/:id/punish").blockingHandler(new POSTUserPunish()); - mainRouter.delete("/punishment/:id").blockingHandler(new DELETEPunishment()); - mainRouter.delete("/user/:id/punishment").blockingHandler(new DELETEUserPunishment()); + mainRouter.get("/punishment/:id").handler(new GETPunishment()); + mainRouter.get("/punishments").handler(new GETPunishments()); + mainRouter.get("/user/:id/punishments").handler(new GETUserPunishments()); + mainRouter.post("/user/:id/punish").blockingHandler(new POSTUserPunish(), false); + mainRouter.delete("/punishment/:id").blockingHandler(new DELETEPunishment(), false); + mainRouter.delete("/user/:id/punishment").blockingHandler(new DELETEUserPunishment(), false); mainRouter.get("/rank/:id").handler(new GETRank()); mainRouter.get("/ranks").handler(new GETRanks()); - mainRouter.post("/rank").blockingHandler(new POSTRank()); - //mainRouter.put("/rank/:id").blockingHandler(new PUTRank()); - mainRouter.delete("/rank/:id").blockingHandler(new DELETERank()); + mainRouter.post("/rank").blockingHandler(new POSTRank(), false); + //mainRouter.put("/rank/:id").blockingHandler(new PUTRank(), false); + mainRouter.delete("/rank/:id").blockingHandler(new DELETERank(), false); mainRouter.get("/serverGroup/:id").handler(new GETServerGroup()); mainRouter.get("/serverGroups").handler(new GETServerGroups()); - mainRouter.post("/serverGroup").blockingHandler(new POSTServerGroup()); - //mainRouter.put("/serverGroup/:id").blockingHandler(new PUTServerGroup()); - mainRouter.delete("/serverGroup/:id").blockingHandler(new DELETEServerGroup()); + mainRouter.post("/serverGroup").blockingHandler(new POSTServerGroup(), false); + //mainRouter.put("/serverGroup/:id").blockingHandler(new PUTServerGroup(), false); + mainRouter.delete("/serverGroup/:id").blockingHandler(new DELETEServerGroup(), false); mainRouter.get("/server/:id").handler(new GETServer()); mainRouter.get("/servers").handler(new GETServers()); mainRouter.post("/server/heartbeat").handler(new POSTServerHeartbeat()); - mainRouter.post("/server").blockingHandler(new POSTServer()); - //mainRouter.put("/server/:id").blockingHandler(new PUTServer()); - mainRouter.delete("/server/:id").blockingHandler(new DELETEServer()); + mainRouter.post("/server").blockingHandler(new POSTServer(), false); + //mainRouter.put("/server/:id").blockingHandler(new PUTServer(), false); + mainRouter.delete("/server/:id").blockingHandler(new DELETEServer(), false); - mainRouter.get("/staff").blockingHandler(new GETStaff()); - mainRouter.get("/user/:id").blockingHandler(new GETUser()); - mainRouter.get("/user/:id/details").blockingHandler(new GETUserDetails()); - mainRouter.get("/user/:id/meta/:serverGroup").blockingHandler(new GETUserMeta()); - mainRouter.get("/user/:id/requiresTOTP").blockingHandler(new GETUserRequiresTOTP()); - mainRouter.get("/user/:id/verifyPassword").blockingHandler(new GETUserVerifyPassword()); - mainRouter.post("/user/confirmRegister/:emailToken").blockingHandler(new POSTUserConfirmRegister()); + mainRouter.get("/staff").blockingHandler(new GETStaff(), false); + mainRouter.get("/user/:id").handler(new GETUser()); + mainRouter.get("/user/:id/details").blockingHandler(new GETUserDetails(), false); + mainRouter.get("/user/:id/meta/:serverGroup").blockingHandler(new GETUserMeta(), false); + mainRouter.get("/user/:id/permissions").blockingHandler(new GETUserPermissions(), false); + mainRouter.get("/user/:id/requiresTOTP").blockingHandler(new GETUserRequiresTOTP(), false); + mainRouter.get("/user/:id/verifyPassword").blockingHandler(new GETUserVerifyPassword(), false); + mainRouter.post("/user/confirmRegister/:emailToken").blockingHandler(new POSTUserConfirmRegister(), false); mainRouter.post("/user/:id/leave").handler(new POSTUserLeave()); - mainRouter.post("/user/:id/login").handler(new POSTUserLogin()); - mainRouter.post("/user/:id/notify").blockingHandler(new POSTUserNotify()); - mainRouter.post("/user/:id/register").blockingHandler(new POSTUserRegister()); - mainRouter.post("/user/:id/setupTOTP").blockingHandler(new POSTUserSetupTOTP()); - mainRouter.post("/user/:id/verifyTOTP").blockingHandler(new POSTUserVerifyTOTP()); - mainRouter.put("/user/:id/meta/:serverGroup").blockingHandler(new PUTUserMeta()); - mainRouter.delete("/user/:id/meta/:serverGroup").blockingHandler(new DELETEUserMeta()); + mainRouter.post("/user/:id/login").blockingHandler(new POSTUserLogin()); + mainRouter.post("/user/:id/notify").blockingHandler(new POSTUserNotify(), false); + mainRouter.post("/user/:id/register").blockingHandler(new POSTUserRegister(), false); + mainRouter.post("/user/:id/setupTOTP").blockingHandler(new POSTUserSetupTOTP(), false); + mainRouter.post("/user/:id/verifyTOTP").blockingHandler(new POSTUserVerifyTOTP(), false); + mainRouter.put("/user/:id/meta/:serverGroup").blockingHandler(new PUTUserMeta(), false); + mainRouter.delete("/user/:id/meta/:serverGroup").blockingHandler(new DELETEUserMeta(), false); mainRouter.get("/dump/:type").handler(new GETDump()); mainRouter.get("/whoami").handler(new GETWhoAmI()); - mainRouter.post("/metrics").blockingHandler(new POSTMetrics()); + mainRouter.post("/metrics").handler(new POSTMetrics()); int port = Integer.parseInt(config.getProperty("http.port")); webServer.requestHandler(mainRouter::accept).listen(port); @@ -315,6 +322,11 @@ public final class APIv3 extends AbstractVerticle { private void setupHttpClient() { httpClient = vertx.createHttpClient(); + httpsClient = vertx.createHttpClient( + new HttpClientOptions() + .setSsl(true) + .setTrustAll(true) + ); } public static void respondJson(RoutingContext ctx, Object response) { diff --git a/src/main/java/net/frozenorb/apiv3/route/announcements/GETAnnouncements.java b/src/main/java/net/frozenorb/apiv3/route/announcements/GETAnnouncements.java index 85b6dc7..3d104ce 100644 --- a/src/main/java/net/frozenorb/apiv3/route/announcements/GETAnnouncements.java +++ b/src/main/java/net/frozenorb/apiv3/route/announcements/GETAnnouncements.java @@ -13,10 +13,9 @@ public final class GETAnnouncements implements Handler { if (serverGroup == null) { ErrorUtils.respondNotFound(ctx, "Server group", ctx.request().getParam("id")); - return; + } else { + APIv3.respondJson(ctx, serverGroup.getAnnouncements()); } - - APIv3.respondJson(ctx, serverGroup.getAnnouncements()); } } \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/route/announcements/PUTAnnouncements.java b/src/main/java/net/frozenorb/apiv3/route/announcements/PUTAnnouncements.java index a6cd155..3fc9f67 100644 --- a/src/main/java/net/frozenorb/apiv3/route/announcements/PUTAnnouncements.java +++ b/src/main/java/net/frozenorb/apiv3/route/announcements/PUTAnnouncements.java @@ -16,18 +16,22 @@ public final class PUTAnnouncements implements Handler { if (serverGroup == null) { ErrorUtils.respondNotFound(ctx, "Server group", ctx.request().getParam("id")); - return; + } else { + Set announcements = new HashSet<>(); + + for (Object announcement : ctx.getBodyAsJsonArray()) { + announcements.add((String) announcement); + } + + serverGroup.setAnnouncements(announcements); + serverGroup.save((ignored, error) -> { + if (error != null) { + ErrorUtils.respondInternalError(ctx, error); + } else { + APIv3.respondJson(ctx, serverGroup.getAnnouncements()); + } + }); } - - Set announcements = new HashSet<>(); - - for (Object announcement : ctx.getBodyAsJsonArray()) { - announcements.add((String) announcement); - } - - serverGroup.setAnnouncements(announcements); - serverGroup.save(); - APIv3.respondJson(ctx, serverGroup.getAnnouncements()); } } \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/route/auditLog/GETAuditLog.java b/src/main/java/net/frozenorb/apiv3/route/auditLog/GETAuditLog.java index d07cce2..6a93fec 100644 --- a/src/main/java/net/frozenorb/apiv3/route/auditLog/GETAuditLog.java +++ b/src/main/java/net/frozenorb/apiv3/route/auditLog/GETAuditLog.java @@ -13,7 +13,13 @@ public final class GETAuditLog implements Handler { int skip = ctx.request().getParam("skip") == null ? 0 : Integer.parseInt(ctx.request().getParam("skip")); int pageSize = ctx.request().getParam("pageSize") == null ? 100 : Integer.parseInt(ctx.request().getParam("pageSize")); - APIv3.respondJson(ctx, AuditLogEntry.findAllPaginatedSync(skip, pageSize)); + AuditLogEntry.findAllPaginated(skip, pageSize, (auditLog, error) -> { + if (error != null) { + ErrorUtils.respondInternalError(ctx, error); + } else { + APIv3.respondJson(ctx, auditLog); + } + }); } catch (NumberFormatException ex) { ErrorUtils.respondInvalidInput(ctx, "skip and pageSize must be numerical inputs."); } diff --git a/src/main/java/net/frozenorb/apiv3/route/auditLog/POSTUserAuditLogEntry.java b/src/main/java/net/frozenorb/apiv3/route/auditLog/POSTUserAuditLogEntry.java index d449420..883d07a 100644 --- a/src/main/java/net/frozenorb/apiv3/route/auditLog/POSTUserAuditLogEntry.java +++ b/src/main/java/net/frozenorb/apiv3/route/auditLog/POSTUserAuditLogEntry.java @@ -15,33 +15,37 @@ import org.bson.Document; public final class POSTUserAuditLogEntry implements Handler { public void handle(RoutingContext ctx) { - User user = User.findByIdSync(ctx.request().getParam("id")); + User.findById(ctx.request().getParam("id"), (user, error) -> { + if (error != null) { + ErrorUtils.respondInternalError(ctx, error); + } else if (user == null) { + ErrorUtils.respondNotFound(ctx, "User", ctx.request().getParam("id")); + } else { + String userIp = ctx.request().getParam("userIp"); - if (user == null) { - ErrorUtils.respondNotFound(ctx, "User", ctx.request().getParam("id")); - return; - } + if (!IpUtils.isValidIp(userIp)) { + ErrorUtils.respondInvalidInput(ctx, "Ip address \"" + userIp + "\" is not valid."); + return; + } - String userIp = ctx.request().getParam("userIp"); + AuditLogActionType type; - if (!IpUtils.isValidIp(userIp)) { - ErrorUtils.respondInvalidInput(ctx, "Ip address \"" + userIp + "\" is not valid."); - return; - } + try { + type = AuditLogActionType.valueOf(ctx.request().getParam("type")); + } catch (IllegalArgumentException ex) { + ErrorUtils.respondNotFound(ctx, "Audit log action type", ctx.request().getParam("type")); + return; + } - AuditLogActionType type; - - try { - type = AuditLogActionType.valueOf(ctx.request().getParam("type")); - } catch (IllegalArgumentException ex) { - ErrorUtils.respondNotFound(ctx, "Audit log action type", ctx.request().getParam("type")); - return; - } - - BlockingCallback blockingCallback = new BlockingCallback<>(); - AuditLog.log(user, userIp, ctx.get("actor"), type, Document.parse(ctx.getBodyAsString()), blockingCallback); - AuditLogEntry entry = blockingCallback.get(); - APIv3.respondJson(ctx, entry); + AuditLog.log(user, userIp, ctx.get("actor"), type, Document.parse(ctx.getBodyAsString()), (auditLogEntry, error2) -> { + if (error2 != null) { + ErrorUtils.respondInternalError(ctx, error2); + } else { + APIv3.respondJson(ctx, auditLogEntry); + } + }); + } + }); } } \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/route/grants/GETGrant.java b/src/main/java/net/frozenorb/apiv3/route/grants/GETGrant.java index 91588b2..9754ceb 100644 --- a/src/main/java/net/frozenorb/apiv3/route/grants/GETGrant.java +++ b/src/main/java/net/frozenorb/apiv3/route/grants/GETGrant.java @@ -4,11 +4,18 @@ import io.vertx.core.Handler; import io.vertx.ext.web.RoutingContext; import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.model.Grant; +import net.frozenorb.apiv3.util.ErrorUtils; public final class GETGrant implements Handler { public void handle(RoutingContext ctx) { - APIv3.respondJson(ctx, Grant.findByIdSync(ctx.request().getParam("id"))); + Grant.findById(ctx.request().getParam("id"), (grant, error) -> { + if (error != null) { + ErrorUtils.respondInternalError(ctx, error); + } else { + APIv3.respondJson(ctx, grant); + } + }); } } \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/route/grants/GETGrants.java b/src/main/java/net/frozenorb/apiv3/route/grants/GETGrants.java index f7a2dc5..0af423b 100644 --- a/src/main/java/net/frozenorb/apiv3/route/grants/GETGrants.java +++ b/src/main/java/net/frozenorb/apiv3/route/grants/GETGrants.java @@ -13,7 +13,13 @@ public final class GETGrants implements Handler { int skip = ctx.request().getParam("skip") == null ? 0 : Integer.parseInt(ctx.request().getParam("skip")); int pageSize = ctx.request().getParam("pageSize") == null ? 100 : Integer.parseInt(ctx.request().getParam("pageSize")); - APIv3.respondJson(ctx, Grant.findAllPaginatedSync(skip, pageSize)); + Grant.findAllPaginated(skip, pageSize, (grants, error) -> { + if (error != null) { + ErrorUtils.respondInternalError(ctx, error); + } else { + APIv3.respondJson(ctx, grants); + } + }); } catch (NumberFormatException ex) { ErrorUtils.respondInvalidInput(ctx, "skip and pageSize must be numerical inputs."); } diff --git a/src/main/java/net/frozenorb/apiv3/route/grants/GETUserGrants.java b/src/main/java/net/frozenorb/apiv3/route/grants/GETUserGrants.java index c78954b..110a671 100644 --- a/src/main/java/net/frozenorb/apiv3/route/grants/GETUserGrants.java +++ b/src/main/java/net/frozenorb/apiv3/route/grants/GETUserGrants.java @@ -10,14 +10,21 @@ import net.frozenorb.apiv3.util.ErrorUtils; public final class GETUserGrants implements Handler { public void handle(RoutingContext ctx) { - User target = User.findByIdSync(ctx.request().getParam("id")); - - if (target == null) { - ErrorUtils.respondNotFound(ctx, "User", ctx.request().getParam("id")); - return; - } - - APIv3.respondJson(ctx, Grant.findByUserSync(target)); + User.findById(ctx.request().getParam("id"), (user, error) -> { + if (error != null) { + ErrorUtils.respondInternalError(ctx, error); + } else if (user == null) { + ErrorUtils.respondNotFound(ctx, "User", ctx.request().getParam("id")); + } else { + Grant.findByUser(user, (grants, error2) -> { + if (error2 != null) { + ErrorUtils.respondInternalError(ctx, error2); + } else { + APIv3.respondJson(ctx, grants); + } + }); + } + }); } } \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/route/ipBans/GETIpBan.java b/src/main/java/net/frozenorb/apiv3/route/ipBans/GETIpBan.java index bce6429..43e9975 100644 --- a/src/main/java/net/frozenorb/apiv3/route/ipBans/GETIpBan.java +++ b/src/main/java/net/frozenorb/apiv3/route/ipBans/GETIpBan.java @@ -3,12 +3,20 @@ package net.frozenorb.apiv3.route.ipBans; import io.vertx.core.Handler; import io.vertx.ext.web.RoutingContext; import net.frozenorb.apiv3.APIv3; +import net.frozenorb.apiv3.model.Grant; import net.frozenorb.apiv3.model.IpBan; +import net.frozenorb.apiv3.util.ErrorUtils; public final class GETIpBan implements Handler { public void handle(RoutingContext ctx) { - APIv3.respondJson(ctx, IpBan.findByIdSync(ctx.request().getParam("id"))); + IpBan.findById(ctx.request().getParam("id"), (ipBan, error) -> { + if (error != null) { + ErrorUtils.respondInternalError(ctx, error); + } else { + APIv3.respondJson(ctx, ipBan); + } + }); } } \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/route/ipBans/GETIpBans.java b/src/main/java/net/frozenorb/apiv3/route/ipBans/GETIpBans.java index 42e1ef8..e0f8a95 100644 --- a/src/main/java/net/frozenorb/apiv3/route/ipBans/GETIpBans.java +++ b/src/main/java/net/frozenorb/apiv3/route/ipBans/GETIpBans.java @@ -3,6 +3,7 @@ package net.frozenorb.apiv3.route.ipBans; import io.vertx.core.Handler; import io.vertx.ext.web.RoutingContext; import net.frozenorb.apiv3.APIv3; +import net.frozenorb.apiv3.model.Grant; import net.frozenorb.apiv3.model.IpBan; import net.frozenorb.apiv3.util.ErrorUtils; @@ -13,7 +14,13 @@ public final class GETIpBans implements Handler { int skip = ctx.request().getParam("skip") == null ? 0 : Integer.parseInt(ctx.request().getParam("skip")); int pageSize = ctx.request().getParam("pageSize") == null ? 100 : Integer.parseInt(ctx.request().getParam("pageSize")); - APIv3.respondJson(ctx, IpBan.findAllPaginatedSync(skip, pageSize)); + IpBan.findAllPaginated(skip, pageSize, (grants, error) -> { + if (error != null) { + ErrorUtils.respondInternalError(ctx, error); + } else { + APIv3.respondJson(ctx, grants); + } + }); } catch (NumberFormatException ex) { ErrorUtils.respondInvalidInput(ctx, "skip and pageSize must be numerical inputs."); } diff --git a/src/main/java/net/frozenorb/apiv3/route/ipBans/GETIpIpBans.java b/src/main/java/net/frozenorb/apiv3/route/ipBans/GETIpIpBans.java index d22664c..7e93646 100644 --- a/src/main/java/net/frozenorb/apiv3/route/ipBans/GETIpIpBans.java +++ b/src/main/java/net/frozenorb/apiv3/route/ipBans/GETIpIpBans.java @@ -4,6 +4,8 @@ import io.vertx.core.Handler; import io.vertx.ext.web.RoutingContext; import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.model.IpBan; +import net.frozenorb.apiv3.model.Punishment; +import net.frozenorb.apiv3.model.User; import net.frozenorb.apiv3.util.ErrorUtils; import net.frozenorb.apiv3.util.IpUtils; @@ -17,7 +19,13 @@ public final class GETIpIpBans implements Handler { return; } - APIv3.respondJson(ctx, IpBan.findByIpSync(userIp)); + IpBan.findByIp(userIp, (ipBans, error) -> { + if (error != null) { + ErrorUtils.respondInternalError(ctx, error); + } else { + APIv3.respondJson(ctx, ipBans); + } + }); } } \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/route/ipLog/GETUserIpLog.java b/src/main/java/net/frozenorb/apiv3/route/ipLog/GETUserIpLog.java index 6655259..83f40ea 100644 --- a/src/main/java/net/frozenorb/apiv3/route/ipLog/GETUserIpLog.java +++ b/src/main/java/net/frozenorb/apiv3/route/ipLog/GETUserIpLog.java @@ -3,21 +3,30 @@ package net.frozenorb.apiv3.route.ipLog; import io.vertx.core.Handler; import io.vertx.ext.web.RoutingContext; import net.frozenorb.apiv3.APIv3; +import net.frozenorb.apiv3.model.IpBan; import net.frozenorb.apiv3.model.IpLogEntry; +import net.frozenorb.apiv3.model.Punishment; import net.frozenorb.apiv3.model.User; import net.frozenorb.apiv3.util.ErrorUtils; public final class GETUserIpLog implements Handler { public void handle(RoutingContext ctx) { - User target = User.findByIdSync(ctx.request().getParam("id")); - - if (target == null) { - ErrorUtils.respondNotFound(ctx, "User", ctx.request().getParam("id")); - return; - } - - APIv3.respondJson(ctx, IpLogEntry.findByUserSync(target)); + User.findById(ctx.request().getParam("id"), (user, error) -> { + if (error != null) { + ErrorUtils.respondInternalError(ctx, error); + } else if (user == null) { + ErrorUtils.respondNotFound(ctx, "User", ctx.request().getParam("id")); + } else { + IpLogEntry.findByUser(user, (ipLog, error2) -> { + if (error2 != null) { + ErrorUtils.respondInternalError(ctx, error2); + } else { + APIv3.respondJson(ctx, ipLog); + } + }); + } + }); } } \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/route/notificationTemplates/GETNotificationTemplate.java b/src/main/java/net/frozenorb/apiv3/route/notificationTemplates/GETNotificationTemplate.java index 1dc2d64..a67020d 100644 --- a/src/main/java/net/frozenorb/apiv3/route/notificationTemplates/GETNotificationTemplate.java +++ b/src/main/java/net/frozenorb/apiv3/route/notificationTemplates/GETNotificationTemplate.java @@ -3,12 +3,20 @@ package net.frozenorb.apiv3.route.notificationTemplates; import io.vertx.core.Handler; import io.vertx.ext.web.RoutingContext; import net.frozenorb.apiv3.APIv3; +import net.frozenorb.apiv3.model.IpBan; import net.frozenorb.apiv3.model.NotificationTemplate; +import net.frozenorb.apiv3.util.ErrorUtils; public final class GETNotificationTemplate implements Handler { public void handle(RoutingContext ctx) { - APIv3.respondJson(ctx, NotificationTemplate.findByIdSync(ctx.request().getParam("id"))); + NotificationTemplate.findById(ctx.request().getParam("id"), (notificationTemplate, error) -> { + if (error != null) { + ErrorUtils.respondInternalError(ctx, error); + } else { + APIv3.respondJson(ctx, notificationTemplate); + } + }); } } \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/route/notificationTemplates/GETNotificationTemplates.java b/src/main/java/net/frozenorb/apiv3/route/notificationTemplates/GETNotificationTemplates.java index 4bc315c..68d365a 100644 --- a/src/main/java/net/frozenorb/apiv3/route/notificationTemplates/GETNotificationTemplates.java +++ b/src/main/java/net/frozenorb/apiv3/route/notificationTemplates/GETNotificationTemplates.java @@ -3,12 +3,20 @@ package net.frozenorb.apiv3.route.notificationTemplates; import io.vertx.core.Handler; import io.vertx.ext.web.RoutingContext; import net.frozenorb.apiv3.APIv3; +import net.frozenorb.apiv3.model.IpBan; import net.frozenorb.apiv3.model.NotificationTemplate; +import net.frozenorb.apiv3.util.ErrorUtils; public final class GETNotificationTemplates implements Handler { public void handle(RoutingContext ctx) { - APIv3.respondJson(ctx, NotificationTemplate.findAllSync()); + NotificationTemplate.findAll((notificationTemplates, error) -> { + if (error != null) { + ErrorUtils.respondInternalError(ctx, error); + } else { + APIv3.respondJson(ctx, notificationTemplates); + } + }); } } \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/route/punishments/GETPunishment.java b/src/main/java/net/frozenorb/apiv3/route/punishments/GETPunishment.java index 876c091..8f899c3 100644 --- a/src/main/java/net/frozenorb/apiv3/route/punishments/GETPunishment.java +++ b/src/main/java/net/frozenorb/apiv3/route/punishments/GETPunishment.java @@ -3,12 +3,20 @@ package net.frozenorb.apiv3.route.punishments; import io.vertx.core.Handler; import io.vertx.ext.web.RoutingContext; import net.frozenorb.apiv3.APIv3; +import net.frozenorb.apiv3.model.IpBan; import net.frozenorb.apiv3.model.Punishment; +import net.frozenorb.apiv3.util.ErrorUtils; public final class GETPunishment implements Handler { public void handle(RoutingContext ctx) { - APIv3.respondJson(ctx, Punishment.findByIdSync(ctx.request().getParam("id"))); + Punishment.findById(ctx.request().getParam("id"), (punishment, error) -> { + if (error != null) { + ErrorUtils.respondInternalError(ctx, error); + } else { + APIv3.respondJson(ctx, punishment); + } + }); } } \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/route/punishments/GETPunishments.java b/src/main/java/net/frozenorb/apiv3/route/punishments/GETPunishments.java index ff07bc8..cfe3c83 100644 --- a/src/main/java/net/frozenorb/apiv3/route/punishments/GETPunishments.java +++ b/src/main/java/net/frozenorb/apiv3/route/punishments/GETPunishments.java @@ -3,6 +3,7 @@ package net.frozenorb.apiv3.route.punishments; import io.vertx.core.Handler; import io.vertx.ext.web.RoutingContext; import net.frozenorb.apiv3.APIv3; +import net.frozenorb.apiv3.model.Grant; import net.frozenorb.apiv3.model.Punishment; import net.frozenorb.apiv3.util.ErrorUtils; @@ -13,7 +14,13 @@ public final class GETPunishments implements Handler { int skip = ctx.request().getParam("skip") == null ? 0 : Integer.parseInt(ctx.request().getParam("skip")); int pageSize = ctx.request().getParam("pageSize") == null ? 100 : Integer.parseInt(ctx.request().getParam("pageSize")); - APIv3.respondJson(ctx, Punishment.findAllPaginatedSync(skip, pageSize)); + Punishment.findAllPaginated(skip, pageSize, (grants, error) -> { + if (error != null) { + ErrorUtils.respondInternalError(ctx, error); + } else { + APIv3.respondJson(ctx, grants); + } + }); } catch (NumberFormatException ex) { ErrorUtils.respondInvalidInput(ctx, "skip and pageSize must be numerical inputs."); } diff --git a/src/main/java/net/frozenorb/apiv3/route/punishments/GETUserPunishments.java b/src/main/java/net/frozenorb/apiv3/route/punishments/GETUserPunishments.java index a094b38..9402c15 100644 --- a/src/main/java/net/frozenorb/apiv3/route/punishments/GETUserPunishments.java +++ b/src/main/java/net/frozenorb/apiv3/route/punishments/GETUserPunishments.java @@ -3,6 +3,7 @@ package net.frozenorb.apiv3.route.punishments; import io.vertx.core.Handler; import io.vertx.ext.web.RoutingContext; import net.frozenorb.apiv3.APIv3; +import net.frozenorb.apiv3.model.Grant; import net.frozenorb.apiv3.model.Punishment; import net.frozenorb.apiv3.model.User; import net.frozenorb.apiv3.util.ErrorUtils; @@ -10,14 +11,21 @@ import net.frozenorb.apiv3.util.ErrorUtils; public final class GETUserPunishments implements Handler { public void handle(RoutingContext ctx) { - User target = User.findByIdSync(ctx.request().getParam("id")); - - if (target == null) { - ErrorUtils.respondNotFound(ctx, "User", ctx.request().getParam("id")); - return; - } - - APIv3.respondJson(ctx, Punishment.findByUserSync(target)); + User.findById(ctx.request().getParam("id"), (user, error) -> { + if (error != null) { + ErrorUtils.respondInternalError(ctx, error); + } else if (user == null) { + ErrorUtils.respondNotFound(ctx, "User", ctx.request().getParam("id")); + } else { + Punishment.findByUser(user, (punishments, error2) -> { + if (error2 != null) { + ErrorUtils.respondInternalError(ctx, error2); + } else { + APIv3.respondJson(ctx, punishments); + } + }); + } + }); } } \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/route/punishments/POSTUserPunish.java b/src/main/java/net/frozenorb/apiv3/route/punishments/POSTUserPunish.java index 0fd7298..8c46133 100644 --- a/src/main/java/net/frozenorb/apiv3/route/punishments/POSTUserPunish.java +++ b/src/main/java/net/frozenorb/apiv3/route/punishments/POSTUserPunish.java @@ -73,7 +73,7 @@ public final class POSTUserPunish implements Handler { Punishment punishment = new Punishment(target, reason, type, expiresAt, addedBy, ctx.get("actor"), meta); String accessDenialReason = punishment.getAccessDenialReason(); - String userIp = ctx.request().getParam("userIp"); + String userIp = ctx.request().getParam("playerIp"); // TODO: YELL AT GRIFFIN FOR THIS, IT SHOULD BE USERIP if (userIp != null) { IpBan ipBan = new IpBan(userIp, punishment); diff --git a/src/main/java/net/frozenorb/apiv3/route/servers/POSTServerHeartbeat.java b/src/main/java/net/frozenorb/apiv3/route/servers/POSTServerHeartbeat.java index b0c3a36..6bc92e5 100644 --- a/src/main/java/net/frozenorb/apiv3/route/servers/POSTServerHeartbeat.java +++ b/src/main/java/net/frozenorb/apiv3/route/servers/POSTServerHeartbeat.java @@ -13,7 +13,7 @@ import net.frozenorb.apiv3.actor.ActorType; import net.frozenorb.apiv3.model.*; import net.frozenorb.apiv3.util.ErrorUtils; import net.frozenorb.apiv3.util.PermissionUtils; -import net.frozenorb.apiv3.util.UUIDUtils; +import net.frozenorb.apiv3.util.UuidUtils; import org.bson.Document; import java.time.Instant; @@ -190,7 +190,7 @@ public final class POSTServerHeartbeat implements Handler { UUID uuid = UUID.fromString(playerJson.getString("uuid")); String username = playerJson.getString("username"); - if (UUIDUtils.isAcceptableUUID(uuid)) { + if (UuidUtils.isAcceptableUuid(uuid)) { result.put(uuid, username); } } diff --git a/src/main/java/net/frozenorb/apiv3/route/users/GETUser.java b/src/main/java/net/frozenorb/apiv3/route/users/GETUser.java index 1323359..82e9c56 100644 --- a/src/main/java/net/frozenorb/apiv3/route/users/GETUser.java +++ b/src/main/java/net/frozenorb/apiv3/route/users/GETUser.java @@ -3,12 +3,20 @@ package net.frozenorb.apiv3.route.users; import io.vertx.core.Handler; import io.vertx.ext.web.RoutingContext; import net.frozenorb.apiv3.APIv3; +import net.frozenorb.apiv3.model.IpBan; import net.frozenorb.apiv3.model.User; +import net.frozenorb.apiv3.util.ErrorUtils; public final class GETUser implements Handler { public void handle(RoutingContext ctx) { - APIv3.respondJson(ctx, User.findByIdSync(ctx.request().getParam("id"))); + User.findById(ctx.request().getParam("id"), (user, error) -> { + if (error != null) { + ErrorUtils.respondInternalError(ctx, error); + } else { + APIv3.respondJson(ctx, user); + } + }); } } \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/route/users/GETUserRequiresTOTP.java b/src/main/java/net/frozenorb/apiv3/route/users/GETUserRequiresTOTP.java index abc04f4..201830a 100644 --- a/src/main/java/net/frozenorb/apiv3/route/users/GETUserRequiresTOTP.java +++ b/src/main/java/net/frozenorb/apiv3/route/users/GETUserRequiresTOTP.java @@ -8,7 +8,7 @@ import net.frozenorb.apiv3.model.User; import net.frozenorb.apiv3.unsorted.BlockingCallback; import net.frozenorb.apiv3.util.ErrorUtils; import net.frozenorb.apiv3.util.IpUtils; -import net.frozenorb.apiv3.util.TOTPUtils; +import net.frozenorb.apiv3.util.TotpUtils; public final class GETUserRequiresTOTP implements Handler { @@ -36,7 +36,7 @@ public final class GETUserRequiresTOTP implements Handler { } BlockingCallback preAuthorizedCallback = new BlockingCallback<>(); - TOTPUtils.isPreAuthorized(user, userIp, preAuthorizedCallback); + TotpUtils.isPreAuthorized(user, userIp, preAuthorizedCallback); if (preAuthorizedCallback.get()) { APIv3.respondJson(ctx, ImmutableMap.of( diff --git a/src/main/java/net/frozenorb/apiv3/route/users/POSTUserLogin.java b/src/main/java/net/frozenorb/apiv3/route/users/POSTUserLogin.java index 359aac9..a09062a 100644 --- a/src/main/java/net/frozenorb/apiv3/route/users/POSTUserLogin.java +++ b/src/main/java/net/frozenorb/apiv3/route/users/POSTUserLogin.java @@ -11,7 +11,7 @@ import net.frozenorb.apiv3.model.Server; import net.frozenorb.apiv3.model.User; import net.frozenorb.apiv3.util.ErrorUtils; import net.frozenorb.apiv3.util.IpUtils; -import net.frozenorb.apiv3.util.UUIDUtils; +import net.frozenorb.apiv3.util.UuidUtils; import java.util.UUID; @@ -20,11 +20,14 @@ public final class POSTUserLogin implements Handler { public void handle(RoutingContext ctx) { UUID uuid = UUID.fromString(ctx.request().getParam("id")); - if (!UUIDUtils.isAcceptableUUID(uuid)) { + if (!UuidUtils.isAcceptableUuid(uuid)) { ErrorUtils.respondInvalidInput(ctx, "UUID \"" + uuid + "\" is not valid - must be version 4 UUID."); return; } + User user = User.findByIdSync(uuid); + String username = ctx.request().getParam("username"); + String userIp = ctx.request().getParam("userIp"); Actor actor = ctx.get("actor"); Server server; @@ -39,52 +42,38 @@ public final class POSTUserLogin implements Handler { } } - String username = ctx.request().getParam("username"); - String userIp = ctx.request().getParam("userIp"); - if (!IpUtils.isValidIp(userIp)) { - ErrorUtils.respondInvalidInput(ctx, "Ip address \"" + userIp + "\" is not valid."); + ErrorUtils.respondInvalidInput(ctx, "IP address \"" + userIp + "\" is not valid."); return; } - User.findById(uuid, (user, error) -> { + if (user == null) { + // Will be saved in the User constructor + user = new User(uuid, username); + } + + Server actorServer = Server.findById(actor.getName()); + + IpLogEntry ipLogEntry = IpLogEntry.findByUserAndIpSync(user, userIp); + + // We use a little bit more verbose code here to save on the + // overhead of a .insert() immediately followed by a .save() + if (ipLogEntry == null) { + ipLogEntry = new IpLogEntry(user, userIp); + ipLogEntry.used(); + ipLogEntry.insert(); + } else { + ipLogEntry.used(); + ipLogEntry.save(); + } + + user.updateUsername(username); + user.getLoginInfo(actorServer, userIp, (loginInfo, error) -> { if (error != null) { ErrorUtils.respondInternalError(ctx, error); - return; - } else if (user == null) { - user = new User(uuid, username); - user.insert(); // TODO + } else { + APIv3.respondJson(ctx, loginInfo); } - - User finalUser = user; - - IpLogEntry.findByUserAndIp(user, userIp, (ipLogEntry, error2) -> { - if (error2 != null) { - ErrorUtils.respondInternalError(ctx, error2); - return; - } else if (ipLogEntry == null) { - ipLogEntry = new IpLogEntry(finalUser, userIp); - ipLogEntry.used(); - ipLogEntry.insert(); //TODO - } else { - ipLogEntry.used(); - ipLogEntry.save(); // TODO - } - - finalUser.updateUsername(username, (ignored, error3) -> { - if (error3 != null) { - ErrorUtils.respondInternalError(ctx, error3); - } else { - finalUser.getLoginInfo(server, userIp, (loginInfo, error4) -> { - if (error4 != null) { - ErrorUtils.respondInternalError(ctx, error4); - } else { - APIv3.respondJson(ctx, loginInfo); - } - }); - } - }); - }); }); } diff --git a/src/main/java/net/frozenorb/apiv3/route/users/POSTUserSetupTOTP.java b/src/main/java/net/frozenorb/apiv3/route/users/POSTUserSetupTOTP.java index 8135b7d..1d0ae19 100644 --- a/src/main/java/net/frozenorb/apiv3/route/users/POSTUserSetupTOTP.java +++ b/src/main/java/net/frozenorb/apiv3/route/users/POSTUserSetupTOTP.java @@ -7,7 +7,7 @@ import io.vertx.ext.web.RoutingContext; import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.model.User; import net.frozenorb.apiv3.util.ErrorUtils; -import net.frozenorb.apiv3.util.TOTPUtils; +import net.frozenorb.apiv3.util.TotpUtils; public final class POSTUserSetupTOTP implements Handler { @@ -24,13 +24,13 @@ public final class POSTUserSetupTOTP implements Handler { return; } - GoogleAuthenticatorKey generated = TOTPUtils.generateTOTPSecret(); + GoogleAuthenticatorKey generated = TotpUtils.generateTotpSecret(); user.setTotpSecret(generated.getKey()); user.save(); APIv3.respondJson(ctx, ImmutableMap.of( - "qrCode", TOTPUtils.getQRCodeURL(user, generated) + "qrCode", TotpUtils.getQrCodeUrl(user, generated) )); } diff --git a/src/main/java/net/frozenorb/apiv3/route/users/POSTUserVerifyTOTP.java b/src/main/java/net/frozenorb/apiv3/route/users/POSTUserVerifyTOTP.java index 72e0faf..1f3ab5e 100644 --- a/src/main/java/net/frozenorb/apiv3/route/users/POSTUserVerifyTOTP.java +++ b/src/main/java/net/frozenorb/apiv3/route/users/POSTUserVerifyTOTP.java @@ -8,7 +8,7 @@ import net.frozenorb.apiv3.model.User; import net.frozenorb.apiv3.unsorted.BlockingCallback; import net.frozenorb.apiv3.util.ErrorUtils; import net.frozenorb.apiv3.util.IpUtils; -import net.frozenorb.apiv3.util.TOTPUtils; +import net.frozenorb.apiv3.util.TotpUtils; import java.util.concurrent.TimeUnit; @@ -36,7 +36,7 @@ public final class POSTUserVerifyTOTP implements Handler { int providedCode = Integer.parseInt(ctx.request().getParam("code")); BlockingCallback recentlyUsedCallback = new BlockingCallback<>(); - TOTPUtils.wasRecentlyUsed(user, providedCode, recentlyUsedCallback); + TotpUtils.wasRecentlyUsed(user, providedCode, recentlyUsedCallback); if (recentlyUsedCallback.get()) { APIv3.respondJson(ctx, ImmutableMap.of( @@ -46,15 +46,15 @@ public final class POSTUserVerifyTOTP implements Handler { return; } - boolean authorized = TOTPUtils.authorizeUser(user, providedCode); + boolean authorized = TotpUtils.authorizeUser(user, providedCode); if (authorized) { BlockingCallback markPreAuthorizedCallback = new BlockingCallback<>(); - TOTPUtils.markPreAuthorized(user, userIp, 3, TimeUnit.DAYS, markPreAuthorizedCallback); + TotpUtils.markPreAuthorized(user, userIp, 3, TimeUnit.DAYS, markPreAuthorizedCallback); markPreAuthorizedCallback.get(); BlockingCallback markRecentlyUsedCallback = new BlockingCallback<>(); - TOTPUtils.markRecentlyUsed(user, providedCode, markRecentlyUsedCallback); + TotpUtils.markRecentlyUsed(user, providedCode, markRecentlyUsedCallback); markRecentlyUsedCallback.get(); APIv3.respondJson(ctx, ImmutableMap.of( From 3c25095340d2082212053911d13fec2df7b2168d Mon Sep 17 00:00:00 2001 From: Colin McDonald Date: Fri, 17 Jun 2016 01:10:38 -0400 Subject: [PATCH 05/24] Add / before endpoint to ensure the call always works --- src/main/java/net/frozenorb/apiv3/util/MojangUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/net/frozenorb/apiv3/util/MojangUtils.java b/src/main/java/net/frozenorb/apiv3/util/MojangUtils.java index b37e4d1..9b554f8 100644 --- a/src/main/java/net/frozenorb/apiv3/util/MojangUtils.java +++ b/src/main/java/net/frozenorb/apiv3/util/MojangUtils.java @@ -12,7 +12,7 @@ import java.util.UUID; public class MojangUtils { public static void getName(UUID id, SingleResultCallback callback) { - APIv3.getHttpClient().get("sessionserver.mojang.com", "session/minecraft/profile/" + id.toString().replace("-", ""), (response) -> { + APIv3.getHttpClient().get("sessionserver.mojang.com", "/session/minecraft/profile/" + id.toString().replace("-", ""), (response) -> { response.bodyHandler((body) -> { Document resJson = Document.parse(body.toString()); String name = resJson.getString("name"); From adf8d49334a9626bd5a7ba79e75acde96a4fdc03 Mon Sep 17 00:00:00 2001 From: Colin McDonald Date: Fri, 17 Jun 2016 01:11:01 -0400 Subject: [PATCH 06/24] Properly name InstantTypeAdapter --- .../gson/{InstantTimeAdapter.java => InstantTypeAdapter.java} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename src/main/java/net/frozenorb/apiv3/serialization/gson/{InstantTimeAdapter.java => InstantTypeAdapter.java} (91%) diff --git a/src/main/java/net/frozenorb/apiv3/serialization/gson/InstantTimeAdapter.java b/src/main/java/net/frozenorb/apiv3/serialization/gson/InstantTypeAdapter.java similarity index 91% rename from src/main/java/net/frozenorb/apiv3/serialization/gson/InstantTimeAdapter.java rename to src/main/java/net/frozenorb/apiv3/serialization/gson/InstantTypeAdapter.java index e6263f2..d53c6ea 100644 --- a/src/main/java/net/frozenorb/apiv3/serialization/gson/InstantTimeAdapter.java +++ b/src/main/java/net/frozenorb/apiv3/serialization/gson/InstantTypeAdapter.java @@ -7,7 +7,7 @@ import com.google.gson.stream.JsonWriter; import java.io.IOException; import java.time.Instant; -public final class InstantTimeAdapter extends TypeAdapter { +public final class InstantTypeAdapter extends TypeAdapter { public void write(JsonWriter writer, Instant write) throws IOException { if (write == null) { From 15a8ba70db047dfc7a87629cc8972b6ee605d38e Mon Sep 17 00:00:00 2001 From: Colin McDonald Date: Fri, 17 Jun 2016 01:11:28 -0400 Subject: [PATCH 07/24] Add Jackson Instant serializers --- .../jackson/InstantJsonDeserializer.java | 18 ++++++++++++++++++ .../jackson/InstantJsonSerializer.java | 18 ++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 src/main/java/net/frozenorb/apiv3/serialization/jackson/InstantJsonDeserializer.java create mode 100644 src/main/java/net/frozenorb/apiv3/serialization/jackson/InstantJsonSerializer.java diff --git a/src/main/java/net/frozenorb/apiv3/serialization/jackson/InstantJsonDeserializer.java b/src/main/java/net/frozenorb/apiv3/serialization/jackson/InstantJsonDeserializer.java new file mode 100644 index 0000000..ac464b3 --- /dev/null +++ b/src/main/java/net/frozenorb/apiv3/serialization/jackson/InstantJsonDeserializer.java @@ -0,0 +1,18 @@ +package net.frozenorb.apiv3.serialization.jackson; + +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonDeserializer; + +import java.io.IOException; +import java.time.Instant; + +public final class InstantJsonDeserializer extends JsonDeserializer { + + @Override + public Instant deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { + return Instant.ofEpochMilli(jsonParser.getValueAsLong()); + } + +} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/serialization/jackson/InstantJsonSerializer.java b/src/main/java/net/frozenorb/apiv3/serialization/jackson/InstantJsonSerializer.java new file mode 100644 index 0000000..2af3601 --- /dev/null +++ b/src/main/java/net/frozenorb/apiv3/serialization/jackson/InstantJsonSerializer.java @@ -0,0 +1,18 @@ +package net.frozenorb.apiv3.serialization.jackson; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonSerializer; +import com.fasterxml.jackson.databind.SerializerProvider; + +import java.io.IOException; +import java.time.Instant; + +public final class InstantJsonSerializer extends JsonSerializer { + + @Override + public void serialize(Instant instant, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException { + jsonGenerator.writeNumber(instant.toEpochMilli()); + } + +} \ No newline at end of file From c830372f2927a1021e9826bc580fad6b482dd371 Mon Sep 17 00:00:00 2001 From: Colin McDonald Date: Fri, 17 Jun 2016 01:11:45 -0400 Subject: [PATCH 08/24] Add GET /user/:id/punishments --- .../apiv3/route/users/GETUserPermissions.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/main/java/net/frozenorb/apiv3/route/users/GETUserPermissions.java diff --git a/src/main/java/net/frozenorb/apiv3/route/users/GETUserPermissions.java b/src/main/java/net/frozenorb/apiv3/route/users/GETUserPermissions.java new file mode 100644 index 0000000..8aa2425 --- /dev/null +++ b/src/main/java/net/frozenorb/apiv3/route/users/GETUserPermissions.java @@ -0,0 +1,23 @@ +package net.frozenorb.apiv3.route.users; + +import io.vertx.core.Handler; +import io.vertx.ext.web.RoutingContext; +import net.frozenorb.apiv3.APIv3; +import net.frozenorb.apiv3.model.Punishment; +import net.frozenorb.apiv3.model.User; +import net.frozenorb.apiv3.util.ErrorUtils; + +public final class GETUserPermissions implements Handler { + + public void handle(RoutingContext ctx) { + User target = User.findByIdSync(ctx.request().getParam("id")); + + if (target == null) { + ErrorUtils.respondNotFound(ctx, "User", ctx.request().getParam("id")); + return; + } + + APIv3.respondJson(ctx, target.getGlobalPermissions()); + } + +} \ No newline at end of file From 07e0a3328ab2eefe92995f81bc4e74071896977a Mon Sep 17 00:00:00 2001 From: Colin McDonald Date: Fri, 17 Jun 2016 01:12:04 -0400 Subject: [PATCH 09/24] Add blocking TODO/warning --- src/main/java/net/frozenorb/apiv3/actor/actors/UserActor.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/net/frozenorb/apiv3/actor/actors/UserActor.java b/src/main/java/net/frozenorb/apiv3/actor/actors/UserActor.java index 1f2ddeb..49dd451 100644 --- a/src/main/java/net/frozenorb/apiv3/actor/actors/UserActor.java +++ b/src/main/java/net/frozenorb/apiv3/actor/actors/UserActor.java @@ -24,6 +24,7 @@ public final class UserActor implements Actor { if (cachedAuthorized != null) { return cachedAuthorized; } else { + // TODO: THIS IS KINDA BLOCKING boolean authorized = user.hasPermissionAnywhere(Permissions.SIGN_API_REQUEST); cachedAuthorized = authorized; return authorized; From 26e84251b620b7b01632e8eca28d228862e70aed Mon Sep 17 00:00:00 2001 From: Colin McDonald Date: Fri, 17 Jun 2016 01:12:32 -0400 Subject: [PATCH 10/24] Add some more async functionality --- .../java/net/frozenorb/apiv3/model/IpBan.java | 49 ++++++++++--- .../java/net/frozenorb/apiv3/model/User.java | 70 +++++++++---------- 2 files changed, 76 insertions(+), 43 deletions(-) diff --git a/src/main/java/net/frozenorb/apiv3/model/IpBan.java b/src/main/java/net/frozenorb/apiv3/model/IpBan.java index dbff14a..cb23fea 100644 --- a/src/main/java/net/frozenorb/apiv3/model/IpBan.java +++ b/src/main/java/net/frozenorb/apiv3/model/IpBan.java @@ -131,16 +131,49 @@ public final class IpBan { return removedBy != null; } - public String getAccessDenialReason() { - String accessDenialReason = "Your ip address has been suspended from the MineHQ Network. \n\n"; + // TODO: CLEANUP + public void getAccessDenialReason(SingleResultCallback callback) { + Punishment.findByLinkedIpBanId(id, (punishment, error) -> { + if (error != null) { + callback.onResult(null, error); + return; + }; - if (getExpiresAt() != null) { - accessDenialReason += "Expires in " + TimeUtils.formatIntoDetailedString(TimeUtils.getSecondsBetween(getExpiresAt(), Instant.now())); - } else { - accessDenialReason += "Appeal at MineHQ.com/appeal"; - } + if (punishment != null) { + User.findById(punishment.getUser(), (user, error2) -> { + if (error2 != null) { + callback.onResult(null, error2); + return; + } - return accessDenialReason; + String accessDenialReason; + + if (user != null) { + accessDenialReason = "Your IP address has been suspended from the MineHQ Network for a punishment related to " + user.getLastUsername() + ". \n\n"; + } else { + accessDenialReason = "Your IP address has been suspended from the MineHQ Network. \n\n"; + } + + if (getExpiresAt() != null) { + accessDenialReason += "Expires in " + TimeUtils.formatIntoDetailedString(TimeUtils.getSecondsBetween(getExpiresAt(), Instant.now())); + } else { + accessDenialReason += "Appeal at MineHQ.com/appeal"; + } + + callback.onResult(accessDenialReason, null); + }); + } else { + String accessDenialReason = "Your IP address has been suspended from the MineHQ Network. \n\n"; + + if (getExpiresAt() != null) { + accessDenialReason += "Expires in " + TimeUtils.formatIntoDetailedString(TimeUtils.getSecondsBetween(getExpiresAt(), Instant.now())); + } else { + accessDenialReason += "Appeal at MineHQ.com/appeal"; + } + + callback.onResult(accessDenialReason, null); + } + }); } public void insert() { diff --git a/src/main/java/net/frozenorb/apiv3/model/User.java b/src/main/java/net/frozenorb/apiv3/model/User.java index 7a47dc1..22e878d 100644 --- a/src/main/java/net/frozenorb/apiv3/model/User.java +++ b/src/main/java/net/frozenorb/apiv3/model/User.java @@ -13,18 +13,19 @@ import fr.javatic.mongo.jacksonCodec.Entity; import fr.javatic.mongo.jacksonCodec.objectId.Id; import io.vertx.core.CompositeFuture; import io.vertx.core.Future; +import io.vertx.core.cli.converters.BooleanConverter; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.Setter; import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.serialization.gson.ExcludeFromReplies; -import net.frozenorb.apiv3.serialization.jackson.UUIDJsonDeserializer; -import net.frozenorb.apiv3.serialization.jackson.UUIDJsonSerializer; +import net.frozenorb.apiv3.serialization.jackson.UuidJsonDeserializer; +import net.frozenorb.apiv3.serialization.jackson.UuidJsonSerializer; import net.frozenorb.apiv3.unsorted.BlockingCallback; import net.frozenorb.apiv3.util.MojangUtils; import net.frozenorb.apiv3.util.PermissionUtils; import net.frozenorb.apiv3.util.SyncUtils; -import net.frozenorb.apiv3.util.UUIDUtils; +import net.frozenorb.apiv3.util.UuidUtils; import org.bson.Document; import java.time.Instant; @@ -36,7 +37,7 @@ public final class User { private static final MongoCollection usersCollection = APIv3.getDatabase().getCollection("users", User.class); - @Getter @Id @JsonSerialize(using=UUIDJsonSerializer.class) @JsonDeserialize(using=UUIDJsonDeserializer.class) private UUID id; + @Getter @Id @JsonSerialize(using=UuidJsonSerializer.class) @JsonDeserialize(using=UuidJsonDeserializer.class) private UUID id; @Getter private String lastUsername; @Getter @ExcludeFromReplies private Map aliases = new HashMap<>(); @Getter @ExcludeFromReplies @Setter private String totpSecret; @@ -59,7 +60,7 @@ public final class User { try { uuid = UUID.fromString(id); - } catch (IllegalArgumentException ex) { + } catch (NullPointerException | IllegalArgumentException ex) { return null; } @@ -67,7 +68,7 @@ public final class User { } public static User findByIdSync(UUID id) { - if (UUIDUtils.isAcceptableUUID(id)) { + if (UuidUtils.isAcceptableUuid(id)) { return SyncUtils.blockOne(usersCollection.find(new Document("_id", id))); } else { return null; @@ -96,7 +97,7 @@ public final class User { } public static void findById(UUID id, SingleResultCallback callback) { - if (UUIDUtils.isAcceptableUUID(id)) { + if (UuidUtils.isAcceptableUuid(id)) { usersCollection.find(new Document("_id", id)).first(callback); } else { callback.onResult(null, null); @@ -133,6 +134,8 @@ public final class User { public User() {} // For Morphia + // TODO: THIS IS CURRENTLY BLOCKING. MAYBE FOR THE HEARTBEAT WE CAN DO SOMETHING + // TO MAKE IT NOT SO BLOCKING public User(UUID id, String lastUsername) { this.id = id; this.lastUsername = ""; // Intentional, so updateUsername actually does something. @@ -145,13 +148,16 @@ public final class User { this.lastSeenAt = Instant.now(); this.firstSeenAt = Instant.now(); - // TODO: MAKE THIS ASYNC? SOMEHOW? - BlockingCallback blockingCallback = new BlockingCallback<>(); - updateUsername(lastUsername, blockingCallback); - blockingCallback.get(); + updateUsername(lastUsername); } public boolean hasPermissionAnywhere(String permission) { + Map globalPermissions = getGlobalPermissions(); + return globalPermissions.containsKey(permission) && globalPermissions.get(permission); + } + + // TODO: ASYNC + public Map getGlobalPermissions() { Map globalPermissions = PermissionUtils.getDefaultPermissions(getHighestRankAnywhere()); for (Map.Entry serverGroupEntry : getHighestRanks().entrySet()) { @@ -164,7 +170,7 @@ public final class User { ); } - return globalPermissions.containsKey(permission) && globalPermissions.get(permission); + return ImmutableMap.copyOf(globalPermissions); } // TODO: Clean @@ -188,31 +194,20 @@ public final class User { this.online = false; } - public void updateUsername(String newUsername, SingleResultCallback callback) { - this.aliases.put(newUsername, Instant.now()); + public void updateUsername(String newUsername) { + if (!newUsername.equals(lastUsername)) { + this.lastUsername = newUsername; - if (newUsername.equalsIgnoreCase(lastUsername)) { - callback.onResult(null, null); - return; + User withNewUsername; + + while ((withNewUsername = User.findByLastUsernameSync(newUsername)) != null) { + BlockingCallback callback = new BlockingCallback<>(); + MojangUtils.getName(withNewUsername.getId(), callback); + withNewUsername.updateUsername(callback.get()); + } } - this.lastUsername = newUsername; - - User.findByLastUsername(newUsername, (otherUser, error) -> { - if (error != null) { - callback.onResult(null, error); - } else if (otherUser != null) { - MojangUtils.getName(otherUser.getId(), (newName, error2) -> { - if (error2 != null) { - callback.onResult(null, error2); - } else { - otherUser.updateUsername(newName, callback); - } - }); - } else { - callback.onResult(null, null); - } - }); + this.aliases.put(newUsername, Instant.now()); } public void setPassword(String input) { @@ -378,9 +373,14 @@ public final class User { "activeBanId", activeBan.getId() ); } else if (activeIpBan != null) { + // TODO: ASYNC + BlockingCallback callback = new BlockingCallback<>(); + activeIpBan.getAccessDenialReason(callback); + String reason = callback.get(); + access = ImmutableMap.of( "allowed", false, - "message", activeIpBan.getAccessDenialReason(), + "message", reason, "activeIpBanId", activeIpBan.getId() ); } From dff1e16283e759f1d6a796f3d29b9005db9f2146 Mon Sep 17 00:00:00 2001 From: Colin McDonald Date: Fri, 17 Jun 2016 01:13:03 -0400 Subject: [PATCH 11/24] Finish linked ip ban integration --- .../java/net/frozenorb/apiv3/model/Punishment.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main/java/net/frozenorb/apiv3/model/Punishment.java b/src/main/java/net/frozenorb/apiv3/model/Punishment.java index ef62520..74bb994 100644 --- a/src/main/java/net/frozenorb/apiv3/model/Punishment.java +++ b/src/main/java/net/frozenorb/apiv3/model/Punishment.java @@ -60,6 +60,10 @@ public final class Punishment { return SyncUtils.blockOne(punishmentsCollection.find(new Document("_id", id))); } + public static Punishment findByLinkedIpBanIdSync(String id) { + return SyncUtils.blockOne(punishmentsCollection.find(new Document("linkedIpBanId", id))); + } + public static List findByUserSync(User user) { return findByUserSync(user.getId()); } @@ -94,6 +98,10 @@ public final class Punishment { punishmentsCollection.find(new Document("_id", id)).first(callback); } + public static void findByLinkedIpBanId(String id, SingleResultCallback callback) { + punishmentsCollection.find(new Document("linkedIpBanId", id)).first(callback); + } + public static void findByUser(User user, SingleResultCallback> callback) { findByUser(user.getId(), callback); } @@ -182,7 +190,7 @@ public final class Punishment { } public void linkIpBan(IpBan ipBan) { - + this.linkedIpBanId = ipBan.getId(); } public void insert() { From ba17babfb827e7ef7a23275fe0ad303d643e381a Mon Sep 17 00:00:00 2001 From: Colin McDonald Date: Fri, 17 Jun 2016 01:13:20 -0400 Subject: [PATCH 12/24] Add an async save method to ServerGroup --- src/main/java/net/frozenorb/apiv3/model/ServerGroup.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/main/java/net/frozenorb/apiv3/model/ServerGroup.java b/src/main/java/net/frozenorb/apiv3/model/ServerGroup.java index 6ca53b0..eada73a 100644 --- a/src/main/java/net/frozenorb/apiv3/model/ServerGroup.java +++ b/src/main/java/net/frozenorb/apiv3/model/ServerGroup.java @@ -1,5 +1,6 @@ package net.frozenorb.apiv3.model; +import com.mongodb.async.SingleResultCallback; import com.mongodb.async.client.MongoCollection; import com.mongodb.client.result.DeleteResult; import com.mongodb.client.result.UpdateResult; @@ -77,10 +78,8 @@ public final class ServerGroup { callback.get(); } - public void save() { - BlockingCallback callback = new BlockingCallback<>(); + public void save(SingleResultCallback callback) { serverGroupsCollection.replaceOne(new Document("_id", id), this, callback); - callback.get(); } public void delete() { From cec5031aea9b2535d6f39bc040ee1b5361b18a5e Mon Sep 17 00:00:00 2001 From: Colin McDonald Date: Fri, 17 Jun 2016 01:13:39 -0400 Subject: [PATCH 13/24] Optimize imports --- src/main/java/net/frozenorb/apiv3/model/User.java | 1 - .../frozenorb/apiv3/route/auditLog/POSTUserAuditLogEntry.java | 2 -- src/main/java/net/frozenorb/apiv3/route/ipBans/GETIpBan.java | 1 - src/main/java/net/frozenorb/apiv3/route/ipBans/GETIpBans.java | 1 - src/main/java/net/frozenorb/apiv3/route/ipBans/GETIpIpBans.java | 2 -- src/main/java/net/frozenorb/apiv3/route/ipLog/GETUserIpLog.java | 2 -- .../route/notificationTemplates/GETNotificationTemplate.java | 1 - .../route/notificationTemplates/GETNotificationTemplates.java | 1 - .../net/frozenorb/apiv3/route/punishments/GETPunishment.java | 1 - .../net/frozenorb/apiv3/route/punishments/GETPunishments.java | 1 - .../frozenorb/apiv3/route/punishments/GETUserPunishments.java | 1 - src/main/java/net/frozenorb/apiv3/route/users/GETUser.java | 1 - .../net/frozenorb/apiv3/route/users/GETUserPermissions.java | 1 - 13 files changed, 16 deletions(-) diff --git a/src/main/java/net/frozenorb/apiv3/model/User.java b/src/main/java/net/frozenorb/apiv3/model/User.java index 22e878d..2852f1f 100644 --- a/src/main/java/net/frozenorb/apiv3/model/User.java +++ b/src/main/java/net/frozenorb/apiv3/model/User.java @@ -13,7 +13,6 @@ import fr.javatic.mongo.jacksonCodec.Entity; import fr.javatic.mongo.jacksonCodec.objectId.Id; import io.vertx.core.CompositeFuture; import io.vertx.core.Future; -import io.vertx.core.cli.converters.BooleanConverter; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.Setter; diff --git a/src/main/java/net/frozenorb/apiv3/route/auditLog/POSTUserAuditLogEntry.java b/src/main/java/net/frozenorb/apiv3/route/auditLog/POSTUserAuditLogEntry.java index 883d07a..5c29e73 100644 --- a/src/main/java/net/frozenorb/apiv3/route/auditLog/POSTUserAuditLogEntry.java +++ b/src/main/java/net/frozenorb/apiv3/route/auditLog/POSTUserAuditLogEntry.java @@ -5,9 +5,7 @@ import io.vertx.ext.web.RoutingContext; import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.auditLog.AuditLog; import net.frozenorb.apiv3.auditLog.AuditLogActionType; -import net.frozenorb.apiv3.model.AuditLogEntry; import net.frozenorb.apiv3.model.User; -import net.frozenorb.apiv3.unsorted.BlockingCallback; import net.frozenorb.apiv3.util.ErrorUtils; import net.frozenorb.apiv3.util.IpUtils; import org.bson.Document; diff --git a/src/main/java/net/frozenorb/apiv3/route/ipBans/GETIpBan.java b/src/main/java/net/frozenorb/apiv3/route/ipBans/GETIpBan.java index 43e9975..873bfac 100644 --- a/src/main/java/net/frozenorb/apiv3/route/ipBans/GETIpBan.java +++ b/src/main/java/net/frozenorb/apiv3/route/ipBans/GETIpBan.java @@ -3,7 +3,6 @@ package net.frozenorb.apiv3.route.ipBans; import io.vertx.core.Handler; import io.vertx.ext.web.RoutingContext; import net.frozenorb.apiv3.APIv3; -import net.frozenorb.apiv3.model.Grant; import net.frozenorb.apiv3.model.IpBan; import net.frozenorb.apiv3.util.ErrorUtils; diff --git a/src/main/java/net/frozenorb/apiv3/route/ipBans/GETIpBans.java b/src/main/java/net/frozenorb/apiv3/route/ipBans/GETIpBans.java index e0f8a95..ff605bd 100644 --- a/src/main/java/net/frozenorb/apiv3/route/ipBans/GETIpBans.java +++ b/src/main/java/net/frozenorb/apiv3/route/ipBans/GETIpBans.java @@ -3,7 +3,6 @@ package net.frozenorb.apiv3.route.ipBans; import io.vertx.core.Handler; import io.vertx.ext.web.RoutingContext; import net.frozenorb.apiv3.APIv3; -import net.frozenorb.apiv3.model.Grant; import net.frozenorb.apiv3.model.IpBan; import net.frozenorb.apiv3.util.ErrorUtils; diff --git a/src/main/java/net/frozenorb/apiv3/route/ipBans/GETIpIpBans.java b/src/main/java/net/frozenorb/apiv3/route/ipBans/GETIpIpBans.java index 7e93646..e5c1820 100644 --- a/src/main/java/net/frozenorb/apiv3/route/ipBans/GETIpIpBans.java +++ b/src/main/java/net/frozenorb/apiv3/route/ipBans/GETIpIpBans.java @@ -4,8 +4,6 @@ import io.vertx.core.Handler; import io.vertx.ext.web.RoutingContext; import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.model.IpBan; -import net.frozenorb.apiv3.model.Punishment; -import net.frozenorb.apiv3.model.User; import net.frozenorb.apiv3.util.ErrorUtils; import net.frozenorb.apiv3.util.IpUtils; diff --git a/src/main/java/net/frozenorb/apiv3/route/ipLog/GETUserIpLog.java b/src/main/java/net/frozenorb/apiv3/route/ipLog/GETUserIpLog.java index 83f40ea..19637f2 100644 --- a/src/main/java/net/frozenorb/apiv3/route/ipLog/GETUserIpLog.java +++ b/src/main/java/net/frozenorb/apiv3/route/ipLog/GETUserIpLog.java @@ -3,9 +3,7 @@ package net.frozenorb.apiv3.route.ipLog; import io.vertx.core.Handler; import io.vertx.ext.web.RoutingContext; import net.frozenorb.apiv3.APIv3; -import net.frozenorb.apiv3.model.IpBan; import net.frozenorb.apiv3.model.IpLogEntry; -import net.frozenorb.apiv3.model.Punishment; import net.frozenorb.apiv3.model.User; import net.frozenorb.apiv3.util.ErrorUtils; diff --git a/src/main/java/net/frozenorb/apiv3/route/notificationTemplates/GETNotificationTemplate.java b/src/main/java/net/frozenorb/apiv3/route/notificationTemplates/GETNotificationTemplate.java index a67020d..3874364 100644 --- a/src/main/java/net/frozenorb/apiv3/route/notificationTemplates/GETNotificationTemplate.java +++ b/src/main/java/net/frozenorb/apiv3/route/notificationTemplates/GETNotificationTemplate.java @@ -3,7 +3,6 @@ package net.frozenorb.apiv3.route.notificationTemplates; import io.vertx.core.Handler; import io.vertx.ext.web.RoutingContext; import net.frozenorb.apiv3.APIv3; -import net.frozenorb.apiv3.model.IpBan; import net.frozenorb.apiv3.model.NotificationTemplate; import net.frozenorb.apiv3.util.ErrorUtils; diff --git a/src/main/java/net/frozenorb/apiv3/route/notificationTemplates/GETNotificationTemplates.java b/src/main/java/net/frozenorb/apiv3/route/notificationTemplates/GETNotificationTemplates.java index 68d365a..e8cb6b8 100644 --- a/src/main/java/net/frozenorb/apiv3/route/notificationTemplates/GETNotificationTemplates.java +++ b/src/main/java/net/frozenorb/apiv3/route/notificationTemplates/GETNotificationTemplates.java @@ -3,7 +3,6 @@ package net.frozenorb.apiv3.route.notificationTemplates; import io.vertx.core.Handler; import io.vertx.ext.web.RoutingContext; import net.frozenorb.apiv3.APIv3; -import net.frozenorb.apiv3.model.IpBan; import net.frozenorb.apiv3.model.NotificationTemplate; import net.frozenorb.apiv3.util.ErrorUtils; diff --git a/src/main/java/net/frozenorb/apiv3/route/punishments/GETPunishment.java b/src/main/java/net/frozenorb/apiv3/route/punishments/GETPunishment.java index 8f899c3..756d2e8 100644 --- a/src/main/java/net/frozenorb/apiv3/route/punishments/GETPunishment.java +++ b/src/main/java/net/frozenorb/apiv3/route/punishments/GETPunishment.java @@ -3,7 +3,6 @@ package net.frozenorb.apiv3.route.punishments; import io.vertx.core.Handler; import io.vertx.ext.web.RoutingContext; import net.frozenorb.apiv3.APIv3; -import net.frozenorb.apiv3.model.IpBan; import net.frozenorb.apiv3.model.Punishment; import net.frozenorb.apiv3.util.ErrorUtils; diff --git a/src/main/java/net/frozenorb/apiv3/route/punishments/GETPunishments.java b/src/main/java/net/frozenorb/apiv3/route/punishments/GETPunishments.java index cfe3c83..2bbc71e 100644 --- a/src/main/java/net/frozenorb/apiv3/route/punishments/GETPunishments.java +++ b/src/main/java/net/frozenorb/apiv3/route/punishments/GETPunishments.java @@ -3,7 +3,6 @@ package net.frozenorb.apiv3.route.punishments; import io.vertx.core.Handler; import io.vertx.ext.web.RoutingContext; import net.frozenorb.apiv3.APIv3; -import net.frozenorb.apiv3.model.Grant; import net.frozenorb.apiv3.model.Punishment; import net.frozenorb.apiv3.util.ErrorUtils; diff --git a/src/main/java/net/frozenorb/apiv3/route/punishments/GETUserPunishments.java b/src/main/java/net/frozenorb/apiv3/route/punishments/GETUserPunishments.java index 9402c15..7fb06f4 100644 --- a/src/main/java/net/frozenorb/apiv3/route/punishments/GETUserPunishments.java +++ b/src/main/java/net/frozenorb/apiv3/route/punishments/GETUserPunishments.java @@ -3,7 +3,6 @@ package net.frozenorb.apiv3.route.punishments; import io.vertx.core.Handler; import io.vertx.ext.web.RoutingContext; import net.frozenorb.apiv3.APIv3; -import net.frozenorb.apiv3.model.Grant; import net.frozenorb.apiv3.model.Punishment; import net.frozenorb.apiv3.model.User; import net.frozenorb.apiv3.util.ErrorUtils; diff --git a/src/main/java/net/frozenorb/apiv3/route/users/GETUser.java b/src/main/java/net/frozenorb/apiv3/route/users/GETUser.java index 82e9c56..bbcee15 100644 --- a/src/main/java/net/frozenorb/apiv3/route/users/GETUser.java +++ b/src/main/java/net/frozenorb/apiv3/route/users/GETUser.java @@ -3,7 +3,6 @@ package net.frozenorb.apiv3.route.users; import io.vertx.core.Handler; import io.vertx.ext.web.RoutingContext; import net.frozenorb.apiv3.APIv3; -import net.frozenorb.apiv3.model.IpBan; import net.frozenorb.apiv3.model.User; import net.frozenorb.apiv3.util.ErrorUtils; diff --git a/src/main/java/net/frozenorb/apiv3/route/users/GETUserPermissions.java b/src/main/java/net/frozenorb/apiv3/route/users/GETUserPermissions.java index 8aa2425..bdd8ef6 100644 --- a/src/main/java/net/frozenorb/apiv3/route/users/GETUserPermissions.java +++ b/src/main/java/net/frozenorb/apiv3/route/users/GETUserPermissions.java @@ -3,7 +3,6 @@ package net.frozenorb.apiv3.route.users; import io.vertx.core.Handler; import io.vertx.ext.web.RoutingContext; import net.frozenorb.apiv3.APIv3; -import net.frozenorb.apiv3.model.Punishment; import net.frozenorb.apiv3.model.User; import net.frozenorb.apiv3.util.ErrorUtils; From a1e70b47f7b008f6e358944442eb2add14188a19 Mon Sep 17 00:00:00 2001 From: Colin McDonald Date: Fri, 17 Jun 2016 12:40:47 -0400 Subject: [PATCH 14/24] Some small changes to make IntelliJ's code analysis happy --- .../apiv3/dataImport/V2Importer.java | 2 +- .../dataImport/converters/GrantConverter.java | 1 + .../dataImport/converters/IpLogConverter.java | 1 + .../converters/PunishmentConverter.java | 1 + .../apiv3/handler/ActorAttributeHandler.java | 2 +- .../frozenorb/apiv3/model/AuditLogEntry.java | 30 +------------------ .../java/net/frozenorb/apiv3/model/Grant.java | 23 ++------------ .../java/net/frozenorb/apiv3/model/IpBan.java | 26 ++-------------- .../net/frozenorb/apiv3/model/IpLogEntry.java | 2 +- .../apiv3/model/NotificationTemplate.java | 12 +------- .../net/frozenorb/apiv3/model/Punishment.java | 29 ++---------------- .../java/net/frozenorb/apiv3/model/Rank.java | 8 +---- .../net/frozenorb/apiv3/model/Server.java | 8 ++--- .../frozenorb/apiv3/model/ServerGroup.java | 8 ++--- .../java/net/frozenorb/apiv3/model/User.java | 25 ++-------------- .../frozenorb/apiv3/model/UserMetaEntry.java | 26 +--------------- .../net/frozenorb/apiv3/route/GETDump.java | 1 - .../apiv3/route/grants/POSTUserGrant.java | 6 ++-- .../apiv3/route/ipBans/POSTIpIpBan.java | 6 ++-- .../route/punishments/POSTUserPunish.java | 6 ++-- .../route/servers/POSTServerHeartbeat.java | 14 ++++----- .../apiv3/route/users/POSTUserLogin.java | 4 +-- 22 files changed, 43 insertions(+), 198 deletions(-) diff --git a/src/main/java/net/frozenorb/apiv3/dataImport/V2Importer.java b/src/main/java/net/frozenorb/apiv3/dataImport/V2Importer.java index dc29a29..bd17093 100644 --- a/src/main/java/net/frozenorb/apiv3/dataImport/V2Importer.java +++ b/src/main/java/net/frozenorb/apiv3/dataImport/V2Importer.java @@ -56,7 +56,7 @@ public final class V2Importer { private final Future future; - public FutureCompatibilityCallback(Future future) { + private FutureCompatibilityCallback(Future future) { this.future = future; } diff --git a/src/main/java/net/frozenorb/apiv3/dataImport/converters/GrantConverter.java b/src/main/java/net/frozenorb/apiv3/dataImport/converters/GrantConverter.java index f2de982..085618a 100644 --- a/src/main/java/net/frozenorb/apiv3/dataImport/converters/GrantConverter.java +++ b/src/main/java/net/frozenorb/apiv3/dataImport/converters/GrantConverter.java @@ -21,6 +21,7 @@ public final class GrantConverter implements Block { this.oidToUniqueId = oidToUniqueId; } + @SuppressWarnings("unchecked") @Override public void apply(Document grant) { UUID target = oidToUniqueId.get(((Map) grant.get("target")).get("$id")); diff --git a/src/main/java/net/frozenorb/apiv3/dataImport/converters/IpLogConverter.java b/src/main/java/net/frozenorb/apiv3/dataImport/converters/IpLogConverter.java index 6d6c3d7..05e16e0 100644 --- a/src/main/java/net/frozenorb/apiv3/dataImport/converters/IpLogConverter.java +++ b/src/main/java/net/frozenorb/apiv3/dataImport/converters/IpLogConverter.java @@ -20,6 +20,7 @@ public final class IpLogConverter implements Block { this.oidToUniqueId = oidToUniqueId; } + @SuppressWarnings("unchecked") @Override public void apply(Document ipLogEntry) { UUID user = oidToUniqueId.get(((Map) ipLogEntry.get("user")).get("$id")); diff --git a/src/main/java/net/frozenorb/apiv3/dataImport/converters/PunishmentConverter.java b/src/main/java/net/frozenorb/apiv3/dataImport/converters/PunishmentConverter.java index 1274c6a..178d5be 100644 --- a/src/main/java/net/frozenorb/apiv3/dataImport/converters/PunishmentConverter.java +++ b/src/main/java/net/frozenorb/apiv3/dataImport/converters/PunishmentConverter.java @@ -21,6 +21,7 @@ public final class PunishmentConverter implements Block { this.oidToUniqueId = oidToUniqueId; } + @SuppressWarnings("unchecked") @Override public void apply(Document punishment) { UUID target = oidToUniqueId.get(((Map) punishment.get("user")).get("$id")); diff --git a/src/main/java/net/frozenorb/apiv3/handler/ActorAttributeHandler.java b/src/main/java/net/frozenorb/apiv3/handler/ActorAttributeHandler.java index 37c456d..cf5a30d 100644 --- a/src/main/java/net/frozenorb/apiv3/handler/ActorAttributeHandler.java +++ b/src/main/java/net/frozenorb/apiv3/handler/ActorAttributeHandler.java @@ -119,7 +119,7 @@ public final class ActorAttributeHandler implements Handler { } } - public void processNoAuthorization(RoutingContext ctx) { + private void processNoAuthorization(RoutingContext ctx) { ctx.put("actor", new UnknownActor()); ctx.next(); } diff --git a/src/main/java/net/frozenorb/apiv3/model/AuditLogEntry.java b/src/main/java/net/frozenorb/apiv3/model/AuditLogEntry.java index 388389f..ac5ddba 100644 --- a/src/main/java/net/frozenorb/apiv3/model/AuditLogEntry.java +++ b/src/main/java/net/frozenorb/apiv3/model/AuditLogEntry.java @@ -34,47 +34,19 @@ public final class AuditLogEntry { @Getter private AuditLogActionType type; @Getter private Map metadata; - public static List findAllSync() { - return SyncUtils.blockMulti(auditLogCollection.find().sort(new Document("performedAt", -1))); - } - - public static List findAllPaginatedSync(int skip, int pageSize) { - return SyncUtils.blockMulti(auditLogCollection.find().sort(new Document("performedAt", -1)).skip(skip).limit(pageSize)); - } - - public static AuditLogEntry findByIdSync(String id) { - return SyncUtils.blockOne(auditLogCollection.find(new Document("_id", id))); - } - - public static List findByUserSync(User user) { - return findByUserSync(user.getId()); - } - public static List findByUserSync(UUID user) { return SyncUtils.blockMulti(auditLogCollection.find(new Document("user", user))); } - public static void findAll(SingleResultCallback> callback) { - auditLogCollection.find().sort(new Document("performedAt", -1)).into(new ArrayList<>(), callback); - } - public static void findAllPaginated(int skip, int pageSize, SingleResultCallback> callback) { auditLogCollection.find().sort(new Document("performedAt", -1)).skip(skip).limit(pageSize).into(new ArrayList<>(), callback); } - public static void findById(String id, SingleResultCallback callback) { - auditLogCollection.find(new Document("_id", id)).first(callback); - } - - public static void findByUser(User user, SingleResultCallback> callback) { - findByUser(user.getId(), callback); - } - public static void findByUser(UUID user, SingleResultCallback> callback) { auditLogCollection.find(new Document("user", user)).into(new ArrayList<>(), callback); } - public AuditLogEntry() {} // For Morphia + public AuditLogEntry() {} // For Jackson public AuditLogEntry(User user, String userIp, Actor actor, AuditLogActionType type, Map metadata) { this.id = new ObjectId().toString(); diff --git a/src/main/java/net/frozenorb/apiv3/model/Grant.java b/src/main/java/net/frozenorb/apiv3/model/Grant.java index 726a992..bef6794 100644 --- a/src/main/java/net/frozenorb/apiv3/model/Grant.java +++ b/src/main/java/net/frozenorb/apiv3/model/Grant.java @@ -27,7 +27,7 @@ public final class Grant { @Getter @Id private String id; @Getter private UUID user; @Getter private String reason; - @Getter private Set scopes = new HashSet<>(); // So on things w/o scopes we still load properly (Morphia drops empty sets) + @Getter private Set scopes; @Getter private String rank; @Getter private Instant expiresAt; @@ -42,10 +42,6 @@ public final class Grant { return SyncUtils.blockMulti(grantsCollection.find().sort(new Document("addedAt", -1))); } - public static List findAllPaginatedSync(int skip, int pageSize) { - return SyncUtils.blockMulti(grantsCollection.find().sort(new Document("addedAt", -1)).skip(skip).limit(pageSize)); - } - public static List findByRankSync(Collection ranks) { Collection convertedRanks = ranks.stream().map(Rank::getId).collect(Collectors.toList()); return SyncUtils.blockMulti(grantsCollection.find(new Document("rank", new Document("$in", convertedRanks)))); @@ -63,19 +59,10 @@ public final class Grant { return SyncUtils.blockMulti(grantsCollection.find(new Document("user", user))); } - public static void findAll(SingleResultCallback> callback) { - grantsCollection.find().sort(new Document("addedAt", -1)).into(new ArrayList<>(), callback); - } - public static void findAllPaginated(int skip, int pageSize, SingleResultCallback> callback) { grantsCollection.find().sort(new Document("addedAt", -1)).skip(skip).limit(pageSize).into(new ArrayList<>(), callback); } - public static void findByRank(Collection ranks, SingleResultCallback> callback) { - Collection convertedRanks = ranks.stream().map(Rank::getId).collect(Collectors.toList()); - grantsCollection.find(new Document("rank", new Document("$in", convertedRanks))).into(new ArrayList<>(), callback); - } - public static void findById(String id, SingleResultCallback callback) { grantsCollection.find(new Document("_id", id)).first(callback); } @@ -108,7 +95,7 @@ public final class Grant { }); } - public Grant() {} // For Morphia + public Grant() {} // For Jackson public Grant(User user, String reason, Set scopes, Rank rank, Instant expiresAt, User addedBy) { this.id = new ObjectId().toString(); @@ -126,11 +113,7 @@ public final class Grant { } public boolean isExpired() { - if (expiresAt == null) { - return false; // Never expires - } else { - return expiresAt.isBefore(Instant.now()); - } + return expiresAt != null && expiresAt.isBefore(Instant.now()); } public boolean isRemoved() { diff --git a/src/main/java/net/frozenorb/apiv3/model/IpBan.java b/src/main/java/net/frozenorb/apiv3/model/IpBan.java index cb23fea..5a44a43 100644 --- a/src/main/java/net/frozenorb/apiv3/model/IpBan.java +++ b/src/main/java/net/frozenorb/apiv3/model/IpBan.java @@ -39,26 +39,10 @@ public final class IpBan { @Getter private Instant removedAt; @Getter private String removalReason; - public static List findAllSync() { - return SyncUtils.blockMulti(ipBansCollection.find().sort(new Document("addedAt", -1))); - } - - public static List findAllPaginatedSync(int skip, int pageSize) { - return SyncUtils.blockMulti(ipBansCollection.find().sort(new Document("addedAt", -1)).skip(skip).limit(pageSize)); - } - public static IpBan findByIdSync(String id) { return SyncUtils.blockOne(ipBansCollection.find(new Document("_id", id))); } - public static List findByIpSync(String userIp) { - return SyncUtils.blockMulti(ipBansCollection.find(new Document("userIp", userIp))); - } - - public static void findAll(SingleResultCallback> callback) { - ipBansCollection.find().sort(new Document("addedAt", -1)).into(new ArrayList<>(), callback); - } - public static void findAllPaginated(int skip, int pageSize, SingleResultCallback> callback) { ipBansCollection.find().sort(new Document("addedAt", -1)).skip(skip).limit(pageSize).into(new ArrayList<>(), callback); } @@ -91,7 +75,7 @@ public final class IpBan { }); } - public IpBan() {} // For Morphia + public IpBan() {} // For Jackson public IpBan(String userIp, Punishment linked) { this.id = new ObjectId().toString(); @@ -120,11 +104,7 @@ public final class IpBan { } public boolean isExpired() { - if (expiresAt == null) { - return false; // Never expires - } else { - return expiresAt.isBefore(Instant.now()); - } + return expiresAt != null && expiresAt.isBefore(Instant.now()); } public boolean isRemoved() { @@ -137,7 +117,7 @@ public final class IpBan { if (error != null) { callback.onResult(null, error); return; - }; + } if (punishment != null) { User.findById(punishment.getUser(), (user, error2) -> { diff --git a/src/main/java/net/frozenorb/apiv3/model/IpLogEntry.java b/src/main/java/net/frozenorb/apiv3/model/IpLogEntry.java index 15fbfe6..4620e10 100644 --- a/src/main/java/net/frozenorb/apiv3/model/IpLogEntry.java +++ b/src/main/java/net/frozenorb/apiv3/model/IpLogEntry.java @@ -79,7 +79,7 @@ public final class IpLogEntry { ipLogCollection.find(new Document("user", user).append("userIp", userIp)).first(callback); } - public IpLogEntry() {} // For Morphia + public IpLogEntry() {} // For Jackson public IpLogEntry(User user, String userIp) { this.id = new ObjectId().toString(); diff --git a/src/main/java/net/frozenorb/apiv3/model/NotificationTemplate.java b/src/main/java/net/frozenorb/apiv3/model/NotificationTemplate.java index fb748d6..9925f03 100644 --- a/src/main/java/net/frozenorb/apiv3/model/NotificationTemplate.java +++ b/src/main/java/net/frozenorb/apiv3/model/NotificationTemplate.java @@ -26,10 +26,6 @@ public final class NotificationTemplate { @Getter @Setter private String subject; @Getter @Setter private String body; - public static List findAllSync() { - return SyncUtils.blockMulti(notificationTemplatesCollection.find()); - } - public static NotificationTemplate findByIdSync(String id) { return SyncUtils.blockOne(notificationTemplatesCollection.find(new Document("_id", id))); } @@ -42,7 +38,7 @@ public final class NotificationTemplate { notificationTemplatesCollection.find(new Document("_id", id)).first(callback); } - public NotificationTemplate() {} // For Morphia + public NotificationTemplate() {} // For Jackson public NotificationTemplate(String id, String subject, String body) { this.id = id; @@ -75,12 +71,6 @@ public final class NotificationTemplate { callback.get(); } - public void save() { - BlockingCallback callback = new BlockingCallback<>(); - notificationTemplatesCollection.replaceOne(new Document("_id", id), this, callback); - callback.get(); - } - public void delete() { BlockingCallback callback = new BlockingCallback<>(); notificationTemplatesCollection.deleteOne(new Document("_id", id), callback); diff --git a/src/main/java/net/frozenorb/apiv3/model/Punishment.java b/src/main/java/net/frozenorb/apiv3/model/Punishment.java index 74bb994..e6f15f7 100644 --- a/src/main/java/net/frozenorb/apiv3/model/Punishment.java +++ b/src/main/java/net/frozenorb/apiv3/model/Punishment.java @@ -43,14 +43,6 @@ public final class Punishment { @Getter private Instant removedAt; @Getter private String removalReason; - public static List findAllSync() { - return SyncUtils.blockMulti(punishmentsCollection.find().sort(new Document("addedAt", -1))); - } - - public static List findAllPaginatedSync(int skip, int pageSize) { - return SyncUtils.blockMulti(punishmentsCollection.find().sort(new Document("addedAt", -1)).skip(skip).limit(pageSize)); - } - public static List findByTypeSync(Collection types) { Collection convertedTypes = types.stream().map(PunishmentType::name).collect(Collectors.toList()); return SyncUtils.blockMulti(punishmentsCollection.find(new Document("type", new Document("$in", convertedTypes)))); @@ -60,10 +52,6 @@ public final class Punishment { return SyncUtils.blockOne(punishmentsCollection.find(new Document("_id", id))); } - public static Punishment findByLinkedIpBanIdSync(String id) { - return SyncUtils.blockOne(punishmentsCollection.find(new Document("linkedIpBanId", id))); - } - public static List findByUserSync(User user) { return findByUserSync(user.getId()); } @@ -81,19 +69,10 @@ public final class Punishment { return SyncUtils.blockMulti(punishmentsCollection.find(new Document("user", user).append("type", new Document("$in", convertedTypes)))); } - public static void findAll(SingleResultCallback> callback) { - punishmentsCollection.find().sort(new Document("addedAt", -1)).into(new ArrayList<>(), callback); - } - public static void findAllPaginated(int skip, int pageSize, SingleResultCallback> callback) { punishmentsCollection.find().sort(new Document("addedAt", -1)).skip(skip).limit(pageSize).into(new ArrayList<>(), callback); } - public static void findByType(Collection types, SingleResultCallback> callback) { - Collection convertedTypes = types.stream().map(PunishmentType::name).collect(Collectors.toList()); - punishmentsCollection.find(new Document("type", new Document("$in", convertedTypes))).into(new ArrayList<>(), callback); - } - public static void findById(String id, SingleResultCallback callback) { punishmentsCollection.find(new Document("_id", id)).first(callback); } @@ -139,7 +118,7 @@ public final class Punishment { punishmentsCollection.find(new Document("user", user).append("type", new Document("$in", convertedTypes))).into(new ArrayList<>(), callback); } - public Punishment() {} // For Morphia + public Punishment() {} // For Jackson public Punishment(User user, String reason, PunishmentType type, Instant expiresAt, User addedBy, Actor actor, Map metadata) { this.id = new ObjectId().toString(); @@ -159,11 +138,7 @@ public final class Punishment { } public boolean isExpired() { - if (expiresAt == null) { - return false; // Never expires - } else { - return expiresAt.isBefore(Instant.now()); - } + return expiresAt != null && expiresAt.isBefore(Instant.now()); } public boolean isRemoved() { diff --git a/src/main/java/net/frozenorb/apiv3/model/Rank.java b/src/main/java/net/frozenorb/apiv3/model/Rank.java index 44c9933..c75f359 100644 --- a/src/main/java/net/frozenorb/apiv3/model/Rank.java +++ b/src/main/java/net/frozenorb/apiv3/model/Rank.java @@ -45,7 +45,7 @@ public final class Rank { return rankCache.get(id); } - public Rank() {} // For Morphia + public Rank() {} // For Jackson public Rank(String id, int weight, String displayName, String gameColor, String websiteColor, boolean staffRank) { this.id = id; @@ -80,12 +80,6 @@ public final class Rank { callback.get(); } - public void save() { - BlockingCallback callback = new BlockingCallback<>(); - ranksCollection.replaceOne(new Document("_id", id), this, callback); - callback.get(); - } - public void delete() { BlockingCallback callback = new BlockingCallback<>(); ranksCollection.deleteOne(new Document("_id", id), callback); diff --git a/src/main/java/net/frozenorb/apiv3/model/Server.java b/src/main/java/net/frozenorb/apiv3/model/Server.java index 94c7ca3..5fd84ec 100644 --- a/src/main/java/net/frozenorb/apiv3/model/Server.java +++ b/src/main/java/net/frozenorb/apiv3/model/Server.java @@ -32,9 +32,9 @@ public final class Server { @Getter @ExcludeFromReplies String apiKey; @Getter private String serverGroup; @Getter private String serverIp; - @Getter @Setter private Instant lastUpdatedAt; - @Getter @Setter private double lastTps; - @Getter @Setter @ExcludeFromReplies private Set players; + @Getter private Instant lastUpdatedAt; + @Getter private double lastTps; + @Getter @ExcludeFromReplies private Set players; public static List findAll() { updateCacheIfNeeded(); @@ -46,7 +46,7 @@ public final class Server { return serverCache.get(id); } - public Server() {} // For Morphia + public Server() {} // For Jackson public Server(String id, String displayName, String apiKey, ServerGroup serverGroup, String serverIp) { this.id = id; diff --git a/src/main/java/net/frozenorb/apiv3/model/ServerGroup.java b/src/main/java/net/frozenorb/apiv3/model/ServerGroup.java index eada73a..64bd245 100644 --- a/src/main/java/net/frozenorb/apiv3/model/ServerGroup.java +++ b/src/main/java/net/frozenorb/apiv3/model/ServerGroup.java @@ -29,10 +29,8 @@ public final class ServerGroup { @Getter @Id private String id; @Getter private String image; - // We define these HashSets up here because, in the event they're - // empty, Morphia will load them as null, not empty sets. - @Getter @Setter @ExcludeFromReplies private Set announcements = new HashSet<>(); - @Getter @Setter @ExcludeFromReplies private Map> permissions = new HashMap<>(); + @Getter @Setter @ExcludeFromReplies private Set announcements; + @Getter @Setter @ExcludeFromReplies private Map> permissions; // make this and other stuff async public static List findAll() { @@ -45,7 +43,7 @@ public final class ServerGroup { return serverGroupCache.get(id); } - public ServerGroup() {} // For Morphia + public ServerGroup() {} // For Jackson public ServerGroup(String id, String image) { this.id = id; diff --git a/src/main/java/net/frozenorb/apiv3/model/User.java b/src/main/java/net/frozenorb/apiv3/model/User.java index 2852f1f..12bde14 100644 --- a/src/main/java/net/frozenorb/apiv3/model/User.java +++ b/src/main/java/net/frozenorb/apiv3/model/User.java @@ -50,10 +50,6 @@ public final class User { @Getter private Instant firstSeenAt; @Getter private boolean online; - public static List findAllSync() { - return SyncUtils.blockMulti(usersCollection.find().sort(new Document("lastSeenAt", -1))); - } - public static User findByIdSync(String id) { UUID uuid; @@ -82,10 +78,6 @@ public final class User { return SyncUtils.blockOne(usersCollection.find(new Document("lastUsername", lastUsername))); } - public static void findAll(SingleResultCallback> callback) { - usersCollection.find().sort(new Document("lastSeenAt", -1)).into(new ArrayList<>(), callback); - } - public static void findById(String id, SingleResultCallback callback) { try { UUID uuid = UUID.fromString(id); @@ -123,15 +115,11 @@ public final class User { }); } - public static void findByEmailToken(String emailToken, SingleResultCallback callback) { - usersCollection.find(new Document("emailToken", emailToken)).first(callback); - } - public static void findByLastUsername(String lastUsername, SingleResultCallback callback) { usersCollection.find(new Document("lastUsername", lastUsername)).first(callback); } - public User() {} // For Morphia + public User() {} // For Jackson // TODO: THIS IS CURRENTLY BLOCKING. MAYBE FOR THE HEARTBEAT WE CAN DO SOMETHING // TO MAKE IT NOT SO BLOCKING @@ -139,11 +127,6 @@ public final class User { this.id = id; this.lastUsername = ""; // Intentional, so updateUsername actually does something. this.aliases = new HashMap<>(); - this.totpSecret = null; - this.password = null; - this.email = null; - this.phoneNumber = null; - this.lastSeenOn = null; this.lastSeenAt = Instant.now(); this.firstSeenAt = Instant.now(); @@ -226,11 +209,7 @@ public final class User { } public Rank getHighestRankAnywhere() { - return getHighestRankScoped(null); - } - - public Rank getHighestRankScoped(ServerGroup serverGroup) { - return getHighestRankScoped(serverGroup, Grant.findByUserSync(this)); + return getHighestRankScoped(null, Grant.findByUserSync(this)); } // TODO: Clean diff --git a/src/main/java/net/frozenorb/apiv3/model/UserMetaEntry.java b/src/main/java/net/frozenorb/apiv3/model/UserMetaEntry.java index 4e6a40a..e713b0e 100644 --- a/src/main/java/net/frozenorb/apiv3/model/UserMetaEntry.java +++ b/src/main/java/net/frozenorb/apiv3/model/UserMetaEntry.java @@ -30,14 +30,6 @@ public final class UserMetaEntry { @Getter private String serverGroup; @Getter @Setter private Map data; - public static List findAllSync() { - return SyncUtils.blockMulti(userMetaCollection.find()); - } - - public static UserMetaEntry findByIdSync(String id) { - return SyncUtils.blockOne(userMetaCollection.find(new Document("_id", id))); - } - public static UserMetaEntry findByUserAndGroupSync(User user, ServerGroup serverGroup) { return findByUserAndGroupSync(user.getId(), serverGroup); } @@ -46,23 +38,7 @@ public final class UserMetaEntry { return SyncUtils.blockOne(userMetaCollection.find(new Document("user", user).append("serverGroup", serverGroup.getId()))); } - public static void findAll(SingleResultCallback> callback) { - userMetaCollection.find().into(new ArrayList<>(), callback); - } - - public static void findById(String id, SingleResultCallback callback) { - userMetaCollection.find(new Document("_id", id)).first(callback); - } - - public static void findByUserAndGroup(User user, ServerGroup serverGroup, SingleResultCallback callback) { - findByUserAndGroup(user.getId(), serverGroup, callback); - } - - public static void findByUserAndGroup(UUID user, ServerGroup serverGroup, SingleResultCallback callback) { - userMetaCollection.find(new Document("user", user).append("serverGroup", serverGroup.getId())).first(callback); - } - - public UserMetaEntry() {} // For Morphia + public UserMetaEntry() {} // For Jackson public UserMetaEntry(User user, ServerGroup serverGroup, Map data) { this.id = new ObjectId().toString(); diff --git a/src/main/java/net/frozenorb/apiv3/route/GETDump.java b/src/main/java/net/frozenorb/apiv3/route/GETDump.java index c972f87..d90c193 100644 --- a/src/main/java/net/frozenorb/apiv3/route/GETDump.java +++ b/src/main/java/net/frozenorb/apiv3/route/GETDump.java @@ -108,7 +108,6 @@ public final class GETDump implements Handler { return; default: ErrorUtils.respondInvalidInput(ctx, type + " is not a valid type. Not in [ban, blacklist, accessDeniable, grant]"); - return; } } diff --git a/src/main/java/net/frozenorb/apiv3/route/grants/POSTUserGrant.java b/src/main/java/net/frozenorb/apiv3/route/grants/POSTUserGrant.java index 3254651..770970a 100644 --- a/src/main/java/net/frozenorb/apiv3/route/grants/POSTUserGrant.java +++ b/src/main/java/net/frozenorb/apiv3/route/grants/POSTUserGrant.java @@ -53,12 +53,12 @@ public final class POSTUserGrant implements Handler { return; } - Instant expiresAt; + Instant expiresAt = null; try { expiresAt = Instant.ofEpochMilli(Long.parseLong(ctx.request().getParam("expiresAt"))); - } catch (NumberFormatException ex) { - expiresAt = null; + } catch (NumberFormatException ignored) { + // Just leave it null, we don't need an expiration date. } if (expiresAt != null && expiresAt.isBefore(Instant.now())) { diff --git a/src/main/java/net/frozenorb/apiv3/route/ipBans/POSTIpIpBan.java b/src/main/java/net/frozenorb/apiv3/route/ipBans/POSTIpIpBan.java index 81fb1db..7476d60 100644 --- a/src/main/java/net/frozenorb/apiv3/route/ipBans/POSTIpIpBan.java +++ b/src/main/java/net/frozenorb/apiv3/route/ipBans/POSTIpIpBan.java @@ -27,12 +27,12 @@ public final class POSTIpIpBan implements Handler { return; } - Instant expiresAt; + Instant expiresAt = null; try { expiresAt = Instant.ofEpochMilli(Long.parseLong(ctx.request().getParam("expiresAt"))); - } catch (NumberFormatException ex) { - expiresAt = null; + } catch (NumberFormatException ignored) { + // Just leave it null, we don't need an expiration date. } if (expiresAt != null && expiresAt.isBefore(Instant.now())) { diff --git a/src/main/java/net/frozenorb/apiv3/route/punishments/POSTUserPunish.java b/src/main/java/net/frozenorb/apiv3/route/punishments/POSTUserPunish.java index 8c46133..65dd99c 100644 --- a/src/main/java/net/frozenorb/apiv3/route/punishments/POSTUserPunish.java +++ b/src/main/java/net/frozenorb/apiv3/route/punishments/POSTUserPunish.java @@ -43,12 +43,12 @@ public final class POSTUserPunish implements Handler { } } - Instant expiresAt; + Instant expiresAt = null; try { expiresAt = Instant.ofEpochMilli(Long.parseLong(ctx.request().getParam("expiresAt"))); - } catch (NumberFormatException ex) { - expiresAt = null; + } catch (NumberFormatException ignored) { + // Just leave it null, we don't need an expiration date. } if (expiresAt != null && expiresAt.isBefore(Instant.now())) { diff --git a/src/main/java/net/frozenorb/apiv3/route/servers/POSTServerHeartbeat.java b/src/main/java/net/frozenorb/apiv3/route/servers/POSTServerHeartbeat.java index 6bc92e5..2f0fc59 100644 --- a/src/main/java/net/frozenorb/apiv3/route/servers/POSTServerHeartbeat.java +++ b/src/main/java/net/frozenorb/apiv3/route/servers/POSTServerHeartbeat.java @@ -59,19 +59,17 @@ public final class POSTServerHeartbeat implements Handler { } // TODO: ASYNC (MAKE ALL SAVES/INSERTS/ETC USED HERE ASYNC - public Future createInfoResponse(Server server, double tps, Map playerNames) { + private Future createInfoResponse(Server server, double tps, Map playerNames) { Future callback = Future.future(); - server.setPlayers(playerNames.keySet()); - server.setLastTps(tps); - server.setLastUpdatedAt(Instant.now()); + server.receivedHeartbeat(tps, playerNames.keySet()); server.save(); callback.complete(); return callback; } - public Future> createPlayerResponse(Server server, Map playerNames) { + private Future> createPlayerResponse(Server server, Map playerNames) { Future> callback = Future.future(); Future> userLookupCallback = Future.future(); @@ -142,7 +140,7 @@ public final class POSTServerHeartbeat implements Handler { return callback; } - public Future> createPermissionsResponse(ServerGroup serverGroup) { + private Future> createPermissionsResponse(ServerGroup serverGroup) { Future> callback = Future.future(); Map permissionsResponse = new HashMap<>(); @@ -159,7 +157,7 @@ public final class POSTServerHeartbeat implements Handler { return callback; } - public Future> createEventsResponse(List eventsData) { + private Future> createEventsResponse(List eventsData) { Future> callback = Future.future(); for (Object event : eventsData) { @@ -182,7 +180,7 @@ public final class POSTServerHeartbeat implements Handler { return callback; } - public Map extractPlayerNames(Document reqJson) { + private Map extractPlayerNames(Document reqJson) { Map result = new HashMap<>(); for (Object player : (List) reqJson.get("players")) { diff --git a/src/main/java/net/frozenorb/apiv3/route/users/POSTUserLogin.java b/src/main/java/net/frozenorb/apiv3/route/users/POSTUserLogin.java index a09062a..2786a34 100644 --- a/src/main/java/net/frozenorb/apiv3/route/users/POSTUserLogin.java +++ b/src/main/java/net/frozenorb/apiv3/route/users/POSTUserLogin.java @@ -52,8 +52,6 @@ public final class POSTUserLogin implements Handler { user = new User(uuid, username); } - Server actorServer = Server.findById(actor.getName()); - IpLogEntry ipLogEntry = IpLogEntry.findByUserAndIpSync(user, userIp); // We use a little bit more verbose code here to save on the @@ -68,7 +66,7 @@ public final class POSTUserLogin implements Handler { } user.updateUsername(username); - user.getLoginInfo(actorServer, userIp, (loginInfo, error) -> { + user.getLoginInfo(server, userIp, (loginInfo, error) -> { if (error != null) { ErrorUtils.respondInternalError(ctx, error); } else { From 3a836e8db685851875956bc4080f6b8748164962 Mon Sep 17 00:00:00 2001 From: Colin McDonald Date: Fri, 17 Jun 2016 12:41:04 -0400 Subject: [PATCH 15/24] Optimize imports --- .../java/net/frozenorb/apiv3/model/NotificationTemplate.java | 1 - src/main/java/net/frozenorb/apiv3/model/Rank.java | 1 - src/main/java/net/frozenorb/apiv3/model/Server.java | 1 - src/main/java/net/frozenorb/apiv3/model/UserMetaEntry.java | 3 --- .../net/frozenorb/apiv3/route/servers/POSTServerHeartbeat.java | 1 - 5 files changed, 7 deletions(-) diff --git a/src/main/java/net/frozenorb/apiv3/model/NotificationTemplate.java b/src/main/java/net/frozenorb/apiv3/model/NotificationTemplate.java index 9925f03..2dc96b9 100644 --- a/src/main/java/net/frozenorb/apiv3/model/NotificationTemplate.java +++ b/src/main/java/net/frozenorb/apiv3/model/NotificationTemplate.java @@ -3,7 +3,6 @@ package net.frozenorb.apiv3.model; import com.mongodb.async.SingleResultCallback; import com.mongodb.async.client.MongoCollection; import com.mongodb.client.result.DeleteResult; -import com.mongodb.client.result.UpdateResult; import fr.javatic.mongo.jacksonCodec.Entity; import fr.javatic.mongo.jacksonCodec.objectId.Id; import lombok.Getter; diff --git a/src/main/java/net/frozenorb/apiv3/model/Rank.java b/src/main/java/net/frozenorb/apiv3/model/Rank.java index c75f359..605b09c 100644 --- a/src/main/java/net/frozenorb/apiv3/model/Rank.java +++ b/src/main/java/net/frozenorb/apiv3/model/Rank.java @@ -4,7 +4,6 @@ import com.google.common.collect.ImmutableList; import com.google.common.primitives.Ints; import com.mongodb.async.client.MongoCollection; import com.mongodb.client.result.DeleteResult; -import com.mongodb.client.result.UpdateResult; import fr.javatic.mongo.jacksonCodec.Entity; import fr.javatic.mongo.jacksonCodec.objectId.Id; import lombok.Getter; diff --git a/src/main/java/net/frozenorb/apiv3/model/Server.java b/src/main/java/net/frozenorb/apiv3/model/Server.java index 5fd84ec..411743b 100644 --- a/src/main/java/net/frozenorb/apiv3/model/Server.java +++ b/src/main/java/net/frozenorb/apiv3/model/Server.java @@ -7,7 +7,6 @@ import com.mongodb.client.result.UpdateResult; import fr.javatic.mongo.jacksonCodec.Entity; import fr.javatic.mongo.jacksonCodec.objectId.Id; import lombok.Getter; -import lombok.Setter; import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.serialization.gson.ExcludeFromReplies; import net.frozenorb.apiv3.unsorted.BlockingCallback; diff --git a/src/main/java/net/frozenorb/apiv3/model/UserMetaEntry.java b/src/main/java/net/frozenorb/apiv3/model/UserMetaEntry.java index e713b0e..08aec82 100644 --- a/src/main/java/net/frozenorb/apiv3/model/UserMetaEntry.java +++ b/src/main/java/net/frozenorb/apiv3/model/UserMetaEntry.java @@ -1,7 +1,6 @@ package net.frozenorb.apiv3.model; import com.google.common.collect.ImmutableMap; -import com.mongodb.async.SingleResultCallback; import com.mongodb.async.client.MongoCollection; import com.mongodb.client.result.DeleteResult; import com.mongodb.client.result.UpdateResult; @@ -15,8 +14,6 @@ import net.frozenorb.apiv3.util.SyncUtils; import org.bson.Document; import org.bson.types.ObjectId; -import java.util.ArrayList; -import java.util.List; import java.util.Map; import java.util.UUID; diff --git a/src/main/java/net/frozenorb/apiv3/route/servers/POSTServerHeartbeat.java b/src/main/java/net/frozenorb/apiv3/route/servers/POSTServerHeartbeat.java index 2f0fc59..baa664f 100644 --- a/src/main/java/net/frozenorb/apiv3/route/servers/POSTServerHeartbeat.java +++ b/src/main/java/net/frozenorb/apiv3/route/servers/POSTServerHeartbeat.java @@ -16,7 +16,6 @@ import net.frozenorb.apiv3.util.PermissionUtils; import net.frozenorb.apiv3.util.UuidUtils; import org.bson.Document; -import java.time.Instant; import java.util.HashMap; import java.util.List; import java.util.Map; From 63e8683b67610b788ce665a6b187ffb09d559dcd Mon Sep 17 00:00:00 2001 From: Colin McDonald Date: Fri, 17 Jun 2016 14:58:06 -0400 Subject: [PATCH 16/24] Add MaxMind result models, IpIntel model --- .../frozenorb/apiv3/maxmind/MaxMindCity.java | 23 +++++++- .../apiv3/maxmind/MaxMindContinent.java | 21 ++++++- .../apiv3/maxmind/MaxMindCountry.java | 23 +++++++- .../apiv3/maxmind/MaxMindLocation.java | 28 ++++++++- .../apiv3/maxmind/MaxMindPostal.java | 19 +++++- .../maxmind/MaxMindRegisteredCountry.java | 21 ++++++- .../apiv3/maxmind/MaxMindResult.java | 43 ++++++++++---- .../apiv3/maxmind/MaxMindSubdivision.java | 23 +++++++- .../apiv3/maxmind/MaxMindTraits.java | 26 +++++++- .../net/frozenorb/apiv3/model/IpIntel.java | 59 +++++++++++++++++++ .../frozenorb/apiv3/util/MaxMindUtils.java | 10 +++- 11 files changed, 267 insertions(+), 29 deletions(-) create mode 100644 src/main/java/net/frozenorb/apiv3/model/IpIntel.java diff --git a/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindCity.java b/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindCity.java index 83f3afe..0ad52d0 100644 --- a/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindCity.java +++ b/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindCity.java @@ -1,4 +1,23 @@ package net.frozenorb.apiv3.maxmind; -public class MaxMindCity { -} +import lombok.Getter; +import net.frozenorb.apiv3.util.MaxMindUtils; +import org.bson.Document; + +import java.util.Map; + +public final class MaxMindCity { + + @Getter private int confidence; + @Getter private int geonameId; + @Getter private String name; + + public MaxMindCity() {} // For Jackson + + public MaxMindCity(Document legacy) { + this.confidence = legacy.getInteger("confidence"); + this.geonameId = legacy.getInteger("geoname_id"); + this.name = MaxMindUtils.getEnglishName(legacy); + } + +} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindContinent.java b/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindContinent.java index 48f1312..c2e50a1 100644 --- a/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindContinent.java +++ b/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindContinent.java @@ -1,4 +1,21 @@ package net.frozenorb.apiv3.maxmind; -public class MaxMindContinent { -} +import lombok.Getter; +import net.frozenorb.apiv3.util.MaxMindUtils; +import org.bson.Document; + +public final class MaxMindContinent { + + @Getter private String code; + @Getter private int geonameId; + @Getter private String name; + + public MaxMindContinent() {} // For Jackson + + public MaxMindContinent(Document legacy) { + this.code = legacy.getString("code"); + this.geonameId = legacy.getInteger("geoname_id"); + this.name = MaxMindUtils.getEnglishName(legacy); + } + +} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindCountry.java b/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindCountry.java index b6d82c3..66cf4af 100644 --- a/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindCountry.java +++ b/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindCountry.java @@ -1,4 +1,23 @@ package net.frozenorb.apiv3.maxmind; -public class MaxMindCountry { -} +import lombok.Getter; +import net.frozenorb.apiv3.util.MaxMindUtils; +import org.bson.Document; + +public final class MaxMindCountry { + + @Getter private String isoCode; + @Getter private int confidence; + @Getter private int geonameId; + @Getter private String name; + + public MaxMindCountry() {} // For Jackson + + public MaxMindCountry(Document legacy) { + this.isoCode = legacy.getString("iso_code"); + this.confidence = legacy.getInteger("confidence"); + this.geonameId = legacy.getInteger("geoname_id"); + this.name = MaxMindUtils.getEnglishName(legacy); + } + +} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindLocation.java b/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindLocation.java index 1fc0675..a509c50 100644 --- a/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindLocation.java +++ b/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindLocation.java @@ -1,4 +1,28 @@ package net.frozenorb.apiv3.maxmind; -public class MaxMindLocation { -} +import lombok.Getter; +import org.bson.Document; + +public final class MaxMindLocation { + + @Getter private double latitude; + @Getter private double longitude; + @Getter private int accuracyRadius; + @Getter private String timeZone; + @Getter private int populationDensity; + @Getter private int metroCode; + @Getter private int averageIncome; + + public MaxMindLocation() {} // For Jackson + + public MaxMindLocation(Document legacy) { + this.latitude = legacy.getDouble("latitude"); + this.longitude = legacy.getDouble("longitude"); + this.accuracyRadius = legacy.getInteger("accuracy_radius"); + this.timeZone = legacy.getString("time_zone"); + this.populationDensity = legacy.getInteger("population_density"); + this.metroCode = legacy.getInteger("metro_code"); + this.averageIncome = legacy.getInteger("average_income"); + } + +} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindPostal.java b/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindPostal.java index d098f9f..96dc5e5 100644 --- a/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindPostal.java +++ b/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindPostal.java @@ -1,4 +1,19 @@ package net.frozenorb.apiv3.maxmind; -public class MaxMindPostal { -} +import lombok.Getter; +import net.frozenorb.apiv3.util.MaxMindUtils; +import org.bson.Document; + +public final class MaxMindPostal { + + @Getter private String code; + @Getter private int confidence; + + public MaxMindPostal() {} // For Jackson + + public MaxMindPostal(Document legacy) { + this.code = legacy.getString("code"); + this.confidence = legacy.getInteger("confidence"); + } + +} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindRegisteredCountry.java b/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindRegisteredCountry.java index db69f89..07c8b85 100644 --- a/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindRegisteredCountry.java +++ b/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindRegisteredCountry.java @@ -1,4 +1,21 @@ package net.frozenorb.apiv3.maxmind; -public class MaxMindRegisteredCountry { -} +import lombok.Getter; +import net.frozenorb.apiv3.util.MaxMindUtils; +import org.bson.Document; + +public final class MaxMindRegisteredCountry { + + @Getter private String isoCode; + @Getter private int geonameId; + @Getter private String name; + + public MaxMindRegisteredCountry() {} // For Jackson + + public MaxMindRegisteredCountry(Document legacy) { + this.isoCode = legacy.getString("iso_code"); + this.geonameId = legacy.getInteger("geoname_id"); + this.name = MaxMindUtils.getEnglishName(legacy); + } + +} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindResult.java b/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindResult.java index c5ebce2..81024d2 100644 --- a/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindResult.java +++ b/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindResult.java @@ -1,20 +1,41 @@ package net.frozenorb.apiv3.maxmind; +import lombok.Getter; +import org.bson.Document; + import java.time.Instant; +import java.util.ArrayList; +import java.util.List; public final class MaxMindResult { - private String ip; - private Instant lastUpdatedAt; - private MaxMindContinent continent; - private MaxMindCity city; - private MaxMindPostal postal; - private MaxMindTraits traits; - private MaxMindLocation location; - private MaxMindSubdivision[] subdivisions; - private MaxMindCountry country; - private MaxMindRegisteredCountry registeredCountry; + @Getter private MaxMindContinent continent; + @Getter private MaxMindCity city; + @Getter private MaxMindPostal postal; + @Getter private MaxMindTraits traits; + @Getter private MaxMindLocation location; + @Getter private MaxMindSubdivision[] subdivisions; + @Getter private MaxMindCountry country; + @Getter private MaxMindRegisteredCountry registeredCountry; - public MaxMindResult() {} + public MaxMindResult() {} // For Jackson + + public MaxMindResult(Document legacy) { + this.continent = new MaxMindContinent((Document) legacy.get("continent")); + this.city = new MaxMindCity((Document) legacy.get("city")); + this.postal = new MaxMindPostal((Document) legacy.get("postal")); + this.traits = new MaxMindTraits((Document) legacy.get("traits")); + this.location = new MaxMindLocation((Document) legacy.get("location")); + this.country = new MaxMindCountry((Document) legacy.get("country")); + this.registeredCountry = new MaxMindRegisteredCountry((Document) legacy.get("registered_country")); + + List subdivisions = new ArrayList<>(); + + for (Object subdivision : (List) legacy.get("subdivisions")) { + subdivisions.add(new MaxMindSubdivision((Document) subdivision)); + } + + this.subdivisions = subdivisions.toArray(new MaxMindSubdivision[subdivisions.size()]); + } } \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindSubdivision.java b/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindSubdivision.java index 7576f98..80185f5 100644 --- a/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindSubdivision.java +++ b/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindSubdivision.java @@ -1,4 +1,23 @@ package net.frozenorb.apiv3.maxmind; -public class MaxMindSubdivision { -} +import lombok.Getter; +import net.frozenorb.apiv3.util.MaxMindUtils; +import org.bson.Document; + +public final class MaxMindSubdivision { + + @Getter private String isoCode; + @Getter private int confidence; + @Getter private int geonameId; + @Getter private String name; + + public MaxMindSubdivision() {} // For Jackson + + public MaxMindSubdivision(Document legacy) { + this.isoCode = legacy.getString("iso_code"); + this.confidence = legacy.getInteger("confidence"); + this.geonameId = legacy.getInteger("geoname_id"); + this.name = MaxMindUtils.getEnglishName(legacy); + } + +} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindTraits.java b/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindTraits.java index 986b8f2..c8edf7d 100644 --- a/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindTraits.java +++ b/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindTraits.java @@ -1,4 +1,26 @@ package net.frozenorb.apiv3.maxmind; -public class MaxMindTraits { -} +import lombok.Getter; +import org.bson.Document; + +public final class MaxMindTraits { + + @Getter private String isp; + @Getter private String domain; + @Getter private int asn; + @Getter private String asnOrganization; + @Getter private String type; // TODO: MAKE ENUM + @Getter private String organization; + + public MaxMindTraits() {} // For Jackson + + public MaxMindTraits(Document legacy) { + this.isp = legacy.getString("isp"); + this.domain = legacy.getString("domain"); + this.asn = legacy.getInteger("autonomous_system_number"); + this.asnOrganization = legacy.getString("autonomous_system_organization"); + this.type = legacy.getString("user_type"); + this.organization = legacy.getString("organization"); + } + +} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/model/IpIntel.java b/src/main/java/net/frozenorb/apiv3/model/IpIntel.java new file mode 100644 index 0000000..de83aa9 --- /dev/null +++ b/src/main/java/net/frozenorb/apiv3/model/IpIntel.java @@ -0,0 +1,59 @@ +package net.frozenorb.apiv3.model; + +import com.mongodb.async.SingleResultCallback; +import com.mongodb.async.client.MongoCollection; +import fr.javatic.mongo.jacksonCodec.Entity; +import fr.javatic.mongo.jacksonCodec.objectId.Id; +import lombok.AllArgsConstructor; +import lombok.Getter; +import net.frozenorb.apiv3.APIv3; +import net.frozenorb.apiv3.maxmind.MaxMindResult; +import net.frozenorb.apiv3.unsorted.BlockingCallback; +import net.frozenorb.apiv3.util.SyncUtils; +import org.bson.Document; + +import java.time.Instant; +import java.util.ArrayList; +import java.util.List; + +@Entity +@AllArgsConstructor +public final class IpIntel { + + private static final MongoCollection ipIntelCollection = APIv3.getDatabase().getCollection("ipIntel", IpIntel.class); + + @Getter @Id private String id; + @Getter private Instant lastUpdatedAt; + @Getter private MaxMindResult result; + + public static List findAllSync() { + return SyncUtils.blockMulti(ipIntelCollection.find().sort(new Document("lastSeenAt", -1))); + } + + public static IpIntel findByIdSync(String id) { + return SyncUtils.blockOne(ipIntelCollection.find(new Document("_id", id))); + } + + public static void findAll(SingleResultCallback> callback) { + ipIntelCollection.find().sort(new Document("lastSeenAt", -1)).into(new ArrayList<>(), callback); + } + + public static void findById(String id, SingleResultCallback callback) { + ipIntelCollection.find(new Document("_id", id)).first(callback); + } + + public IpIntel() {} // For Jackson + + public IpIntel(String ip, MaxMindResult result) { + this.id = ip; + this.lastUpdatedAt = Instant.now(); + this.result = result; + } + + public void insert() { + BlockingCallback callback = new BlockingCallback<>(); + ipIntelCollection.insertOne(this, callback); + callback.get(); + } + +} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/util/MaxMindUtils.java b/src/main/java/net/frozenorb/apiv3/util/MaxMindUtils.java index c96e877..80d1464 100644 --- a/src/main/java/net/frozenorb/apiv3/util/MaxMindUtils.java +++ b/src/main/java/net/frozenorb/apiv3/util/MaxMindUtils.java @@ -4,9 +4,11 @@ import com.google.common.base.Charsets; import com.mongodb.async.SingleResultCallback; import lombok.experimental.UtilityClass; import net.frozenorb.apiv3.APIv3; +import net.frozenorb.apiv3.maxmind.MaxMindResult; import org.bson.Document; import java.util.Base64; +import java.util.Map; @UtilityClass public class MaxMindUtils { @@ -14,12 +16,12 @@ public class MaxMindUtils { private static final String maxMindUserId = APIv3.getConfig().getProperty("maxMind.userId"); private static final String maxMindLicenseKey = APIv3.getConfig().getProperty("maxMind.maxMindLicenseKey"); - public static void getInsights(String ip, SingleResultCallback callback) { + public static void getInsights(String ip, SingleResultCallback callback) { // We have to specifically use getHttpSClient(), vertx's http client is dumb. APIv3.getHttpsClient().get(443, "geoip.maxmind.com", "/geoip/v2.1/insights/" + ip, (response) -> { response.bodyHandler((body) -> { Document resJson = Document.parse(body.toString()); - callback.onResult(resJson, null); + callback.onResult(new MaxMindResult(ip, resJson), null); }); response.exceptionHandler((error) -> { @@ -28,4 +30,8 @@ public class MaxMindUtils { }).putHeader("Authorization", "Basic " + Base64.getEncoder().encodeToString((maxMindUserId + ":" + maxMindLicenseKey).getBytes(Charsets.UTF_8))).end(); } + public static String getEnglishName(Document source) { + return ((Map) source.get("names")).get("en"); + } + } \ No newline at end of file From 2db90eedd507839ce1965552a0a0587c8ae44f2e Mon Sep 17 00:00:00 2001 From: Colin McDonald Date: Fri, 17 Jun 2016 14:58:56 -0400 Subject: [PATCH 17/24] Make MaxMindUtils compile properly --- src/main/java/net/frozenorb/apiv3/util/MaxMindUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/net/frozenorb/apiv3/util/MaxMindUtils.java b/src/main/java/net/frozenorb/apiv3/util/MaxMindUtils.java index 80d1464..729e9c5 100644 --- a/src/main/java/net/frozenorb/apiv3/util/MaxMindUtils.java +++ b/src/main/java/net/frozenorb/apiv3/util/MaxMindUtils.java @@ -21,7 +21,7 @@ public class MaxMindUtils { APIv3.getHttpsClient().get(443, "geoip.maxmind.com", "/geoip/v2.1/insights/" + ip, (response) -> { response.bodyHandler((body) -> { Document resJson = Document.parse(body.toString()); - callback.onResult(new MaxMindResult(ip, resJson), null); + callback.onResult(new MaxMindResult(resJson), null); }); response.exceptionHandler((error) -> { From 0974d2e20ff217b35f3aaf2fea167b522f2fa126 Mon Sep 17 00:00:00 2001 From: Colin McDonald Date: Fri, 17 Jun 2016 14:59:10 -0400 Subject: [PATCH 18/24] Optimize imports --- src/main/java/net/frozenorb/apiv3/maxmind/MaxMindCity.java | 2 -- src/main/java/net/frozenorb/apiv3/maxmind/MaxMindPostal.java | 1 - src/main/java/net/frozenorb/apiv3/maxmind/MaxMindResult.java | 1 - 3 files changed, 4 deletions(-) diff --git a/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindCity.java b/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindCity.java index 0ad52d0..a5cdb35 100644 --- a/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindCity.java +++ b/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindCity.java @@ -4,8 +4,6 @@ import lombok.Getter; import net.frozenorb.apiv3.util.MaxMindUtils; import org.bson.Document; -import java.util.Map; - public final class MaxMindCity { @Getter private int confidence; diff --git a/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindPostal.java b/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindPostal.java index 96dc5e5..d6fd924 100644 --- a/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindPostal.java +++ b/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindPostal.java @@ -1,7 +1,6 @@ package net.frozenorb.apiv3.maxmind; import lombok.Getter; -import net.frozenorb.apiv3.util.MaxMindUtils; import org.bson.Document; public final class MaxMindPostal { diff --git a/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindResult.java b/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindResult.java index 81024d2..55e272d 100644 --- a/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindResult.java +++ b/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindResult.java @@ -3,7 +3,6 @@ package net.frozenorb.apiv3.maxmind; import lombok.Getter; import org.bson.Document; -import java.time.Instant; import java.util.ArrayList; import java.util.List; From 99b30ef290cc2f97b3f3021be5bd7fb3cf3c8b95 Mon Sep 17 00:00:00 2001 From: Colin McDonald Date: Fri, 17 Jun 2016 19:16:03 -0400 Subject: [PATCH 19/24] Map coowner -> owner in grant conversions --- .../frozenorb/apiv3/dataImport/converters/GrantConverter.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/net/frozenorb/apiv3/dataImport/converters/GrantConverter.java b/src/main/java/net/frozenorb/apiv3/dataImport/converters/GrantConverter.java index 085618a..22e5715 100644 --- a/src/main/java/net/frozenorb/apiv3/dataImport/converters/GrantConverter.java +++ b/src/main/java/net/frozenorb/apiv3/dataImport/converters/GrantConverter.java @@ -38,6 +38,8 @@ public final class GrantConverter implements Block { rank = "high-roller"; } else if (rank.equalsIgnoreCase("dev")) { rank = "developer"; + } else if (rank.equalsIgnoreCase("coowner")) { + rank = "owner"; } Grant created = new Grant( From b98dcd0a8383511bf010a6f5c8f3ae472a16cb1c Mon Sep 17 00:00:00 2001 From: Colin McDonald Date: Fri, 17 Jun 2016 19:17:50 -0400 Subject: [PATCH 20/24] Change /user/:id/verifyPassword from a GET to a POST (this will probably be undone later) --- src/main/java/net/frozenorb/apiv3/APIv3.java | 5 ++++- ...ETUserVerifyPassword.java => POSTUserVerifyPassword.java} | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) rename src/main/java/net/frozenorb/apiv3/route/users/{GETUserVerifyPassword.java => POSTUserVerifyPassword.java} (82%) diff --git a/src/main/java/net/frozenorb/apiv3/APIv3.java b/src/main/java/net/frozenorb/apiv3/APIv3.java index e8bfda5..9f205e0 100644 --- a/src/main/java/net/frozenorb/apiv3/APIv3.java +++ b/src/main/java/net/frozenorb/apiv3/APIv3.java @@ -43,6 +43,7 @@ import net.frozenorb.apiv3.route.auditLog.POSTUserAuditLogEntry; import net.frozenorb.apiv3.route.chatFilterList.GETChatFilterList; import net.frozenorb.apiv3.route.grants.*; import net.frozenorb.apiv3.route.ipBans.*; +import net.frozenorb.apiv3.route.ipIntel.GETIpIntel; import net.frozenorb.apiv3.route.ipLog.GETUserIpLog; import net.frozenorb.apiv3.route.notificationTemplates.DELETENotificationTemplate; import net.frozenorb.apiv3.route.notificationTemplates.GETNotificationTemplate; @@ -261,6 +262,8 @@ public final class APIv3 extends AbstractVerticle { mainRouter.post("/ip/:id/ipBan").blockingHandler(new POSTIpIpBan(), false); mainRouter.delete("/ipBan/:id").blockingHandler(new DELETEIpBan(), false); + mainRouter.get("/ip/:id/intel").handler(new GETIpIntel()); + mainRouter.get("/user/:id/ipLog").handler(new GETUserIpLog()); mainRouter.get("/notificationTemplate/:id").handler(new GETNotificationTemplate()); @@ -301,7 +304,7 @@ public final class APIv3 extends AbstractVerticle { mainRouter.get("/user/:id/meta/:serverGroup").blockingHandler(new GETUserMeta(), false); mainRouter.get("/user/:id/permissions").blockingHandler(new GETUserPermissions(), false); mainRouter.get("/user/:id/requiresTOTP").blockingHandler(new GETUserRequiresTOTP(), false); - mainRouter.get("/user/:id/verifyPassword").blockingHandler(new GETUserVerifyPassword(), false); + mainRouter.post("/user/:id/verifyPassword").blockingHandler(new POSTUserVerifyPassword(), false); mainRouter.post("/user/confirmRegister/:emailToken").blockingHandler(new POSTUserConfirmRegister(), false); mainRouter.post("/user/:id/leave").handler(new POSTUserLeave()); mainRouter.post("/user/:id/login").blockingHandler(new POSTUserLogin()); diff --git a/src/main/java/net/frozenorb/apiv3/route/users/GETUserVerifyPassword.java b/src/main/java/net/frozenorb/apiv3/route/users/POSTUserVerifyPassword.java similarity index 82% rename from src/main/java/net/frozenorb/apiv3/route/users/GETUserVerifyPassword.java rename to src/main/java/net/frozenorb/apiv3/route/users/POSTUserVerifyPassword.java index 5de1c1a..a609762 100644 --- a/src/main/java/net/frozenorb/apiv3/route/users/GETUserVerifyPassword.java +++ b/src/main/java/net/frozenorb/apiv3/route/users/POSTUserVerifyPassword.java @@ -7,7 +7,7 @@ import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.model.User; import net.frozenorb.apiv3.util.ErrorUtils; -public final class GETUserVerifyPassword implements Handler { +public final class POSTUserVerifyPassword implements Handler { public void handle(RoutingContext ctx) { User user = User.findByIdSync(ctx.request().getParam("id")); @@ -22,7 +22,7 @@ public final class GETUserVerifyPassword implements Handler { return; } - boolean authorized = user.checkPassword(ctx.request().getParam("password")); + boolean authorized = user.checkPassword(ctx.getBodyAsJson().getString("password")); APIv3.respondJson(ctx, ImmutableMap.of( "authorized", authorized From 8d77802cde763eda3fcab05009f4b5c1438218da Mon Sep 17 00:00:00 2001 From: Colin McDonald Date: Fri, 17 Jun 2016 19:18:27 -0400 Subject: [PATCH 21/24] Print stack traces for internal errors --- src/main/java/net/frozenorb/apiv3/util/ErrorUtils.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/net/frozenorb/apiv3/util/ErrorUtils.java b/src/main/java/net/frozenorb/apiv3/util/ErrorUtils.java index ebb7dbd..254102f 100644 --- a/src/main/java/net/frozenorb/apiv3/util/ErrorUtils.java +++ b/src/main/java/net/frozenorb/apiv3/util/ErrorUtils.java @@ -21,6 +21,7 @@ public class ErrorUtils { } public static void respondInternalError(RoutingContext ctx, Throwable error) { + error.printStackTrace(); respondGeneric(ctx, 500, "Internal error: " + error.getClass().getSimpleName()); } From 22f3b3dd7274d6a4ae1a26ee93b9ed3b6478404e Mon Sep 17 00:00:00 2001 From: Colin McDonald Date: Fri, 17 Jun 2016 19:19:01 -0400 Subject: [PATCH 22/24] Implement MaxMind data into the API, add GET /ip/:id/intel. Closes #3 --- .../apiv3/maxmind/MaxMindLocation.java | 6 +-- .../apiv3/maxmind/MaxMindPostal.java | 6 +++ .../apiv3/maxmind/MaxMindSubdivision.java | 2 +- .../net/frozenorb/apiv3/model/IpIntel.java | 41 +++++++++++++------ .../apiv3/route/ipIntel/GETIPIntel.java | 29 +++++++++++++ 5 files changed, 67 insertions(+), 17 deletions(-) create mode 100644 src/main/java/net/frozenorb/apiv3/route/ipIntel/GETIPIntel.java diff --git a/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindLocation.java b/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindLocation.java index a509c50..f896123 100644 --- a/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindLocation.java +++ b/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindLocation.java @@ -20,9 +20,9 @@ public final class MaxMindLocation { this.longitude = legacy.getDouble("longitude"); this.accuracyRadius = legacy.getInteger("accuracy_radius"); this.timeZone = legacy.getString("time_zone"); - this.populationDensity = legacy.getInteger("population_density"); - this.metroCode = legacy.getInteger("metro_code"); - this.averageIncome = legacy.getInteger("average_income"); + this.populationDensity = legacy.getInteger("population_density", -1); + this.metroCode = legacy.getInteger("metro_code", -1); // Metro codes are US only + this.averageIncome = legacy.getInteger("average_income", -1); } } \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindPostal.java b/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindPostal.java index d6fd924..31247d9 100644 --- a/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindPostal.java +++ b/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindPostal.java @@ -12,6 +12,12 @@ public final class MaxMindPostal { public MaxMindPostal(Document legacy) { this.code = legacy.getString("code"); + + // Postal codes aren't guaranteed to exist for all areas + if (code == null) { + code = ""; + } + this.confidence = legacy.getInteger("confidence"); } diff --git a/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindSubdivision.java b/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindSubdivision.java index 80185f5..ae28e47 100644 --- a/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindSubdivision.java +++ b/src/main/java/net/frozenorb/apiv3/maxmind/MaxMindSubdivision.java @@ -15,7 +15,7 @@ public final class MaxMindSubdivision { public MaxMindSubdivision(Document legacy) { this.isoCode = legacy.getString("iso_code"); - this.confidence = legacy.getInteger("confidence"); + this.confidence = legacy.getInteger("confidence", -1); this.geonameId = legacy.getInteger("geoname_id"); this.name = MaxMindUtils.getEnglishName(legacy); } diff --git a/src/main/java/net/frozenorb/apiv3/model/IpIntel.java b/src/main/java/net/frozenorb/apiv3/model/IpIntel.java index de83aa9..1dc550b 100644 --- a/src/main/java/net/frozenorb/apiv3/model/IpIntel.java +++ b/src/main/java/net/frozenorb/apiv3/model/IpIntel.java @@ -8,8 +8,7 @@ import lombok.AllArgsConstructor; import lombok.Getter; import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.maxmind.MaxMindResult; -import net.frozenorb.apiv3.unsorted.BlockingCallback; -import net.frozenorb.apiv3.util.SyncUtils; +import net.frozenorb.apiv3.util.MaxMindUtils; import org.bson.Document; import java.time.Instant; @@ -26,14 +25,6 @@ public final class IpIntel { @Getter private Instant lastUpdatedAt; @Getter private MaxMindResult result; - public static List findAllSync() { - return SyncUtils.blockMulti(ipIntelCollection.find().sort(new Document("lastSeenAt", -1))); - } - - public static IpIntel findByIdSync(String id) { - return SyncUtils.blockOne(ipIntelCollection.find(new Document("_id", id))); - } - public static void findAll(SingleResultCallback> callback) { ipIntelCollection.find().sort(new Document("lastSeenAt", -1)).into(new ArrayList<>(), callback); } @@ -42,6 +33,32 @@ public final class IpIntel { ipIntelCollection.find(new Document("_id", id)).first(callback); } + public static void findByIdOrInsert(String id, SingleResultCallback callback) { + findById(id, (existingIpIntel, error) -> { + if (error != null) { + callback.onResult(null, error); + } else if (existingIpIntel != null) { + callback.onResult(existingIpIntel, null); + } else { + MaxMindUtils.getInsights(id, (maxMindResult, error2) -> { + if (error2 != null) { + callback.onResult(null, error2); + } else { + IpIntel newIpIntel = new IpIntel(id, maxMindResult); + + newIpIntel.insert((ignored, error3) -> { + if (error3 != null) { + callback.onResult(null, error3); + } else { + callback.onResult(newIpIntel, null); + } + }); + } + }); + } + }); + } + public IpIntel() {} // For Jackson public IpIntel(String ip, MaxMindResult result) { @@ -50,10 +67,8 @@ public final class IpIntel { this.result = result; } - public void insert() { - BlockingCallback callback = new BlockingCallback<>(); + public void insert(SingleResultCallback callback) { ipIntelCollection.insertOne(this, callback); - callback.get(); } } \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/route/ipIntel/GETIPIntel.java b/src/main/java/net/frozenorb/apiv3/route/ipIntel/GETIPIntel.java new file mode 100644 index 0000000..2678e7f --- /dev/null +++ b/src/main/java/net/frozenorb/apiv3/route/ipIntel/GETIPIntel.java @@ -0,0 +1,29 @@ +package net.frozenorb.apiv3.route.ipIntel; + +import io.vertx.core.Handler; +import io.vertx.ext.web.RoutingContext; +import net.frozenorb.apiv3.APIv3; +import net.frozenorb.apiv3.model.IpIntel; +import net.frozenorb.apiv3.util.ErrorUtils; +import net.frozenorb.apiv3.util.IpUtils; + +public final class GETIpIntel implements Handler { + + public void handle(RoutingContext ctx) { + String userIp = ctx.request().getParam("id"); + + if (!IpUtils.isValidIp(userIp)) { + ErrorUtils.respondInvalidInput(ctx, "Ip address \"" + userIp + "\" is not valid."); + return; + } + + IpIntel.findByIdOrInsert(userIp, (ipIntel, error) -> { + if (error != null) { + ErrorUtils.respondInternalError(ctx, error); + } else { + APIv3.respondJson(ctx, ipIntel); + } + }); + } + +} \ No newline at end of file From 9054974f55756fab8cff74d69da26866af58ee91 Mon Sep 17 00:00:00 2001 From: Colin McDonald Date: Fri, 17 Jun 2016 20:47:59 -0400 Subject: [PATCH 23/24] Change /user/:id/verifyPassword back to a GET, allow email+username as ids as well --- src/main/java/net/frozenorb/apiv3/APIv3.java | 2 +- src/main/java/net/frozenorb/apiv3/model/User.java | 4 ++++ ...erifyPassword.java => GETUserVerifyPassword.java} | 12 ++++++++++-- 3 files changed, 15 insertions(+), 3 deletions(-) rename src/main/java/net/frozenorb/apiv3/route/users/{POSTUserVerifyPassword.java => GETUserVerifyPassword.java} (67%) diff --git a/src/main/java/net/frozenorb/apiv3/APIv3.java b/src/main/java/net/frozenorb/apiv3/APIv3.java index 9f205e0..767360a 100644 --- a/src/main/java/net/frozenorb/apiv3/APIv3.java +++ b/src/main/java/net/frozenorb/apiv3/APIv3.java @@ -304,7 +304,7 @@ public final class APIv3 extends AbstractVerticle { mainRouter.get("/user/:id/meta/:serverGroup").blockingHandler(new GETUserMeta(), false); mainRouter.get("/user/:id/permissions").blockingHandler(new GETUserPermissions(), false); mainRouter.get("/user/:id/requiresTOTP").blockingHandler(new GETUserRequiresTOTP(), false); - mainRouter.post("/user/:id/verifyPassword").blockingHandler(new POSTUserVerifyPassword(), false); + mainRouter.get("/user/:id/verifyPassword").blockingHandler(new GETUserVerifyPassword(), false); mainRouter.post("/user/confirmRegister/:emailToken").blockingHandler(new POSTUserConfirmRegister(), false); mainRouter.post("/user/:id/leave").handler(new POSTUserLeave()); mainRouter.post("/user/:id/login").blockingHandler(new POSTUserLogin()); diff --git a/src/main/java/net/frozenorb/apiv3/model/User.java b/src/main/java/net/frozenorb/apiv3/model/User.java index 12bde14..f34e1fc 100644 --- a/src/main/java/net/frozenorb/apiv3/model/User.java +++ b/src/main/java/net/frozenorb/apiv3/model/User.java @@ -70,6 +70,10 @@ public final class User { } } + public static User findByEmailSync(String email) { + return SyncUtils.blockOne(usersCollection.find(new Document("email", email))); + } + public static User findByEmailTokenSync(String emailToken) { return SyncUtils.blockOne(usersCollection.find(new Document("emailToken", emailToken))); } diff --git a/src/main/java/net/frozenorb/apiv3/route/users/POSTUserVerifyPassword.java b/src/main/java/net/frozenorb/apiv3/route/users/GETUserVerifyPassword.java similarity index 67% rename from src/main/java/net/frozenorb/apiv3/route/users/POSTUserVerifyPassword.java rename to src/main/java/net/frozenorb/apiv3/route/users/GETUserVerifyPassword.java index a609762..e365042 100644 --- a/src/main/java/net/frozenorb/apiv3/route/users/POSTUserVerifyPassword.java +++ b/src/main/java/net/frozenorb/apiv3/route/users/GETUserVerifyPassword.java @@ -7,11 +7,19 @@ import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.model.User; import net.frozenorb.apiv3.util.ErrorUtils; -public final class POSTUserVerifyPassword implements Handler { +public final class GETUserVerifyPassword implements Handler { public void handle(RoutingContext ctx) { User user = User.findByIdSync(ctx.request().getParam("id")); + if (user == null) { + user = User.findByLastUsernameSync(ctx.request().getParam("id")); + } + + if (user == null) { + user = User.findByEmailSync(ctx.request().getParam("id")); + } + if (user == null) { ErrorUtils.respondNotFound(ctx, "User", ctx.request().getParam("id")); return; @@ -22,7 +30,7 @@ public final class POSTUserVerifyPassword implements Handler { return; } - boolean authorized = user.checkPassword(ctx.getBodyAsJson().getString("password")); + boolean authorized = user.checkPassword(ctx.request().getParam("password")); APIv3.respondJson(ctx, ImmutableMap.of( "authorized", authorized From 56487b04a7693ef4315d58582ebb2ed46359ddff Mon Sep 17 00:00:00 2001 From: Colin McDonald Date: Sat, 18 Jun 2016 10:03:31 -0400 Subject: [PATCH 24/24] Add loader.io verification --- src/main/java/net/frozenorb/apiv3/APIv3.java | 2 ++ .../apiv3/handler/LoaderIoHandler.java | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 src/main/java/net/frozenorb/apiv3/handler/LoaderIoHandler.java diff --git a/src/main/java/net/frozenorb/apiv3/APIv3.java b/src/main/java/net/frozenorb/apiv3/APIv3.java index 767360a..eb34d45 100644 --- a/src/main/java/net/frozenorb/apiv3/APIv3.java +++ b/src/main/java/net/frozenorb/apiv3/APIv3.java @@ -33,6 +33,7 @@ import lombok.Getter; import lombok.extern.slf4j.Slf4j; import net.frozenorb.apiv3.handler.ActorAttributeHandler; import net.frozenorb.apiv3.handler.AuthorizationHandler; +import net.frozenorb.apiv3.handler.LoaderIoHandler; import net.frozenorb.apiv3.route.GETDump; import net.frozenorb.apiv3.route.GETWhoAmI; import net.frozenorb.apiv3.route.POSTMetrics; @@ -235,6 +236,7 @@ public final class APIv3 extends AbstractVerticle { HttpServer webServer = vertx.createHttpServer(); Router mainRouter = Router.router(vertx); + mainRouter.route().handler(new LoaderIoHandler()); mainRouter.route().handler(new ActorAttributeHandler()); mainRouter.route().handler(new AuthorizationHandler()); mainRouter.route().handler(LoggerHandler.create(LoggerFormat.TINY)); diff --git a/src/main/java/net/frozenorb/apiv3/handler/LoaderIoHandler.java b/src/main/java/net/frozenorb/apiv3/handler/LoaderIoHandler.java new file mode 100644 index 0000000..8c46e06 --- /dev/null +++ b/src/main/java/net/frozenorb/apiv3/handler/LoaderIoHandler.java @@ -0,0 +1,19 @@ +package net.frozenorb.apiv3.handler; + +import io.vertx.core.Handler; +import io.vertx.ext.web.RoutingContext; + +public final class LoaderIoHandler implements Handler { + + @Override + public void handle(RoutingContext ctx) { + String path = ctx.request().path().replace("/", ""); + + if (path.equals("loaderio-1c81aa574f79c573e7220e15e30a96aa")) { + ctx.response().end(path); + } else { + ctx.next(); + } + } + +} \ No newline at end of file