From 7d626afa204101eaf2d5647990a9b436490a6022 Mon Sep 17 00:00:00 2001 From: Colin McDonald Date: Wed, 1 Jun 2016 14:23:37 -0400 Subject: [PATCH] Vertx prototyping --- pom.xml | 11 +++++-- src/main/java/net/frozenorb/apiv3/APIv3.java | 13 ++++++-- .../apiv3/filters/ActorAttributeFilter.java | 2 +- .../apiv3/filters/AuthorizationFilter.java | 2 +- .../apiv3/filters/LoggingFilter.java | 2 +- .../java/net/frozenorb/apiv3/models/Rank.java | 5 ++- .../net/frozenorb/apiv3/routes/GETDump.java | 14 ++++---- .../net/frozenorb/apiv3/routes/GETWhoAmI.java | 9 ++---- .../net/frozenorb/apiv3/routes/NotFound.java | 9 ++---- .../frozenorb/apiv3/routes/POSTMetrics.java | 7 ++-- .../announcements/GETAnnouncements.java | 19 +++++------ .../apiv3/routes/auditLog/GETAuditLog.java | 17 +++++----- .../chatFilterList/GETChatFilterList.java | 13 ++++---- .../apiv3/routes/grants/DELETEGrant.java | 32 +++++++++++-------- .../apiv3/routes/grants/GETGrant.java | 12 +++---- .../apiv3/routes/grants/GETGrants.java | 17 +++++----- .../apiv3/routes/grants/GETUserGrants.java | 17 +++++----- .../apiv3/routes/grants/POSTUserGrant.java | 29 ++++++++--------- .../apiv3/routes/ipLog/GETUserIPLog.java | 17 +++++----- .../DELETENotificationTemplate.java | 17 +++++----- .../GETNotificationTemplate.java | 12 +++---- .../GETNotificationTemplates.java | 12 +++---- .../POSTNotificationTemplate.java | 17 +++++----- .../routes/punishments/DELETEPunishment.java | 23 ++++++------- .../routes/punishments/GETPunishment.java | 9 ++---- .../routes/punishments/GETPunishments.java | 13 +++----- .../punishments/GETUserPunishments.java | 11 +++---- .../routes/punishments/POSTUserPunish.java | 27 +++++++--------- .../apiv3/routes/ranks/DELETERank.java | 11 +++---- .../frozenorb/apiv3/routes/ranks/GETRank.java | 9 ++---- .../apiv3/routes/ranks/GETRanks.java | 7 ++-- .../apiv3/routes/ranks/POSTRank.java | 19 +++++------ .../serverGroups/DELETEServerGroup.java | 11 +++---- .../routes/serverGroups/GETServerGroup.java | 9 ++---- .../routes/serverGroups/GETServerGroups.java | 7 ++-- .../routes/serverGroups/POSTServerGroup.java | 13 +++----- .../apiv3/routes/servers/DELETEServer.java | 11 +++---- .../apiv3/routes/servers/GETServer.java | 9 ++---- .../apiv3/routes/servers/GETServers.java | 7 ++-- .../apiv3/routes/servers/POSTServer.java | 21 ++++++------ .../routes/servers/POSTServerHeartbeat.java | 11 +++---- .../apiv3/routes/users/DELETEUserMeta.java | 15 ++++----- .../routes/users/DELETEUserPunishment.java | 23 ++++++------- .../apiv3/routes/users/GETStaff.java | 7 ++-- .../frozenorb/apiv3/routes/users/GETUser.java | 9 ++---- .../apiv3/routes/users/GETUserDetails.java | 11 +++---- .../apiv3/routes/users/GETUserMeta.java | 15 ++++----- .../routes/users/GETUserRequiresTOTP.java | 15 ++++----- .../routes/users/GETUserVerifyPassword.java | 15 ++++----- .../routes/users/POSTUserConfirmRegister.java | 13 +++----- .../apiv3/routes/users/POSTUserLeave.java | 7 ++-- .../apiv3/routes/users/POSTUserLogin.java | 22 ++++++------- .../apiv3/routes/users/POSTUserNotify.java | 15 ++++----- .../apiv3/routes/users/POSTUserRegister.java | 15 ++++----- .../apiv3/routes/users/POSTUserSetupTOTP.java | 13 +++----- .../routes/users/POSTUserVerifyTOTP.java | 19 +++++------ .../apiv3/routes/users/PUTUserMeta.java | 15 ++++----- .../net/frozenorb/apiv3/utils/ErrorUtils.java | 26 +++++++-------- 58 files changed, 348 insertions(+), 440 deletions(-) diff --git a/pom.xml b/pom.xml index 931f5cc..e8340c0 100644 --- a/pom.xml +++ b/pom.xml @@ -59,9 +59,14 @@ - com.sparkjava - spark-core - 2.5 + io.vertx + vertx-core + LATEST + + + io.vertx + vertx-web + LATEST com.google.guava diff --git a/src/main/java/net/frozenorb/apiv3/APIv3.java b/src/main/java/net/frozenorb/apiv3/APIv3.java index 5841fd3..41e0d09 100644 --- a/src/main/java/net/frozenorb/apiv3/APIv3.java +++ b/src/main/java/net/frozenorb/apiv3/APIv3.java @@ -10,11 +10,16 @@ import com.mongodb.*; import com.mongodb.client.MongoDatabase; import com.timgroup.statsd.NonBlockingStatsDClient; import com.timgroup.statsd.StatsDClient; +import io.vertx.ext.web.Router; +import io.vertx.ext.web.RoutingContext; import lombok.Getter; import lombok.extern.slf4j.Slf4j; import net.frozenorb.apiv3.actors.ActorType; import net.frozenorb.apiv3.filters.*; -import net.frozenorb.apiv3.models.*; +import net.frozenorb.apiv3.models.Grant; +import net.frozenorb.apiv3.models.IPLogEntry; +import net.frozenorb.apiv3.models.Punishment; +import net.frozenorb.apiv3.models.User; import net.frozenorb.apiv3.routes.GETDump; import net.frozenorb.apiv3.routes.GETWhoAmI; import net.frozenorb.apiv3.routes.NotFound; @@ -45,7 +50,6 @@ import net.frozenorb.apiv3.serialization.ObjectIdTypeAdapter; import net.frozenorb.apiv3.unsorted.BugsnagSLF4JLogger; import net.frozenorb.apiv3.unsorted.LoggingExceptionHandler; import net.frozenorb.apiv3.utils.IPUtils; -import net.frozenorb.apiv3.utils.PermissionUtils; import net.frozenorb.apiv3.utils.UUIDUtils; import org.bson.Document; import org.bson.types.ObjectId; @@ -157,6 +161,7 @@ public final class APIv3 { } private void setupHttp() { + Router.router() ipAddress(config.getProperty("http.address")); port(Integer.parseInt(config.getProperty("http.port"))); String workerThreads = config.getProperty("http.workerThreads"); @@ -245,6 +250,10 @@ public final class APIv3 { delete("/*", new NotFound(), gson::toJson); } + public static void respond(RoutingContext ctx, Object response) { + ctx.response().end(gson.toJson(response)); + } + private void convertData(String oldIp, boolean forReal) { // A lot of unneeded .toString()'s and cloning objects is our ghetto null validation. MongoDatabase importFrom = new MongoClient(oldIp).getDatabase("minehq"); diff --git a/src/main/java/net/frozenorb/apiv3/filters/ActorAttributeFilter.java b/src/main/java/net/frozenorb/apiv3/filters/ActorAttributeFilter.java index b0b6a95..293e754 100644 --- a/src/main/java/net/frozenorb/apiv3/filters/ActorAttributeFilter.java +++ b/src/main/java/net/frozenorb/apiv3/filters/ActorAttributeFilter.java @@ -63,7 +63,7 @@ public final class ActorAttributeFilter implements Filter { Server server = Server.byId(split[1]); if (server == null) { - Spark.halt(401, APIv3.getGson().toJson(ErrorUtils.notFound("Server", split[1]))); + Spark.halt(401, APIv3.getGson().toJson(ErrorUtils.respondNotFound("Server", split[1]))); } String givenKey = split[2]; diff --git a/src/main/java/net/frozenorb/apiv3/filters/AuthorizationFilter.java b/src/main/java/net/frozenorb/apiv3/filters/AuthorizationFilter.java index f9070cd..407a5bf 100644 --- a/src/main/java/net/frozenorb/apiv3/filters/AuthorizationFilter.java +++ b/src/main/java/net/frozenorb/apiv3/filters/AuthorizationFilter.java @@ -11,7 +11,7 @@ import spark.Spark; public final class AuthorizationFilter implements Filter { public void handle(Request req, Response res) { - Actor actor = req.attribute("actor"); + Actor actor = ctx.get("actor"); if (!actor.isAuthorized()) { APIv3.getStatsD().incrementCounter("apiv3.http.unauthorized"); diff --git a/src/main/java/net/frozenorb/apiv3/filters/LoggingFilter.java b/src/main/java/net/frozenorb/apiv3/filters/LoggingFilter.java index 0eb5dc6..55b2667 100644 --- a/src/main/java/net/frozenorb/apiv3/filters/LoggingFilter.java +++ b/src/main/java/net/frozenorb/apiv3/filters/LoggingFilter.java @@ -14,7 +14,7 @@ public final class LoggingFilter implements Filter { @Getter @Setter private static boolean debug = false; public void handle(Request req, Response res) { - Actor actor = req.attribute("actor"); + Actor actor = ctx.get("actor"); log.info("(" + actor.getName() + " - " + actor.getType() + ") " + req.requestMethod().toUpperCase() + " " + req.pathInfo() + (debug ? "\n" + res.body() : "")); } diff --git a/src/main/java/net/frozenorb/apiv3/models/Rank.java b/src/main/java/net/frozenorb/apiv3/models/Rank.java index 49b0e4f..18373bc 100644 --- a/src/main/java/net/frozenorb/apiv3/models/Rank.java +++ b/src/main/java/net/frozenorb/apiv3/models/Rank.java @@ -7,7 +7,10 @@ import org.mongodb.morphia.annotations.Entity; import org.mongodb.morphia.annotations.Id; import org.mongodb.morphia.annotations.Indexed; -import java.util.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import java.util.concurrent.TimeUnit; @Entity(value = "ranks", noClassnameStored = true) diff --git a/src/main/java/net/frozenorb/apiv3/routes/GETDump.java b/src/main/java/net/frozenorb/apiv3/routes/GETDump.java index 6bacbfb..72d16b6 100644 --- a/src/main/java/net/frozenorb/apiv3/routes/GETDump.java +++ b/src/main/java/net/frozenorb/apiv3/routes/GETDump.java @@ -1,19 +1,16 @@ package net.frozenorb.apiv3.routes; import com.google.common.collect.ImmutableSet; -import com.timgroup.statsd.Event; +import io.vertx.ext.web.RoutingContext; import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.models.Grant; import net.frozenorb.apiv3.models.Punishment; import net.frozenorb.apiv3.utils.ErrorUtils; -import spark.Request; -import spark.Response; -import spark.Route; import java.util.*; import java.util.concurrent.TimeUnit; -public final class GETDump implements Route { +public final class GETDump implements Handler { private List banCache = new ArrayList<>(); private List blacklistCache = new ArrayList<>(); @@ -86,8 +83,8 @@ public final class GETDump implements Route { dumpUpdater.start(); } - public Object handle(Request req, Response res) { - String type = req.params("type"); + public void handle(RoutingContext ctx) { + String type = ctx.request().getParam("type"); switch (type.toLowerCase()) { case "ban": @@ -104,7 +101,8 @@ public final class GETDump implements Route { case "grant": return grantCache; default: - return ErrorUtils.invalidInput(type + " is not a valid type. Not in [ban, blacklist, accessDeniable, grant]"); + ErrorUtils.respondInvalidInput(ctx, "type", type + " is not a valid type. Not in [ban, blacklist, accessDeniable, grant]"); + return } } diff --git a/src/main/java/net/frozenorb/apiv3/routes/GETWhoAmI.java b/src/main/java/net/frozenorb/apiv3/routes/GETWhoAmI.java index 3126bed..6301ccd 100644 --- a/src/main/java/net/frozenorb/apiv3/routes/GETWhoAmI.java +++ b/src/main/java/net/frozenorb/apiv3/routes/GETWhoAmI.java @@ -2,14 +2,11 @@ package net.frozenorb.apiv3.routes; import com.google.common.collect.ImmutableMap; import net.frozenorb.apiv3.actors.Actor; -import spark.Request; -import spark.Response; -import spark.Route; -public final class GETWhoAmI implements Route { +public final class GETWhoAmI implements Handler { - public Object handle(Request req, Response res) { - Actor actor = req.attribute("actor"); + public void handle(RoutingContext ctx) { + Actor actor = ctx.get("actor"); return ImmutableMap.of( "name", actor.getName(), diff --git a/src/main/java/net/frozenorb/apiv3/routes/NotFound.java b/src/main/java/net/frozenorb/apiv3/routes/NotFound.java index be8d636..da5b900 100644 --- a/src/main/java/net/frozenorb/apiv3/routes/NotFound.java +++ b/src/main/java/net/frozenorb/apiv3/routes/NotFound.java @@ -3,17 +3,14 @@ package net.frozenorb.apiv3.routes; import lombok.extern.slf4j.Slf4j; import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.utils.ErrorUtils; -import spark.Request; -import spark.Response; -import spark.Route; import spark.Spark; @Slf4j -public final class NotFound implements Route { +public final class NotFound implements Handler { - public Object handle(Request req, Response res) { + public void handle(RoutingContext ctx) { log.info(req.requestMethod().toUpperCase() + " " + req.url()); - Spark.halt(404, APIv3.getGson().toJson(ErrorUtils.notFound("Route", req.url()))); + Spark.halt(404, APIv3.getGson().toJson(ErrorUtils.respondNotFound("Route", req.url()))); return null; } diff --git a/src/main/java/net/frozenorb/apiv3/routes/POSTMetrics.java b/src/main/java/net/frozenorb/apiv3/routes/POSTMetrics.java index 9ff93d4..a6708f8 100644 --- a/src/main/java/net/frozenorb/apiv3/routes/POSTMetrics.java +++ b/src/main/java/net/frozenorb/apiv3/routes/POSTMetrics.java @@ -1,13 +1,10 @@ package net.frozenorb.apiv3.routes; import com.google.common.collect.ImmutableMap; -import spark.Request; -import spark.Response; -import spark.Route; -public final class POSTMetrics implements Route { +public final class POSTMetrics implements Handler { - public Object handle(Request req, Response res) { + public void handle(RoutingContext ctx) { //LibratoBatch batch = new LibratoBatch(); return ImmutableMap.of(); diff --git a/src/main/java/net/frozenorb/apiv3/routes/announcements/GETAnnouncements.java b/src/main/java/net/frozenorb/apiv3/routes/announcements/GETAnnouncements.java index 2be906a..3bb43e8 100644 --- a/src/main/java/net/frozenorb/apiv3/routes/announcements/GETAnnouncements.java +++ b/src/main/java/net/frozenorb/apiv3/routes/announcements/GETAnnouncements.java @@ -1,28 +1,29 @@ package net.frozenorb.apiv3.routes.announcements; +import io.vertx.core.Handler; +import io.vertx.ext.web.RoutingContext; +import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.actors.Actor; import net.frozenorb.apiv3.actors.ActorType; import net.frozenorb.apiv3.actors.ServerActor; import net.frozenorb.apiv3.models.Server; import net.frozenorb.apiv3.models.ServerGroup; import net.frozenorb.apiv3.utils.ErrorUtils; -import spark.Request; -import spark.Response; -import spark.Route; -public final class GETAnnouncements implements Route { +public final class GETAnnouncements implements Handler { - public Object handle(Request req, Response res) { - Actor actor = req.attribute("actor"); + public void handle(RoutingContext ctx) { + Actor actor = ctx.get("actor"); if (actor.getType() != ActorType.SERVER) { - return ErrorUtils.serverOnly(); + ErrorUtils.respondServerOnly(ctx); + return; } - Server sender = ((ServerActor) req.attribute("actor")).getServer(); + Server sender = ((ServerActor) actor).getServer(); ServerGroup senderGroup = ServerGroup.byId(sender.getServerGroup()); - return senderGroup.getAnnouncements(); + APIv3.respond(ctx, senderGroup.getAnnouncements()); } } \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/routes/auditLog/GETAuditLog.java b/src/main/java/net/frozenorb/apiv3/routes/auditLog/GETAuditLog.java index a196e8c..32df4bc 100644 --- a/src/main/java/net/frozenorb/apiv3/routes/auditLog/GETAuditLog.java +++ b/src/main/java/net/frozenorb/apiv3/routes/auditLog/GETAuditLog.java @@ -1,22 +1,21 @@ package net.frozenorb.apiv3.routes.auditLog; +import io.vertx.core.Handler; +import io.vertx.ext.web.RoutingContext; import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.models.AuditLogEntry; import net.frozenorb.apiv3.utils.ErrorUtils; -import spark.Request; -import spark.Response; -import spark.Route; -public final class GETAuditLog implements Route { +public final class GETAuditLog implements Handler { - public Object handle(Request req, Response res) { + public void handle(RoutingContext ctx) { try { - int limit = req.queryParams("limit") == null ? 100 : Integer.parseInt(req.queryParams("limit")); - int offset = req.queryParams("offset") == null ? 0 : Integer.parseInt(req.queryParams("offset")); + int limit = ctx.request().getParam("limit") == null ? 100 : Integer.parseInt(ctx.request().getParam("limit")); + int offset = ctx.request().getParam("offset") == null ? 0 : Integer.parseInt(ctx.request().getParam("offset")); - return APIv3.getDatastore().createQuery(AuditLogEntry.class).order("performedAt").limit(limit).offset(offset).asList(); + APIv3.respond(ctx, APIv3.getDatastore().createQuery(AuditLogEntry.class).order("performedAt").limit(limit).offset(offset).asList()); } catch (NumberFormatException ex) { - return ErrorUtils.invalidInput("limit and offset must be numerical inputs."); + ErrorUtils.respondInvalidInput(ctx, "limit/offset must be numerical inputs."); } } diff --git a/src/main/java/net/frozenorb/apiv3/routes/chatFilterList/GETChatFilterList.java b/src/main/java/net/frozenorb/apiv3/routes/chatFilterList/GETChatFilterList.java index 0a644ce..8c4e414 100644 --- a/src/main/java/net/frozenorb/apiv3/routes/chatFilterList/GETChatFilterList.java +++ b/src/main/java/net/frozenorb/apiv3/routes/chatFilterList/GETChatFilterList.java @@ -1,14 +1,15 @@ package net.frozenorb.apiv3.routes.chatFilterList; +import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; -import spark.Request; -import spark.Response; -import spark.Route; +import io.vertx.core.Handler; +import io.vertx.ext.web.RoutingContext; +import net.frozenorb.apiv3.APIv3; -public final class GETChatFilterList implements Route { +public final class GETChatFilterList implements Handler { - public Object handle(Request req, Response res) { - return ImmutableSet.of(); + public void handle(RoutingContext ctx) { + APIv3.respond(ctx, ImmutableMap.of()); } } \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/routes/grants/DELETEGrant.java b/src/main/java/net/frozenorb/apiv3/routes/grants/DELETEGrant.java index 6e4a46a..ed3116a 100644 --- a/src/main/java/net/frozenorb/apiv3/routes/grants/DELETEGrant.java +++ b/src/main/java/net/frozenorb/apiv3/routes/grants/DELETEGrant.java @@ -1,41 +1,45 @@ package net.frozenorb.apiv3.routes.grants; import com.google.common.collect.ImmutableMap; +import io.vertx.core.Handler; +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.models.Grant; import net.frozenorb.apiv3.models.User; import net.frozenorb.apiv3.utils.ErrorUtils; -import spark.Request; -import spark.Response; -import spark.Route; -public final class DELETEGrant implements Route { +public final class DELETEGrant implements Handler { - public Object handle(Request req, Response res) { - Grant grant = Grant.byId(req.params("id")); + public void handle(RoutingContext ctx) { + Grant grant = Grant.byId(ctx.request().getParam("id")); if (grant == null) { - return ErrorUtils.notFound("Grant", req.params("id")); + ErrorUtils.respondNotFound(ctx, "Grant", ctx.request().getParam("id")); + return; } else if (!grant.isActive()) { - return ErrorUtils.invalidInput("Cannot remove an inactive grant."); + ErrorUtils.respondInvalidInput(ctx, "Cannot remove an inactive grant."); + return; } - User removedBy = User.byId(req.queryParams("removedBy")); + User removedBy = User.byId(ctx.request().getParam("removedBy")); if (removedBy == null) { - return ErrorUtils.notFound("User", req.queryParams("removedBy")); + ErrorUtils.respondNotFound(ctx, "User", ctx.request().getParam("removedBy")); + return; } - String reason = req.queryParams("reason"); + String reason = ctx.request().getParam("reason"); if (reason == null || reason.trim().isEmpty()) { - return ErrorUtils.requiredInput("reason"); + ErrorUtils.respondRequiredInput(ctx, "reason"); + return; } grant.delete(removedBy, reason); - AuditLog.log(removedBy, "", req.attribute("actor"), AuditLogActionType.DELETE_GRANT, ImmutableMap.of()); - return grant; + AuditLog.log(removedBy, "", ctx.get("actor"), AuditLogActionType.DELETE_GRANT, ImmutableMap.of()); + APIv3.respond(ctx, grant); } } \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/routes/grants/GETGrant.java b/src/main/java/net/frozenorb/apiv3/routes/grants/GETGrant.java index 226ac9f..7be04ae 100644 --- a/src/main/java/net/frozenorb/apiv3/routes/grants/GETGrant.java +++ b/src/main/java/net/frozenorb/apiv3/routes/grants/GETGrant.java @@ -1,14 +1,14 @@ package net.frozenorb.apiv3.routes.grants; +import io.vertx.core.Handler; +import io.vertx.ext.web.RoutingContext; +import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.models.Grant; -import spark.Request; -import spark.Response; -import spark.Route; -public final class GETGrant implements Route { +public final class GETGrant implements Handler { - public Object handle(Request req, Response res) { - return Grant.byId(req.params("id")); + public void handle(RoutingContext ctx) { + APIv3.respond(ctx, Grant.byId(ctx.request().getParam("id"))); } } \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/routes/grants/GETGrants.java b/src/main/java/net/frozenorb/apiv3/routes/grants/GETGrants.java index 37c33ca..e3403c8 100644 --- a/src/main/java/net/frozenorb/apiv3/routes/grants/GETGrants.java +++ b/src/main/java/net/frozenorb/apiv3/routes/grants/GETGrants.java @@ -1,22 +1,21 @@ package net.frozenorb.apiv3.routes.grants; +import io.vertx.core.Handler; +import io.vertx.ext.web.RoutingContext; import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.models.Grant; import net.frozenorb.apiv3.utils.ErrorUtils; -import spark.Request; -import spark.Response; -import spark.Route; -public final class GETGrants implements Route { +public final class GETGrants implements Handler { - public Object handle(Request req, Response res) { + public void handle(RoutingContext ctx) { try { - int limit = req.queryParams("limit") == null ? 100 : Integer.parseInt(req.queryParams("limit")); - int offset = req.queryParams("offset") == null ? 0 : Integer.parseInt(req.queryParams("offset")); + int limit = ctx.request().getParam("limit") == null ? 100 : Integer.parseInt(ctx.request().getParam("limit")); + int offset = ctx.request().getParam("offset") == null ? 0 : Integer.parseInt(ctx.request().getParam("offset")); - return APIv3.getDatastore().createQuery(Grant.class).order("addedAt").limit(limit).offset(offset).asList(); + APIv3.respond(ctx, APIv3.getDatastore().createQuery(Grant.class).order("addedAt").limit(limit).offset(offset).asList()); } catch (NumberFormatException ex) { - return ErrorUtils.invalidInput("limit and offset must be numerical inputs."); + ErrorUtils.respondInvalidInput(ctx, "limit and offset must be numerical inputs."); } } diff --git a/src/main/java/net/frozenorb/apiv3/routes/grants/GETUserGrants.java b/src/main/java/net/frozenorb/apiv3/routes/grants/GETUserGrants.java index 726e15b..028d17b 100644 --- a/src/main/java/net/frozenorb/apiv3/routes/grants/GETUserGrants.java +++ b/src/main/java/net/frozenorb/apiv3/routes/grants/GETUserGrants.java @@ -1,21 +1,22 @@ package net.frozenorb.apiv3.routes.grants; +import io.vertx.core.Handler; +import io.vertx.ext.web.RoutingContext; +import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.models.User; import net.frozenorb.apiv3.utils.ErrorUtils; -import spark.Request; -import spark.Response; -import spark.Route; -public final class GETUserGrants implements Route { +public final class GETUserGrants implements Handler { - public Object handle(Request req, Response res) { - User target = User.byId(req.params("id")); + public void handle(RoutingContext ctx) { + User target = User.byId(ctx.request().getParam("id")); if (target == null) { - return ErrorUtils.notFound("User", req.params("id")); + ErrorUtils.respondNotFound(ctx, "User", ctx.request().getParam("id")); + return; } - return target.getGrants(); + APIv3.respond(ctx, target.getGrants()); } } \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/routes/grants/POSTUserGrant.java b/src/main/java/net/frozenorb/apiv3/routes/grants/POSTUserGrant.java index 14bc083..6c12062 100644 --- a/src/main/java/net/frozenorb/apiv3/routes/grants/POSTUserGrant.java +++ b/src/main/java/net/frozenorb/apiv3/routes/grants/POSTUserGrant.java @@ -6,64 +6,61 @@ import net.frozenorb.apiv3.models.Rank; import net.frozenorb.apiv3.models.ServerGroup; import net.frozenorb.apiv3.models.User; import net.frozenorb.apiv3.utils.ErrorUtils; -import spark.Request; -import spark.Response; -import spark.Route; import java.util.Date; import java.util.HashSet; import java.util.Set; -public final class POSTUserGrant implements Route { +public final class POSTUserGrant implements Handler { - public Object handle(Request req, Response res) { - User target = User.byId(req.params("id")); + public void handle(RoutingContext ctx) { + User target = User.byId(ctx.request().getParam("id")); if (target == null) { - return ErrorUtils.notFound("User", req.params("id")); + return ErrorUtils.respondNotFound("User", ctx.request().getParam("id")); } - String reason = req.queryParams("reason"); + String reason = ctx.request().getParam("reason"); if (reason == null || reason.trim().isEmpty()) { - return ErrorUtils.requiredInput("reason"); + return ErrorUtils.respondRequiredInput("reason"); } Set scopes = new HashSet<>(); - String scopesUnparsed = req.queryParams("scopes"); + String scopesUnparsed = ctx.request().getParam("scopes"); if (!scopesUnparsed.isEmpty()) { for (String serverGroupId : scopesUnparsed.split(",")) { ServerGroup serverGroup = ServerGroup.byId(serverGroupId); if (serverGroup == null) { - return ErrorUtils.notFound("Server group", serverGroupId); + return ErrorUtils.respondNotFound("Server group", serverGroupId); } scopes.add(serverGroup); } } - Rank rank = Rank.byId(req.queryParams("rank")); + Rank rank = Rank.byId(ctx.request().getParam("rank")); if (rank == null) { - return ErrorUtils.notFound("Rank", req.queryParams("rank")); + return ErrorUtils.respondNotFound("Rank", ctx.request().getParam("rank")); } Date expiresAt; try { - expiresAt = new Date(Long.parseLong(req.queryParams("expiresAt"))); + expiresAt = new Date(Long.parseLong(ctx.request().getParam("expiresAt"))); } catch (NumberFormatException ex) { expiresAt = null; } if (expiresAt != null && expiresAt.before(new Date())) { - return ErrorUtils.invalidInput("Expiration date cannot be in the past."); + return ErrorUtils.respondInvalidInput("Expiration date cannot be in the past."); } // We purposely don't do a null check, grants don't have to have a source. - User addedBy = User.byId(req.queryParams("addedBy")); + User addedBy = User.byId(ctx.request().getParam("addedBy")); Grant grant = new Grant(target, reason, scopes, rank, expiresAt, addedBy); APIv3.getDatastore().save(grant); diff --git a/src/main/java/net/frozenorb/apiv3/routes/ipLog/GETUserIPLog.java b/src/main/java/net/frozenorb/apiv3/routes/ipLog/GETUserIPLog.java index 13149ab..863a7d4 100644 --- a/src/main/java/net/frozenorb/apiv3/routes/ipLog/GETUserIPLog.java +++ b/src/main/java/net/frozenorb/apiv3/routes/ipLog/GETUserIPLog.java @@ -1,21 +1,22 @@ package net.frozenorb.apiv3.routes.ipLog; +import io.vertx.core.Handler; +import io.vertx.ext.web.RoutingContext; +import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.models.User; import net.frozenorb.apiv3.utils.ErrorUtils; -import spark.Request; -import spark.Response; -import spark.Route; -public final class GETUserIPLog implements Route { +public final class GETUserIPLog implements Handler { - public Object handle(Request req, Response res) { - User target = User.byId(req.params("id")); + public void handle(RoutingContext ctx) { + User target = User.byId(ctx.request().getParam("id")); if (target == null) { - return ErrorUtils.notFound("User", req.params("id")); + ErrorUtils.respondNotFound(ctx, "User", ctx.request().getParam("id")); + return; } - return target.getIPLog(); + APIv3.respond(ctx, target.getIPLog()); } } \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/routes/notificationTemplate/DELETENotificationTemplate.java b/src/main/java/net/frozenorb/apiv3/routes/notificationTemplate/DELETENotificationTemplate.java index 22ee7e9..95fe657 100644 --- a/src/main/java/net/frozenorb/apiv3/routes/notificationTemplate/DELETENotificationTemplate.java +++ b/src/main/java/net/frozenorb/apiv3/routes/notificationTemplate/DELETENotificationTemplate.java @@ -1,22 +1,23 @@ package net.frozenorb.apiv3.routes.notificationTemplate; +import io.vertx.core.Handler; +import io.vertx.ext.web.RoutingContext; +import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.models.NotificationTemplate; import net.frozenorb.apiv3.utils.ErrorUtils; -import spark.Request; -import spark.Response; -import spark.Route; -public final class DELETENotificationTemplate implements Route { +public final class DELETENotificationTemplate implements Handler { - public Object handle(Request req, Response res) { - NotificationTemplate notificationTemplate = NotificationTemplate.byId(req.params("id")); + public void handle(RoutingContext ctx) { + NotificationTemplate notificationTemplate = NotificationTemplate.byId(ctx.request().getParam("id")); if (notificationTemplate == null) { - return ErrorUtils.notFound("Notification template", req.params("id")); + ErrorUtils.respondNotFound(ctx, "Notification template", ctx.request().getParam("id")); + return; } notificationTemplate.delete(); - return notificationTemplate; + APIv3.respond(ctx, notificationTemplate); } } \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/routes/notificationTemplate/GETNotificationTemplate.java b/src/main/java/net/frozenorb/apiv3/routes/notificationTemplate/GETNotificationTemplate.java index efe8129..9e55dbf 100644 --- a/src/main/java/net/frozenorb/apiv3/routes/notificationTemplate/GETNotificationTemplate.java +++ b/src/main/java/net/frozenorb/apiv3/routes/notificationTemplate/GETNotificationTemplate.java @@ -1,14 +1,14 @@ package net.frozenorb.apiv3.routes.notificationTemplate; +import io.vertx.core.Handler; +import io.vertx.ext.web.RoutingContext; +import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.models.NotificationTemplate; -import spark.Request; -import spark.Response; -import spark.Route; -public final class GETNotificationTemplate implements Route { +public final class GETNotificationTemplate implements Handler { - public Object handle(Request req, Response res) { - return NotificationTemplate.byId(req.params("id")); + public void handle(RoutingContext ctx) { + APIv3.respond(ctx, NotificationTemplate.byId(ctx.request().getParam("id"))); } } \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/routes/notificationTemplate/GETNotificationTemplates.java b/src/main/java/net/frozenorb/apiv3/routes/notificationTemplate/GETNotificationTemplates.java index 398510b..3c6f162 100644 --- a/src/main/java/net/frozenorb/apiv3/routes/notificationTemplate/GETNotificationTemplates.java +++ b/src/main/java/net/frozenorb/apiv3/routes/notificationTemplate/GETNotificationTemplates.java @@ -1,14 +1,14 @@ package net.frozenorb.apiv3.routes.notificationTemplate; +import io.vertx.core.Handler; +import io.vertx.ext.web.RoutingContext; +import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.models.NotificationTemplate; -import spark.Request; -import spark.Response; -import spark.Route; -public final class GETNotificationTemplates implements Route { +public final class GETNotificationTemplates implements Handler { - public Object handle(Request req, Response res) { - return NotificationTemplate.values(); + public void handle(RoutingContext ctx) { + APIv3.respond(ctx, NotificationTemplate.values()); } } \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/routes/notificationTemplate/POSTNotificationTemplate.java b/src/main/java/net/frozenorb/apiv3/routes/notificationTemplate/POSTNotificationTemplate.java index 43e8398..9aa8c4c 100644 --- a/src/main/java/net/frozenorb/apiv3/routes/notificationTemplate/POSTNotificationTemplate.java +++ b/src/main/java/net/frozenorb/apiv3/routes/notificationTemplate/POSTNotificationTemplate.java @@ -1,21 +1,20 @@ package net.frozenorb.apiv3.routes.notificationTemplate; +import io.vertx.core.Handler; +import io.vertx.ext.web.RoutingContext; import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.models.NotificationTemplate; -import spark.Request; -import spark.Response; -import spark.Route; -public final class POSTNotificationTemplate implements Route { +public final class POSTNotificationTemplate implements Handler { - public Object handle(Request req, Response res) { - String id = req.queryParams("id"); - String subject = req.queryParams("subject"); - String body = req.queryParams("body"); + public void handle(RoutingContext ctx) { + String id = ctx.request().getParam("id"); + String subject = ctx.request().getParam("subject"); + String body = ctx.request().getParam("body"); NotificationTemplate notificationTemplate = new NotificationTemplate(id, subject, body); APIv3.getDatastore().save(notificationTemplate); - return notificationTemplate; + APIv3.respond(ctx, notificationTemplate); } } \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/routes/punishments/DELETEPunishment.java b/src/main/java/net/frozenorb/apiv3/routes/punishments/DELETEPunishment.java index b2fb077..bd578e1 100644 --- a/src/main/java/net/frozenorb/apiv3/routes/punishments/DELETEPunishment.java +++ b/src/main/java/net/frozenorb/apiv3/routes/punishments/DELETEPunishment.java @@ -6,35 +6,32 @@ import net.frozenorb.apiv3.auditLog.AuditLogActionType; import net.frozenorb.apiv3.models.Punishment; import net.frozenorb.apiv3.models.User; import net.frozenorb.apiv3.utils.ErrorUtils; -import spark.Request; -import spark.Response; -import spark.Route; -public final class DELETEPunishment implements Route { +public final class DELETEPunishment implements Handler { - public Object handle(Request req, Response res) { - Punishment punishment = Punishment.byId(req.params("id")); + public void handle(RoutingContext ctx) { + Punishment punishment = Punishment.byId(ctx.request().getParam("id")); if (punishment == null) { - return ErrorUtils.notFound("Punishment", req.params("id")); + return ErrorUtils.respondNotFound("Punishment", ctx.request().getParam("id")); } else if (!punishment.isActive()) { - return ErrorUtils.invalidInput("Cannot remove an inactive punishment."); + return ErrorUtils.respondInvalidInput("Cannot remove an inactive punishment."); } - User removedBy = User.byId(req.queryParams("removedBy")); + User removedBy = User.byId(ctx.request().getParam("removedBy")); if (removedBy == null) { - return ErrorUtils.notFound("User", req.queryParams("removedBy")); + return ErrorUtils.respondNotFound("User", ctx.request().getParam("removedBy")); } - String reason = req.queryParams("reason"); + String reason = ctx.request().getParam("reason"); if (reason == null || reason.trim().isEmpty()) { - return ErrorUtils.requiredInput("reason"); + return ErrorUtils.respondRequiredInput("reason"); } punishment.delete(removedBy, reason); - AuditLog.log(removedBy, "", req.attribute("actor"), AuditLogActionType.DELETE_PUNISHMENT, ImmutableMap.of()); + AuditLog.log(removedBy, "", ctx.get("actor"), AuditLogActionType.DELETE_PUNISHMENT, ImmutableMap.of()); return punishment; } diff --git a/src/main/java/net/frozenorb/apiv3/routes/punishments/GETPunishment.java b/src/main/java/net/frozenorb/apiv3/routes/punishments/GETPunishment.java index bfcd539..1315bd4 100644 --- a/src/main/java/net/frozenorb/apiv3/routes/punishments/GETPunishment.java +++ b/src/main/java/net/frozenorb/apiv3/routes/punishments/GETPunishment.java @@ -1,14 +1,11 @@ package net.frozenorb.apiv3.routes.punishments; import net.frozenorb.apiv3.models.Punishment; -import spark.Request; -import spark.Response; -import spark.Route; -public final class GETPunishment implements Route { +public final class GETPunishment implements Handler { - public Object handle(Request req, Response res) { - return Punishment.byId(req.params("id")); + public void handle(RoutingContext ctx) { + return Punishment.byId(ctx.request().getParam("id")); } } \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/routes/punishments/GETPunishments.java b/src/main/java/net/frozenorb/apiv3/routes/punishments/GETPunishments.java index 52b1946..821dfb1 100644 --- a/src/main/java/net/frozenorb/apiv3/routes/punishments/GETPunishments.java +++ b/src/main/java/net/frozenorb/apiv3/routes/punishments/GETPunishments.java @@ -3,20 +3,17 @@ package net.frozenorb.apiv3.routes.punishments; import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.models.Punishment; import net.frozenorb.apiv3.utils.ErrorUtils; -import spark.Request; -import spark.Response; -import spark.Route; -public final class GETPunishments implements Route { +public final class GETPunishments implements Handler { - public Object handle(Request req, Response res) { + public void handle(RoutingContext ctx) { try { - int limit = req.queryParams("limit") == null ? 100 : Integer.parseInt(req.queryParams("limit")); - int offset = req.queryParams("offset") == null ? 0 : Integer.parseInt(req.queryParams("offset")); + int limit = ctx.request().getParam("limit") == null ? 100 : Integer.parseInt(ctx.request().getParam("limit")); + int offset = ctx.request().getParam("offset") == null ? 0 : Integer.parseInt(ctx.request().getParam("offset")); return APIv3.getDatastore().createQuery(Punishment.class).order("addedAt").limit(limit).offset(offset).asList(); } catch (NumberFormatException ex) { - return ErrorUtils.invalidInput("limit and offset must be numerical inputs."); + return ErrorUtils.respondInvalidInput("limit and offset must be numerical inputs."); } } diff --git a/src/main/java/net/frozenorb/apiv3/routes/punishments/GETUserPunishments.java b/src/main/java/net/frozenorb/apiv3/routes/punishments/GETUserPunishments.java index 99aece5..79d5774 100644 --- a/src/main/java/net/frozenorb/apiv3/routes/punishments/GETUserPunishments.java +++ b/src/main/java/net/frozenorb/apiv3/routes/punishments/GETUserPunishments.java @@ -2,17 +2,14 @@ package net.frozenorb.apiv3.routes.punishments; import net.frozenorb.apiv3.models.User; import net.frozenorb.apiv3.utils.ErrorUtils; -import spark.Request; -import spark.Response; -import spark.Route; -public final class GETUserPunishments implements Route { +public final class GETUserPunishments implements Handler { - public Object handle(Request req, Response res) { - User target = User.byId(req.params("id")); + public void handle(RoutingContext ctx) { + User target = User.byId(ctx.request().getParam("id")); if (target == null) { - return ErrorUtils.notFound("User", req.params("id")); + return ErrorUtils.respondNotFound("User", ctx.request().getParam("id")); } return target.getPunishments(); diff --git a/src/main/java/net/frozenorb/apiv3/routes/punishments/POSTUserPunish.java b/src/main/java/net/frozenorb/apiv3/routes/punishments/POSTUserPunish.java index 9bb1f40..483c29c 100644 --- a/src/main/java/net/frozenorb/apiv3/routes/punishments/POSTUserPunish.java +++ b/src/main/java/net/frozenorb/apiv3/routes/punishments/POSTUserPunish.java @@ -8,29 +8,26 @@ import net.frozenorb.apiv3.models.User; import net.frozenorb.apiv3.unsorted.Permissions; import net.frozenorb.apiv3.utils.ErrorUtils; import org.bson.Document; -import spark.Request; -import spark.Response; -import spark.Route; import java.util.Date; import java.util.Map; -public final class POSTUserPunish implements Route { +public final class POSTUserPunish implements Handler { - public Object handle(Request req, Response res) { - User target = User.byId(req.params("id")); + public void handle(RoutingContext ctx) { + User target = User.byId(ctx.request().getParam("id")); if (target == null) { - return ErrorUtils.notFound("User", req.params("id")); + return ErrorUtils.respondNotFound("User", ctx.request().getParam("id")); } - String reason = req.queryParams("reason"); + String reason = ctx.request().getParam("reason"); if (reason == null || reason.trim().isEmpty()) { - return ErrorUtils.requiredInput("reason"); + return ErrorUtils.respondRequiredInput("reason"); } - Punishment.PunishmentType type = Punishment.PunishmentType.valueOf(req.queryParams("type")); + Punishment.PunishmentType type = Punishment.PunishmentType.valueOf(ctx.request().getParam("type")); if (type != Punishment.PunishmentType.WARN) { for (Punishment punishment : target.getPunishments(ImmutableSet.of(type))) { @@ -43,29 +40,29 @@ public final class POSTUserPunish implements Route { Date expiresAt; try { - expiresAt = new Date(Long.parseLong(req.queryParams("expiresAt"))); + expiresAt = new Date(Long.parseLong(ctx.request().getParam("expiresAt"))); } catch (NumberFormatException ex) { expiresAt = null; } if (expiresAt != null && expiresAt.before(new Date())) { - return ErrorUtils.invalidInput("Expiration date cannot be in the past."); + return ErrorUtils.respondInvalidInput("Expiration date cannot be in the past."); } Map meta = Document.parse(req.body()); if (meta == null) { - return ErrorUtils.requiredInput("request body meta"); + return ErrorUtils.respondRequiredInput("request body meta"); } // We purposely don't do a null check, grants don't have to have a source. - User addedBy = User.byId(req.queryParams("addedBy")); + User addedBy = User.byId(ctx.request().getParam("addedBy")); if (target.hasPermissionAnywhere(Permissions.PROTECTED_PUNISHMENT)) { return ErrorUtils.error(target.getLastSeenOn() + " is protected from punishments."); } - Punishment punishment = new Punishment(target, reason, type, expiresAt, addedBy, req.attribute("actor"), meta); + Punishment punishment = new Punishment(target, reason, type, expiresAt, addedBy, ctx.get("actor"), meta); String accessDenialReason = punishment.getAccessDenialReason(); APIv3.getDatastore().save(punishment); diff --git a/src/main/java/net/frozenorb/apiv3/routes/ranks/DELETERank.java b/src/main/java/net/frozenorb/apiv3/routes/ranks/DELETERank.java index 2972a3a..b9e1092 100644 --- a/src/main/java/net/frozenorb/apiv3/routes/ranks/DELETERank.java +++ b/src/main/java/net/frozenorb/apiv3/routes/ranks/DELETERank.java @@ -2,17 +2,14 @@ package net.frozenorb.apiv3.routes.ranks; import net.frozenorb.apiv3.models.Rank; import net.frozenorb.apiv3.utils.ErrorUtils; -import spark.Request; -import spark.Response; -import spark.Route; -public final class DELETERank implements Route { +public final class DELETERank implements Handler { - public Object handle(Request req, Response res) { - Rank rank = Rank.byId(req.params("id")); + public void handle(RoutingContext ctx) { + Rank rank = Rank.byId(ctx.request().getParam("id")); if (rank == null) { - return ErrorUtils.notFound("Rank", req.params("id")); + return ErrorUtils.respondNotFound("Rank", ctx.request().getParam("id")); } rank.delete(); diff --git a/src/main/java/net/frozenorb/apiv3/routes/ranks/GETRank.java b/src/main/java/net/frozenorb/apiv3/routes/ranks/GETRank.java index de038cb..91a692f 100644 --- a/src/main/java/net/frozenorb/apiv3/routes/ranks/GETRank.java +++ b/src/main/java/net/frozenorb/apiv3/routes/ranks/GETRank.java @@ -1,14 +1,11 @@ package net.frozenorb.apiv3.routes.ranks; import net.frozenorb.apiv3.models.Rank; -import spark.Request; -import spark.Response; -import spark.Route; -public final class GETRank implements Route { +public final class GETRank implements Handler { - public Object handle(Request req, Response res) { - return Rank.byId(req.params("id")); + public void handle(RoutingContext ctx) { + return Rank.byId(ctx.request().getParam("id")); } } \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/routes/ranks/GETRanks.java b/src/main/java/net/frozenorb/apiv3/routes/ranks/GETRanks.java index 6d366da..d8dd68e 100644 --- a/src/main/java/net/frozenorb/apiv3/routes/ranks/GETRanks.java +++ b/src/main/java/net/frozenorb/apiv3/routes/ranks/GETRanks.java @@ -1,13 +1,10 @@ package net.frozenorb.apiv3.routes.ranks; import net.frozenorb.apiv3.models.Rank; -import spark.Request; -import spark.Response; -import spark.Route; -public final class GETRanks implements Route { +public final class GETRanks implements Handler { - public Object handle(Request req, Response res) { + public void handle(RoutingContext ctx) { return Rank.values(); } diff --git a/src/main/java/net/frozenorb/apiv3/routes/ranks/POSTRank.java b/src/main/java/net/frozenorb/apiv3/routes/ranks/POSTRank.java index 5ad1351..e7ed2e8 100644 --- a/src/main/java/net/frozenorb/apiv3/routes/ranks/POSTRank.java +++ b/src/main/java/net/frozenorb/apiv3/routes/ranks/POSTRank.java @@ -2,19 +2,16 @@ package net.frozenorb.apiv3.routes.ranks; import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.models.Rank; -import spark.Request; -import spark.Response; -import spark.Route; -public final class POSTRank implements Route { +public final class POSTRank implements Handler { - public Object handle(Request req, Response res) { - String id = req.queryParams("id"); - int weight = Integer.parseInt(req.queryParams("weight")); - String displayName = req.queryParams("displayName"); - String gameColor = req.queryParams("gameColor"); - String websiteColor = req.queryParams("websiteColor"); - boolean staffRank = Boolean.parseBoolean(req.queryParams("staffRank")); + public void handle(RoutingContext ctx) { + String id = ctx.request().getParam("id"); + int weight = Integer.parseInt(ctx.request().getParam("weight")); + String displayName = ctx.request().getParam("displayName"); + String gameColor = ctx.request().getParam("gameColor"); + String websiteColor = ctx.request().getParam("websiteColor"); + boolean staffRank = Boolean.parseBoolean(ctx.request().getParam("staffRank")); Rank rank = new Rank(id, weight, displayName, gameColor, websiteColor, staffRank); APIv3.getDatastore().save(rank); diff --git a/src/main/java/net/frozenorb/apiv3/routes/serverGroups/DELETEServerGroup.java b/src/main/java/net/frozenorb/apiv3/routes/serverGroups/DELETEServerGroup.java index 37bb85e..1fa5451 100644 --- a/src/main/java/net/frozenorb/apiv3/routes/serverGroups/DELETEServerGroup.java +++ b/src/main/java/net/frozenorb/apiv3/routes/serverGroups/DELETEServerGroup.java @@ -2,17 +2,14 @@ package net.frozenorb.apiv3.routes.serverGroups; import net.frozenorb.apiv3.models.ServerGroup; import net.frozenorb.apiv3.utils.ErrorUtils; -import spark.Request; -import spark.Response; -import spark.Route; -public final class DELETEServerGroup implements Route { +public final class DELETEServerGroup implements Handler { - public Object handle(Request req, Response res) { - ServerGroup serverGroup = ServerGroup.byId(req.params("id")); + public void handle(RoutingContext ctx) { + ServerGroup serverGroup = ServerGroup.byId(ctx.request().getParam("id")); if (serverGroup == null) { - return ErrorUtils.notFound("Server group", req.params("id")); + return ErrorUtils.respondNotFound("Server group", ctx.request().getParam("id")); } serverGroup.delete(); diff --git a/src/main/java/net/frozenorb/apiv3/routes/serverGroups/GETServerGroup.java b/src/main/java/net/frozenorb/apiv3/routes/serverGroups/GETServerGroup.java index f6ef344..39ba0ea 100644 --- a/src/main/java/net/frozenorb/apiv3/routes/serverGroups/GETServerGroup.java +++ b/src/main/java/net/frozenorb/apiv3/routes/serverGroups/GETServerGroup.java @@ -1,14 +1,11 @@ package net.frozenorb.apiv3.routes.serverGroups; import net.frozenorb.apiv3.models.ServerGroup; -import spark.Request; -import spark.Response; -import spark.Route; -public final class GETServerGroup implements Route { +public final class GETServerGroup implements Handler { - public Object handle(Request req, Response res) { - return ServerGroup.byId(req.params("id")); + public void handle(RoutingContext ctx) { + return ServerGroup.byId(ctx.request().getParam("id")); } } \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/routes/serverGroups/GETServerGroups.java b/src/main/java/net/frozenorb/apiv3/routes/serverGroups/GETServerGroups.java index c330538..1788dda 100644 --- a/src/main/java/net/frozenorb/apiv3/routes/serverGroups/GETServerGroups.java +++ b/src/main/java/net/frozenorb/apiv3/routes/serverGroups/GETServerGroups.java @@ -1,13 +1,10 @@ package net.frozenorb.apiv3.routes.serverGroups; import net.frozenorb.apiv3.models.ServerGroup; -import spark.Request; -import spark.Response; -import spark.Route; -public final class GETServerGroups implements Route { +public final class GETServerGroups implements Handler { - public Object handle(Request req, Response res) { + public void handle(RoutingContext ctx) { return ServerGroup.values(); } diff --git a/src/main/java/net/frozenorb/apiv3/routes/serverGroups/POSTServerGroup.java b/src/main/java/net/frozenorb/apiv3/routes/serverGroups/POSTServerGroup.java index 100a44a..3055f8e 100644 --- a/src/main/java/net/frozenorb/apiv3/routes/serverGroups/POSTServerGroup.java +++ b/src/main/java/net/frozenorb/apiv3/routes/serverGroups/POSTServerGroup.java @@ -2,16 +2,13 @@ package net.frozenorb.apiv3.routes.serverGroups; import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.models.ServerGroup; -import spark.Request; -import spark.Response; -import spark.Route; -public final class POSTServerGroup implements Route { +public final class POSTServerGroup implements Handler { - public Object handle(Request req, Response res) { - String id = req.queryParams("id"); - String image = req.queryParams("image"); - boolean isPublic = Boolean.valueOf(req.queryParams("public")); + public void handle(RoutingContext ctx) { + String id = ctx.request().getParam("id"); + String image = ctx.request().getParam("image"); + boolean isPublic = Boolean.valueOf(ctx.request().getParam("public")); ServerGroup serverGroup = new ServerGroup(id, image, isPublic); APIv3.getDatastore().save(serverGroup); diff --git a/src/main/java/net/frozenorb/apiv3/routes/servers/DELETEServer.java b/src/main/java/net/frozenorb/apiv3/routes/servers/DELETEServer.java index 8333b89..83cf362 100644 --- a/src/main/java/net/frozenorb/apiv3/routes/servers/DELETEServer.java +++ b/src/main/java/net/frozenorb/apiv3/routes/servers/DELETEServer.java @@ -2,17 +2,14 @@ package net.frozenorb.apiv3.routes.servers; import net.frozenorb.apiv3.models.Server; import net.frozenorb.apiv3.utils.ErrorUtils; -import spark.Request; -import spark.Response; -import spark.Route; -public final class DELETEServer implements Route { +public final class DELETEServer implements Handler { - public Object handle(Request req, Response res) { - Server server = Server.byId(req.params("id")); + public void handle(RoutingContext ctx) { + Server server = Server.byId(ctx.request().getParam("id")); if (server == null) { - return ErrorUtils.notFound("Server", req.params("id")); + return ErrorUtils.respondNotFound("Server", ctx.request().getParam("id")); } server.delete(); diff --git a/src/main/java/net/frozenorb/apiv3/routes/servers/GETServer.java b/src/main/java/net/frozenorb/apiv3/routes/servers/GETServer.java index 6058c1b..973528c 100644 --- a/src/main/java/net/frozenorb/apiv3/routes/servers/GETServer.java +++ b/src/main/java/net/frozenorb/apiv3/routes/servers/GETServer.java @@ -1,14 +1,11 @@ package net.frozenorb.apiv3.routes.servers; import net.frozenorb.apiv3.models.Server; -import spark.Request; -import spark.Response; -import spark.Route; -public final class GETServer implements Route { +public final class GETServer implements Handler { - public Object handle(Request req, Response res) { - return Server.byId(req.params("id")); + public void handle(RoutingContext ctx) { + return Server.byId(ctx.request().getParam("id")); } } \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/routes/servers/GETServers.java b/src/main/java/net/frozenorb/apiv3/routes/servers/GETServers.java index c1a853f..52cb5ad 100644 --- a/src/main/java/net/frozenorb/apiv3/routes/servers/GETServers.java +++ b/src/main/java/net/frozenorb/apiv3/routes/servers/GETServers.java @@ -1,13 +1,10 @@ package net.frozenorb.apiv3.routes.servers; import net.frozenorb.apiv3.models.Server; -import spark.Request; -import spark.Response; -import spark.Route; -public final class GETServers implements Route { +public final class GETServers implements Handler { - public Object handle(Request req, Response res) { + public void handle(RoutingContext ctx) { return Server.values(); } diff --git a/src/main/java/net/frozenorb/apiv3/routes/servers/POSTServer.java b/src/main/java/net/frozenorb/apiv3/routes/servers/POSTServer.java index 8a41865..a534f03 100644 --- a/src/main/java/net/frozenorb/apiv3/routes/servers/POSTServer.java +++ b/src/main/java/net/frozenorb/apiv3/routes/servers/POSTServer.java @@ -5,25 +5,22 @@ import net.frozenorb.apiv3.models.Server; import net.frozenorb.apiv3.models.ServerGroup; import net.frozenorb.apiv3.utils.ErrorUtils; import net.frozenorb.apiv3.utils.IPUtils; -import spark.Request; -import spark.Response; -import spark.Route; -public final class POSTServer implements Route { +public final class POSTServer implements Handler { - public Object handle(Request req, Response res) { - String id = req.queryParams("id"); - String displayName = req.queryParams("displayName"); - String apiKey = req.queryParams("apiKey"); - ServerGroup group = ServerGroup.byId(req.queryParams("group")); - String ip = req.queryParams("ip"); + public void handle(RoutingContext ctx) { + String id = ctx.request().getParam("id"); + String displayName = ctx.request().getParam("displayName"); + String apiKey = ctx.request().getParam("apiKey"); + ServerGroup group = ServerGroup.byId(ctx.request().getParam("group")); + String ip = ctx.request().getParam("ip"); if (group == null) { - return ErrorUtils.notFound("Server group", req.queryParams("group")); + return ErrorUtils.respondNotFound("Server group", ctx.request().getParam("group")); } if (!IPUtils.isValidIP(ip)) { - return ErrorUtils.invalidInput("IP address \"" + ip + "\" is not valid."); + return ErrorUtils.respondInvalidInput("IP address \"" + ip + "\" is not valid."); } Server server = new Server(id, displayName, apiKey, group, ip); diff --git a/src/main/java/net/frozenorb/apiv3/routes/servers/POSTServerHeartbeat.java b/src/main/java/net/frozenorb/apiv3/routes/servers/POSTServerHeartbeat.java index 278f592..bbd3721 100644 --- a/src/main/java/net/frozenorb/apiv3/routes/servers/POSTServerHeartbeat.java +++ b/src/main/java/net/frozenorb/apiv3/routes/servers/POSTServerHeartbeat.java @@ -10,21 +10,18 @@ import net.frozenorb.apiv3.utils.ErrorUtils; import net.frozenorb.apiv3.utils.PermissionUtils; import net.frozenorb.apiv3.utils.UUIDUtils; import org.bson.Document; -import spark.Request; -import spark.Response; -import spark.Route; import java.util.*; @Slf4j -public final class POSTServerHeartbeat implements Route { +public final class POSTServerHeartbeat implements Handler { @SuppressWarnings("unchecked") - public Object handle(Request req, Response res) { - Actor actor = req.attribute("actor"); + public void handle(RoutingContext ctx) { + Actor actor = ctx.get("actor"); if (actor.getType() != ActorType.SERVER) { - return ErrorUtils.serverOnly(); + return ErrorUtils.respondServerOnly(); } Server actorServer = Server.byId(actor.getName()); diff --git a/src/main/java/net/frozenorb/apiv3/routes/users/DELETEUserMeta.java b/src/main/java/net/frozenorb/apiv3/routes/users/DELETEUserMeta.java index cfca7ce..e7c9cd6 100644 --- a/src/main/java/net/frozenorb/apiv3/routes/users/DELETEUserMeta.java +++ b/src/main/java/net/frozenorb/apiv3/routes/users/DELETEUserMeta.java @@ -4,23 +4,20 @@ import net.frozenorb.apiv3.models.ServerGroup; import net.frozenorb.apiv3.models.User; import net.frozenorb.apiv3.models.UserMetaEntry; import net.frozenorb.apiv3.utils.ErrorUtils; -import spark.Request; -import spark.Response; -import spark.Route; -public final class DELETEUserMeta implements Route { +public final class DELETEUserMeta implements Handler { - public Object handle(Request req, Response res) { - User user = User.byId(req.params("id")); + public void handle(RoutingContext ctx) { + User user = User.byId(ctx.request().getParam("id")); if (user == null) { - return ErrorUtils.notFound("User", req.params("id")); + return ErrorUtils.respondNotFound("User", ctx.request().getParam("id")); } - ServerGroup serverGroup = ServerGroup.byId(req.params("serverGroup")); + ServerGroup serverGroup = ServerGroup.byId(ctx.request().getParam("serverGroup")); if (serverGroup == null) { - return ErrorUtils.notFound("Server group", req.params("serverGroup")); + return ErrorUtils.respondNotFound("Server group", ctx.request().getParam("serverGroup")); } UserMetaEntry userMetaEntry = user.getMeta(serverGroup); diff --git a/src/main/java/net/frozenorb/apiv3/routes/users/DELETEUserPunishment.java b/src/main/java/net/frozenorb/apiv3/routes/users/DELETEUserPunishment.java index 2dd304e..ecfc385 100644 --- a/src/main/java/net/frozenorb/apiv3/routes/users/DELETEUserPunishment.java +++ b/src/main/java/net/frozenorb/apiv3/routes/users/DELETEUserPunishment.java @@ -7,36 +7,33 @@ import net.frozenorb.apiv3.auditLog.AuditLogActionType; import net.frozenorb.apiv3.models.Punishment; import net.frozenorb.apiv3.models.User; import net.frozenorb.apiv3.utils.ErrorUtils; -import spark.Request; -import spark.Response; -import spark.Route; -public final class DELETEUserPunishment implements Route { +public final class DELETEUserPunishment implements Handler { - public Object handle(Request req, Response res) { - User target = User.byId(req.params("id")); + public void handle(RoutingContext ctx) { + User target = User.byId(ctx.request().getParam("id")); if (target == null) { - return ErrorUtils.notFound("User", req.params("id")); + return ErrorUtils.respondNotFound("User", ctx.request().getParam("id")); } - Punishment.PunishmentType type = Punishment.PunishmentType.valueOf(req.queryParams("type").toUpperCase()); - User removedBy = User.byId(req.queryParams("removedBy")); + Punishment.PunishmentType type = Punishment.PunishmentType.valueOf(ctx.request().getParam("type").toUpperCase()); + User removedBy = User.byId(ctx.request().getParam("removedBy")); if (removedBy == null) { - return ErrorUtils.notFound("User", req.queryParams("removedBy")); + return ErrorUtils.respondNotFound("User", ctx.request().getParam("removedBy")); } - String reason = req.queryParams("reason"); + String reason = ctx.request().getParam("reason"); if (reason == null || reason.trim().isEmpty()) { - return ErrorUtils.requiredInput("reason"); + return ErrorUtils.respondRequiredInput("reason"); } for (Punishment punishment : target.getPunishments(ImmutableSet.of(type))) { if (punishment.isActive()) { punishment.delete(removedBy, reason); - AuditLog.log(removedBy, "", req.attribute("actor"), AuditLogActionType.DELETE_PUNISHMENT, ImmutableMap.of()); + AuditLog.log(removedBy, "", ctx.get("actor"), AuditLogActionType.DELETE_PUNISHMENT, ImmutableMap.of()); return punishment; } } diff --git a/src/main/java/net/frozenorb/apiv3/routes/users/GETStaff.java b/src/main/java/net/frozenorb/apiv3/routes/users/GETStaff.java index 271eb1a..55291f2 100644 --- a/src/main/java/net/frozenorb/apiv3/routes/users/GETStaff.java +++ b/src/main/java/net/frozenorb/apiv3/routes/users/GETStaff.java @@ -4,15 +4,12 @@ import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.models.Grant; import net.frozenorb.apiv3.models.Rank; import net.frozenorb.apiv3.models.User; -import spark.Request; -import spark.Response; -import spark.Route; import java.util.*; -public final class GETStaff implements Route { +public final class GETStaff implements Handler { - public Object handle(Request req, Response res) { + public void handle(RoutingContext ctx) { Map staffRanks = new HashMap<>(); Rank.values().forEach(rank -> { diff --git a/src/main/java/net/frozenorb/apiv3/routes/users/GETUser.java b/src/main/java/net/frozenorb/apiv3/routes/users/GETUser.java index 42fefbb..f18c8ce 100644 --- a/src/main/java/net/frozenorb/apiv3/routes/users/GETUser.java +++ b/src/main/java/net/frozenorb/apiv3/routes/users/GETUser.java @@ -1,14 +1,11 @@ package net.frozenorb.apiv3.routes.users; import net.frozenorb.apiv3.models.User; -import spark.Request; -import spark.Response; -import spark.Route; -public final class GETUser implements Route { +public final class GETUser implements Handler { - public Object handle(Request req, Response res) { - return User.byId(req.params("id")); + public void handle(RoutingContext ctx) { + return User.byId(ctx.request().getParam("id")); } } \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/routes/users/GETUserDetails.java b/src/main/java/net/frozenorb/apiv3/routes/users/GETUserDetails.java index 76079c9..c4cf2ff 100644 --- a/src/main/java/net/frozenorb/apiv3/routes/users/GETUserDetails.java +++ b/src/main/java/net/frozenorb/apiv3/routes/users/GETUserDetails.java @@ -3,17 +3,14 @@ package net.frozenorb.apiv3.routes.users; import com.google.common.collect.ImmutableMap; import net.frozenorb.apiv3.models.User; import net.frozenorb.apiv3.utils.ErrorUtils; -import spark.Request; -import spark.Response; -import spark.Route; -public final class GETUserDetails implements Route { +public final class GETUserDetails implements Handler { - public Object handle(Request req, Response res) { - User user = User.byId(req.params("id")); + public void handle(RoutingContext ctx) { + User user = User.byId(ctx.request().getParam("id")); if (user == null) { - return ErrorUtils.notFound("User", req.params("id")); + return ErrorUtils.respondNotFound("User", ctx.request().getParam("id")); } // Too many fields to use .of() diff --git a/src/main/java/net/frozenorb/apiv3/routes/users/GETUserMeta.java b/src/main/java/net/frozenorb/apiv3/routes/users/GETUserMeta.java index 90dbde4..4d1e38e 100644 --- a/src/main/java/net/frozenorb/apiv3/routes/users/GETUserMeta.java +++ b/src/main/java/net/frozenorb/apiv3/routes/users/GETUserMeta.java @@ -4,23 +4,20 @@ import net.frozenorb.apiv3.models.ServerGroup; import net.frozenorb.apiv3.models.User; import net.frozenorb.apiv3.models.UserMetaEntry; import net.frozenorb.apiv3.utils.ErrorUtils; -import spark.Request; -import spark.Response; -import spark.Route; -public final class GETUserMeta implements Route { +public final class GETUserMeta implements Handler { - public Object handle(Request req, Response res) { - User user = User.byId(req.params("id")); + public void handle(RoutingContext ctx) { + User user = User.byId(ctx.request().getParam("id")); if (user == null) { - return ErrorUtils.notFound("User", req.params("id")); + return ErrorUtils.respondNotFound("User", ctx.request().getParam("id")); } - ServerGroup serverGroup = ServerGroup.byId(req.params("serverGroup")); + ServerGroup serverGroup = ServerGroup.byId(ctx.request().getParam("serverGroup")); if (serverGroup == null) { - return ErrorUtils.notFound("Server group", req.params("serverGroup")); + return ErrorUtils.respondNotFound("Server group", ctx.request().getParam("serverGroup")); } UserMetaEntry userMetaEntry = user.getMeta(serverGroup); diff --git a/src/main/java/net/frozenorb/apiv3/routes/users/GETUserRequiresTOTP.java b/src/main/java/net/frozenorb/apiv3/routes/users/GETUserRequiresTOTP.java index 201d0c7..f8b080f 100644 --- a/src/main/java/net/frozenorb/apiv3/routes/users/GETUserRequiresTOTP.java +++ b/src/main/java/net/frozenorb/apiv3/routes/users/GETUserRequiresTOTP.java @@ -5,17 +5,14 @@ import net.frozenorb.apiv3.models.User; import net.frozenorb.apiv3.utils.ErrorUtils; import net.frozenorb.apiv3.utils.IPUtils; import net.frozenorb.apiv3.utils.TOTPUtils; -import spark.Request; -import spark.Response; -import spark.Route; -public final class GETUserRequiresTOTP implements Route { +public final class GETUserRequiresTOTP implements Handler { - public Object handle(Request req, Response res) { - User user = User.byId(req.params("id")); + public void handle(RoutingContext ctx) { + User user = User.byId(ctx.request().getParam("id")); if (user == null) { - return ErrorUtils.notFound("User", req.params("id")); + return ErrorUtils.respondNotFound("User", ctx.request().getParam("id")); } if (user.getTotpSecret() == null) { @@ -25,10 +22,10 @@ public final class GETUserRequiresTOTP implements Route { ); } - String userIp = req.queryParams("userIp"); + String userIp = ctx.request().getParam("userIp"); if (!IPUtils.isValidIP(userIp)) { - return ErrorUtils.invalidInput("IP address \"" + userIp + "\" is not valid."); + return ErrorUtils.respondInvalidInput("IP address \"" + userIp + "\" is not valid."); } if (TOTPUtils.isPreAuthorized(user, userIp)) { diff --git a/src/main/java/net/frozenorb/apiv3/routes/users/GETUserVerifyPassword.java b/src/main/java/net/frozenorb/apiv3/routes/users/GETUserVerifyPassword.java index 35fdf69..cd6794d 100644 --- a/src/main/java/net/frozenorb/apiv3/routes/users/GETUserVerifyPassword.java +++ b/src/main/java/net/frozenorb/apiv3/routes/users/GETUserVerifyPassword.java @@ -3,24 +3,21 @@ package net.frozenorb.apiv3.routes.users; import com.google.common.collect.ImmutableMap; import net.frozenorb.apiv3.models.User; import net.frozenorb.apiv3.utils.ErrorUtils; -import spark.Request; -import spark.Response; -import spark.Route; -public final class GETUserVerifyPassword implements Route { +public final class GETUserVerifyPassword implements Handler { - public Object handle(Request req, Response res) { - User user = User.byId(req.params("id")); + public void handle(RoutingContext ctx) { + User user = User.byId(ctx.request().getParam("id")); if (user == null) { - return ErrorUtils.notFound("User", req.params("id")); + return ErrorUtils.respondNotFound("User", ctx.request().getParam("id")); } if (user.getPassword() == null) { - return ErrorUtils.invalidInput("User provided does not have password set."); + return ErrorUtils.respondInvalidInput("User provided does not have password set."); } - boolean authorized = user.checkPassword(req.queryParams("password")); + boolean authorized = user.checkPassword(ctx.request().getParam("password")); return ImmutableMap.of( "authorized", authorized diff --git a/src/main/java/net/frozenorb/apiv3/routes/users/POSTUserConfirmRegister.java b/src/main/java/net/frozenorb/apiv3/routes/users/POSTUserConfirmRegister.java index b70b41f..af17685 100644 --- a/src/main/java/net/frozenorb/apiv3/routes/users/POSTUserConfirmRegister.java +++ b/src/main/java/net/frozenorb/apiv3/routes/users/POSTUserConfirmRegister.java @@ -5,14 +5,11 @@ import com.google.common.collect.ImmutableMap; import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.models.User; import net.frozenorb.apiv3.utils.ErrorUtils; -import spark.Request; -import spark.Response; -import spark.Route; import java.util.List; import java.util.concurrent.TimeUnit; -public final class POSTUserConfirmRegister implements Route { +public final class POSTUserConfirmRegister implements Handler { private final List commonPasswords = ImmutableList.copyOf(("123456 password 12345678 qwerty 123456789 12345 1234 111111 1234567 dragon " + "123123 baseball abc123 football monkey letmein 696969 shadow master 666666 qwertyuiop 123321 mustang 1234567890 " + @@ -22,11 +19,11 @@ public final class POSTUserConfirmRegister implements Route { " 131313 freedom 777777 pass fuck maggie 159753 aaaaaa ginger princess joshua cheese amanda summer love ashley 6969 " + "nicole chelsea biteme matthew access yankees 987654321 dallas austin thunder taylor matrix").split(" ")); - public Object handle(Request req, Response res) { - User user = User.byEmailToken(req.params("emailToken")); + public void handle(RoutingContext ctx) { + User user = User.byEmailToken(ctx.request().getParam("emailToken")); if (user == null) { - return ErrorUtils.notFound("Email token", req.params("emailToken")); + return ErrorUtils.respondNotFound("Email token", ctx.request().getParam("emailToken")); } // We can't check email != null as that's set while we're pending @@ -39,7 +36,7 @@ public final class POSTUserConfirmRegister implements Route { return ErrorUtils.error("Email token is expired"); } - String password = req.queryParams("password"); + String password = ctx.request().getParam("password"); if (password.length() < 8) { return ErrorUtils.error("Your password is too short."); diff --git a/src/main/java/net/frozenorb/apiv3/routes/users/POSTUserLeave.java b/src/main/java/net/frozenorb/apiv3/routes/users/POSTUserLeave.java index b3c47b1..8e2a6e5 100644 --- a/src/main/java/net/frozenorb/apiv3/routes/users/POSTUserLeave.java +++ b/src/main/java/net/frozenorb/apiv3/routes/users/POSTUserLeave.java @@ -5,16 +5,15 @@ import net.frozenorb.apiv3.models.User; import net.frozenorb.apiv3.utils.ErrorUtils; import spark.Request; import spark.Response; -import spark.Route; -public class POSTUserLeave implements Route { +public class POSTUserLeave implements Handler { @Override public Object handle(Request req, Response res) throws Exception { - User user = User.byId(req.params("id")); + User user = User.byId(ctx.request().getParam("id")); if (user == null) { - return ErrorUtils.notFound("User", req.params("id")); + return ErrorUtils.respondNotFound("User", ctx.request().getParam("id")); } user.leftServer(); diff --git a/src/main/java/net/frozenorb/apiv3/routes/users/POSTUserLogin.java b/src/main/java/net/frozenorb/apiv3/routes/users/POSTUserLogin.java index ad6e4d5..9fbc900 100644 --- a/src/main/java/net/frozenorb/apiv3/routes/users/POSTUserLogin.java +++ b/src/main/java/net/frozenorb/apiv3/routes/users/POSTUserLogin.java @@ -1,6 +1,5 @@ package net.frozenorb.apiv3.routes.users; -import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.actors.Actor; import net.frozenorb.apiv3.actors.ActorType; import net.frozenorb.apiv3.models.Server; @@ -8,32 +7,29 @@ import net.frozenorb.apiv3.models.User; import net.frozenorb.apiv3.utils.ErrorUtils; import net.frozenorb.apiv3.utils.IPUtils; import net.frozenorb.apiv3.utils.UUIDUtils; -import spark.Request; -import spark.Response; -import spark.Route; import java.util.UUID; -public final class POSTUserLogin implements Route { +public final class POSTUserLogin implements Handler { - public Object handle(Request req, Response res) { - UUID uuid = UUID.fromString(req.params("id")); + public void handle(RoutingContext ctx) { + UUID uuid = UUID.fromString(ctx.request().getParam("id")); if (!UUIDUtils.isAcceptableUUID(uuid)) { - return ErrorUtils.invalidInput("UUID \"" + uuid + "\" is not valid - must be version 4 UUID."); + return ErrorUtils.respondInvalidInput("UUID \"" + uuid + "\" is not valid - must be version 4 UUID."); } User user = User.byId(uuid); - String username = req.queryParams("username"); - String userIp = req.queryParams("userIp"); - Actor actor = req.attribute("actor"); + String username = ctx.request().getParam("username"); + String userIp = ctx.request().getParam("userIp"); + Actor actor = ctx.get("actor"); if (actor.getType() != ActorType.SERVER) { - return ErrorUtils.serverOnly(); + return ErrorUtils.respondServerOnly(); } if (!IPUtils.isValidIP(userIp)) { - return ErrorUtils.invalidInput("IP address \"" + userIp + "\" is not valid."); + return ErrorUtils.respondInvalidInput("IP address \"" + userIp + "\" is not valid."); } if (user == null) { diff --git a/src/main/java/net/frozenorb/apiv3/routes/users/POSTUserNotify.java b/src/main/java/net/frozenorb/apiv3/routes/users/POSTUserNotify.java index 4774817..d79d783 100644 --- a/src/main/java/net/frozenorb/apiv3/routes/users/POSTUserNotify.java +++ b/src/main/java/net/frozenorb/apiv3/routes/users/POSTUserNotify.java @@ -5,30 +5,27 @@ import net.frozenorb.apiv3.models.NotificationTemplate; import net.frozenorb.apiv3.models.User; import net.frozenorb.apiv3.unsorted.Notification; import net.frozenorb.apiv3.utils.ErrorUtils; -import spark.Request; -import spark.Response; -import spark.Route; import java.util.HashMap; import java.util.Map; -public final class POSTUserNotify implements Route { +public final class POSTUserNotify implements Handler { - public Object handle(Request req, Response res) { - User user = User.byId(req.params("id")); + public void handle(RoutingContext ctx) { + User user = User.byId(ctx.request().getParam("id")); if (user == null) { - return ErrorUtils.notFound("User", req.params("id")); + return ErrorUtils.respondNotFound("User", ctx.request().getParam("id")); } if (user.getEmail() == null) { return ErrorUtils.error("User provided does not have email set."); } - NotificationTemplate template = NotificationTemplate.byId(req.queryParams("template")); + NotificationTemplate template = NotificationTemplate.byId(ctx.request().getParam("template")); if (template == null) { - return ErrorUtils.notFound("Notification template", req.queryParams("template")); + return ErrorUtils.respondNotFound("Notification template", ctx.request().getParam("template")); } Map subjectReplacements = new HashMap<>(); diff --git a/src/main/java/net/frozenorb/apiv3/routes/users/POSTUserRegister.java b/src/main/java/net/frozenorb/apiv3/routes/users/POSTUserRegister.java index 185fa74..7b80338 100644 --- a/src/main/java/net/frozenorb/apiv3/routes/users/POSTUserRegister.java +++ b/src/main/java/net/frozenorb/apiv3/routes/users/POSTUserRegister.java @@ -6,9 +6,6 @@ import net.frozenorb.apiv3.models.NotificationTemplate; import net.frozenorb.apiv3.models.User; import net.frozenorb.apiv3.unsorted.Notification; import net.frozenorb.apiv3.utils.ErrorUtils; -import spark.Request; -import spark.Response; -import spark.Route; import java.math.BigInteger; import java.util.Date; @@ -17,25 +14,25 @@ import java.util.Random; import java.util.concurrent.TimeUnit; import java.util.regex.Pattern; -public final class POSTUserRegister implements Route { +public final class POSTUserRegister implements Handler { private static final Pattern VALID_EMAIL_PATTERN = Pattern.compile( "^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$", Pattern.CASE_INSENSITIVE ); - public Object handle(Request req, Response res) { - User user = User.byId(req.params("id")); + public void handle(RoutingContext ctx) { + User user = User.byId(ctx.request().getParam("id")); if (user == null) { - return ErrorUtils.notFound("User", req.params("id")); + return ErrorUtils.respondNotFound("User", ctx.request().getParam("id")); } if (user.getEmail() != null) { - return ErrorUtils.invalidInput("User provided already has email set."); + return ErrorUtils.respondInvalidInput("User provided already has email set."); } - String email = req.queryParams("email"); + String email = ctx.request().getParam("email"); if (!VALID_EMAIL_PATTERN.matcher(email).matches()) { return ErrorUtils.error(email + " is not a valid email."); diff --git a/src/main/java/net/frozenorb/apiv3/routes/users/POSTUserSetupTOTP.java b/src/main/java/net/frozenorb/apiv3/routes/users/POSTUserSetupTOTP.java index 0305087..5a4e9c6 100644 --- a/src/main/java/net/frozenorb/apiv3/routes/users/POSTUserSetupTOTP.java +++ b/src/main/java/net/frozenorb/apiv3/routes/users/POSTUserSetupTOTP.java @@ -6,21 +6,18 @@ import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.models.User; import net.frozenorb.apiv3.utils.ErrorUtils; import net.frozenorb.apiv3.utils.TOTPUtils; -import spark.Request; -import spark.Response; -import spark.Route; -public final class POSTUserSetupTOTP implements Route { +public final class POSTUserSetupTOTP implements Handler { - public Object handle(Request req, Response res) { - User user = User.byId(req.params("id")); + public void handle(RoutingContext ctx) { + User user = User.byId(ctx.request().getParam("id")); if (user == null) { - return ErrorUtils.notFound("User", req.params("id")); + return ErrorUtils.respondNotFound("User", ctx.request().getParam("id")); } if (user.getTotpSecret() != null) { - return ErrorUtils.invalidInput("User provided already has TOTP code set."); + return ErrorUtils.respondInvalidInput("User provided already has TOTP code set."); } GoogleAuthenticatorKey generated = TOTPUtils.generateTOTPKey(); diff --git a/src/main/java/net/frozenorb/apiv3/routes/users/POSTUserVerifyTOTP.java b/src/main/java/net/frozenorb/apiv3/routes/users/POSTUserVerifyTOTP.java index 2e0c19b..2deb032 100644 --- a/src/main/java/net/frozenorb/apiv3/routes/users/POSTUserVerifyTOTP.java +++ b/src/main/java/net/frozenorb/apiv3/routes/users/POSTUserVerifyTOTP.java @@ -5,32 +5,29 @@ import net.frozenorb.apiv3.models.User; import net.frozenorb.apiv3.utils.ErrorUtils; import net.frozenorb.apiv3.utils.IPUtils; import net.frozenorb.apiv3.utils.TOTPUtils; -import spark.Request; -import spark.Response; -import spark.Route; import java.util.concurrent.TimeUnit; -public final class POSTUserVerifyTOTP implements Route { +public final class POSTUserVerifyTOTP implements Handler { - public Object handle(Request req, Response res) { - User user = User.byId(req.params("id")); + public void handle(RoutingContext ctx) { + User user = User.byId(ctx.request().getParam("id")); if (user == null) { - return ErrorUtils.notFound("User", req.params("id")); + return ErrorUtils.respondNotFound("User", ctx.request().getParam("id")); } if (user.getTotpSecret() == null) { - return ErrorUtils.invalidInput("User provided does not have TOTP code set."); + return ErrorUtils.respondInvalidInput("User provided does not have TOTP code set."); } - String userIp = req.queryParams("userIp"); + String userIp = ctx.request().getParam("userIp"); if (!IPUtils.isValidIP(userIp)) { - return ErrorUtils.invalidInput("IP address \"" + userIp + "\" is not valid."); + return ErrorUtils.respondInvalidInput("IP address \"" + userIp + "\" is not valid."); } - int providedCode = Integer.parseInt(req.queryParams("code")); + int providedCode = Integer.parseInt(ctx.request().getParam("code")); if (TOTPUtils.wasRecentlyUsed(user, providedCode)) { return ImmutableMap.of( diff --git a/src/main/java/net/frozenorb/apiv3/routes/users/PUTUserMeta.java b/src/main/java/net/frozenorb/apiv3/routes/users/PUTUserMeta.java index 3eded9f..0ff4eca 100644 --- a/src/main/java/net/frozenorb/apiv3/routes/users/PUTUserMeta.java +++ b/src/main/java/net/frozenorb/apiv3/routes/users/PUTUserMeta.java @@ -4,23 +4,20 @@ import net.frozenorb.apiv3.models.ServerGroup; import net.frozenorb.apiv3.models.User; import net.frozenorb.apiv3.utils.ErrorUtils; import org.bson.Document; -import spark.Request; -import spark.Response; -import spark.Route; -public final class PUTUserMeta implements Route { +public final class PUTUserMeta implements Handler { - public Object handle(Request req, Response res) { - User user = User.byId(req.params("id")); + public void handle(RoutingContext ctx) { + User user = User.byId(ctx.request().getParam("id")); if (user == null) { - return ErrorUtils.notFound("User", req.params("id")); + return ErrorUtils.respondNotFound("User", ctx.request().getParam("id")); } - ServerGroup serverGroup = ServerGroup.byId(req.params("serverGroup")); + ServerGroup serverGroup = ServerGroup.byId(ctx.request().getParam("serverGroup")); if (serverGroup == null) { - return ErrorUtils.notFound("Server group", req.params("serverGroup")); + return ErrorUtils.respondNotFound("Server group", ctx.request().getParam("serverGroup")); } Document data = Document.parse(req.body()); diff --git a/src/main/java/net/frozenorb/apiv3/utils/ErrorUtils.java b/src/main/java/net/frozenorb/apiv3/utils/ErrorUtils.java index e0b4c83..9e25530 100644 --- a/src/main/java/net/frozenorb/apiv3/utils/ErrorUtils.java +++ b/src/main/java/net/frozenorb/apiv3/utils/ErrorUtils.java @@ -1,34 +1,34 @@ package net.frozenorb.apiv3.utils; import com.google.common.collect.ImmutableMap; +import io.vertx.ext.web.RoutingContext; import lombok.experimental.UtilityClass; - -import java.util.Map; +import net.frozenorb.apiv3.APIv3; @UtilityClass public class ErrorUtils { - public static Map serverOnly() { - return error("This action can only be performed when requested by a server."); + public static void respondServerOnly(RoutingContext ctx) { + error(ctx, "This action can only be performed when requested by a server."); } - public static Map notFound(String itemType, String id) { - return error("Not found: " + itemType + " with id " + id + " cannot be found."); + public static void respondNotFound(RoutingContext ctx, String itemType, String id) { + error(ctx, "Not found: " + itemType + " with id " + id + " cannot be found."); } - public static Map invalidInput(String message) { - return error("Invalid input: " + message); + public static void respondInvalidInput(RoutingContext ctx, String message) { + error(ctx, "Invalid input: " + message + "."); } - public static Map requiredInput(String field) { - return error("Field \"" + field + "\" is required."); + public static void respondRequiredInput(RoutingContext ctx, String field) { + error(ctx, "Field \"" + field + "\" is required."); } - public static Map error(String message) { - return ImmutableMap.of( + public static void error(RoutingContext ctx, String message) { + APIv3.respond(ctx, ImmutableMap.of( "success", false, "message", message - ); + )); } } \ No newline at end of file