diff --git a/Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resolver/CompositeResolver.java b/Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resolver/CompositeResolver.java index 1fee6bf53..ba17e3532 100644 --- a/Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resolver/CompositeResolver.java +++ b/Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resolver/CompositeResolver.java @@ -22,34 +22,45 @@ import com.djrapitops.plan.delivery.web.resolver.request.URIPath; import java.util.ArrayList; import java.util.List; import java.util.Optional; +import java.util.function.Function; +import java.util.function.Predicate; /** * Utility Resolver for organizing resolution in a tree-like structure. *

* CompositeResolver removes first part of the target with {@link URIPath#omitFirst()} * before calling the child Resolvers. + *

+ * Example: {@code resolverService.registerResolver("/test/", compositeResolver);} + * The Resolvers added to CompositeResolver will be given Request with URIPath "/". * * @author Rsl1122 */ public final class CompositeResolver implements Resolver { private final List prefixes; - private final List resolvers; + private final List>> resolvers; + private final List> canAccess; CompositeResolver() { this.prefixes = new ArrayList<>(); this.resolvers = new ArrayList<>(); + this.canAccess = new ArrayList<>(); } public static CompositeResolver.Builder builder() { return new Builder(); } - private Optional getResolver(URIPath target) { - return target.getPart(0).flatMap(this::find); + private Optional>> getResolver(URIPath target) { + return target.getPart(0).flatMap(this::findResolver); } - private Optional find(String prefix) { + private Optional> getAccessCheck(URIPath target) { + return target.getPart(0).flatMap(this::findAccessCheck); + } + + private Optional>> findResolver(String prefix) { for (int i = 0; i < prefixes.size(); i++) { if (prefixes.get(i).equals(prefix)) { return Optional.of(resolvers.get(i)); @@ -58,24 +69,44 @@ public final class CompositeResolver implements Resolver { return Optional.empty(); } + private Optional> findAccessCheck(String prefix) { + for (int i = 0; i < prefixes.size(); i++) { + if (prefixes.get(i).equals(prefix)) { + return Optional.of(canAccess.get(i)); + } + } + return Optional.empty(); + } + void add(String prefix, Resolver resolver) { if (prefix == null) throw new IllegalArgumentException("Prefix can not be null"); if (resolver == null) throw new IllegalArgumentException("Resolver can not be null"); prefixes.add(prefix); - resolvers.add(resolver); + resolvers.add(resolver::resolve); + canAccess.add(resolver::canAccess); + } + + void add(String prefix, Function resolver, Predicate accessCheck) { + if (prefix == null) throw new IllegalArgumentException("Prefix can not be null"); + if (resolver == null) throw new IllegalArgumentException("Resolver can not be null"); + if (accessCheck == null) throw new IllegalArgumentException("Resolver can not be null"); + prefixes.add(prefix); + resolvers.add(request -> Optional.ofNullable(resolver.apply(request))); + canAccess.add(accessCheck); } @Override public boolean canAccess(Request request) { - return getResolver(request.getPath()) - .map(resolver -> resolver.canAccess(request)) + Request forThis = request.omitFirstInPath(); + return getAccessCheck(forThis.getPath()) + .map(resolver -> resolver.test(forThis)) .orElse(true); } @Override public Optional resolve(Request request) { - return getResolver(request.getPath()) - .flatMap(resolver -> resolver.resolve(request)); + Request forThis = request.omitFirstInPath(); + return getResolver(forThis.getPath()).flatMap(resolver -> resolver.apply(forThis)); } public static class Builder { @@ -97,6 +128,18 @@ public final class CompositeResolver implements Resolver { return this; } + /** + * Add a new resolver to the CompositeResolver by using functional interfaces + * + * @param prefix Start of the target (first part of the target string, eg "example" in "/example/target/", or "" in "/") + * @param resolver Resolver to call for this target, {@link URIPath#omitFirst()} will be called for Resolver method calls. + * @return this builder. + */ + public Builder add(String prefix, Function resolver, Predicate accessCheck) { + composite.add(prefix, resolver, accessCheck); + return this; + } + public CompositeResolver build() { return composite; } diff --git a/Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resolver/Response.java b/Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resolver/Response.java index 2ea1e169b..b1766e5de 100644 --- a/Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resolver/Response.java +++ b/Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resolver/Response.java @@ -17,6 +17,7 @@ package com.djrapitops.plan.delivery.web.resolver; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; import java.util.Optional; @@ -46,6 +47,10 @@ public final class Response { return bytes; } + public String getAsString() { + return new String(bytes, StandardCharsets.UTF_8); + } + public int getCode() { return code; } diff --git a/Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resolver/request/Request.java b/Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resolver/request/Request.java index 1fa7fec0b..cb67a000d 100644 --- a/Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resolver/request/Request.java +++ b/Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resolver/request/Request.java @@ -51,6 +51,21 @@ public final class Request { this.headers = headers; } + // Special constructor that figures out URIPath and URIQuery from "/path/and?query=params" + public Request(String method, String target, WebUser user, Map headers) { + this.method = method; + if (target.contains("?")) { + String[] halves = target.split("\\?", 2); + this.path = new URIPath(halves[0]); + this.query = new URIQuery(halves[1]); + } else { + this.path = new URIPath(target); + this.query = new URIQuery(""); + } + this.user = user; + this.headers = headers; + } + /** * Get HTTP method. * @@ -96,4 +111,8 @@ public final class Request { public Optional getHeader(String key) { return Optional.ofNullable(headers.get(key)); } + + public Request omitFirstInPath() { + return new Request(method, path.omitFirst(), query, user, headers); + } } \ No newline at end of file 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 c4ec6d44f..76539227a 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 @@ -18,10 +18,9 @@ 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.web.resolver.Response; +import com.djrapitops.plan.delivery.web.resolver.request.Request; import com.djrapitops.plan.delivery.webserver.pages.json.RootJSONResolver; -import com.djrapitops.plan.delivery.webserver.response.Response_old; -import com.djrapitops.plan.delivery.webserver.response.errors.ErrorResponse; import com.djrapitops.plan.exceptions.connection.NotFoundException; import com.djrapitops.plan.exceptions.connection.WebException; import com.djrapitops.plan.identification.Server; @@ -36,8 +35,9 @@ import org.apache.commons.lang3.StringUtils; import javax.inject.Inject; import javax.inject.Singleton; import java.io.IOException; -import java.net.URI; import java.nio.file.Path; +import java.util.Collections; +import java.util.Optional; /** * Handles exporting of /network page html, data and resources. @@ -121,9 +121,9 @@ public class NetworkPageExporter extends FileExporter { } private void exportJSON(Path toDirectory, String resource) throws NotFoundException, IOException { - Response_old found = getJSONResponse(resource); - if (found instanceof ErrorResponse) { - throw new NotFoundException(resource + " was not properly exported: " + found.getContent()); + Optional found = getJSONResponse(resource); + if (!found.isPresent()) { + throw new NotFoundException(resource + " was not properly exported: not found"); } String jsonResourceName = toFileName(toJSONResourceName(resource)) + ".json"; @@ -131,7 +131,7 @@ public class NetworkPageExporter extends FileExporter { String relativePlayerLink = toRelativePathFromRoot("player"); export(toDirectory.resolve("data").resolve(jsonResourceName), // Replace ../player in urls to fix player page links - StringUtils.replaceEach(found.getContent(), + StringUtils.replaceEach(found.get().getAsString(), new String[]{"../player", "./player"}, new String[]{relativePlayerLink, relativePlayerLink} ) @@ -143,9 +143,9 @@ public class NetworkPageExporter extends FileExporter { return StringUtils.replaceEach(resource, new String[]{"?", "&", "type=", "server="}, new String[]{"-", "_", "", ""}); } - private Response_old getJSONResponse(String resource) { + private Optional getJSONResponse(String resource) { try { - return jsonHandler.resolve(null, new RequestTarget(URI.create(resource))); + return jsonHandler.getResolver().resolve(new Request("GET", "/v1/" + resource, null, Collections.emptyMap())); } 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 468b92a97..a65c98109 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 @@ -18,10 +18,9 @@ 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.web.resolver.Response; +import com.djrapitops.plan.delivery.web.resolver.request.Request; import com.djrapitops.plan.delivery.webserver.pages.json.RootJSONResolver; -import com.djrapitops.plan.delivery.webserver.response.Response_old; -import com.djrapitops.plan.delivery.webserver.response.errors.ErrorResponse; import com.djrapitops.plan.exceptions.connection.NotFoundException; import com.djrapitops.plan.exceptions.connection.WebException; import com.djrapitops.plan.settings.locale.Locale; @@ -36,8 +35,9 @@ import org.apache.commons.lang3.StringUtils; import javax.inject.Inject; import javax.inject.Singleton; import java.io.IOException; -import java.net.URI; import java.nio.file.Path; +import java.util.Collections; +import java.util.Optional; import java.util.UUID; /** @@ -104,14 +104,14 @@ public class PlayerPageExporter extends FileExporter { } private void exportJSON(ExportPaths exportPaths, Path toDirectory, String resource, String playerName) throws NotFoundException, IOException { - Response_old found = getJSONResponse(resource); - if (found instanceof ErrorResponse) { - throw new NotFoundException(resource + " was not properly exported: " + found.getContent()); + Optional found = getJSONResponse(resource); + if (!found.isPresent()) { + throw new NotFoundException(resource + " was not properly exported: no response"); } String jsonResourceName = toFileName(toJSONResourceName(resource)) + ".json"; - export(toDirectory.resolve(jsonResourceName), found.getContent()); + export(toDirectory.resolve(jsonResourceName), found.get().getBytes()); exportPaths.put("../v1/player?player=" + playerName, "./" + jsonResourceName); } @@ -119,9 +119,9 @@ public class PlayerPageExporter extends FileExporter { return StringUtils.replaceEach(resource, new String[]{"?", "&", "type=", "player="}, new String[]{"-", "_", "", ""}); } - private Response_old getJSONResponse(String resource) { + private Optional getJSONResponse(String resource) { try { - return jsonHandler.resolve(null, new RequestTarget(URI.create(resource))); + return jsonHandler.getResolver().resolve(new Request("GET", "/v1/" + resource, null, Collections.emptyMap())); } 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 74a24f932..616bcd56c 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 @@ -18,10 +18,9 @@ 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.web.resolver.Response; +import com.djrapitops.plan.delivery.web.resolver.request.Request; import com.djrapitops.plan.delivery.webserver.pages.json.RootJSONResolver; -import com.djrapitops.plan.delivery.webserver.response.Response_old; -import com.djrapitops.plan.delivery.webserver.response.errors.ErrorResponse; import com.djrapitops.plan.exceptions.connection.NotFoundException; import com.djrapitops.plan.exceptions.connection.WebException; import com.djrapitops.plan.identification.ServerInfo; @@ -36,8 +35,9 @@ import org.apache.commons.lang3.StringUtils; import javax.inject.Inject; import javax.inject.Singleton; import java.io.IOException; -import java.net.URI; import java.nio.file.Path; +import java.util.Collections; +import java.util.Optional; /** * Handles exporting of /players page html, data and resources. @@ -99,16 +99,16 @@ public class PlayersPageExporter extends FileExporter { } private void exportJSON(Path toDirectory) throws NotFoundException, IOException { - Response_old found = getJSONResponse("players"); - if (found instanceof ErrorResponse) { - throw new NotFoundException("players page was not properly exported: " + found.getContent()); + Optional found = getJSONResponse("players"); + if (!found.isPresent()) { + throw new NotFoundException("players page was not properly exported: not found"); } String jsonResourceName = toFileName(toJSONResourceName("players")) + ".json"; export(toDirectory.resolve("data").resolve(jsonResourceName), // Replace ../player in urls to fix player page links - StringUtils.replace(found.getContent(), "../player", toRelativePathFromRoot("player")) + StringUtils.replace(found.get().getAsString(), "../player", toRelativePathFromRoot("player")) ); exportPaths.put("./v1/players", toRelativePathFromRoot("data/" + jsonResourceName)); } @@ -117,9 +117,9 @@ public class PlayersPageExporter extends FileExporter { return StringUtils.replaceEach(resource, new String[]{"?", "&", "type=", "server="}, new String[]{"-", "_", "", ""}); } - private Response_old getJSONResponse(String resource) { + private Optional getJSONResponse(String resource) { try { - return jsonHandler.resolve(null, new RequestTarget(URI.create(resource))); + return jsonHandler.getResolver().resolve(new Request("GET", "/v1/" + resource, null, Collections.emptyMap())); } 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 7ecee8c45..8682638a1 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 @@ -18,10 +18,9 @@ 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.web.resolver.Response; +import com.djrapitops.plan.delivery.web.resolver.request.Request; import com.djrapitops.plan.delivery.webserver.pages.json.RootJSONResolver; -import com.djrapitops.plan.delivery.webserver.response.Response_old; -import com.djrapitops.plan.delivery.webserver.response.errors.ErrorResponse; import com.djrapitops.plan.exceptions.connection.NotFoundException; import com.djrapitops.plan.exceptions.connection.WebException; import com.djrapitops.plan.identification.Server; @@ -37,8 +36,9 @@ import org.apache.commons.lang3.StringUtils; import javax.inject.Inject; import javax.inject.Singleton; import java.io.IOException; -import java.net.URI; import java.nio.file.Path; +import java.util.Collections; +import java.util.Optional; import java.util.UUID; /** @@ -133,16 +133,16 @@ public class ServerPageExporter extends FileExporter { } private void exportJSON(Path toDirectory, String resource) throws NotFoundException, IOException { - Response_old found = getJSONResponse(resource); - if (found instanceof ErrorResponse) { - throw new NotFoundException(resource + " was not properly exported: " + found.getContent()); + Optional found = getJSONResponse(resource); + if (!found.isPresent()) { + throw new NotFoundException(resource + " was not properly exported: not found"); } String jsonResourceName = toFileName(toJSONResourceName(resource)) + ".json"; export(toDirectory.resolve("data").resolve(jsonResourceName), // Replace ../player in urls to fix player page links - StringUtils.replace(found.getContent(), "../player", toRelativePathFromRoot("player")) + StringUtils.replace(found.get().getAsString(), "../player", toRelativePathFromRoot("player")) ); exportPaths.put("../v1/" + resource, toRelativePathFromRoot("data/" + jsonResourceName)); } @@ -151,9 +151,9 @@ public class ServerPageExporter extends FileExporter { return StringUtils.replaceEach(resource, new String[]{"?", "&", "type=", "server="}, new String[]{"-", "_", "", ""}); } - private Response_old getJSONResponse(String resource) { + private Optional getJSONResponse(String resource) { try { - return jsonHandler.resolve(null, new RequestTarget(URI.create(resource))); + return jsonHandler.getResolver().resolve(new Request("GET", "/v1/" + resource, null, Collections.emptyMap())); } 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/ResponseResolver.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/ResponseResolver.java index a83caa2b6..151378401 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/ResponseResolver.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/ResponseResolver.java @@ -50,7 +50,7 @@ import java.util.regex.Pattern; * @author Rsl1122 */ @Singleton -public class ResponseResolver extends CompositePageResolver { +public class ResponseResolver { private final DebugPageResolver debugPageResolver; private final PlayersPageResolver playersPageResolver; @@ -62,6 +62,7 @@ public class ResponseResolver extends CompositePageResolver { private final ErrorHandler errorHandler; private final ResolverService resolverService; + private final ResponseFactory responseFactory; private final Lazy webServer; @Inject @@ -80,8 +81,8 @@ public class ResponseResolver extends CompositePageResolver { ErrorHandler errorHandler ) { - super(responseFactory); this.resolverService = resolverService; + this.responseFactory = responseFactory; this.webServer = webServer; this.debugPageResolver = debugPageResolver; this.playersPageResolver = playersPageResolver; @@ -94,17 +95,17 @@ public class ResponseResolver extends CompositePageResolver { } public void registerPages() { - String pluginName = "Plan"; - resolverService.registerResolver(pluginName, "/debug", debugPageResolver); - resolverService.registerResolver(pluginName, "/players", playersPageResolver); - resolverService.registerResolver(pluginName, "/player", playerPageResolver); - resolverService.registerResolver(pluginName, "/favicon.ico", noAuthResolverFor(responseFactory.faviconResponse())); - resolverService.registerResolver(pluginName, "/network", serverPageResolver); - resolverService.registerResolver(pluginName, "/server", serverPageResolver); - resolverService.registerResolverForMatches(pluginName, Pattern.compile("^/$"), rootPageResolver); - resolverService.registerResolverForMatches(pluginName, Pattern.compile("^/(vendor|css|js|img)/.*"), staticResourceResolver); + String plugin = "Plan"; + resolverService.registerResolver(plugin, "/debug", debugPageResolver); + resolverService.registerResolver(plugin, "/players", playersPageResolver); + resolverService.registerResolver(plugin, "/player", playerPageResolver); + resolverService.registerResolver(plugin, "/favicon.ico", noAuthResolverFor(responseFactory.faviconResponse())); + resolverService.registerResolver(plugin, "/network", serverPageResolver); + resolverService.registerResolver(plugin, "/server", serverPageResolver); + resolverService.registerResolverForMatches(plugin, Pattern.compile("^/$"), rootPageResolver); + resolverService.registerResolverForMatches(plugin, Pattern.compile("^/(vendor|css|js|img)/.*"), staticResourceResolver); - registerPage("v1", rootJSONResolver); + resolverService.registerResolver(plugin, "/v1", rootJSONResolver.getResolver()); } public NoAuthResolver noAuthResolverFor(Response response) { 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 75d1834cd..2e9905630 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,9 +16,9 @@ */ package com.djrapitops.plan.delivery.webserver.cache; +import com.djrapitops.plan.delivery.web.resolver.MimeType; +import com.djrapitops.plan.delivery.web.resolver.Response; import com.djrapitops.plan.delivery.webserver.pages.json.RootJSONResolver; -import com.djrapitops.plan.delivery.webserver.response.Response_old; -import com.djrapitops.plan.delivery.webserver.response.data.JSONResponse; import com.djrapitops.plan.storage.file.ResourceCache; import com.djrapitops.plugin.task.AbsRunnable; import com.github.benmanes.caffeine.cache.Cache; @@ -27,6 +27,7 @@ import org.apache.commons.lang3.StringUtils; import javax.inject.Inject; import javax.inject.Singleton; +import java.nio.charset.StandardCharsets; import java.util.*; import java.util.concurrent.TimeUnit; import java.util.function.Supplier; @@ -39,7 +40,7 @@ import java.util.stream.Collectors; */ public class JSONCache { - private static final Cache cache = Caffeine.newBuilder() + private static final Cache cache = Caffeine.newBuilder() .expireAfterAccess(2, TimeUnit.MINUTES) .build(); @@ -47,33 +48,42 @@ public class JSONCache { // Static class } - public static Response_old getOrCache(String identifier, Supplier jsonResponseSupplier) { - String found = cache.getIfPresent(identifier); + public static Response getOrCache(String identifier, Supplier jsonResponseSupplier) { + byte[] found = cache.getIfPresent(identifier); if (found == null) { - JSONResponse response = jsonResponseSupplier.get(); - cache.put(identifier, response.getContent()); + Response response = jsonResponseSupplier.get(); + cache.put(identifier, response.getBytes()); return response; } - return new JSONResponse(found); + return Response.builder() + .setMimeType(MimeType.JSON) + .setContent(found) + .build(); } public static String getOrCacheString(DataID dataID, UUID serverUUID, Supplier stringSupplier) { String identifier = dataID.of(serverUUID); - String found = cache.getIfPresent(identifier); + byte[] found = cache.getIfPresent(identifier); if (found == null) { String result = stringSupplier.get(); - cache.put(identifier, result); + cache.put(identifier, result.getBytes(StandardCharsets.UTF_8)); return result; } - return found; + return new String(found, StandardCharsets.UTF_8); } - public static Response_old getOrCache(DataID dataID, Supplier jsonResponseSupplier) { - return getOrCache(dataID.name(), jsonResponseSupplier); + public static Response getOrCache(DataID dataID, Supplier jsonResponseSupplier) { + return getOrCache(dataID.name(), () -> Response.builder() + .setMimeType(MimeType.JSON) + .setJSONContent(jsonResponseSupplier.get()) + .build()); } - public static Response_old getOrCache(DataID dataID, UUID serverUUID, Supplier jsonResponseSupplier) { - return getOrCache(dataID.of(serverUUID), jsonResponseSupplier); + public static Response getOrCache(DataID dataID, UUID serverUUID, Supplier jsonResponseSupplier) { + return getOrCache(dataID.of(serverUUID), () -> Response.builder() + .setMimeType(MimeType.JSON) + .setJSONContent(jsonResponseSupplier.get()) + .build()); } public static void invalidate(String identifier) { diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/GraphsJSONResolver.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/GraphsJSONResolver.java index 8e7c3d779..0a0f43158 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/GraphsJSONResolver.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/GraphsJSONResolver.java @@ -17,22 +17,19 @@ package com.djrapitops.plan.delivery.webserver.pages.json; import com.djrapitops.plan.delivery.rendering.json.graphs.GraphJSONCreator; -import com.djrapitops.plan.delivery.webserver.RequestInternal; -import com.djrapitops.plan.delivery.webserver.RequestTarget; -import com.djrapitops.plan.delivery.webserver.auth.Authentication; +import com.djrapitops.plan.delivery.web.resolver.Resolver; +import com.djrapitops.plan.delivery.web.resolver.Response; +import com.djrapitops.plan.delivery.web.resolver.request.Request; +import com.djrapitops.plan.delivery.web.resolver.request.WebUser; import com.djrapitops.plan.delivery.webserver.cache.DataID; import com.djrapitops.plan.delivery.webserver.cache.JSONCache; -import com.djrapitops.plan.delivery.webserver.pages.PageResolver; -import com.djrapitops.plan.delivery.webserver.response.Response_old; -import com.djrapitops.plan.delivery.webserver.response.data.JSONResponse; -import com.djrapitops.plan.exceptions.WebUserAuthException; import com.djrapitops.plan.exceptions.connection.BadRequestException; -import com.djrapitops.plan.exceptions.connection.WebException; import com.djrapitops.plan.identification.Identifiers; import javax.inject.Inject; import javax.inject.Singleton; import java.util.Collections; +import java.util.Optional; import java.util.UUID; /** @@ -41,7 +38,7 @@ import java.util.UUID; * @author Rsl1122 */ @Singleton -public class GraphsJSONResolver implements PageResolver { +public class GraphsJSONResolver implements Resolver { private final Identifiers identifiers; private final GraphJSONCreator graphJSON; @@ -56,14 +53,23 @@ public class GraphsJSONResolver implements PageResolver { } @Override - public Response_old resolve(RequestInternal request, RequestTarget target) throws WebException { - String type = target.getParameter("type") + public boolean canAccess(Request request) { + return request.getUser().orElse(new WebUser("")).hasPermission("page.server"); + } + + @Override + public Optional resolve(Request request) { + return Optional.of(getResponse(request)); + } + + private Response getResponse(Request request) { + String type = request.getQuery().get("type") .orElseThrow(() -> new BadRequestException("'type' parameter was not defined.")); DataID dataID = getDataID(type); - if (target.getParameter("server").isPresent()) { - UUID serverUUID = identifiers.getServerUUID(target); // Can throw BadRequestException + if (request.getQuery().get("server").isPresent()) { + UUID serverUUID = identifiers.getServerUUID(request); // Can throw BadRequestException return JSONCache.getOrCache(dataID, serverUUID, () -> generateGraphDataJSONOfType(dataID, serverUUID)); } // Assume network @@ -86,48 +92,43 @@ public class GraphsJSONResolver implements PageResolver { } } - private JSONResponse generateGraphDataJSONOfType(DataID id, UUID serverUUID) { + private Object generateGraphDataJSONOfType(DataID id, UUID serverUUID) { switch (id) { case GRAPH_PERFORMANCE: - return new JSONResponse(graphJSON.performanceGraphJSON(serverUUID)); + return graphJSON.performanceGraphJSON(serverUUID); case GRAPH_ONLINE: - return new JSONResponse(graphJSON.playersOnlineGraph(serverUUID)); + return graphJSON.playersOnlineGraph(serverUUID); case GRAPH_UNIQUE_NEW: - return new JSONResponse(graphJSON.uniqueAndNewGraphJSON(serverUUID)); + return graphJSON.uniqueAndNewGraphJSON(serverUUID); case GRAPH_CALENDAR: - return new JSONResponse(graphJSON.serverCalendarJSON(serverUUID)); + return graphJSON.serverCalendarJSON(serverUUID); case GRAPH_WORLD_PIE: - return new JSONResponse(graphJSON.serverWorldPieJSONAsMap(serverUUID)); + return graphJSON.serverWorldPieJSONAsMap(serverUUID); case GRAPH_ACTIVITY: - return new JSONResponse(graphJSON.activityGraphsJSONAsMap(serverUUID)); + return graphJSON.activityGraphsJSONAsMap(serverUUID); case GRAPH_WORLD_MAP: - return new JSONResponse(graphJSON.geolocationGraphsJSONAsMap(serverUUID)); + return graphJSON.geolocationGraphsJSONAsMap(serverUUID); case GRAPH_PING: - return new JSONResponse(graphJSON.pingGraphsJSON(serverUUID)); + return graphJSON.pingGraphsJSON(serverUUID); case GRAPH_PUNCHCARD: - return new JSONResponse(graphJSON.punchCardJSONAsMap(serverUUID)); + return graphJSON.punchCardJSONAsMap(serverUUID); default: - return new JSONResponse(Collections.singletonMap("error", "Undefined ID: " + id.name())); + return Collections.singletonMap("error", "Undefined ID: " + id.name()); } } - private JSONResponse generateGraphDataJSONOfType(DataID id) { + private Object generateGraphDataJSONOfType(DataID id) { switch (id) { case GRAPH_ACTIVITY: - return new JSONResponse(graphJSON.activityGraphsJSONAsMap()); + return graphJSON.activityGraphsJSONAsMap(); case GRAPH_UNIQUE_NEW: - return new JSONResponse(graphJSON.uniqueAndNewGraphJSON()); + return graphJSON.uniqueAndNewGraphJSON(); case GRAPH_SERVER_PIE: - return new JSONResponse(graphJSON.serverPreferencePieJSONAsMap()); + return graphJSON.serverPreferencePieJSONAsMap(); case GRAPH_WORLD_MAP: - return new JSONResponse(graphJSON.geolocationGraphsJSONAsMap()); + return graphJSON.geolocationGraphsJSONAsMap(); default: - return new JSONResponse(Collections.singletonMap("error", "Undefined ID: " + id.name())); + return Collections.singletonMap("error", "Undefined ID: " + id.name()); } } - - @Override - public boolean isAuthorized(Authentication auth, RequestTarget target) throws WebUserAuthException { - return auth.getWebUser().getPermLevel() <= 0; - } } \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/NetworkJSONResolver.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/NetworkJSONResolver.java index cef2612c7..b29e04016 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/NetworkJSONResolver.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/NetworkJSONResolver.java @@ -21,11 +21,8 @@ import com.djrapitops.plan.delivery.rendering.json.network.NetworkOverviewJSONCr import com.djrapitops.plan.delivery.rendering.json.network.NetworkPlayerBaseOverviewJSONCreator; import com.djrapitops.plan.delivery.rendering.json.network.NetworkSessionsOverviewJSONCreator; import com.djrapitops.plan.delivery.rendering.json.network.NetworkTabJSONCreator; -import com.djrapitops.plan.delivery.webserver.RequestTarget; -import com.djrapitops.plan.delivery.webserver.auth.Authentication; +import com.djrapitops.plan.delivery.web.resolver.CompositeResolver; import com.djrapitops.plan.delivery.webserver.cache.DataID; -import com.djrapitops.plan.delivery.webserver.pages.CompositePageResolver; -import com.djrapitops.plan.delivery.webserver.response.ResponseFactory; import javax.inject.Inject; import javax.inject.Singleton; @@ -36,31 +33,31 @@ import javax.inject.Singleton; * @author Rsl1122 */ @Singleton -public class NetworkJSONResolver extends CompositePageResolver { +public class NetworkJSONResolver { + + private final CompositeResolver resolver; @Inject public NetworkJSONResolver( - ResponseFactory responseFactory, JSONFactory jsonFactory, NetworkOverviewJSONCreator networkOverviewJSONCreator, NetworkPlayerBaseOverviewJSONCreator networkPlayerBaseOverviewJSONCreator, NetworkSessionsOverviewJSONCreator networkSessionsOverviewJSONCreator ) { - super(responseFactory); - - registerPage("overview", DataID.SERVER_OVERVIEW, networkOverviewJSONCreator); - registerPage("playerbaseOverview", DataID.PLAYERBASE_OVERVIEW, networkPlayerBaseOverviewJSONCreator); - registerPage("sessionsOverview", DataID.SESSIONS_OVERVIEW, networkSessionsOverviewJSONCreator); - registerPage("servers", DataID.SERVERS, jsonFactory::serversAsJSONMaps); - registerPage("pingTable", DataID.PING_TABLE, jsonFactory::pingPerGeolocation); + resolver = CompositeResolver.builder() + .add("overview", forJSON(DataID.SERVER_OVERVIEW, networkOverviewJSONCreator)) + .add("playerbaseOverview", forJSON(DataID.PLAYERBASE_OVERVIEW, networkPlayerBaseOverviewJSONCreator)) + .add("sessionsOverview", forJSON(DataID.SESSIONS_OVERVIEW, networkSessionsOverviewJSONCreator)) + .add("servers", forJSON(DataID.SERVERS, jsonFactory::serversAsJSONMaps)) + .add("pingTable", forJSON(DataID.PING_TABLE, jsonFactory::pingPerGeolocation)) + .build(); } - private void registerPage(String identifier, DataID dataID, NetworkTabJSONCreator tabJSONCreator) { - registerPage(identifier, new NetworkTabJSONResolver<>(dataID, tabJSONCreator)); + private NetworkTabJSONResolver forJSON(DataID dataID, NetworkTabJSONCreator tabJSONCreator) { + return new NetworkTabJSONResolver<>(dataID, tabJSONCreator); } - @Override - public boolean isAuthorized(Authentication auth, RequestTarget target) { - return true; + public CompositeResolver getResolver() { + return resolver; } } \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/NetworkTabJSONResolver.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/NetworkTabJSONResolver.java index b44ff8e3f..c74b1c6ce 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/NetworkTabJSONResolver.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/NetworkTabJSONResolver.java @@ -17,16 +17,14 @@ package com.djrapitops.plan.delivery.webserver.pages.json; import com.djrapitops.plan.delivery.rendering.json.network.NetworkTabJSONCreator; -import com.djrapitops.plan.delivery.webserver.RequestInternal; -import com.djrapitops.plan.delivery.webserver.RequestTarget; -import com.djrapitops.plan.delivery.webserver.auth.Authentication; +import com.djrapitops.plan.delivery.web.resolver.Resolver; +import com.djrapitops.plan.delivery.web.resolver.Response; +import com.djrapitops.plan.delivery.web.resolver.request.Request; +import com.djrapitops.plan.delivery.web.resolver.request.WebUser; import com.djrapitops.plan.delivery.webserver.cache.DataID; import com.djrapitops.plan.delivery.webserver.cache.JSONCache; -import com.djrapitops.plan.delivery.webserver.pages.PageResolver; -import com.djrapitops.plan.delivery.webserver.response.Response_old; -import com.djrapitops.plan.delivery.webserver.response.data.JSONResponse; -import com.djrapitops.plan.exceptions.WebUserAuthException; +import java.util.Optional; import java.util.function.Supplier; /** @@ -34,7 +32,7 @@ import java.util.function.Supplier; * * @author Rsl1122 */ -public class NetworkTabJSONResolver implements PageResolver { +public class NetworkTabJSONResolver implements Resolver { private final DataID dataID; private final Supplier jsonCreator; @@ -45,12 +43,17 @@ public class NetworkTabJSONResolver implements PageResolver { } @Override - public Response_old resolve(RequestInternal request, RequestTarget target) { - return JSONCache.getOrCache(dataID, () -> new JSONResponse(jsonCreator.get())); + public boolean canAccess(Request request) { + return request.getUser().orElse(new WebUser("")).hasPermission("page.network"); } @Override - public boolean isAuthorized(Authentication auth, RequestTarget target) throws WebUserAuthException { - return auth.getWebUser().getPermLevel() <= 0; + public Optional resolve(Request request) { + return Optional.of(getResponse()); } + + private Response getResponse() { + return JSONCache.getOrCache(dataID, jsonCreator); + } + } \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/PlayerJSONResolver.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/PlayerJSONResolver.java index 30f6b40e5..aae7e33ba 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/PlayerJSONResolver.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/PlayerJSONResolver.java @@ -16,24 +16,21 @@ */ package com.djrapitops.plan.delivery.webserver.pages.json; -import com.djrapitops.plan.delivery.domain.WebUser_old; import com.djrapitops.plan.delivery.rendering.json.PlayerJSONCreator; -import com.djrapitops.plan.delivery.webserver.RequestInternal; -import com.djrapitops.plan.delivery.webserver.RequestTarget; -import com.djrapitops.plan.delivery.webserver.auth.Authentication; -import com.djrapitops.plan.delivery.webserver.pages.PageResolver; -import com.djrapitops.plan.delivery.webserver.response.Response_old; -import com.djrapitops.plan.delivery.webserver.response.data.JSONResponse; -import com.djrapitops.plan.exceptions.WebUserAuthException; -import com.djrapitops.plan.exceptions.connection.WebException; +import com.djrapitops.plan.delivery.web.resolver.MimeType; +import com.djrapitops.plan.delivery.web.resolver.Resolver; +import com.djrapitops.plan.delivery.web.resolver.Response; +import com.djrapitops.plan.delivery.web.resolver.request.Request; +import com.djrapitops.plan.delivery.web.resolver.request.WebUser; import com.djrapitops.plan.identification.Identifiers; import javax.inject.Inject; import javax.inject.Singleton; +import java.util.Optional; import java.util.UUID; @Singleton -public class PlayerJSONResolver implements PageResolver { +public class PlayerJSONResolver implements Resolver { private final Identifiers identifiers; private final PlayerJSONCreator jsonCreator; @@ -45,15 +42,24 @@ public class PlayerJSONResolver implements PageResolver { } @Override - public Response_old resolve(RequestInternal request, RequestTarget target) throws WebException { - UUID playerUUID = identifiers.getPlayerUUID(target); // Can throw BadRequestException - return new JSONResponse(jsonCreator.createJSONAsMap(playerUUID)); + public boolean canAccess(Request request) { + WebUser user = request.getUser().orElse(new WebUser("")); + UUID playerUUID = identifiers.getPlayerUUID(request); + UUID webUserUUID = identifiers.getPlayerUUID(user.getName()); + boolean isOwnPage = playerUUID.equals(webUserUUID); + return user.hasPermission("page.player.other") || (user.hasPermission("page.player.self") && isOwnPage); } @Override - public boolean isAuthorized(Authentication auth, RequestTarget target) throws WebUserAuthException { - WebUser_old webUser = auth.getWebUser(); - return webUser.getPermLevel() <= 1 || webUser.getName().equalsIgnoreCase(target.get(target.size() - 1)); + public Optional resolve(Request request) { + return Optional.of(getResponse(request)); + } + private Response getResponse(Request request) { + UUID playerUUID = identifiers.getPlayerUUID(request); // Can throw BadRequestException + return Response.builder() + .setMimeType(MimeType.JSON) + .setJSONContent(jsonCreator.createJSONAsMap(playerUUID)) + .build(); } } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/PlayerKillsJSONResolver.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/PlayerKillsJSONResolver.java index 63ea87303..60d34589c 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/PlayerKillsJSONResolver.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/PlayerKillsJSONResolver.java @@ -17,21 +17,18 @@ package com.djrapitops.plan.delivery.webserver.pages.json; import com.djrapitops.plan.delivery.rendering.json.JSONFactory; -import com.djrapitops.plan.delivery.webserver.RequestInternal; -import com.djrapitops.plan.delivery.webserver.RequestTarget; -import com.djrapitops.plan.delivery.webserver.auth.Authentication; +import com.djrapitops.plan.delivery.web.resolver.Resolver; +import com.djrapitops.plan.delivery.web.resolver.Response; +import com.djrapitops.plan.delivery.web.resolver.request.Request; +import com.djrapitops.plan.delivery.web.resolver.request.WebUser; import com.djrapitops.plan.delivery.webserver.cache.DataID; import com.djrapitops.plan.delivery.webserver.cache.JSONCache; -import com.djrapitops.plan.delivery.webserver.pages.PageResolver; -import com.djrapitops.plan.delivery.webserver.response.Response_old; -import com.djrapitops.plan.delivery.webserver.response.data.JSONResponse; -import com.djrapitops.plan.exceptions.WebUserAuthException; -import com.djrapitops.plan.exceptions.connection.WebException; import com.djrapitops.plan.identification.Identifiers; import javax.inject.Inject; import javax.inject.Singleton; import java.util.Collections; +import java.util.Optional; import java.util.UUID; /** @@ -40,7 +37,7 @@ import java.util.UUID; * @author Rsl1122 */ @Singleton -public class PlayerKillsJSONResolver implements PageResolver { +public class PlayerKillsJSONResolver implements Resolver { private final Identifiers identifiers; private final JSONFactory jsonFactory; @@ -55,15 +52,17 @@ public class PlayerKillsJSONResolver implements PageResolver { } @Override - public Response_old resolve(RequestInternal 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))) - ); + public boolean canAccess(Request request) { + return request.getUser().orElse(new WebUser("")).hasPermission("page.server"); } @Override - public boolean isAuthorized(Authentication auth, RequestTarget target) throws WebUserAuthException { - return auth.getWebUser().getPermLevel() <= 0; + public Optional resolve(Request request) { + return Optional.of(getResponse(request)); + } + + private Response getResponse(Request request) { + UUID serverUUID = identifiers.getServerUUID(request); + return JSONCache.getOrCache(DataID.KILLS, serverUUID, () -> Collections.singletonMap("player_kills", jsonFactory.serverPlayerKillsAsJSONMap(serverUUID))); } } \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/PlayersTableJSONResolver.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/PlayersTableJSONResolver.java index 969ce833e..3cac51150 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/PlayersTableJSONResolver.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/PlayersTableJSONResolver.java @@ -17,20 +17,17 @@ package com.djrapitops.plan.delivery.webserver.pages.json; import com.djrapitops.plan.delivery.rendering.json.JSONFactory; -import com.djrapitops.plan.delivery.webserver.RequestInternal; -import com.djrapitops.plan.delivery.webserver.RequestTarget; -import com.djrapitops.plan.delivery.webserver.auth.Authentication; +import com.djrapitops.plan.delivery.web.resolver.Resolver; +import com.djrapitops.plan.delivery.web.resolver.Response; +import com.djrapitops.plan.delivery.web.resolver.request.Request; +import com.djrapitops.plan.delivery.web.resolver.request.WebUser; import com.djrapitops.plan.delivery.webserver.cache.DataID; import com.djrapitops.plan.delivery.webserver.cache.JSONCache; -import com.djrapitops.plan.delivery.webserver.pages.PageResolver; -import com.djrapitops.plan.delivery.webserver.response.Response_old; -import com.djrapitops.plan.delivery.webserver.response.data.JSONResponse; -import com.djrapitops.plan.exceptions.WebUserAuthException; -import com.djrapitops.plan.exceptions.connection.WebException; import com.djrapitops.plan.identification.Identifiers; import javax.inject.Inject; import javax.inject.Singleton; +import java.util.Optional; import java.util.UUID; /** @@ -39,7 +36,7 @@ import java.util.UUID; * @author Rsl1122 */ @Singleton -public class PlayersTableJSONResolver implements PageResolver { +public class PlayersTableJSONResolver implements Resolver { private final Identifiers identifiers; private final JSONFactory jsonFactory; @@ -54,21 +51,26 @@ public class PlayersTableJSONResolver implements PageResolver { } @Override - public Response_old resolve(RequestInternal 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))); + public boolean canAccess(Request request) { + WebUser user = request.getUser().orElse(new WebUser("")); + if (request.getQuery().get("server").isPresent()) { + return user.hasPermission("page.server"); } // Assume players page - return JSONCache.getOrCache(DataID.PLAYERS, () -> new JSONResponse(jsonFactory.networkPlayersTableJSON())); + return user.hasPermission("page.players"); } @Override - public boolean isAuthorized(Authentication auth, RequestTarget target) throws WebUserAuthException { - if (target.getParameter("server").isPresent()) { - return auth.getWebUser().getPermLevel() <= 0; + public Optional resolve(Request request) { + return Optional.of(getResponse(request)); + } + + private Response getResponse(Request request) { + if (request.getQuery().get("server").isPresent()) { + UUID serverUUID = identifiers.getServerUUID(request); // Can throw BadRequestException + return JSONCache.getOrCache(DataID.PLAYERS, serverUUID, () -> jsonFactory.serverPlayersTableJSON(serverUUID)); } // Assume players page - return auth.getWebUser().getPermLevel() <= 1; + return JSONCache.getOrCache(DataID.PLAYERS, jsonFactory::networkPlayersTableJSON); } } \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/RootJSONResolver.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/RootJSONResolver.java index b72d4f8e6..7b548a085 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/RootJSONResolver.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/RootJSONResolver.java @@ -17,11 +17,8 @@ package com.djrapitops.plan.delivery.webserver.pages.json; 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.web.resolver.CompositeResolver; import com.djrapitops.plan.delivery.webserver.cache.DataID; -import com.djrapitops.plan.delivery.webserver.pages.CompositePageResolver; -import com.djrapitops.plan.delivery.webserver.response.ResponseFactory; import com.djrapitops.plan.identification.Identifiers; import javax.inject.Inject; @@ -33,13 +30,13 @@ import javax.inject.Singleton; * @author Rsl1122 */ @Singleton -public class RootJSONResolver extends CompositePageResolver { +public class RootJSONResolver { private final Identifiers identifiers; + private final CompositeResolver resolver; @Inject public RootJSONResolver( - ResponseFactory responseFactory, Identifiers identifiers, JSONFactory jsonFactory, @@ -57,32 +54,30 @@ public class RootJSONResolver extends CompositePageResolver { PlayerJSONResolver playerJSONResolver, NetworkJSONResolver networkJSONResolver ) { - super(responseFactory); this.identifiers = identifiers; - registerPage("players", playersTableJSONResolver, 1); - registerPage("sessions", sessionsJSONResolver, 0); - registerPage("kills", playerKillsJSONResolver, 0); - registerPage("pingTable", DataID.PING_TABLE, jsonFactory::pingPerGeolocation); - registerPage("graph", graphsJSONResolver, 0); - - registerPage("serverOverview", DataID.SERVER_OVERVIEW, serverOverviewJSONCreator); - registerPage("onlineOverview", DataID.ONLINE_OVERVIEW, onlineActivityOverviewJSONCreator); - registerPage("sessionsOverview", DataID.SESSIONS_OVERVIEW, sessionsOverviewJSONCreator); - registerPage("playerVersus", DataID.PVP_PVE, pvPPvEJSONCreator); - registerPage("playerbaseOverview", DataID.PLAYERBASE_OVERVIEW, playerBaseOverviewJSONCreator); - registerPage("performanceOverview", DataID.PERFORMANCE_OVERVIEW, performanceJSONCreator); - - registerPage("player", playerJSONResolver, 2); - registerPage("network", networkJSONResolver, 0); + resolver = CompositeResolver.builder() + .add("players", playersTableJSONResolver) + .add("sessions", sessionsJSONResolver) + .add("kills", playerKillsJSONResolver) + .add("graph", graphsJSONResolver) + .add("pingTable", forJSON(DataID.PING_TABLE, jsonFactory::pingPerGeolocation)) + .add("serverOverview", forJSON(DataID.SERVER_OVERVIEW, serverOverviewJSONCreator)) + .add("onlineOverview", forJSON(DataID.ONLINE_OVERVIEW, onlineActivityOverviewJSONCreator)) + .add("sessionsOverview", forJSON(DataID.SESSIONS_OVERVIEW, sessionsOverviewJSONCreator)) + .add("playerVersus", forJSON(DataID.PVP_PVE, pvPPvEJSONCreator)) + .add("playerbaseOverview", forJSON(DataID.PLAYERBASE_OVERVIEW, playerBaseOverviewJSONCreator)) + .add("performanceOverview", forJSON(DataID.PERFORMANCE_OVERVIEW, performanceJSONCreator)) + .add("player", playerJSONResolver) + .add("network", networkJSONResolver.getResolver()) + .build(); } - private void registerPage(String identifier, DataID dataID, ServerTabJSONCreator tabJSONCreator) { - registerPage(identifier, new ServerTabJSONResolver<>(dataID, identifiers, tabJSONCreator), 0); + private ServerTabJSONResolver forJSON(DataID dataID, ServerTabJSONCreator tabJSONCreator) { + return new ServerTabJSONResolver<>(dataID, identifiers, tabJSONCreator); } - @Override - public boolean isAuthorized(Authentication auth, RequestTarget target) { - return true; + public CompositeResolver getResolver() { + return resolver; } } \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/ServerTabJSONResolver.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/ServerTabJSONResolver.java index 05debe8ac..0517dd451 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/ServerTabJSONResolver.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/ServerTabJSONResolver.java @@ -17,18 +17,15 @@ package com.djrapitops.plan.delivery.webserver.pages.json; import com.djrapitops.plan.delivery.rendering.json.ServerTabJSONCreator; -import com.djrapitops.plan.delivery.webserver.RequestInternal; -import com.djrapitops.plan.delivery.webserver.RequestTarget; -import com.djrapitops.plan.delivery.webserver.auth.Authentication; +import com.djrapitops.plan.delivery.web.resolver.Resolver; +import com.djrapitops.plan.delivery.web.resolver.Response; +import com.djrapitops.plan.delivery.web.resolver.request.Request; +import com.djrapitops.plan.delivery.web.resolver.request.WebUser; import com.djrapitops.plan.delivery.webserver.cache.DataID; import com.djrapitops.plan.delivery.webserver.cache.JSONCache; -import com.djrapitops.plan.delivery.webserver.pages.PageResolver; -import com.djrapitops.plan.delivery.webserver.response.Response_old; -import com.djrapitops.plan.delivery.webserver.response.data.JSONResponse; -import com.djrapitops.plan.exceptions.WebUserAuthException; -import com.djrapitops.plan.exceptions.connection.WebException; import com.djrapitops.plan.identification.Identifiers; +import java.util.Optional; import java.util.UUID; import java.util.function.Function; @@ -37,7 +34,7 @@ import java.util.function.Function; * * @author Rsl1122 */ -public class ServerTabJSONResolver implements PageResolver { +public class ServerTabJSONResolver implements Resolver { private final DataID dataID; private final Identifiers identifiers; @@ -54,13 +51,14 @@ public class ServerTabJSONResolver implements PageResolver { } @Override - public Response_old resolve(RequestInternal request, RequestTarget target) throws WebException { - UUID serverUUID = identifiers.getServerUUID(target); // Can throw BadRequestException - return JSONCache.getOrCache(dataID, serverUUID, () -> new JSONResponse(jsonCreator.apply(serverUUID))); + public boolean canAccess(Request request) { + return request.getUser().orElse(new WebUser("")).hasPermission("page.server"); } @Override - public boolean isAuthorized(Authentication auth, RequestTarget target) throws WebUserAuthException { - return auth.getWebUser().getPermLevel() <= 0; + public Optional resolve(Request request) { + UUID serverUUID = identifiers.getServerUUID(request); // Can throw BadRequestException + return Optional.of(JSONCache.getOrCache(dataID, serverUUID, () -> jsonCreator.apply(serverUUID))); } + } \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/SessionsJSONResolver.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/SessionsJSONResolver.java index 0bbe0e8ba..5996fa373 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/SessionsJSONResolver.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/SessionsJSONResolver.java @@ -17,21 +17,18 @@ package com.djrapitops.plan.delivery.webserver.pages.json; import com.djrapitops.plan.delivery.rendering.json.JSONFactory; -import com.djrapitops.plan.delivery.webserver.RequestInternal; -import com.djrapitops.plan.delivery.webserver.RequestTarget; -import com.djrapitops.plan.delivery.webserver.auth.Authentication; +import com.djrapitops.plan.delivery.web.resolver.Resolver; +import com.djrapitops.plan.delivery.web.resolver.Response; +import com.djrapitops.plan.delivery.web.resolver.request.Request; +import com.djrapitops.plan.delivery.web.resolver.request.WebUser; import com.djrapitops.plan.delivery.webserver.cache.DataID; import com.djrapitops.plan.delivery.webserver.cache.JSONCache; -import com.djrapitops.plan.delivery.webserver.pages.PageResolver; -import com.djrapitops.plan.delivery.webserver.response.Response_old; -import com.djrapitops.plan.delivery.webserver.response.data.JSONResponse; -import com.djrapitops.plan.exceptions.WebUserAuthException; -import com.djrapitops.plan.exceptions.connection.WebException; import com.djrapitops.plan.identification.Identifiers; import javax.inject.Inject; import javax.inject.Singleton; import java.util.Collections; +import java.util.Optional; import java.util.UUID; /** @@ -40,7 +37,7 @@ import java.util.UUID; * @author Rsl1122 */ @Singleton -public class SessionsJSONResolver implements PageResolver { +public class SessionsJSONResolver implements Resolver { private final Identifiers identifiers; private final JSONFactory jsonFactory; @@ -55,21 +52,21 @@ public class SessionsJSONResolver implements PageResolver { } @Override - public Response_old resolve(RequestInternal request, RequestTarget target) throws WebException { - if (target.getParameter("server").isPresent()) { - UUID serverUUID = identifiers.getServerUUID(target); - return JSONCache.getOrCache(DataID.SESSIONS, serverUUID, () -> - new JSONResponse(Collections.singletonMap("sessions", jsonFactory.serverSessionsAsJSONMap(serverUUID))) - ); - } - // Assume network - return JSONCache.getOrCache(DataID.SESSIONS, () -> - new JSONResponse(Collections.singletonMap("sessions", jsonFactory.networkSessionsAsJSONMap())) - ); + public boolean canAccess(Request request) { + return request.getUser().orElse(new WebUser("")).hasPermission("page.server"); } @Override - public boolean isAuthorized(Authentication auth, RequestTarget target) throws WebUserAuthException { - return auth.getWebUser().getPermLevel() <= 0; + public Optional resolve(Request request) { + return Optional.of(getResponse(request)); + } + + private Response getResponse(Request request) { + if (request.getQuery().get("server").isPresent()) { + UUID serverUUID = identifiers.getServerUUID(request); + return JSONCache.getOrCache(DataID.SESSIONS, serverUUID, () -> Collections.singletonMap("sessions", jsonFactory.serverSessionsAsJSONMap(serverUUID))); + } + // Assume network + return JSONCache.getOrCache(DataID.SESSIONS, () -> Collections.singletonMap("sessions", jsonFactory.networkSessionsAsJSONMap())); } } \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/exceptions/connection/WebException.java b/Plan/common/src/main/java/com/djrapitops/plan/exceptions/connection/WebException.java index 938e7a175..60a6003e7 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/exceptions/connection/WebException.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/exceptions/connection/WebException.java @@ -21,7 +21,7 @@ package com.djrapitops.plan.exceptions.connection; * * @author Rsl1122 */ -public class WebException extends Exception { +public class WebException extends IllegalStateException { public WebException() { } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/identification/Identifiers.java b/Plan/common/src/main/java/com/djrapitops/plan/identification/Identifiers.java index 9377c7ec8..1ba8048b3 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/identification/Identifiers.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/identification/Identifiers.java @@ -16,7 +16,7 @@ */ package com.djrapitops.plan.identification; -import com.djrapitops.plan.delivery.webserver.RequestTarget; +import com.djrapitops.plan.delivery.web.resolver.request.Request; import com.djrapitops.plan.exceptions.connection.BadRequestException; import com.djrapitops.plan.storage.database.DBSystem; import com.djrapitops.plan.storage.database.queries.objects.ServerQueries; @@ -36,21 +36,20 @@ import java.util.UUID; public class Identifiers { protected final DBSystem dbSystem; + private final UUIDUtility uuidUtility; @Inject - public Identifiers(DBSystem dbSystem) { + public Identifiers(DBSystem dbSystem, UUIDUtility uuidUtility) { this.dbSystem = dbSystem; + this.uuidUtility = uuidUtility; } - public UUID getServerUUID(RequestTarget target) throws BadRequestException { - String serverIndentifier = target.getParameter("server") + public UUID getServerUUID(Request request) throws BadRequestException { + String serverIndentifier = request.getQuery().get("server") .orElseThrow(() -> new BadRequestException("'server' parameter was not defined.")); Optional parsed = UUIDUtility.parseFromString(serverIndentifier); - if (parsed.isPresent()) { - return parsed.get(); - } - return getServerUUIDFromName(serverIndentifier); + return parsed.orElseGet(() -> getServerUUIDFromName(serverIndentifier)); } private UUID getServerUUIDFromName(String serverName) throws BadRequestException { @@ -59,15 +58,12 @@ public class Identifiers { .orElseThrow(() -> new BadRequestException("Given 'server' was not found in the database: '" + serverName + "'")); } - public UUID getPlayerUUID(RequestTarget target) throws BadRequestException { - String playerIdentifier = target.getParameter("player") + public UUID getPlayerUUID(Request request) throws BadRequestException { + String playerIdentifier = request.getQuery().get("player") .orElseThrow(() -> new BadRequestException("'player' parameter was not defined.")).trim(); Optional parsed = UUIDUtility.parseFromString(playerIdentifier); - if (parsed.isPresent()) { - return parsed.get(); - } - return getPlayerUUIDFromName(playerIdentifier); + return parsed.orElseGet(() -> getPlayerUUIDFromName(playerIdentifier)); } private UUID getPlayerUUIDFromName(String playerName) throws BadRequestException { @@ -75,4 +71,8 @@ public class Identifiers { .query(UserIdentifierQueries.fetchPlayerUUIDOf(playerName)) .orElseThrow(() -> new BadRequestException("Given 'player' was not found in the database: '" + playerName + "'")); } + + public UUID getPlayerUUID(String name) { + return uuidUtility.getUUIDOf(name); + } } \ No newline at end of file