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
This commit is contained in:
Rsl1122 2019-12-05 19:42:20 +02:00
parent 887c8d4f4c
commit b2cd332c9b
25 changed files with 170 additions and 163 deletions

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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<Response> 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;
}
}

View File

@ -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.
* <p>
* - 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> webServer;
@Inject
public ResponseHandler(
public ResponseResolver(
ResponseFactory responseFactory,
Lazy<WebServer> 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();
}

View File

@ -111,7 +111,7 @@ public class WebServer implements SubSystem {
}
}
requestHandler.getResponseHandler().registerPages();
requestHandler.getResponseResolver().registerPages();
}
/**

View File

@ -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
*/

View File

@ -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.
* <p>
* 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).
* <p>
* 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<String, PageHandler> pages;
private final Map<String, PageResolver> 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);
}
}

View File

@ -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();
}

View File

@ -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;

View File

@ -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();
}

View File

@ -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");

View File

@ -31,26 +31,24 @@ import com.djrapitops.plan.identification.ServerInfo;
import java.util.Optional;
/**
* PageHandler for / page (Address root).
* <p>
* 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()));

View File

@ -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> 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<UUID> serverUUID = getServerUUID(target);
boolean proxy = serverInfo.getServer().isProxy();
if (serverUUID.isPresent()) {

View File

@ -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."));

View File

@ -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 <T> void registerPage(String identifier, DataID dataID, NetworkTabJSONParser<T> tabJSONParser) {
registerPage(identifier, new NetworkTabJSONHandler<>(dataID, tabJSONParser));
registerPage(identifier, new NetworkTabJSONResolver<>(dataID, tabJSONParser));
}
@Override

View File

@ -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<T> implements PageHandler {
public class NetworkTabJSONResolver<T> implements PageResolver {
private final DataID dataID;
private final Supplier<T> jsonParser;
public NetworkTabJSONHandler(DataID dataID, NetworkTabJSONParser<T> jsonParser) {
public NetworkTabJSONResolver(DataID dataID, NetworkTabJSONParser<T> 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()));
}

View File

@ -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));
}

View File

@ -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)))

View File

@ -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)));

View File

@ -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 <T> void registerPage(String identifier, DataID dataID, ServerTabJSONParser<T> tabJSONParser) {
registerPage(identifier, new ServerTabJSONHandler<>(dataID, identifiers, tabJSONParser), 0);
registerPage(identifier, new ServerTabJSONResolver<>(dataID, identifiers, tabJSONParser), 0);
}
@Override

View File

@ -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<T> implements PageHandler {
public class ServerTabJSONResolver<T> implements PageResolver {
private final DataID dataID;
private final Identifiers identifiers;
private final Function<UUID, T> jsonParser;
public ServerTabJSONHandler(
public ServerTabJSONResolver(
DataID dataID,
Identifiers identifiers,
ServerTabJSONParser<T> jsonParser
@ -54,7 +54,7 @@ public class ServerTabJSONHandler<T> 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)));
}

View File

@ -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, () ->

View File

@ -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));