From 38f4b4d08456885d68169c9ed9eb0642fd802e05 Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Sat, 6 Jul 2019 15:24:01 +0300 Subject: [PATCH] Split ServerParameterJSONHandler to own utility --- .../djrapitops/plan/system/Identifiers.java | 69 +++++++++++++++++++ .../pages/json/GraphsJSONHandler.java | 12 ++-- .../OnlineActivityOverviewJSONHandler.java | 12 ++-- .../pages/json/PlayersTableJSONHandler.java | 12 ++-- .../pages/json/ServerOverviewJSONHandler.java | 12 ++-- 5 files changed, 97 insertions(+), 20 deletions(-) create mode 100644 Plan/common/src/main/java/com/djrapitops/plan/system/Identifiers.java diff --git a/Plan/common/src/main/java/com/djrapitops/plan/system/Identifiers.java b/Plan/common/src/main/java/com/djrapitops/plan/system/Identifiers.java new file mode 100644 index 000000000..f2342a8fa --- /dev/null +++ b/Plan/common/src/main/java/com/djrapitops/plan/system/Identifiers.java @@ -0,0 +1,69 @@ +/* + * This file is part of Player Analytics (Plan). + * + * Plan is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License v3 as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Plan is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Plan. If not, see . + */ +package com.djrapitops.plan.system; + +import com.djrapitops.plan.api.exceptions.connection.BadRequestException; +import com.djrapitops.plan.db.access.queries.objects.ServerQueries; +import com.djrapitops.plan.system.database.DBSystem; +import com.djrapitops.plan.system.info.server.Server; +import com.djrapitops.plan.system.webserver.RequestTarget; + +import javax.inject.Inject; +import javax.inject.Singleton; +import java.util.Optional; +import java.util.UUID; + +/** + * Utility for getting server identifier from different sources. + * + * @author Rsl1122 + */ +@Singleton +public class Identifiers { + + protected final DBSystem dbSystem; + + @Inject + public Identifiers(DBSystem dbSystem) { + this.dbSystem = dbSystem; + } + + public UUID getServerUUID(RequestTarget target) throws BadRequestException { + Optional serverUUID = target.getParameter("serverUUID"); + if (serverUUID.isPresent()) { + return getServerUUIDDirectly(serverUUID.get()); + } else { + return getServerUUIDFromName(target); // Preferred + } + } + + private UUID getServerUUIDFromName(RequestTarget target) throws BadRequestException { + String serverName = target.getParameter("serverName") + .orElseThrow(() -> new BadRequestException("'serverName' parameter was not defined.")); + return dbSystem.getDatabase().query(ServerQueries.fetchServerMatchingIdentifier(serverName)) + .map(Server::getUuid) + .orElseThrow(() -> new BadRequestException("'serverName' was not found in the database.: '" + serverName + "'")); + } + + private UUID getServerUUIDDirectly(String serverUUIDString) throws BadRequestException { + try { + return UUID.fromString(serverUUIDString); + } catch (IllegalArgumentException malformedUUIDException) { + throw new BadRequestException("'serverName' was not a valid UUID: " + malformedUUIDException.getMessage()); + } + } +} \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/system/webserver/pages/json/GraphsJSONHandler.java b/Plan/common/src/main/java/com/djrapitops/plan/system/webserver/pages/json/GraphsJSONHandler.java index e0b427a71..44be51da2 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/system/webserver/pages/json/GraphsJSONHandler.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/system/webserver/pages/json/GraphsJSONHandler.java @@ -19,11 +19,12 @@ package com.djrapitops.plan.system.webserver.pages.json; import com.djrapitops.plan.api.exceptions.WebUserAuthException; import com.djrapitops.plan.api.exceptions.connection.BadRequestException; import com.djrapitops.plan.api.exceptions.connection.WebException; -import com.djrapitops.plan.system.database.DBSystem; +import com.djrapitops.plan.system.Identifiers; import com.djrapitops.plan.system.json.GraphJSONParser; import com.djrapitops.plan.system.webserver.Request; import com.djrapitops.plan.system.webserver.RequestTarget; import com.djrapitops.plan.system.webserver.auth.Authentication; +import com.djrapitops.plan.system.webserver.pages.PageHandler; import com.djrapitops.plan.system.webserver.response.Response; import com.djrapitops.plan.system.webserver.response.data.JSONResponse; @@ -37,22 +38,23 @@ import java.util.UUID; * @author Rsl1122 */ @Singleton -public class GraphsJSONHandler extends ServerParameterJSONHandler { +public class GraphsJSONHandler implements PageHandler { + private final Identifiers identifiers; private final GraphJSONParser graphJSON; @Inject public GraphsJSONHandler( - DBSystem dbSystem, + Identifiers identifiers, GraphJSONParser graphJSON ) { - super(dbSystem); + this.identifiers = identifiers; this.graphJSON = graphJSON; } @Override public Response getResponse(Request request, RequestTarget target) throws WebException { - UUID serverUUID = getServerUUID(target); // Can throw BadRequestException + UUID serverUUID = identifiers.getServerUUID(target); // Can throw BadRequestException String type = target.getParameter("type") .orElseThrow(() -> new BadRequestException("'type' parameter was not defined.")); String graphDataJSON = generateGraphDataJSONOfType(type, serverUUID); diff --git a/Plan/common/src/main/java/com/djrapitops/plan/system/webserver/pages/json/OnlineActivityOverviewJSONHandler.java b/Plan/common/src/main/java/com/djrapitops/plan/system/webserver/pages/json/OnlineActivityOverviewJSONHandler.java index a21e44404..b1793d916 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/system/webserver/pages/json/OnlineActivityOverviewJSONHandler.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/system/webserver/pages/json/OnlineActivityOverviewJSONHandler.java @@ -18,11 +18,12 @@ package com.djrapitops.plan.system.webserver.pages.json; import com.djrapitops.plan.api.exceptions.WebUserAuthException; import com.djrapitops.plan.api.exceptions.connection.WebException; -import com.djrapitops.plan.system.database.DBSystem; +import com.djrapitops.plan.system.Identifiers; import com.djrapitops.plan.system.json.OnlineActivityOverviewJSONParser; import com.djrapitops.plan.system.webserver.Request; import com.djrapitops.plan.system.webserver.RequestTarget; import com.djrapitops.plan.system.webserver.auth.Authentication; +import com.djrapitops.plan.system.webserver.pages.PageHandler; import com.djrapitops.plan.system.webserver.response.Response; import com.djrapitops.plan.system.webserver.response.data.JSONResponse; @@ -36,22 +37,23 @@ import java.util.UUID; * @author Rsl1122 */ @Singleton -public class OnlineActivityOverviewJSONHandler extends ServerParameterJSONHandler { +public class OnlineActivityOverviewJSONHandler implements PageHandler { + private final Identifiers identifiers; private final OnlineActivityOverviewJSONParser jsonParser; @Inject public OnlineActivityOverviewJSONHandler( - DBSystem dbSystem, + Identifiers identifiers, OnlineActivityOverviewJSONParser jsonParser ) { - super(dbSystem); + this.identifiers = identifiers; this.jsonParser = jsonParser; } @Override public Response getResponse(Request request, RequestTarget target) throws WebException { - UUID serverUUID = getServerUUID(target); // Can throw BadRequestException + UUID serverUUID = identifiers.getServerUUID(target); // Can throw BadRequestException return new JSONResponse<>(jsonParser.createJSONAsMap(serverUUID)); } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/system/webserver/pages/json/PlayersTableJSONHandler.java b/Plan/common/src/main/java/com/djrapitops/plan/system/webserver/pages/json/PlayersTableJSONHandler.java index ec00450d9..67de843d6 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/system/webserver/pages/json/PlayersTableJSONHandler.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/system/webserver/pages/json/PlayersTableJSONHandler.java @@ -18,12 +18,13 @@ package com.djrapitops.plan.system.webserver.pages.json; import com.djrapitops.plan.api.exceptions.WebUserAuthException; import com.djrapitops.plan.api.exceptions.connection.WebException; -import com.djrapitops.plan.system.database.DBSystem; +import com.djrapitops.plan.system.Identifiers; import com.djrapitops.plan.system.json.JSONFactory; import com.djrapitops.plan.system.json.PlayersTableJSONParser; import com.djrapitops.plan.system.webserver.Request; import com.djrapitops.plan.system.webserver.RequestTarget; import com.djrapitops.plan.system.webserver.auth.Authentication; +import com.djrapitops.plan.system.webserver.pages.PageHandler; import com.djrapitops.plan.system.webserver.response.Response; import com.djrapitops.plan.system.webserver.response.data.JSONResponse; @@ -38,22 +39,23 @@ import java.util.UUID; * @see PlayersTableJSONParser For JSON parsing of /server players table. */ @Singleton -public class PlayersTableJSONHandler extends ServerParameterJSONHandler { +public class PlayersTableJSONHandler implements PageHandler { + private final Identifiers identifiers; private final JSONFactory jsonFactory; @Inject public PlayersTableJSONHandler( - DBSystem dbSystem, + Identifiers identifiers, JSONFactory jsonFactory ) { - super(dbSystem); + this.identifiers = identifiers; this.jsonFactory = jsonFactory; } @Override public Response getResponse(Request request, RequestTarget target) throws WebException { - UUID serverUUID = getServerUUID(target); // Can throw BadRequestException + UUID serverUUID = identifiers.getServerUUID(target); // Can throw BadRequestException return new JSONResponse(jsonFactory.serverPlayersTableJSON(serverUUID)); } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/system/webserver/pages/json/ServerOverviewJSONHandler.java b/Plan/common/src/main/java/com/djrapitops/plan/system/webserver/pages/json/ServerOverviewJSONHandler.java index 4fb13f7d7..3103b6507 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/system/webserver/pages/json/ServerOverviewJSONHandler.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/system/webserver/pages/json/ServerOverviewJSONHandler.java @@ -18,11 +18,12 @@ package com.djrapitops.plan.system.webserver.pages.json; import com.djrapitops.plan.api.exceptions.WebUserAuthException; import com.djrapitops.plan.api.exceptions.connection.WebException; -import com.djrapitops.plan.system.database.DBSystem; +import com.djrapitops.plan.system.Identifiers; import com.djrapitops.plan.system.json.ServerOverviewJSONParser; import com.djrapitops.plan.system.webserver.Request; import com.djrapitops.plan.system.webserver.RequestTarget; import com.djrapitops.plan.system.webserver.auth.Authentication; +import com.djrapitops.plan.system.webserver.pages.PageHandler; import com.djrapitops.plan.system.webserver.response.Response; import com.djrapitops.plan.system.webserver.response.data.JSONResponse; @@ -36,22 +37,23 @@ import java.util.UUID; * @author Rsl1122 */ @Singleton -public class ServerOverviewJSONHandler extends ServerParameterJSONHandler { +public class ServerOverviewJSONHandler implements PageHandler { + private final Identifiers identifiers; private final ServerOverviewJSONParser jsonParser; @Inject public ServerOverviewJSONHandler( - DBSystem dbSystem, + Identifiers identifiers, ServerOverviewJSONParser jsonParser ) { - super(dbSystem); + this.identifiers = identifiers; this.jsonParser = jsonParser; } @Override public Response getResponse(Request request, RequestTarget target) throws WebException { - UUID serverUUID = getServerUUID(target); // Can throw BadRequestException + UUID serverUUID = identifiers.getServerUUID(target); // Can throw BadRequestException return new JSONResponse<>(jsonParser.createJSONAsMap(serverUUID)); }