From 6ed6dd1f9f99610c84b14daf2505db366ae36ad3 Mon Sep 17 00:00:00 2001 From: Rsl1122 <24460436+Rsl1122@users.noreply.github.com> Date: Sun, 16 Feb 2020 18:18:14 +0200 Subject: [PATCH] Refactored Resolver to use new Request class URIPath and URIQuery were not enough info on the Request In addition the Request supports - WebUser - Request Headers - Request method (GET, POST etc) --- .../web/resolver/CompositeResolver.java | 15 +-- .../delivery/web/resolver/NoAuthResolver.java | 11 ++- .../plan/delivery/web/resolver/Resolver.java | 19 ++-- .../web/resolver/request/Headers.java | 22 +++++ .../web/resolver/request/Request.java | 99 +++++++++++++++++++ .../web/resolver/{ => request}/URIPath.java | 2 +- .../web/resolver/{ => request}/URIQuery.java | 2 +- .../web/resolver/{ => request}/WebUser.java | 2 +- .../delivery/web/resolver/URIPathTest.java | 1 + .../plan/delivery/domain/WebUser_old.java | 6 +- .../delivery/rendering/pages/PageFactory.java | 2 + .../delivery/webserver/RequestHandler.java | 4 +- .../{Request.java => RequestInternal.java} | 38 ++++++- .../delivery/webserver/ResponseResolver.java | 34 ++++--- .../delivery/webserver/ResponseSender.java | 90 +++++++++++++++++ .../pages/CompositePageResolver.java | 8 +- .../webserver/pages/DebugPageResolver.java | 10 +- .../webserver/pages/PageResolver.java | 4 +- .../webserver/pages/PlayerPageResolver.java | 19 ++-- .../webserver/pages/PlayersPageResolver.java | 10 +- .../webserver/pages/RootPageResolver.java | 14 ++- .../webserver/pages/ServerPageResolver.java | 15 ++- .../pages/json/GraphsJSONResolver.java | 4 +- .../pages/json/NetworkTabJSONResolver.java | 4 +- .../pages/json/PlayerJSONResolver.java | 4 +- .../pages/json/PlayerKillsJSONResolver.java | 4 +- .../pages/json/PlayersTableJSONResolver.java | 4 +- .../pages/json/ServerTabJSONResolver.java | 4 +- .../pages/json/SessionsJSONResolver.java | 4 +- .../webserver/response/ResponseFactory.java | 18 +++- 30 files changed, 384 insertions(+), 89 deletions(-) create mode 100644 Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resolver/request/Headers.java create mode 100644 Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resolver/request/Request.java rename Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resolver/{ => request}/URIPath.java (98%) rename Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resolver/{ => request}/URIQuery.java (97%) rename Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resolver/{ => request}/WebUser.java (95%) rename Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/{Request.java => RequestInternal.java} (66%) create mode 100644 Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/ResponseSender.java 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 30ab13680..1fee6bf53 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 @@ -16,6 +16,9 @@ */ package com.djrapitops.plan.delivery.web.resolver; +import com.djrapitops.plan.delivery.web.resolver.request.Request; +import com.djrapitops.plan.delivery.web.resolver.request.URIPath; + import java.util.ArrayList; import java.util.List; import java.util.Optional; @@ -63,16 +66,16 @@ public final class CompositeResolver implements Resolver { } @Override - public boolean canAccess(WebUser permissions, URIPath target, URIQuery query) { - return getResolver(target) - .map(resolver -> resolver.canAccess(permissions, target.omitFirst(), query)) + public boolean canAccess(Request request) { + return getResolver(request.getPath()) + .map(resolver -> resolver.canAccess(request)) .orElse(true); } @Override - public Optional resolve(URIPath target, URIQuery query) { - return getResolver(target) - .flatMap(resolver -> resolver.resolve(target.omitFirst(), query)); + public Optional resolve(Request request) { + return getResolver(request.getPath()) + .flatMap(resolver -> resolver.resolve(request)); } public static class Builder { diff --git a/Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resolver/NoAuthResolver.java b/Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resolver/NoAuthResolver.java index 664351fba..6d6af1f43 100644 --- a/Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resolver/NoAuthResolver.java +++ b/Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resolver/NoAuthResolver.java @@ -16,6 +16,8 @@ */ package com.djrapitops.plan.delivery.web.resolver; +import com.djrapitops.plan.delivery.web.resolver.request.Request; + import java.util.Optional; /** @@ -25,25 +27,24 @@ import java.util.Optional; */ public interface NoAuthResolver extends Resolver { - default boolean canAccess(WebUser permissions, URIPath target, URIQuery query) { + default boolean canAccess(Request request) { return true; } /** * Implement request resolution. * - * @param target Target that is being accessed, /example/target - * @param query Parameters in the URL, ?param=value etc. + * @param request HTTP request, contains all information necessary to resolve the request. * @return Response or empty if the response should be 404 (not found). * @see Response for return value */ - Optional resolve(URIPath target, URIQuery query); + Optional resolve(Request request); default ResponseBuilder newResponseBuilder() { return Response.builder(); } - default boolean requiresAuth(URIPath target, URIQuery query) { + default boolean requiresAuth(Request request) { return false; } } diff --git a/Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resolver/Resolver.java b/Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resolver/Resolver.java index 3b54be752..01821e034 100644 --- a/Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resolver/Resolver.java +++ b/Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resolver/Resolver.java @@ -16,6 +16,9 @@ */ package com.djrapitops.plan.delivery.web.resolver; +import com.djrapitops.plan.delivery.web.resolver.request.Request; +import com.djrapitops.plan.delivery.web.resolver.request.WebUser; + import java.util.Optional; /** @@ -31,28 +34,28 @@ public interface Resolver { *

* Is not called when access control is not active. * - * @param permissions WebUser that is accessing this page. - * @param target Target that is being accessed, /example/target - * @param query Parameters in the URL, ?param=value etc. + * @param request HTTP request, contains all information necessary to check access. * @return true if allowed or invalid target, false if response should be 403 (forbidden) + * @see Request#getUser() for {@link WebUser} that has access permissions. */ - boolean canAccess(WebUser permissions, URIPath target, URIQuery query); + boolean canAccess(Request request); /** * Implement request resolution. * - * @param target Target that is being accessed, /example/target - * @param query Parameters in the URL, ?param=value etc. + * @param request HTTP request, contains all information necessary to resolve the request. * @return Response or empty if the response should be 404 (not found). * @see Response for return value + * @see Request#getPath() for path /example/path etc + * @see Request#getQuery() for parameters ?param=value etc */ - Optional resolve(URIPath target, URIQuery query); + Optional resolve(Request request); default ResponseBuilder newResponseBuilder() { return Response.builder(); } - default boolean requiresAuth(URIPath target, URIQuery query) { + default boolean requiresAuth(Request request) { return true; } } diff --git a/Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resolver/request/Headers.java b/Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resolver/request/Headers.java new file mode 100644 index 000000000..3173fd795 --- /dev/null +++ b/Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resolver/request/Headers.java @@ -0,0 +1,22 @@ +package com.djrapitops.plan.delivery.web.resolver.request; + +import java.util.Map; +import java.util.Optional; + +/** + * Request headers, read only. + * + * @author Rsl1122 + */ +public class Headers { + + private final Map headers; + + public Headers(Map headers) { + this.headers = headers; + } + + private Optional get(String key) { + return Optional.ofNullable(headers.get(key)); + } +} \ No newline at end of file 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 new file mode 100644 index 000000000..1fa7fec0b --- /dev/null +++ b/Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resolver/request/Request.java @@ -0,0 +1,99 @@ +/* + * This file is part of Player Analytics (Plan). + * + * Plan is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License v3 as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Plan is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Plan. If not, see . + */ +package com.djrapitops.plan.delivery.web.resolver.request; + +import com.djrapitops.plan.delivery.web.resolver.Resolver; + +import java.util.Map; +import java.util.Optional; + +/** + * Represents a HTTP request to use with {@link Resolver}. + * + * @author Rsl1122 + */ +public final class Request { + + private final String method; + private final URIPath path; + private final URIQuery query; + private final WebUser user; + private final Map headers; + + /** + * Constructor. + * + * @param method HTTP method, GET, PUT, POST, etc + * @param path Requested path /example/target + * @param query Request parameters ?param=value etc + * @param user Web user doing the request (if authenticated) + * @param headers Request headers https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers + */ + public Request(String method, URIPath path, URIQuery query, WebUser user, Map headers) { + this.method = method; + this.path = path; + this.query = query; + this.user = user; + this.headers = headers; + } + + /** + * Get HTTP method. + * + * @return GET, PUT, POST, etc + */ + public String getMethod() { + return method; + } + + /** + * Get the Requested path. + * + * @return {@link URIPath}. + */ + public URIPath getPath() { + return path; + } + + /** + * Get the Request parameters. + * + * @return {@link URIQuery}. + */ + public URIQuery getQuery() { + return query; + } + + /** + * Get the user making the request. + * + * @return the user if authentication is enabled + */ + public Optional getUser() { + return Optional.ofNullable(user); + } + + /** + * Get a header in the request. + * + * @param key https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers + * @return Value if it is present in the request. + */ + public Optional getHeader(String key) { + return Optional.ofNullable(headers.get(key)); + } +} \ No newline at end of file diff --git a/Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resolver/URIPath.java b/Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resolver/request/URIPath.java similarity index 98% rename from Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resolver/URIPath.java rename to Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resolver/request/URIPath.java index ce15c1e5d..6144b1576 100644 --- a/Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resolver/URIPath.java +++ b/Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resolver/request/URIPath.java @@ -14,7 +14,7 @@ * You should have received a copy of the GNU Lesser General Public License * along with Plan. If not, see . */ -package com.djrapitops.plan.delivery.web.resolver; +package com.djrapitops.plan.delivery.web.resolver.request; import java.util.Optional; diff --git a/Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resolver/URIQuery.java b/Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resolver/request/URIQuery.java similarity index 97% rename from Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resolver/URIQuery.java rename to Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resolver/request/URIQuery.java index 0039f2230..557d207aa 100644 --- a/Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resolver/URIQuery.java +++ b/Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resolver/request/URIQuery.java @@ -14,7 +14,7 @@ * You should have received a copy of the GNU Lesser General Public License * along with Plan. If not, see . */ -package com.djrapitops.plan.delivery.web.resolver; +package com.djrapitops.plan.delivery.web.resolver.request; import org.apache.commons.lang3.StringUtils; diff --git a/Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resolver/WebUser.java b/Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resolver/request/WebUser.java similarity index 95% rename from Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resolver/WebUser.java rename to Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resolver/request/WebUser.java index 90ef2efff..115c41392 100644 --- a/Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resolver/WebUser.java +++ b/Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resolver/request/WebUser.java @@ -14,7 +14,7 @@ * You should have received a copy of the GNU Lesser General Public License * along with Plan. If not, see . */ -package com.djrapitops.plan.delivery.web.resolver; +package com.djrapitops.plan.delivery.web.resolver.request; import java.util.Arrays; import java.util.HashSet; diff --git a/Plan/api/src/test/java/com/djrapitops/plan/delivery/web/resolver/URIPathTest.java b/Plan/api/src/test/java/com/djrapitops/plan/delivery/web/resolver/URIPathTest.java index 9076bb82e..2fb1e30e0 100644 --- a/Plan/api/src/test/java/com/djrapitops/plan/delivery/web/resolver/URIPathTest.java +++ b/Plan/api/src/test/java/com/djrapitops/plan/delivery/web/resolver/URIPathTest.java @@ -16,6 +16,7 @@ */ package com.djrapitops.plan.delivery.web.resolver; +import com.djrapitops.plan.delivery.web.resolver.request.URIPath; import org.junit.jupiter.api.Test; import org.junit.platform.runner.JUnitPlatform; import org.junit.runner.RunWith; diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/domain/WebUser_old.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/domain/WebUser_old.java index 6af423cda..f2db71b2c 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/domain/WebUser_old.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/domain/WebUser_old.java @@ -16,6 +16,8 @@ */ package com.djrapitops.plan.delivery.domain; +import com.djrapitops.plan.delivery.web.resolver.request.WebUser; + import java.util.ArrayList; import java.util.List; import java.util.Objects; @@ -65,7 +67,7 @@ public class WebUser_old { return Objects.hash(user, saltedPassHash, permLevel); } - public com.djrapitops.plan.delivery.web.resolver.WebUser toNewWebUser() { + public WebUser toNewWebUser() { List permissions = new ArrayList<>(); if (permLevel <= 0) { permissions.add("page.network"); @@ -80,7 +82,7 @@ public class WebUser_old { if (permLevel <= 2) { permissions.add("page.player.self"); } - return new com.djrapitops.plan.delivery.web.resolver.WebUser( + return new WebUser( user, permissions.toArray(new String[0]) ); } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/pages/PageFactory.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/pages/PageFactory.java index 87c40add1..855658967 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/pages/PageFactory.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/pages/PageFactory.java @@ -43,6 +43,8 @@ import java.util.*; /** * Factory for creating different {@link Page} objects. + *

+ * TODO Set theme and locale stuff in here * * @author Rsl1122 */ diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/RequestHandler.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/RequestHandler.java index 2d4a100ba..45808debf 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/RequestHandler.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/RequestHandler.java @@ -92,7 +92,7 @@ public class RequestHandler implements HttpHandler { Headers requestHeaders = exchange.getRequestHeaders(); Headers responseHeaders = exchange.getResponseHeaders(); - Request request = new Request(exchange, locale); + RequestInternal request = new RequestInternal(exchange, locale); request.setAuth(getAuthorization(requestHeaders)); try { @@ -137,7 +137,7 @@ public class RequestHandler implements HttpHandler { return Optional.empty(); } - private Optional handlePasswordBruteForceAttempts(Request request, Response_old response) { + private Optional handlePasswordBruteForceAttempts(RequestInternal request, Response_old response) { if (request.getAuth().isPresent() && response instanceof PromptAuthorizationResponse) { // Authentication was attempted, but failed so new attempt is going to be given if not forbidden diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/Request.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/RequestInternal.java similarity index 66% rename from Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/Request.java rename to Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/RequestInternal.java index b1011e10c..559a31628 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/Request.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/RequestInternal.java @@ -16,14 +16,20 @@ */ package com.djrapitops.plan.delivery.webserver; -import com.djrapitops.plan.delivery.web.resolver.URIPath; -import com.djrapitops.plan.delivery.web.resolver.URIQuery; +import com.djrapitops.plan.delivery.web.resolver.request.Request; +import com.djrapitops.plan.delivery.web.resolver.request.URIPath; +import com.djrapitops.plan.delivery.web.resolver.request.URIQuery; +import com.djrapitops.plan.delivery.web.resolver.request.WebUser; import com.djrapitops.plan.delivery.webserver.auth.Authentication; +import com.djrapitops.plan.exceptions.WebUserAuthException; import com.djrapitops.plan.settings.locale.Locale; import com.sun.net.httpserver.HttpExchange; import java.io.InputStream; import java.net.URI; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import java.util.Optional; /** @@ -33,7 +39,7 @@ import java.util.Optional; * * @author Rsl1122 */ -public class Request { +public class RequestInternal { private final String requestMethod; private final URI requestURI; @@ -43,7 +49,7 @@ public class Request { private final Locale locale; private Authentication auth; - public Request(HttpExchange exchange, Locale locale) { + public RequestInternal(HttpExchange exchange, Locale locale) { this.requestMethod = exchange.getRequestMethod(); requestURI = exchange.getRequestURI(); @@ -99,4 +105,28 @@ public class Request { public RequestTarget getRequestTarget() { return new RequestTarget(requestURI); } + + public Request toAPIRequest() throws WebUserAuthException { + return new Request( + requestMethod, + getPath(), + getQuery(), + getWebUser(), + getRequestHeaders() + ); + } + + private WebUser getWebUser() throws WebUserAuthException { + Optional auth = getAuth(); + return auth.isPresent() ? auth.get().getWebUser().toNewWebUser() : null; + } + + private Map getRequestHeaders() { + Map headers = new HashMap<>(); + for (Map.Entry> e : exchange.getResponseHeaders().entrySet()) { + List value = e.getValue(); + headers.put(e.getKey(), value.toString().substring(0, value.size() - 1)); + } + return headers; + } } 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 50dcc1734..84b695d86 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 @@ -18,7 +18,10 @@ package com.djrapitops.plan.delivery.webserver; import com.djrapitops.plan.delivery.web.ResolverService; import com.djrapitops.plan.delivery.web.ResolverSvc; -import com.djrapitops.plan.delivery.web.resolver.*; +import com.djrapitops.plan.delivery.web.resolver.NoAuthResolver; +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.webserver.auth.Authentication; import com.djrapitops.plan.delivery.webserver.pages.*; import com.djrapitops.plan.delivery.webserver.pages.json.RootJSONResolver; @@ -104,10 +107,10 @@ public class ResponseResolver extends CompositePageResolver { } public NoAuthResolver noAuthResolverFor(Response response) { - return (target, query) -> Optional.of(response); + return (request) -> Optional.of(response); } - public Response_old getResponse(Request request) { + public Response_old getResponse(RequestInternal request) { try { return tryToGetResponse(request); } catch (NotFoundException e) { @@ -124,22 +127,21 @@ public class ResponseResolver extends CompositePageResolver { } } - private Response_old tryToGetResponse(Request request) throws WebException { - if ("OPTIONS".equalsIgnoreCase(request.getRequestMethod())) { + private Response_old tryToGetResponse(RequestInternal internalRequest) throws WebException { + if ("OPTIONS".equalsIgnoreCase(internalRequest.getRequestMethod())) { return new OptionsResponse(); } - Optional authentication = request.getAuth(); + Optional authentication = internalRequest.getAuth(); - URIPath target = request.getPath(); - URIQuery query = request.getQuery(); - - Optional foundResolver = resolverService.getResolver(target.asString()); - if (!foundResolver.isPresent()) return tryToGetResponse_old(request); // TODO Replace with 404 after refactoring + Optional foundResolver = resolverService.getResolver(internalRequest.getPath().asString()); + // TODO Replace with 404 after refactoring + if (!foundResolver.isPresent()) return tryToGetResponse_old(internalRequest); Resolver resolver = foundResolver.get(); - if (resolver.requiresAuth(target, query)) { + Request request = internalRequest.toAPIRequest(); + if (resolver.requiresAuth(request)) { // Get required auth boolean isAuthRequired = webServer.get().isAuthRequired(); if (isAuthRequired && !authentication.isPresent()) { @@ -150,17 +152,17 @@ public class ResponseResolver extends CompositePageResolver { } } - if (!isAuthRequired || resolver.canAccess(authentication.get().getWebUser().toNewWebUser(), target, query)) { - return resolver.resolve(target, query).map(Response_old::from).orElseGet(responseFactory::pageNotFound404_old); + if (!isAuthRequired || resolver.canAccess(request)) { + return resolver.resolve(request).map(Response_old::from).orElseGet(responseFactory::pageNotFound404_old); } else { return responseFactory.forbidden403_old(); } } else { - return resolver.resolve(target, query).map(Response_old::from).orElseGet(responseFactory::pageNotFound404_old); + return resolver.resolve(request).map(Response_old::from).orElseGet(responseFactory::pageNotFound404_old); } } - private Response_old tryToGetResponse_old(Request request) throws WebException { + private Response_old tryToGetResponse_old(RequestInternal request) throws WebException { RequestTarget target = request.getRequestTarget(); Optional authentication = request.getAuth(); String resource = target.getResourceString(); diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/ResponseSender.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/ResponseSender.java new file mode 100644 index 000000000..6c4078a1f --- /dev/null +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/ResponseSender.java @@ -0,0 +1,90 @@ +/* + * This file is part of Player Analytics (Plan). + * + * Plan is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License v3 as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Plan is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Plan. If not, see . + */ +package com.djrapitops.plan.delivery.webserver; + +import com.djrapitops.plan.delivery.web.resolver.Response; +import com.sun.net.httpserver.Headers; +import com.sun.net.httpserver.HttpExchange; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.util.Map; +import java.util.zip.GZIPOutputStream; + +/** + * Utility for sending a Response to HttpExchange. + * + * @author Rsl1122 + */ +public class ResponseSender { + + private final HttpExchange exchange; + private final Response response; + + public ResponseSender(HttpExchange exchange, Response response) { + this.exchange = exchange; + this.response = response; + } + + public void send() throws IOException { + setResponseHeaders(); + if ("bytes".equalsIgnoreCase(response.getHeaders().get("Accept-Ranges"))) { + sendRawBytes(); + } else { + sendCompressed(); + } + } + + public void setResponseHeaders() { + Headers headers = exchange.getResponseHeaders(); + for (Map.Entry header : response.getHeaders().entrySet()) { + headers.set(header.getKey(), header.getKey()); + } + } + + private void sendCompressed() throws IOException { + exchange.getResponseHeaders().set("Content-Encoding", "gzip"); + beginSend(); + try (OutputStream out = new GZIPOutputStream(exchange.getResponseBody())) { + send(out); + } + } + + private void beginSend() throws IOException { + exchange.sendResponseHeaders(response.getCode(), 0); + } + + private void sendRawBytes() throws IOException { + beginSend(); + try (OutputStream out = exchange.getResponseBody()) { + send(out); + } + } + + private void send(OutputStream out) throws IOException { + try ( + ByteArrayInputStream bis = new ByteArrayInputStream(response.getBytes()) + ) { + byte[] buffer = new byte[2048]; + int count; + while ((count = bis.read(buffer)) != -1) { + out.write(buffer, 0, count); + } + } + } +} \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/CompositePageResolver.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/CompositePageResolver.java index 0da42e5fd..10bd1c3c3 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/CompositePageResolver.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/CompositePageResolver.java @@ -16,7 +16,7 @@ */ package com.djrapitops.plan.delivery.webserver.pages; -import com.djrapitops.plan.delivery.webserver.Request; +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.response.ResponseFactory; @@ -58,7 +58,7 @@ public abstract class CompositePageResolver implements PageResolver { public void registerPage(String targetPage, PageResolver resolver, int requiredPerm) { pages.put(targetPage, new PageResolver() { @Override - public Response_old resolve(Request request, RequestTarget target) throws WebException { + public Response_old resolve(RequestInternal request, RequestTarget target) throws WebException { return resolver.resolve(request, target); } @@ -73,7 +73,7 @@ public abstract class CompositePageResolver implements PageResolver { public void registerPage(String targetPage, Response_old response, int requiredPerm) { pages.put(targetPage, new PageResolver() { @Override - public Response_old resolve(Request request, RequestTarget target) { + public Response_old resolve(RequestInternal request, RequestTarget target) { return response; } @@ -86,7 +86,7 @@ public abstract class CompositePageResolver implements PageResolver { @Override @Deprecated - public Response_old resolve(Request request, RequestTarget target) throws WebException { + public Response_old resolve(RequestInternal request, RequestTarget target) throws WebException { PageResolver pageResolver = getPageResolver(target); return pageResolver != null ? pageResolver.resolve(request, target) diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/DebugPageResolver.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/DebugPageResolver.java index 88055e31a..c7060cba8 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/DebugPageResolver.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/DebugPageResolver.java @@ -16,7 +16,9 @@ */ package com.djrapitops.plan.delivery.webserver.pages; -import com.djrapitops.plan.delivery.web.resolver.*; +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.webserver.response.ResponseFactory; import javax.inject.Inject; @@ -39,12 +41,12 @@ public class DebugPageResolver implements Resolver { } @Override - public boolean canAccess(WebUser permissions, URIPath target, URIQuery query) { - return permissions.hasPermission("page.debug"); + public boolean canAccess(Request request) { + return request.getUser().map(user -> user.hasPermission("page.debug")).orElse(false); } @Override - public Optional resolve(URIPath target, URIQuery query) { + public Optional resolve(Request request) { return Optional.of(responseFactory.debugPageResponse()); } } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/PageResolver.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/PageResolver.java index d5a38d13f..6b4ffe0fb 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/PageResolver.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/PageResolver.java @@ -16,7 +16,7 @@ */ package com.djrapitops.plan.delivery.webserver.pages; -import com.djrapitops.plan.delivery.webserver.Request; +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.response.Response_old; @@ -39,7 +39,7 @@ public interface PageResolver { * @param target Rest of the target coordinates after this page has been solved. * @return Appropriate response. */ - Response_old resolve(Request request, RequestTarget target) throws WebException; + Response_old resolve(RequestInternal request, RequestTarget target) throws WebException; default boolean isAuthorized(Authentication auth, RequestTarget target) throws WebUserAuthException { return true; diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/PlayerPageResolver.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/PlayerPageResolver.java index bf31b05c2..169585e77 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/PlayerPageResolver.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/PlayerPageResolver.java @@ -16,7 +16,11 @@ */ package com.djrapitops.plan.delivery.webserver.pages; -import com.djrapitops.plan.delivery.web.resolver.*; +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.URIPath; +import com.djrapitops.plan.delivery.web.resolver.request.WebUser; import com.djrapitops.plan.delivery.webserver.response.ResponseFactory; import com.djrapitops.plan.identification.UUIDUtility; @@ -46,21 +50,24 @@ public class PlayerPageResolver implements Resolver { } @Override - public boolean canAccess(WebUser user, URIPath target, URIQuery query) { - boolean isOwnPage = target.getPart(1).map(user.getName()::equalsIgnoreCase).orElse(true); + public boolean canAccess(Request request) { + URIPath path = request.getPath(); + WebUser user = request.getUser().orElse(new WebUser("")); + boolean isOwnPage = path.getPart(1).map(user.getName()::equalsIgnoreCase).orElse(true); return user.hasPermission("page.player.other") || (user.hasPermission("page.player.self") && isOwnPage); } @Override - public Optional resolve(URIPath target, URIQuery query) { - Optional part = target.getPart(1); + public Optional resolve(Request request) { + URIPath path = request.getPath(); + Optional part = path.getPart(1); if (!part.isPresent()) return Optional.empty(); String playerName = part.get(); UUID playerUUID = uuidUtility.getUUIDOf(playerName); if (playerUUID == null) return Optional.of(responseFactory.uuidNotFound404()); - boolean raw = target.getPart(2).map("raw"::equalsIgnoreCase).orElse(false); + boolean raw = path.getPart(2).map("raw"::equalsIgnoreCase).orElse(false); return Optional.of( raw ? responseFactory.rawPlayerPageResponse(playerUUID) : responseFactory.playerPageResponse(playerUUID) diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/PlayersPageResolver.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/PlayersPageResolver.java index 31350e2a7..337d71f3b 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/PlayersPageResolver.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/PlayersPageResolver.java @@ -16,7 +16,9 @@ */ package com.djrapitops.plan.delivery.webserver.pages; -import com.djrapitops.plan.delivery.web.resolver.*; +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.webserver.response.ResponseFactory; import javax.inject.Inject; @@ -41,12 +43,12 @@ public class PlayersPageResolver implements Resolver { } @Override - public boolean canAccess(WebUser permissions, URIPath target, URIQuery query) { - return permissions.hasPermission("page.players"); + public boolean canAccess(Request request) { + return request.getUser().map(user -> user.hasPermission("page.players")).orElse(false); } @Override - public Optional resolve(URIPath target, URIQuery query) { + public Optional resolve(Request request) { return Optional.of(responseFactory.playersPageResponse()); } } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/RootPageResolver.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/RootPageResolver.java index 73e24817d..71c5ea45d 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/RootPageResolver.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/RootPageResolver.java @@ -18,7 +18,10 @@ package com.djrapitops.plan.delivery.webserver.pages; import com.djrapitops.plan.delivery.domain.WebUser_old; import com.djrapitops.plan.delivery.rendering.html.Html; -import com.djrapitops.plan.delivery.webserver.Request; +import com.djrapitops.plan.delivery.web.resolver.NoAuthResolver; +import com.djrapitops.plan.delivery.web.resolver.Response; +import com.djrapitops.plan.delivery.web.resolver.request.Request; +import com.djrapitops.plan.delivery.webserver.RequestInternal; import com.djrapitops.plan.delivery.webserver.RequestTarget; import com.djrapitops.plan.delivery.webserver.WebServer; import com.djrapitops.plan.delivery.webserver.auth.Authentication; @@ -35,7 +38,7 @@ import java.util.Optional; * * @author Rsl1122 */ -public class RootPageResolver implements PageResolver { +public class RootPageResolver implements PageResolver, NoAuthResolver { private final ResponseFactory responseFactory; private final WebServer webServer; @@ -48,7 +51,12 @@ public class RootPageResolver implements PageResolver { } @Override - public Response_old resolve(Request request, RequestTarget target) throws WebException { + public Optional resolve(Request request) { + return Optional.empty(); + } + + @Override + public Response_old resolve(RequestInternal request, RequestTarget target) throws WebException { Server server = serverInfo.getServer(); if (!webServer.isAuthRequired()) { return responseFactory.redirectResponse_old(server.isProxy() ? "network" : "server/" + Html.encodeToURL(server.getIdentifiableName())); diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/ServerPageResolver.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/ServerPageResolver.java index 6efeaf4fc..fb60e70e4 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/ServerPageResolver.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/ServerPageResolver.java @@ -17,7 +17,11 @@ package com.djrapitops.plan.delivery.webserver.pages; import com.djrapitops.plan.delivery.rendering.html.Html; -import com.djrapitops.plan.delivery.web.resolver.*; +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.URIPath; +import com.djrapitops.plan.delivery.web.resolver.request.WebUser; import com.djrapitops.plan.delivery.webserver.WebServer; import com.djrapitops.plan.delivery.webserver.response.ResponseFactory; import com.djrapitops.plan.identification.Server; @@ -58,16 +62,17 @@ public class ServerPageResolver implements Resolver { } @Override - public boolean canAccess(WebUser permissions, URIPath target, URIQuery query) { - String firstPart = target.getPart(0).orElse(""); + public boolean canAccess(Request request) { + String firstPart = request.getPath().getPart(0).orElse(""); + WebUser permissions = request.getUser().orElse(new WebUser("")); boolean forServerPage = firstPart.equalsIgnoreCase("server") && permissions.hasPermission("page.server"); boolean forNetworkPage = firstPart.equalsIgnoreCase("network") && permissions.hasPermission("page.network"); return forServerPage || forNetworkPage; } @Override - public Optional resolve(URIPath target, URIQuery query) { - return getServerUUID(target) + public Optional resolve(Request request) { + return getServerUUID(request.getPath()) .map(this::getServerPage) .orElseGet(this::redirectToCurrentServer); } 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 9892ddec3..8e7c3d779 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,7 +17,7 @@ package com.djrapitops.plan.delivery.webserver.pages.json; import com.djrapitops.plan.delivery.rendering.json.graphs.GraphJSONCreator; -import com.djrapitops.plan.delivery.webserver.Request; +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.cache.DataID; @@ -56,7 +56,7 @@ public class GraphsJSONResolver implements PageResolver { } @Override - public Response_old resolve(Request request, RequestTarget target) throws WebException { + public Response_old resolve(RequestInternal request, RequestTarget target) throws WebException { String type = target.getParameter("type") .orElseThrow(() -> new BadRequestException("'type' parameter was not defined.")); diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/NetworkTabJSONResolver.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/pages/json/NetworkTabJSONResolver.java index 34604c801..b44ff8e3f 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,7 +17,7 @@ package com.djrapitops.plan.delivery.webserver.pages.json; import com.djrapitops.plan.delivery.rendering.json.network.NetworkTabJSONCreator; -import com.djrapitops.plan.delivery.webserver.Request; +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.cache.DataID; @@ -45,7 +45,7 @@ public class NetworkTabJSONResolver implements PageResolver { } @Override - public Response_old resolve(Request request, RequestTarget target) { + public Response_old resolve(RequestInternal request, RequestTarget target) { return JSONCache.getOrCache(dataID, () -> new JSONResponse(jsonCreator.get())); } 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 eb9248ea4..30f6b40e5 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 @@ -18,7 +18,7 @@ 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.Request; +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; @@ -45,7 +45,7 @@ public class PlayerJSONResolver implements PageResolver { } @Override - public Response_old resolve(Request request, RequestTarget target) throws WebException { + public Response_old resolve(RequestInternal request, RequestTarget target) throws WebException { UUID playerUUID = identifiers.getPlayerUUID(target); // Can throw BadRequestException return new JSONResponse(jsonCreator.createJSONAsMap(playerUUID)); } 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 77fb3e7e0..63ea87303 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,7 +17,7 @@ package com.djrapitops.plan.delivery.webserver.pages.json; import com.djrapitops.plan.delivery.rendering.json.JSONFactory; -import com.djrapitops.plan.delivery.webserver.Request; +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.cache.DataID; @@ -55,7 +55,7 @@ public class PlayerKillsJSONResolver implements PageResolver { } @Override - public Response_old resolve(Request request, RequestTarget target) throws WebException { + 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))) 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 7142b0762..969ce833e 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,7 +17,7 @@ package com.djrapitops.plan.delivery.webserver.pages.json; import com.djrapitops.plan.delivery.rendering.json.JSONFactory; -import com.djrapitops.plan.delivery.webserver.Request; +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.cache.DataID; @@ -54,7 +54,7 @@ public class PlayersTableJSONResolver implements PageResolver { } @Override - public Response_old resolve(Request request, RequestTarget target) throws WebException { + 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))); 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 7c8fd7ee0..05debe8ac 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,7 +17,7 @@ package com.djrapitops.plan.delivery.webserver.pages.json; import com.djrapitops.plan.delivery.rendering.json.ServerTabJSONCreator; -import com.djrapitops.plan.delivery.webserver.Request; +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.cache.DataID; @@ -54,7 +54,7 @@ public class ServerTabJSONResolver implements PageResolver { } @Override - public Response_old resolve(Request request, RequestTarget target) throws WebException { + 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))); } 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 12cf02753..0bbe0e8ba 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,7 +17,7 @@ package com.djrapitops.plan.delivery.webserver.pages.json; import com.djrapitops.plan.delivery.rendering.json.JSONFactory; -import com.djrapitops.plan.delivery.webserver.Request; +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.cache.DataID; @@ -55,7 +55,7 @@ public class SessionsJSONResolver implements PageResolver { } @Override - public Response_old resolve(Request request, RequestTarget target) throws WebException { + 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, () -> diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/response/ResponseFactory.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/response/ResponseFactory.java index 6a83c969f..735442319 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/response/ResponseFactory.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/response/ResponseFactory.java @@ -254,7 +254,6 @@ public class ResponseFactory { } public Response notFound404(String message) { - try { return Response.builder() .setMimeType(MimeType.HTML) @@ -281,6 +280,23 @@ public class ResponseFactory { + "If you believe this is an error contact staff to change your access level."); } + public Response forbidden403() { + return forbidden403("Your user is not authorized to view this page.
" + + "If you believe this is an error contact staff to change your access level."); + } + + public Response forbidden403(String message) { + try { + return Response.builder() + .setMimeType(MimeType.HTML) + .setContent(pageFactory.errorPage("403 Forbidden", message).toHtml()) + .setStatus(403) + .build(); + } catch (IOException e) { + return forInternalError(e, "Failed to generate 403 page"); + } + } + @Deprecated public ErrorResponse forbidden403_old(String message) { try {