From b2cd332c9b1e13485b8be1517573eec8f11c8865 Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Thu, 5 Dec 2019 19:42:20 +0200 Subject: [PATCH] Rename: Page/JSONHandler -> Page/JSONResolver Reasoning: 'Handler' does not give extra information to the reader. 'Resolver' tells something is being decided/found answer to or resolution is found (Response in this case) - Comments related to Response Handlers were updated. - One missed Response in ResponseResolver now built using ResponseFactory --- .../delivery/export/NetworkPageExporter.java | 8 +-- .../delivery/export/PlayerPageExporter.java | 8 +-- .../delivery/export/PlayersPageExporter.java | 8 +-- .../delivery/export/ServerPageExporter.java | 8 +-- .../delivery/webserver/RequestHandler.java | 12 ++-- ...onseHandler.java => ResponseResolver.java} | 66 ++++++++++--------- .../plan/delivery/webserver/WebServer.java | 2 +- .../delivery/webserver/cache/JSONCache.java | 3 +- ...andler.java => CompositePageResolver.java} | 42 +++++++----- ...ageHandler.java => DebugPageResolver.java} | 8 +-- .../{PageHandler.java => PageResolver.java} | 11 ++-- ...geHandler.java => PlayerPageResolver.java} | 8 +-- ...eHandler.java => PlayersPageResolver.java} | 8 +-- ...PageHandler.java => RootPageResolver.java} | 10 ++- ...geHandler.java => ServerPageResolver.java} | 8 +-- ...ONHandler.java => GraphsJSONResolver.java} | 10 +-- ...NHandler.java => NetworkJSONResolver.java} | 10 +-- ...ndler.java => NetworkTabJSONResolver.java} | 10 +-- ...ONHandler.java => PlayerJSONResolver.java} | 8 +-- ...dler.java => PlayerKillsJSONResolver.java} | 8 +-- ...ler.java => PlayersTableJSONResolver.java} | 12 ++-- ...JSONHandler.java => RootJSONResolver.java} | 38 +++++------ ...andler.java => ServerTabJSONResolver.java} | 10 +-- ...Handler.java => SessionsJSONResolver.java} | 8 +-- .../webserver/response/ResponseFactory.java | 9 +-- 25 files changed, 170 insertions(+), 163 deletions(-) rename Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/{ResponseHandler.java => ResponseResolver.java} (68%) rename Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/{TreePageHandler.java => CompositePageResolver.java} (63%) rename Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/{DebugPageHandler.java => DebugPageResolver.java} (88%) rename Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/{PageHandler.java => PageResolver.java} (82%) rename Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/{PlayerPageHandler.java => PlayerPageResolver.java} (93%) rename Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/{PlayersPageHandler.java => PlayersPageResolver.java} (91%) rename Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/{RootPageHandler.java => RootPageResolver.java} (89%) rename Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/{ServerPageHandler.java => ServerPageResolver.java} (94%) rename Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/{GraphsJSONHandler.java => GraphsJSONResolver.java} (94%) rename Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/{NetworkJSONHandler.java => NetworkJSONResolver.java} (89%) rename Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/{NetworkTabJSONHandler.java => NetworkTabJSONResolver.java} (83%) rename Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/{PlayerJSONHandler.java => PlayerJSONResolver.java} (87%) rename Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/{PlayerKillsJSONHandler.java => PlayerKillsJSONResolver.java} (90%) rename Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/{PlayersTableJSONHandler.java => PlayersTableJSONResolver.java} (84%) rename Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/{RootJSONHandler.java => RootJSONResolver.java} (69%) rename Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/{ServerTabJSONHandler.java => ServerTabJSONResolver.java} (86%) rename Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/{SessionsJSONHandler.java => SessionsJSONResolver.java} (91%) diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/export/NetworkPageExporter.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/export/NetworkPageExporter.java index c6fc32bd2..f836819f7 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/export/NetworkPageExporter.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/export/NetworkPageExporter.java @@ -19,7 +19,7 @@ package com.djrapitops.plan.delivery.export; import com.djrapitops.plan.delivery.rendering.pages.Page; import com.djrapitops.plan.delivery.rendering.pages.PageFactory; import com.djrapitops.plan.delivery.webserver.RequestTarget; -import com.djrapitops.plan.delivery.webserver.pages.json.RootJSONHandler; +import com.djrapitops.plan.delivery.webserver.pages.json.RootJSONResolver; import com.djrapitops.plan.delivery.webserver.response.Response; import com.djrapitops.plan.delivery.webserver.response.errors.ErrorResponse; import com.djrapitops.plan.exceptions.ParseException; @@ -51,7 +51,7 @@ public class NetworkPageExporter extends FileExporter { private final PlanFiles files; private final DBSystem dbSystem; private final PageFactory pageFactory; - private final RootJSONHandler jsonHandler; + private final RootJSONResolver jsonHandler; private final Locale locale; private final Theme theme; @@ -62,7 +62,7 @@ public class NetworkPageExporter extends FileExporter { PlanFiles files, DBSystem dbSystem, PageFactory pageFactory, - RootJSONHandler jsonHandler, + RootJSONResolver jsonHandler, Locale locale, Theme theme ) { @@ -141,7 +141,7 @@ public class NetworkPageExporter extends FileExporter { private Response getJSONResponse(String resource) { try { - return jsonHandler.getResponse(null, new RequestTarget(URI.create(resource))); + return jsonHandler.resolve(null, new RequestTarget(URI.create(resource))); } catch (WebException e) { // The rest of the exceptions should not be thrown throw new IllegalStateException("Unexpected exception thrown: " + e.toString(), e); diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/export/PlayerPageExporter.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/export/PlayerPageExporter.java index abfa98971..9d4993666 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/export/PlayerPageExporter.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/export/PlayerPageExporter.java @@ -19,7 +19,7 @@ package com.djrapitops.plan.delivery.export; import com.djrapitops.plan.delivery.rendering.pages.Page; import com.djrapitops.plan.delivery.rendering.pages.PageFactory; import com.djrapitops.plan.delivery.webserver.RequestTarget; -import com.djrapitops.plan.delivery.webserver.pages.json.RootJSONHandler; +import com.djrapitops.plan.delivery.webserver.pages.json.RootJSONResolver; import com.djrapitops.plan.delivery.webserver.response.Response; import com.djrapitops.plan.delivery.webserver.response.errors.ErrorResponse; import com.djrapitops.plan.exceptions.ParseException; @@ -52,7 +52,7 @@ public class PlayerPageExporter extends FileExporter { private final PlanFiles files; private final DBSystem dbSystem; private final PageFactory pageFactory; - private final RootJSONHandler jsonHandler; + private final RootJSONResolver jsonHandler; private final Locale locale; private final Theme theme; @@ -63,7 +63,7 @@ public class PlayerPageExporter extends FileExporter { PlanFiles files, DBSystem dbSystem, PageFactory pageFactory, - RootJSONHandler jsonHandler, + RootJSONResolver jsonHandler, Locale locale, Theme theme ) { @@ -124,7 +124,7 @@ public class PlayerPageExporter extends FileExporter { private Response getJSONResponse(String resource) { try { - return jsonHandler.getResponse(null, new RequestTarget(URI.create(resource))); + return jsonHandler.resolve(null, new RequestTarget(URI.create(resource))); } catch (WebException e) { // The rest of the exceptions should not be thrown throw new IllegalStateException("Unexpected exception thrown: " + e.toString(), e); diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/export/PlayersPageExporter.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/export/PlayersPageExporter.java index 295c34ec9..93dff7f35 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/export/PlayersPageExporter.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/export/PlayersPageExporter.java @@ -19,7 +19,7 @@ package com.djrapitops.plan.delivery.export; import com.djrapitops.plan.delivery.rendering.pages.Page; import com.djrapitops.plan.delivery.rendering.pages.PageFactory; import com.djrapitops.plan.delivery.webserver.RequestTarget; -import com.djrapitops.plan.delivery.webserver.pages.json.RootJSONHandler; +import com.djrapitops.plan.delivery.webserver.pages.json.RootJSONResolver; import com.djrapitops.plan.delivery.webserver.response.Response; import com.djrapitops.plan.delivery.webserver.response.errors.ErrorResponse; import com.djrapitops.plan.exceptions.ParseException; @@ -51,7 +51,7 @@ public class PlayersPageExporter extends FileExporter { private final PlanFiles files; private final DBSystem dbSystem; private final PageFactory pageFactory; - private final RootJSONHandler jsonHandler; + private final RootJSONResolver jsonHandler; private final Locale locale; private final Theme theme; private final ServerInfo serverInfo; @@ -63,7 +63,7 @@ public class PlayersPageExporter extends FileExporter { PlanFiles files, DBSystem dbSystem, PageFactory pageFactory, - RootJSONHandler jsonHandler, + RootJSONResolver jsonHandler, Locale locale, Theme theme, ServerInfo serverInfo @@ -119,7 +119,7 @@ public class PlayersPageExporter extends FileExporter { private Response getJSONResponse(String resource) { try { - return jsonHandler.getResponse(null, new RequestTarget(URI.create(resource))); + return jsonHandler.resolve(null, new RequestTarget(URI.create(resource))); } catch (WebException e) { // The rest of the exceptions should not be thrown throw new IllegalStateException("Unexpected exception thrown: " + e.toString(), e); diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/export/ServerPageExporter.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/export/ServerPageExporter.java index 7140ace28..5c5bfc0be 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/export/ServerPageExporter.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/export/ServerPageExporter.java @@ -19,7 +19,7 @@ package com.djrapitops.plan.delivery.export; import com.djrapitops.plan.delivery.rendering.pages.Page; import com.djrapitops.plan.delivery.rendering.pages.PageFactory; import com.djrapitops.plan.delivery.webserver.RequestTarget; -import com.djrapitops.plan.delivery.webserver.pages.json.RootJSONHandler; +import com.djrapitops.plan.delivery.webserver.pages.json.RootJSONResolver; import com.djrapitops.plan.delivery.webserver.response.Response; import com.djrapitops.plan.delivery.webserver.response.errors.ErrorResponse; import com.djrapitops.plan.exceptions.ParseException; @@ -53,7 +53,7 @@ public class ServerPageExporter extends FileExporter { private final PlanFiles files; private final PageFactory pageFactory; private final DBSystem dbSystem; - private final RootJSONHandler jsonHandler; + private final RootJSONResolver jsonHandler; private final Locale locale; private final Theme theme; private final ServerInfo serverInfo; @@ -65,7 +65,7 @@ public class ServerPageExporter extends FileExporter { PlanFiles files, PageFactory pageFactory, DBSystem dbSystem, - RootJSONHandler jsonHandler, + RootJSONResolver jsonHandler, Locale locale, Theme theme, ServerInfo serverInfo // To know if current server is a Proxy @@ -153,7 +153,7 @@ public class ServerPageExporter extends FileExporter { private Response getJSONResponse(String resource) { try { - return jsonHandler.getResponse(null, new RequestTarget(URI.create(resource))); + return jsonHandler.resolve(null, new RequestTarget(URI.create(resource))); } catch (WebException e) { // The rest of the exceptions should not be thrown throw new IllegalStateException("Unexpected exception thrown: " + e.toString(), e); diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/RequestHandler.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/RequestHandler.java index 952016ea1..8fd94640e 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/RequestHandler.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/RequestHandler.java @@ -56,7 +56,7 @@ public class RequestHandler implements HttpHandler { private final PlanConfig config; private final Theme theme; private final DBSystem dbSystem; - private final ResponseHandler responseHandler; + private final ResponseResolver responseResolver; private final ResponseFactory responseFactory; private final PluginLogger logger; private final ErrorHandler errorHandler; @@ -71,7 +71,7 @@ public class RequestHandler implements HttpHandler { PlanConfig config, Theme theme, DBSystem dbSystem, - ResponseHandler responseHandler, + ResponseResolver responseResolver, ResponseFactory responseFactory, PluginLogger logger, ErrorHandler errorHandler @@ -80,7 +80,7 @@ public class RequestHandler implements HttpHandler { this.config = config; this.theme = theme; this.dbSystem = dbSystem; - this.responseHandler = responseHandler; + this.responseResolver = responseResolver; this.responseFactory = responseFactory; this.logger = logger; this.errorHandler = errorHandler; @@ -96,7 +96,7 @@ public class RequestHandler implements HttpHandler { try { Response response = shouldPreventRequest(request.getRemoteAddress()) // Forbidden response (Optional) - .orElse(responseHandler.getResponse(request)); // Or the actual requested response + .orElse(responseResolver.getResponse(request)); // Or the actual requested response // Increase attempt count and block if too high Optional forbid = handlePasswordBruteForceAttempts(request, response); @@ -180,7 +180,7 @@ public class RequestHandler implements HttpHandler { return null; } - public ResponseHandler getResponseHandler() { - return responseHandler; + public ResponseResolver getResponseResolver() { + return responseResolver; } } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/ResponseHandler.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/ResponseResolver.java similarity index 68% rename from Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/ResponseHandler.java rename to Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/ResponseResolver.java index 30277fa28..acefdd7d3 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/ResponseHandler.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/ResponseResolver.java @@ -18,10 +18,9 @@ package com.djrapitops.plan.delivery.webserver; import com.djrapitops.plan.delivery.webserver.auth.Authentication; import com.djrapitops.plan.delivery.webserver.pages.*; -import com.djrapitops.plan.delivery.webserver.pages.json.RootJSONHandler; +import com.djrapitops.plan.delivery.webserver.pages.json.RootJSONResolver; import com.djrapitops.plan.delivery.webserver.response.Response; import com.djrapitops.plan.delivery.webserver.response.ResponseFactory; -import com.djrapitops.plan.delivery.webserver.response.errors.BadRequestResponse; import com.djrapitops.plan.exceptions.WebUserAuthException; import com.djrapitops.plan.exceptions.connection.BadRequestException; import com.djrapitops.plan.exceptions.connection.ForbiddenException; @@ -37,59 +36,62 @@ import javax.inject.Singleton; import java.util.Optional; /** - * Handles choosing of the correct response to a request. + * Resolves All URLs. + *

+ * - Some URLs are resolved with other PageResolvers pointed at pages. + * - Some URLs point to resources that are resolved differently, those implementations are in this file. * * @author Rsl1122 */ @Singleton -public class ResponseHandler extends TreePageHandler { +public class ResponseResolver extends CompositePageResolver { - private final DebugPageHandler debugPageHandler; - private final PlayersPageHandler playersPageHandler; - private final PlayerPageHandler playerPageHandler; - private final ServerPageHandler serverPageHandler; - private final RootJSONHandler rootJSONHandler; + private final DebugPageResolver debugPageResolver; + private final PlayersPageResolver playersPageResolver; + private final PlayerPageResolver playerPageResolver; + private final ServerPageResolver serverPageResolver; + private final RootJSONResolver rootJSONResolver; private final ErrorHandler errorHandler; private final ServerInfo serverInfo; private final Lazy webServer; @Inject - public ResponseHandler( + public ResponseResolver( ResponseFactory responseFactory, Lazy webServer, ServerInfo serverInfo, - DebugPageHandler debugPageHandler, - PlayersPageHandler playersPageHandler, - PlayerPageHandler playerPageHandler, - ServerPageHandler serverPageHandler, - RootJSONHandler rootJSONHandler, + DebugPageResolver debugPageResolver, + PlayersPageResolver playersPageResolver, + PlayerPageResolver playerPageResolver, + ServerPageResolver serverPageResolver, + RootJSONResolver rootJSONResolver, ErrorHandler errorHandler ) { super(responseFactory); this.webServer = webServer; this.serverInfo = serverInfo; - this.debugPageHandler = debugPageHandler; - this.playersPageHandler = playersPageHandler; - this.playerPageHandler = playerPageHandler; - this.serverPageHandler = serverPageHandler; - this.rootJSONHandler = rootJSONHandler; + this.debugPageResolver = debugPageResolver; + this.playersPageResolver = playersPageResolver; + this.playerPageResolver = playerPageResolver; + this.serverPageResolver = serverPageResolver; + this.rootJSONResolver = rootJSONResolver; this.errorHandler = errorHandler; } public void registerPages() { - registerPage("debug", debugPageHandler); - registerPage("players", playersPageHandler); - registerPage("player", playerPageHandler); + registerPage("debug", debugPageResolver); + registerPage("players", playersPageResolver); + registerPage("player", playerPageResolver); - registerPage("network", serverPageHandler); - registerPage("server", serverPageHandler); + registerPage("network", serverPageResolver); + registerPage("server", serverPageResolver); - registerPage("", new RootPageHandler(responseFactory, webServer.get(), serverInfo)); + registerPage("", new RootPageResolver(responseFactory, webServer.get(), serverInfo)); - registerPage("v1", rootJSONHandler); + registerPage("v1", rootJSONResolver); } public Response getResponse(Request request) { @@ -102,7 +104,7 @@ public class ResponseHandler extends TreePageHandler { } catch (ForbiddenException e) { return responseFactory.forbidden403(e.getMessage()); } catch (BadRequestException e) { - return new BadRequestResponse(e.getMessage() + " (when requesting '" + request.getTargetString() + "')"); + return responseFactory.badRequest(e.getMessage(), request.getTargetString()); } catch (Exception e) { errorHandler.log(L.ERROR, this.getClass(), e); return responseFactory.internalErrorResponse(e, request.getTargetString()); @@ -135,12 +137,12 @@ public class ResponseHandler extends TreePageHandler { return responseFactory.forbidden403(); } } - PageHandler pageHandler = getPageHandler(target); - if (pageHandler == null) { + PageResolver pageResolver = getPageResolver(target); + if (pageResolver == null) { return responseFactory.pageNotFound404(); } else { - if (!isAuthRequired || pageHandler.isAuthorized(authentication.get(), target)) { - return pageHandler.getResponse(request, target); + if (!isAuthRequired || pageResolver.isAuthorized(authentication.get(), target)) { + return pageResolver.resolve(request, target); } return responseFactory.forbidden403(); } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/WebServer.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/WebServer.java index 999d543d1..20e4da208 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/WebServer.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/WebServer.java @@ -111,7 +111,7 @@ public class WebServer implements SubSystem { } } - requestHandler.getResponseHandler().registerPages(); + requestHandler.getResponseResolver().registerPages(); } /** diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/cache/JSONCache.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/cache/JSONCache.java index 68133e5d6..f07cfcd29 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/cache/JSONCache.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/cache/JSONCache.java @@ -16,6 +16,7 @@ */ package com.djrapitops.plan.delivery.webserver.cache; +import com.djrapitops.plan.delivery.webserver.pages.json.RootJSONResolver; import com.djrapitops.plan.delivery.webserver.response.Response; import com.djrapitops.plan.delivery.webserver.response.data.JSONResponse; import com.djrapitops.plan.storage.file.ResourceCache; @@ -32,7 +33,7 @@ import java.util.function.Supplier; import java.util.stream.Collectors; /** - * Cache for any JSON data sent via {@link com.djrapitops.plan.delivery.webserver.pages.json.RootJSONHandler}. + * Cache for any JSON data sent via {@link RootJSONResolver}. * * @author Rsl1122 */ diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/TreePageHandler.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/CompositePageResolver.java similarity index 63% rename from Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/TreePageHandler.java rename to Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/CompositePageResolver.java index 380673286..13c47cc22 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/TreePageHandler.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/CompositePageResolver.java @@ -28,30 +28,35 @@ import java.util.HashMap; import java.util.Map; /** - * Abstract PageHandler that allows Tree-like target deduction. + * Extended {@link PageResolver} to implement layered resolution of addresses. + *

+ * Allows resolving /example/foo and /example/bar with different PageResolvers as if the address being resolved + * was at the root of the address (/example/foo - FooPageResolver sees /foo). + *

+ * Tree-like pattern for URL resolution. * * @author Rsl1122 */ -public abstract class TreePageHandler implements PageHandler { +public abstract class CompositePageResolver implements PageResolver { protected final ResponseFactory responseFactory; - private final Map pages; + private final Map pages; - public TreePageHandler(ResponseFactory responseFactory) { + public CompositePageResolver(ResponseFactory responseFactory) { this.responseFactory = responseFactory; pages = new HashMap<>(); } - public void registerPage(String targetPage, PageHandler handler) { - pages.put(targetPage, handler); + public void registerPage(String targetPage, PageResolver resolver) { + pages.put(targetPage, resolver); } - public void registerPage(String targetPage, PageHandler handler, int requiredPerm) { - pages.put(targetPage, new PageHandler() { + public void registerPage(String targetPage, PageResolver resolver, int requiredPerm) { + pages.put(targetPage, new PageResolver() { @Override - public Response getResponse(Request request, RequestTarget target) throws WebException { - return handler.getResponse(request, target); + public Response resolve(Request request, RequestTarget target) throws WebException { + return resolver.resolve(request, target); } @Override @@ -60,10 +65,11 @@ public abstract class TreePageHandler implements PageHandler { } }); } + public void registerPage(String targetPage, Response response, int requiredPerm) { - pages.put(targetPage, new PageHandler() { + pages.put(targetPage, new PageResolver() { @Override - public Response getResponse(Request request, RequestTarget target) { + public Response resolve(Request request, RequestTarget target) { return response; } @@ -75,14 +81,14 @@ public abstract class TreePageHandler implements PageHandler { } @Override - public Response getResponse(Request request, RequestTarget target) throws WebException { - PageHandler pageHandler = getPageHandler(target); - return pageHandler != null - ? pageHandler.getResponse(request, target) + public Response resolve(Request request, RequestTarget target) throws WebException { + PageResolver pageResolver = getPageResolver(target); + return pageResolver != null + ? pageResolver.resolve(request, target) : responseFactory.pageNotFound404(); } - public PageHandler getPageHandler(RequestTarget target) { + public PageResolver getPageResolver(RequestTarget target) { if (target.isEmpty()) { return pages.get(""); } @@ -91,7 +97,7 @@ public abstract class TreePageHandler implements PageHandler { return pages.get(targetPage); } - public PageHandler getPageHandler(String targetPage) { + public PageResolver getPageResolver(String targetPage) { return pages.get(targetPage); } } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/DebugPageHandler.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/DebugPageResolver.java similarity index 88% rename from Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/DebugPageHandler.java rename to Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/DebugPageResolver.java index 22503cd40..a99ea4ea7 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/DebugPageHandler.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/DebugPageResolver.java @@ -28,22 +28,22 @@ import javax.inject.Inject; import javax.inject.Singleton; /** - * PageHandler for /debug page. + * Resolves /debug URL. * * @author Rsl1122 */ @Singleton -public class DebugPageHandler implements PageHandler { +public class DebugPageResolver implements PageResolver { private final ResponseFactory responseFactory; @Inject - public DebugPageHandler(ResponseFactory responseFactory) { + public DebugPageResolver(ResponseFactory responseFactory) { this.responseFactory = responseFactory; } @Override - public Response getResponse(Request request, RequestTarget target) { + public Response resolve(Request request, RequestTarget target) { return responseFactory.debugPageResponse(); } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/PageHandler.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/PageResolver.java similarity index 82% rename from Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/PageHandler.java rename to Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/PageResolver.java index 7a2f8f42a..9fd06c90e 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/PageHandler.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/PageResolver.java @@ -24,20 +24,21 @@ import com.djrapitops.plan.exceptions.WebUserAuthException; import com.djrapitops.plan.exceptions.connection.WebException; /** - * PageHandlers are used for easier Response management and authorization checking. + * Used for Response resolution and authorization. * * @author Rsl1122 + * @see CompositePageResolver for larger depth resolution than 1. */ -public interface PageHandler { +public interface PageResolver { /** - * Get the Response of a PageHandler. + * Resolve the request to a response. * * @param request Request in case it is useful for choosing page. * @param target Rest of the target coordinates after this page has been solved. - * @return Response appropriate to the PageHandler. + * @return Appropriate response. */ - Response getResponse(Request request, RequestTarget target) throws WebException; + Response resolve(Request request, RequestTarget target) throws WebException; default boolean isAuthorized(Authentication auth, RequestTarget target) throws WebUserAuthException { return true; diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/PlayerPageHandler.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/PlayerPageResolver.java similarity index 93% rename from Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/PlayerPageHandler.java rename to Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/PlayerPageResolver.java index fd1010057..ae033ddba 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/PlayerPageHandler.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/PlayerPageResolver.java @@ -34,19 +34,19 @@ import javax.inject.Singleton; import java.util.UUID; /** - * PageHandler for /player/PlayerName pages. + * Resolves /player/${name/uuid} URLs. * * @author Rsl1122 */ @Singleton -public class PlayerPageHandler implements PageHandler { +public class PlayerPageResolver implements PageResolver { private final ResponseFactory responseFactory; private final DBSystem dbSystem; private final UUIDUtility uuidUtility; @Inject - public PlayerPageHandler( + public PlayerPageResolver( ResponseFactory responseFactory, DBSystem dbSystem, UUIDUtility uuidUtility @@ -57,7 +57,7 @@ public class PlayerPageHandler implements PageHandler { } @Override - public Response getResponse(Request request, RequestTarget target) throws WebException { + public Response resolve(Request request, RequestTarget target) throws WebException { if (target.isEmpty()) { return responseFactory.pageNotFound404(); } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/PlayersPageHandler.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/PlayersPageResolver.java similarity index 91% rename from Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/PlayersPageHandler.java rename to Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/PlayersPageResolver.java index b8197b391..27b998c39 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/PlayersPageHandler.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/PlayersPageResolver.java @@ -31,18 +31,18 @@ import javax.inject.Inject; import javax.inject.Singleton; /** - * PageHandler for /players page. + * Resolves /players URL. * * @author Rsl1122 */ @Singleton -public class PlayersPageHandler implements PageHandler { +public class PlayersPageResolver implements PageResolver { private final DBSystem dbSystem; private final ResponseFactory responseFactory; @Inject - public PlayersPageHandler( + public PlayersPageResolver( DBSystem dbSystem, ResponseFactory responseFactory ) { @@ -51,7 +51,7 @@ public class PlayersPageHandler implements PageHandler { } @Override - public Response getResponse(Request request, RequestTarget target) throws WebException { + public Response resolve(Request request, RequestTarget target) throws WebException { Database.State dbState = dbSystem.getDatabase().getState(); if (dbState != Database.State.OPEN) { throw new ForbiddenException("Database is " + dbState.name() + " - Please try again later. You can check database status with /plan info"); diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/RootPageHandler.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/RootPageResolver.java similarity index 89% rename from Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/RootPageHandler.java rename to Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/RootPageResolver.java index ca7f5ac23..39271213b 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/RootPageHandler.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/RootPageResolver.java @@ -31,26 +31,24 @@ import com.djrapitops.plan.identification.ServerInfo; import java.util.Optional; /** - * PageHandler for / page (Address root). - *

- * Not Available if Authentication is not enabled. + * Resolves '/' URL (Address Root). * * @author Rsl1122 */ -public class RootPageHandler implements PageHandler { +public class RootPageResolver implements PageResolver { private final ResponseFactory responseFactory; private final WebServer webServer; private final ServerInfo serverInfo; - public RootPageHandler(ResponseFactory responseFactory, WebServer webServer, ServerInfo serverInfo) { + public RootPageResolver(ResponseFactory responseFactory, WebServer webServer, ServerInfo serverInfo) { this.responseFactory = responseFactory; this.webServer = webServer; this.serverInfo = serverInfo; } @Override - public Response getResponse(Request request, RequestTarget target) throws WebException { + public Response resolve(Request request, RequestTarget target) throws WebException { Server server = serverInfo.getServer(); if (!webServer.isAuthRequired()) { return responseFactory.redirectResponse(server.isProxy() ? "network" : "server/" + Html.encodeToURL(server.getIdentifiableName())); diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/ServerPageHandler.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/ServerPageResolver.java similarity index 94% rename from Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/ServerPageHandler.java rename to Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/ServerPageResolver.java index af15e780c..acf348664 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/ServerPageHandler.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/ServerPageResolver.java @@ -39,12 +39,12 @@ import java.util.Optional; import java.util.UUID; /** - * PageHandler for /server and /network pages. + * Resolves /network, /server and /server/${name/uuid} URLs. * * @author Rsl1122 */ @Singleton -public class ServerPageHandler implements PageHandler { +public class ServerPageResolver implements PageResolver { private final ResponseFactory responseFactory; private final DBSystem dbSystem; @@ -52,7 +52,7 @@ public class ServerPageHandler implements PageHandler { private final Lazy webServer; @Inject - public ServerPageHandler( + public ServerPageResolver( ResponseFactory responseFactory, DBSystem dbSystem, ServerInfo serverInfo, @@ -65,7 +65,7 @@ public class ServerPageHandler implements PageHandler { } @Override - public Response getResponse(Request request, RequestTarget target) throws WebException { + public Response resolve(Request request, RequestTarget target) throws WebException { Optional serverUUID = getServerUUID(target); boolean proxy = serverInfo.getServer().isProxy(); if (serverUUID.isPresent()) { diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/GraphsJSONHandler.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/GraphsJSONResolver.java similarity index 94% rename from Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/GraphsJSONHandler.java rename to Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/GraphsJSONResolver.java index 802e162e2..a811cc83d 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/GraphsJSONHandler.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/GraphsJSONResolver.java @@ -22,7 +22,7 @@ import com.djrapitops.plan.delivery.webserver.RequestTarget; import com.djrapitops.plan.delivery.webserver.auth.Authentication; import com.djrapitops.plan.delivery.webserver.cache.DataID; import com.djrapitops.plan.delivery.webserver.cache.JSONCache; -import com.djrapitops.plan.delivery.webserver.pages.PageHandler; +import com.djrapitops.plan.delivery.webserver.pages.PageResolver; import com.djrapitops.plan.delivery.webserver.response.Response; import com.djrapitops.plan.delivery.webserver.response.data.JSONResponse; import com.djrapitops.plan.exceptions.WebUserAuthException; @@ -36,18 +36,18 @@ import java.util.Collections; import java.util.UUID; /** - * JSON handler for different graph data JSON requests. + * Resolves /v1/graph JSON requests. * * @author Rsl1122 */ @Singleton -public class GraphsJSONHandler implements PageHandler { +public class GraphsJSONResolver implements PageResolver { private final Identifiers identifiers; private final GraphJSONParser graphJSON; @Inject - public GraphsJSONHandler( + public GraphsJSONResolver( Identifiers identifiers, GraphJSONParser graphJSON ) { @@ -56,7 +56,7 @@ public class GraphsJSONHandler implements PageHandler { } @Override - public Response getResponse(Request request, RequestTarget target) throws WebException { + public Response resolve(Request request, RequestTarget target) throws WebException { String type = target.getParameter("type") .orElseThrow(() -> new BadRequestException("'type' parameter was not defined.")); diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/NetworkJSONHandler.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/NetworkJSONResolver.java similarity index 89% rename from Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/NetworkJSONHandler.java rename to Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/NetworkJSONResolver.java index 8fc5346f5..d6cb91e92 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/NetworkJSONHandler.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/NetworkJSONResolver.java @@ -24,22 +24,22 @@ import com.djrapitops.plan.delivery.rendering.json.network.NetworkTabJSONParser; import com.djrapitops.plan.delivery.webserver.RequestTarget; import com.djrapitops.plan.delivery.webserver.auth.Authentication; import com.djrapitops.plan.delivery.webserver.cache.DataID; -import com.djrapitops.plan.delivery.webserver.pages.TreePageHandler; +import com.djrapitops.plan.delivery.webserver.pages.CompositePageResolver; import com.djrapitops.plan.delivery.webserver.response.ResponseFactory; import javax.inject.Inject; import javax.inject.Singleton; /** - * Root handler for different JSON end points. + * Resolves /v1/network/ JSON requests. * * @author Rsl1122 */ @Singleton -public class NetworkJSONHandler extends TreePageHandler { +public class NetworkJSONResolver extends CompositePageResolver { @Inject - public NetworkJSONHandler( + public NetworkJSONResolver( ResponseFactory responseFactory, JSONFactory jsonFactory, NetworkOverviewJSONParser networkOverviewJSONParser, @@ -56,7 +56,7 @@ public class NetworkJSONHandler extends TreePageHandler { } private void registerPage(String identifier, DataID dataID, NetworkTabJSONParser tabJSONParser) { - registerPage(identifier, new NetworkTabJSONHandler<>(dataID, tabJSONParser)); + registerPage(identifier, new NetworkTabJSONResolver<>(dataID, tabJSONParser)); } @Override diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/NetworkTabJSONHandler.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/NetworkTabJSONResolver.java similarity index 83% rename from Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/NetworkTabJSONHandler.java rename to Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/NetworkTabJSONResolver.java index 84b54c77d..d60cb134d 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/NetworkTabJSONHandler.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/NetworkTabJSONResolver.java @@ -22,7 +22,7 @@ import com.djrapitops.plan.delivery.webserver.RequestTarget; import com.djrapitops.plan.delivery.webserver.auth.Authentication; import com.djrapitops.plan.delivery.webserver.cache.DataID; import com.djrapitops.plan.delivery.webserver.cache.JSONCache; -import com.djrapitops.plan.delivery.webserver.pages.PageHandler; +import com.djrapitops.plan.delivery.webserver.pages.PageResolver; import com.djrapitops.plan.delivery.webserver.response.Response; import com.djrapitops.plan.delivery.webserver.response.data.JSONResponse; import com.djrapitops.plan.exceptions.WebUserAuthException; @@ -30,22 +30,22 @@ import com.djrapitops.plan.exceptions.WebUserAuthException; import java.util.function.Supplier; /** - * Generic Tab JSON handler for any tab's data. + * Functional interface wrapper for resolving network JSON directly from other methods. * * @author Rsl1122 */ -public class NetworkTabJSONHandler implements PageHandler { +public class NetworkTabJSONResolver implements PageResolver { private final DataID dataID; private final Supplier jsonParser; - public NetworkTabJSONHandler(DataID dataID, NetworkTabJSONParser jsonParser) { + public NetworkTabJSONResolver(DataID dataID, NetworkTabJSONParser jsonParser) { this.dataID = dataID; this.jsonParser = jsonParser; } @Override - public Response getResponse(Request request, RequestTarget target) { + public Response resolve(Request request, RequestTarget target) { return JSONCache.getOrCache(dataID, () -> new JSONResponse(jsonParser.get())); } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/PlayerJSONHandler.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/PlayerJSONResolver.java similarity index 87% rename from Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/PlayerJSONHandler.java rename to Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/PlayerJSONResolver.java index 7f81127bd..030b42013 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/PlayerJSONHandler.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/PlayerJSONResolver.java @@ -21,7 +21,7 @@ import com.djrapitops.plan.delivery.rendering.json.PlayerJSONParser; import com.djrapitops.plan.delivery.webserver.Request; import com.djrapitops.plan.delivery.webserver.RequestTarget; import com.djrapitops.plan.delivery.webserver.auth.Authentication; -import com.djrapitops.plan.delivery.webserver.pages.PageHandler; +import com.djrapitops.plan.delivery.webserver.pages.PageResolver; import com.djrapitops.plan.delivery.webserver.response.Response; import com.djrapitops.plan.delivery.webserver.response.data.JSONResponse; import com.djrapitops.plan.exceptions.WebUserAuthException; @@ -33,19 +33,19 @@ import javax.inject.Singleton; import java.util.UUID; @Singleton -public class PlayerJSONHandler implements PageHandler { +public class PlayerJSONResolver implements PageResolver { private final Identifiers identifiers; private final PlayerJSONParser jsonParser; @Inject - public PlayerJSONHandler(Identifiers identifiers, PlayerJSONParser jsonParser) { + public PlayerJSONResolver(Identifiers identifiers, PlayerJSONParser jsonParser) { this.identifiers = identifiers; this.jsonParser = jsonParser; } @Override - public Response getResponse(Request request, RequestTarget target) throws WebException { + public Response resolve(Request request, RequestTarget target) throws WebException { UUID playerUUID = identifiers.getPlayerUUID(target); // Can throw BadRequestException return new JSONResponse(jsonParser.createJSONAsMap(playerUUID)); } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/PlayerKillsJSONHandler.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/PlayerKillsJSONResolver.java similarity index 90% rename from Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/PlayerKillsJSONHandler.java rename to Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/PlayerKillsJSONResolver.java index 44a0cc8e9..ac5c3d75a 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/PlayerKillsJSONHandler.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/PlayerKillsJSONResolver.java @@ -22,7 +22,7 @@ import com.djrapitops.plan.delivery.webserver.RequestTarget; import com.djrapitops.plan.delivery.webserver.auth.Authentication; import com.djrapitops.plan.delivery.webserver.cache.DataID; import com.djrapitops.plan.delivery.webserver.cache.JSONCache; -import com.djrapitops.plan.delivery.webserver.pages.PageHandler; +import com.djrapitops.plan.delivery.webserver.pages.PageResolver; import com.djrapitops.plan.delivery.webserver.response.Response; import com.djrapitops.plan.delivery.webserver.response.data.JSONResponse; import com.djrapitops.plan.exceptions.WebUserAuthException; @@ -40,13 +40,13 @@ import java.util.UUID; * @author Rsl1122 */ @Singleton -public class PlayerKillsJSONHandler implements PageHandler { +public class PlayerKillsJSONResolver implements PageResolver { private final Identifiers identifiers; private final JSONFactory jsonFactory; @Inject - public PlayerKillsJSONHandler( + public PlayerKillsJSONResolver( Identifiers identifiers, JSONFactory jsonFactory ) { @@ -55,7 +55,7 @@ public class PlayerKillsJSONHandler implements PageHandler { } @Override - public Response getResponse(Request request, RequestTarget target) throws WebException { + public Response resolve(Request request, RequestTarget target) throws WebException { UUID serverUUID = identifiers.getServerUUID(target); return JSONCache.getOrCache(DataID.KILLS, serverUUID, () -> new JSONResponse(Collections.singletonMap("player_kills", jsonFactory.serverPlayerKillsAsJSONMap(serverUUID))) diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/PlayersTableJSONHandler.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/PlayersTableJSONResolver.java similarity index 84% rename from Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/PlayersTableJSONHandler.java rename to Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/PlayersTableJSONResolver.java index ce888f972..afacec259 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/PlayersTableJSONHandler.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/PlayersTableJSONResolver.java @@ -17,13 +17,12 @@ package com.djrapitops.plan.delivery.webserver.pages.json; import com.djrapitops.plan.delivery.rendering.json.JSONFactory; -import com.djrapitops.plan.delivery.rendering.json.PlayersTableJSONParser; import com.djrapitops.plan.delivery.webserver.Request; import com.djrapitops.plan.delivery.webserver.RequestTarget; import com.djrapitops.plan.delivery.webserver.auth.Authentication; import com.djrapitops.plan.delivery.webserver.cache.DataID; import com.djrapitops.plan.delivery.webserver.cache.JSONCache; -import com.djrapitops.plan.delivery.webserver.pages.PageHandler; +import com.djrapitops.plan.delivery.webserver.pages.PageResolver; import com.djrapitops.plan.delivery.webserver.response.Response; import com.djrapitops.plan.delivery.webserver.response.data.JSONResponse; import com.djrapitops.plan.exceptions.WebUserAuthException; @@ -35,19 +34,18 @@ import javax.inject.Singleton; import java.util.UUID; /** - * JSON handler for different Player table JSON requests. + * Resolves /v1/players JSON requests. * * @author Rsl1122 - * @see PlayersTableJSONParser For JSON parsing of /server players table. */ @Singleton -public class PlayersTableJSONHandler implements PageHandler { +public class PlayersTableJSONResolver implements PageResolver { private final Identifiers identifiers; private final JSONFactory jsonFactory; @Inject - public PlayersTableJSONHandler( + public PlayersTableJSONResolver( Identifiers identifiers, JSONFactory jsonFactory ) { @@ -56,7 +54,7 @@ public class PlayersTableJSONHandler implements PageHandler { } @Override - public Response getResponse(Request request, RequestTarget target) throws WebException { + public Response resolve(Request request, RequestTarget target) throws WebException { if (target.getParameter("server").isPresent()) { UUID serverUUID = identifiers.getServerUUID(target); // Can throw BadRequestException return JSONCache.getOrCache(DataID.PLAYERS, serverUUID, () -> new JSONResponse(jsonFactory.serverPlayersTableJSON(serverUUID))); diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/RootJSONHandler.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/RootJSONResolver.java similarity index 69% rename from Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/RootJSONHandler.java rename to Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/RootJSONResolver.java index b18f79d22..28feb510b 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/RootJSONHandler.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/RootJSONResolver.java @@ -20,7 +20,7 @@ import com.djrapitops.plan.delivery.rendering.json.*; import com.djrapitops.plan.delivery.webserver.RequestTarget; import com.djrapitops.plan.delivery.webserver.auth.Authentication; import com.djrapitops.plan.delivery.webserver.cache.DataID; -import com.djrapitops.plan.delivery.webserver.pages.TreePageHandler; +import com.djrapitops.plan.delivery.webserver.pages.CompositePageResolver; import com.djrapitops.plan.delivery.webserver.response.ResponseFactory; import com.djrapitops.plan.identification.Identifiers; @@ -28,57 +28,57 @@ import javax.inject.Inject; import javax.inject.Singleton; /** - * Root handler for different JSON end points. + * Root resolver for JSON requests, resolves /v1/ URLs. * * @author Rsl1122 */ @Singleton -public class RootJSONHandler extends TreePageHandler { +public class RootJSONResolver extends CompositePageResolver { private final Identifiers identifiers; @Inject - public RootJSONHandler( + public RootJSONResolver( ResponseFactory responseFactory, Identifiers identifiers, JSONFactory jsonFactory, - GraphsJSONHandler graphsJSONHandler, - SessionsJSONHandler sessionsJSONHandler, - PlayersTableJSONHandler playersTableJSONHandler, - ServerOverviewJSONParser serverOverviewJSONParser, + GraphsJSONResolver graphsJSONResolver, + SessionsJSONResolver sessionsJSONResolver, + PlayersTableJSONResolver playersTableJSONResolver, + ServerOverviewJSONParser overviewJSONParser, OnlineActivityOverviewJSONParser onlineActivityOverviewJSONParser, SessionsOverviewJSONParser sessionsOverviewJSONParser, - PlayerKillsJSONHandler playerKillsJSONHandler, + PlayerKillsJSONResolver playerKillsJSONResolver, PvPPvEJSONParser pvPPvEJSONParser, PlayerBaseOverviewJSONParser playerBaseOverviewJSONParser, PerformanceJSONParser performanceJSONParser, - PlayerJSONHandler playerJSONHandler, - NetworkJSONHandler networkJSONHandler + PlayerJSONResolver playerJSONResolver, + NetworkJSONResolver networkJSONResolver ) { super(responseFactory); this.identifiers = identifiers; - registerPage("players", playersTableJSONHandler, 1); - registerPage("sessions", sessionsJSONHandler, 0); - registerPage("kills", playerKillsJSONHandler, 0); + registerPage("players", playersTableJSONResolver, 1); + registerPage("sessions", sessionsJSONResolver, 0); + registerPage("kills", playerKillsJSONResolver, 0); registerPage("pingTable", DataID.PING_TABLE, jsonFactory::pingPerGeolocation); - registerPage("graph", graphsJSONHandler, 0); + registerPage("graph", graphsJSONResolver, 0); - registerPage("serverOverview", DataID.SERVER_OVERVIEW, serverOverviewJSONParser); + registerPage("serverOverview", DataID.SERVER_OVERVIEW, overviewJSONParser); registerPage("onlineOverview", DataID.ONLINE_OVERVIEW, onlineActivityOverviewJSONParser); registerPage("sessionsOverview", DataID.SESSIONS_OVERVIEW, sessionsOverviewJSONParser); registerPage("playerVersus", DataID.PVP_PVE, pvPPvEJSONParser); registerPage("playerbaseOverview", DataID.PLAYERBASE_OVERVIEW, playerBaseOverviewJSONParser); registerPage("performanceOverview", DataID.PERFORMANCE_OVERVIEW, performanceJSONParser); - registerPage("player", playerJSONHandler, 2); - registerPage("network", networkJSONHandler, 0); + registerPage("player", playerJSONResolver, 2); + registerPage("network", networkJSONResolver, 0); } private void registerPage(String identifier, DataID dataID, ServerTabJSONParser tabJSONParser) { - registerPage(identifier, new ServerTabJSONHandler<>(dataID, identifiers, tabJSONParser), 0); + registerPage(identifier, new ServerTabJSONResolver<>(dataID, identifiers, tabJSONParser), 0); } @Override diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/ServerTabJSONHandler.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/ServerTabJSONResolver.java similarity index 86% rename from Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/ServerTabJSONHandler.java rename to Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/ServerTabJSONResolver.java index ba58fc41b..041096018 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/ServerTabJSONHandler.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/ServerTabJSONResolver.java @@ -22,7 +22,7 @@ import com.djrapitops.plan.delivery.webserver.RequestTarget; import com.djrapitops.plan.delivery.webserver.auth.Authentication; import com.djrapitops.plan.delivery.webserver.cache.DataID; import com.djrapitops.plan.delivery.webserver.cache.JSONCache; -import com.djrapitops.plan.delivery.webserver.pages.PageHandler; +import com.djrapitops.plan.delivery.webserver.pages.PageResolver; import com.djrapitops.plan.delivery.webserver.response.Response; import com.djrapitops.plan.delivery.webserver.response.data.JSONResponse; import com.djrapitops.plan.exceptions.WebUserAuthException; @@ -33,17 +33,17 @@ import java.util.UUID; import java.util.function.Function; /** - * Generic Tab JSON handler for any tab's data. + * Functional interface wrapper for resolving server specific JSON directly from other methods. * * @author Rsl1122 */ -public class ServerTabJSONHandler implements PageHandler { +public class ServerTabJSONResolver implements PageResolver { private final DataID dataID; private final Identifiers identifiers; private final Function jsonParser; - public ServerTabJSONHandler( + public ServerTabJSONResolver( DataID dataID, Identifiers identifiers, ServerTabJSONParser jsonParser @@ -54,7 +54,7 @@ public class ServerTabJSONHandler implements PageHandler { } @Override - public Response getResponse(Request request, RequestTarget target) throws WebException { + public Response resolve(Request request, RequestTarget target) throws WebException { UUID serverUUID = identifiers.getServerUUID(target); // Can throw BadRequestException return JSONCache.getOrCache(dataID, serverUUID, () -> new JSONResponse(jsonParser.apply(serverUUID))); } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/SessionsJSONHandler.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/SessionsJSONResolver.java similarity index 91% rename from Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/SessionsJSONHandler.java rename to Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/SessionsJSONResolver.java index dd5117938..09e5d14e5 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/SessionsJSONHandler.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/SessionsJSONResolver.java @@ -22,7 +22,7 @@ import com.djrapitops.plan.delivery.webserver.RequestTarget; import com.djrapitops.plan.delivery.webserver.auth.Authentication; import com.djrapitops.plan.delivery.webserver.cache.DataID; import com.djrapitops.plan.delivery.webserver.cache.JSONCache; -import com.djrapitops.plan.delivery.webserver.pages.PageHandler; +import com.djrapitops.plan.delivery.webserver.pages.PageResolver; import com.djrapitops.plan.delivery.webserver.response.Response; import com.djrapitops.plan.delivery.webserver.response.data.JSONResponse; import com.djrapitops.plan.exceptions.WebUserAuthException; @@ -40,13 +40,13 @@ import java.util.UUID; * @author Rsl1122 */ @Singleton -public class SessionsJSONHandler implements PageHandler { +public class SessionsJSONResolver implements PageResolver { private final Identifiers identifiers; private final JSONFactory jsonFactory; @Inject - public SessionsJSONHandler( + public SessionsJSONResolver( Identifiers identifiers, JSONFactory jsonFactory ) { @@ -55,7 +55,7 @@ public class SessionsJSONHandler implements PageHandler { } @Override - public Response getResponse(Request request, RequestTarget target) throws WebException { + public Response resolve(Request request, RequestTarget target) throws WebException { if (target.getParameter("server").isPresent()) { UUID serverUUID = identifiers.getServerUUID(target); return JSONCache.getOrCache(DataID.SESSIONS, serverUUID, () -> diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/response/ResponseFactory.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/response/ResponseFactory.java index 1ecf328d9..69dcdc21f 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/response/ResponseFactory.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/response/ResponseFactory.java @@ -17,10 +17,7 @@ package com.djrapitops.plan.delivery.webserver.response; import com.djrapitops.plan.delivery.rendering.pages.PageFactory; -import com.djrapitops.plan.delivery.webserver.response.errors.ErrorResponse; -import com.djrapitops.plan.delivery.webserver.response.errors.ForbiddenResponse; -import com.djrapitops.plan.delivery.webserver.response.errors.InternalErrorResponse; -import com.djrapitops.plan.delivery.webserver.response.errors.NotFoundResponse; +import com.djrapitops.plan.delivery.webserver.response.errors.*; import com.djrapitops.plan.delivery.webserver.response.pages.*; import com.djrapitops.plan.exceptions.ParseException; import com.djrapitops.plan.exceptions.WebUserAuthException; @@ -197,6 +194,10 @@ public class ResponseFactory { } } + public BadRequestResponse badRequest(String errorMessage, String target) { + return new BadRequestResponse(errorMessage + " (when requesting '" + target + "')"); + } + public Response playerPageResponse(UUID playerUUID) { try { return new PageResponse(pageFactory.playerPage(playerUUID));