Introduced PageId for preventing PageCache identifier mistakes in the future

This commit is contained in:
Rsl1122 2017-12-14 16:39:07 +02:00
parent 3fb21b1d1e
commit 9bf4a94510
9 changed files with 97 additions and 43 deletions

View File

@ -21,6 +21,7 @@ import main.java.com.djrapitops.plan.systems.info.parsing.InspectPageParser;
import main.java.com.djrapitops.plan.systems.processing.Processor;
import main.java.com.djrapitops.plan.systems.webserver.WebServer;
import main.java.com.djrapitops.plan.systems.webserver.pagecache.PageCache;
import main.java.com.djrapitops.plan.systems.webserver.pagecache.PageId;
import main.java.com.djrapitops.plan.systems.webserver.response.*;
import main.java.com.djrapitops.plan.systems.webserver.webapi.WebAPIManager;
import main.java.com.djrapitops.plan.systems.webserver.webapi.bukkit.AnalysisReadyWebAPI;
@ -114,7 +115,7 @@ public class BukkitInformationManager extends InformationManager {
}
}
} else {
PageCache.cachePage("inspectPage: " + uuid, () -> {
PageCache.cachePage(PageId.PLAYER.of(uuid), () -> {
try {
return new InspectPageResponse(this, uuid);
} catch (ParseException e) {
@ -188,7 +189,7 @@ public class BukkitInformationManager extends InformationManager {
}
} else {
pluginsTabContents.put(uuid, contents);
Response inspectResponse = PageCache.loadPage("inspectPage: " + uuid);
Response inspectResponse = PageCache.loadPage(PageId.PLAYER.of(uuid));
if (inspectResponse != null && inspectResponse instanceof InspectPageResponse) {
((InspectPageResponse) inspectResponse).setInspectPagePluginsTab(contents);
}
@ -231,7 +232,7 @@ public class BukkitInformationManager extends InformationManager {
return isAnalysisCached(serverUUID);
}
}
return PageCache.isCached("analysisPage:" + serverUUID);
return PageCache.isCached(PageId.SERVER.of(serverUUID));
}
private WebAPIManager getWebAPI() {
@ -306,7 +307,7 @@ public class BukkitInformationManager extends InformationManager {
}
} else {
UUID serverUUID = Plan.getServerUUID();
PageCache.cachePage("analysisPage:" + serverUUID, () -> new AnalysisPageResponse(html));
PageCache.cachePage(PageId.SERVER.of(serverUUID), () -> new AnalysisPageResponse(html));
if (Settings.ANALYSIS_EXPORT.isTrue()) {
HtmlExport.exportServer(plugin, serverUUID);
}

View File

@ -17,6 +17,7 @@ import main.java.com.djrapitops.plan.systems.info.parsing.NetworkPageParser;
import main.java.com.djrapitops.plan.systems.info.server.BungeeServerInfoManager;
import main.java.com.djrapitops.plan.systems.info.server.ServerInfo;
import main.java.com.djrapitops.plan.systems.webserver.pagecache.PageCache;
import main.java.com.djrapitops.plan.systems.webserver.pagecache.PageId;
import main.java.com.djrapitops.plan.systems.webserver.response.*;
import main.java.com.djrapitops.plan.systems.webserver.webapi.WebAPIManager;
import main.java.com.djrapitops.plan.systems.webserver.webapi.bukkit.AnalysisReadyWebAPI;
@ -233,11 +234,7 @@ public class BungeeInformationManager extends InformationManager {
*/
@Override
public boolean isAnalysisCached(UUID serverUUID) {
if (PlanBungee.getServerUUID().equals(serverUUID)) {
return PageCache.isCached("networkPage");
} else {
return PageCache.isCached("analysisPage:" + serverUUID);
}
return PageCache.isCached(PageId.SERVER.of(serverUUID));
}
/**
@ -250,7 +247,7 @@ public class BungeeInformationManager extends InformationManager {
*/
@Override
public String getPlayerHtml(UUID uuid) {
Response response = PageCache.copyPage("inspectPage:" + uuid,
Response response = PageCache.copyPage(PageId.PLAYER.of(uuid),
() -> new NotFoundResponse("No Bukkit Servers were online to process this request"));
if (response instanceof InspectPageResponse) {
((InspectPageResponse) response).setInspectPagePluginsTab(getPluginsTabContent(uuid));
@ -314,7 +311,7 @@ public class BungeeInformationManager extends InformationManager {
Map<UUID, String[]> perServerPluginsTab = pluginsTabContent.getOrDefault(uuid, new HashMap<>());
perServerPluginsTab.put(serverUUID, html);
pluginsTabContent.put(uuid, perServerPluginsTab);
Response inspectResponse = PageCache.loadPage("inspectPage: " + uuid);
Response inspectResponse = PageCache.loadPage(PageId.PLAYER.of(uuid));
if (inspectResponse != null && inspectResponse instanceof InspectPageResponse) {
((InspectPageResponse) inspectResponse).setInspectPagePluginsTab(getPluginsTabContent(uuid));
}
@ -372,7 +369,7 @@ public class BungeeInformationManager extends InformationManager {
@Override
public void updateNetworkPageContent() {
UUID serverUUID = MiscUtils.getIPlan().getServerUuid();
PageCache.cachePage("analysisPage:" + serverUUID, () -> new AnalysisPageResponse(this));
PageCache.cachePage(PageId.SERVER.of(serverUUID), () -> new AnalysisPageResponse(this));
if (Settings.ANALYSIS_EXPORT.isTrue()) {
HtmlExport.exportServer(plugin, serverUUID);
}

View File

@ -9,6 +9,7 @@ import main.java.com.djrapitops.plan.api.exceptions.ParseException;
import main.java.com.djrapitops.plan.systems.cache.DataCache;
import main.java.com.djrapitops.plan.systems.cache.SessionCache;
import main.java.com.djrapitops.plan.systems.webserver.pagecache.PageCache;
import main.java.com.djrapitops.plan.systems.webserver.pagecache.PageId;
import java.io.IOException;
import java.util.*;
@ -46,7 +47,7 @@ public abstract class InformationManager {
}
public boolean isCached(UUID uuid) {
return PageCache.isCached("inspectPage: " + uuid);
return PageCache.isCached(PageId.PLAYER.of(uuid));
}
public abstract String getPlayerHtml(UUID uuid) throws ParseException;

View File

@ -6,6 +6,7 @@ package main.java.com.djrapitops.plan.systems.webserver;
import com.djrapitops.plugin.api.utility.log.Log;
import main.java.com.djrapitops.plan.systems.webserver.pagecache.PageCache;
import main.java.com.djrapitops.plan.systems.webserver.pagecache.PageId;
import main.java.com.djrapitops.plan.systems.webserver.response.*;
import main.java.com.djrapitops.plan.systems.webserver.response.api.BadRequestResponse;
import main.java.com.djrapitops.plan.systems.webserver.webapi.WebAPI;
@ -40,29 +41,29 @@ public class APIResponseHandler {
String[] args = target.split("/");
if ("/favicon.ico".equalsIgnoreCase(target)) {
return PageCache.loadPage("Redirect: favicon", () -> new RedirectResponse("https://puu.sh/tK0KL/6aa2ba141b.ico"));
return PageCache.loadPage(PageId.FAVICON_REDIRECT.id(), () -> new RedirectResponse("https://puu.sh/tK0KL/6aa2ba141b.ico"));
}
if ("/debug".equalsIgnoreCase(target)) {
return new DebugPageResponse();
}
if (target.endsWith(".css")) {
return PageCache.loadPage(target + "css", () -> new CSSResponse(target));
return PageCache.loadPage(PageId.CSS.of(target), () -> new CSSResponse(target));
}
if (target.endsWith(".js")) {
return PageCache.loadPage(target + "js", () -> new JavaScriptResponse(target));
return PageCache.loadPage(PageId.JS.of(target), () -> new JavaScriptResponse(target));
}
if (args.length < 2 || !"api".equals(args[1])) {
String address = MiscUtils.getIPlan().getInfoManager().getWebServerAddress() + target;
String link = Html.LINK.parse(address, address);
return PageCache.loadPage("Non-API Request", () -> new NotFoundResponse("WebServer is in WebAPI-only mode, " +
return PageCache.loadPage(PageId.ERROR.of("Non-API Request"), () -> new NotFoundResponse("WebServer is in WebAPI-only mode, " +
"connect to the Bungee server instead: " + link));
}
if (args.length < 3) {
String error = "API Method not specified";
return PageCache.loadPage(error, () -> new BadRequestResponse(error));
return PageCache.loadPage(PageId.ERROR.of(error), () -> new BadRequestResponse(error));
}
String method = args[2];
@ -74,7 +75,7 @@ public class APIResponseHandler {
if (requestBody == null) {
String error = "Error at reading the POST request." +
"Note that the Encoding must be ISO-8859-1.";
return PageCache.loadPage(error, () -> new BadRequestResponse(error));
return PageCache.loadPage(PageId.ERROR.of(error), () -> new BadRequestResponse(error));
}
Map<String, String> variables = WebAPI.readVariables(requestBody);
@ -93,7 +94,7 @@ public class APIResponseHandler {
if (!checkKey(sender)) {
String error = "Server Key not given or invalid";
Log.debug("Request had invalid Server key: " + sender);
return PageCache.loadPage(error, () -> {
return PageCache.loadPage(PageId.ERROR.of(error), () -> {
ForbiddenResponse forbidden = new ForbiddenResponse();
forbidden.setContent(error);
return forbidden;
@ -103,7 +104,7 @@ public class APIResponseHandler {
if (!webAPI.isAuthorized(accessKey)) {
String error = "Access Key invalid";
Log.debug("Request had invalid Access key: " + accessKey);
return PageCache.loadPage(error, () -> {
return PageCache.loadPage(PageId.ERROR.of(error), () -> {
ForbiddenResponse forbidden = new ForbiddenResponse();
forbidden.setContent(error);
return forbidden;
@ -117,7 +118,7 @@ public class APIResponseHandler {
if (api == null) {
String error = "API Method not found";
Log.debug(error);
return PageCache.loadPage(error, () -> new BadRequestResponse(error));
return PageCache.loadPage(PageId.ERROR.of(error), () -> new BadRequestResponse(error));
}
Response response = api.processRequest(MiscUtils.getIPlan(), variables);

View File

@ -11,6 +11,7 @@ import main.java.com.djrapitops.plan.api.exceptions.WebUserAuthException;
import main.java.com.djrapitops.plan.data.WebUser;
import main.java.com.djrapitops.plan.database.tables.SecurityTable;
import main.java.com.djrapitops.plan.systems.webserver.pagecache.PageCache;
import main.java.com.djrapitops.plan.systems.webserver.pagecache.PageId;
import main.java.com.djrapitops.plan.systems.webserver.response.*;
import main.java.com.djrapitops.plan.utilities.MiscUtils;
import main.java.com.djrapitops.plan.utilities.PassEncryptUtil;
@ -43,17 +44,17 @@ public class ResponseHandler extends APIResponseHandler {
String[] args = target.split("/");
try {
if ("/favicon.ico".equals(target)) {
return PageCache.loadPage("Redirect: favicon", () -> new RedirectResponse("https://puu.sh/tK0KL/6aa2ba141b.ico"));
return PageCache.loadPage(PageId.FAVICON_REDIRECT.id(), () -> new RedirectResponse("https://puu.sh/tK0KL/6aa2ba141b.ico"));
}
if (request.isAPIRequest()) {
return getAPIResponse(request);
}
if (target.endsWith(".css")) {
return PageCache.loadPage(target + "css", () -> new CSSResponse(target));
return PageCache.loadPage(PageId.CSS.of(target), () -> new CSSResponse(target));
}
if (target.endsWith(".js")) {
return PageCache.loadPage(target + "js", () -> new JavaScriptResponse(target));
return PageCache.loadPage(PageId.JS.of(target), () -> new JavaScriptResponse(target));
}
UUID serverUUID = MiscUtils.getIPlan().getServerUuid();
@ -82,7 +83,7 @@ public class ResponseHandler extends APIResponseHandler {
case "debug":
return new DebugPageResponse();
case "players":
return PageCache.loadPage("players", PlayersPageResponse::new);
return PageCache.loadPage(PageId.PLAYERS.id(), PlayersPageResponse::new);
case "player":
return playerResponse(args);
case "network":
@ -103,7 +104,7 @@ public class ResponseHandler extends APIResponseHandler {
}
} catch (WebUserAuthException e) {
return PageCache.loadPage("promptAuthorization", PromptAuthorizationResponse::new);
return PageCache.loadPage(PageId.AUTH_PROMPT.id(), PromptAuthorizationResponse::new);
} catch (Exception e) {
Log.toLog(this.getClass().getName(), e);
return new InternalErrorResponse(e, request.getTarget());
@ -111,7 +112,7 @@ public class ResponseHandler extends APIResponseHandler {
}
private Response forbiddenResponse(int required, int permLevel) {
return PageCache.loadPage("forbidden", () ->
return PageCache.loadPage(PageId.FORBIDDEN.id(), () ->
new ForbiddenResponse("Unauthorized User.<br>"
+ "Make sure your user has the correct access level.<br>"
+ "This page requires permission level of " + required + ",<br>"
@ -183,7 +184,7 @@ public class ResponseHandler extends APIResponseHandler {
case 0:
return serverResponse(serverUUID);
case 1:
return PageCache.loadPage("players", PlayersPageResponse::new);
return PageCache.loadPage(PageId.PLAYERS.id(), PlayersPageResponse::new);
case 2:
return playerResponse(new String[]{"", "", user.getName()});
default:
@ -192,12 +193,12 @@ public class ResponseHandler extends APIResponseHandler {
}
private Response serverResponse(UUID serverUUID) {
return PageCache.loadPage("analysisPage:" + serverUUID, () -> new AnalysisPageResponse(plugin.getInfoManager()));
return PageCache.loadPage(PageId.SERVER.of(serverUUID), () -> new AnalysisPageResponse(plugin.getInfoManager()));
}
private Response playerResponse(String[] args) {
if (args.length < 3) {
return PageCache.loadPage("notFound", NotFoundResponse::new);
return PageCache.loadPage(PageId.NOT_FOUND.id(), NotFoundResponse::new);
}
String playerName = args[2].trim();
@ -205,22 +206,22 @@ public class ResponseHandler extends APIResponseHandler {
if (uuid == null) {
String error = "Player has no UUID";
return PageCache.loadPage("notFound: " + error, () -> new NotFoundResponse(error));
return PageCache.loadPage(PageId.NOT_FOUND.of(error), () -> new NotFoundResponse(error));
}
if (plugin.getDB().wasSeenBefore(uuid)) {
plugin.getInfoManager().cachePlayer(uuid);
Response response = PageCache.loadPage("inspectPage: " + uuid);
Response response = PageCache.loadPage(PageId.PLAYER.of(uuid));
// TODO Create a new method that places NotFoundResponse to PageCache instead.
if (response == null || response.getContent().contains("No Bukkit Servers were online to process this request")) {
PageCache.cachePage("inspectPage: " + uuid, () -> {
PageCache.cachePage(PageId.PLAYER.of(uuid), () -> {
try {
return new InspectPageResponse(plugin.getInfoManager(), uuid);
} catch (ParseException e) {
return new InternalErrorResponse(e, this.getClass().getName());
}
});
response = PageCache.loadPage("inspectPage: " + uuid);
response = PageCache.loadPage(PageId.PLAYER.of(uuid));
}
return response;
}
@ -229,7 +230,7 @@ public class ResponseHandler extends APIResponseHandler {
private Response notFoundResponse() {
String error = "404 Not Found";
return PageCache.loadPage("notFound: " + error, () -> {
return PageCache.loadPage(PageId.NOT_FOUND.of("Wrong Page"), () -> {
String url = plugin.getInfoManager().getWebServerAddress();
return new NotFoundResponse("Make sure you're accessing a link given by a command, Examples:</p>"
+ "<p>" + url + "/player/Playername<br>" +

View File

@ -0,0 +1,50 @@
/*
* Licence is provided in the jar as license.yml also here:
* https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml
*/
package main.java.com.djrapitops.plan.systems.webserver.pagecache;
import java.util.UUID;
/**
* Enum class for "magic" PageCache identifier values.
*
* @author Rsl1122
*/
public enum PageId {
SERVER("serverPage:"),
PLAYER("playerPage:"),
PLAYERS("playersPage"),
//
ERROR("error:"),
FORBIDDEN(ERROR.of("Forbidden")),
NOT_FOUND(ERROR.of("Not Found")),
TRUE("true"),
FALSE("false"),
//
JS("js:"),
CSS("css:"),
//
FAVICON_REDIRECT("Redirect:Favicon"),
AUTH_PROMPT("PromptAuth")
;
private final String id;
PageId(String id) {
this.id = id;
}
public String of(String additionalInfo) {
return id + additionalInfo;
}
public String of(UUID uuid) {
return of(uuid.toString());
}
public String id() {
return id;
}
}

View File

@ -10,6 +10,7 @@ import main.java.com.djrapitops.plan.api.IPlan;
import main.java.com.djrapitops.plan.api.exceptions.*;
import main.java.com.djrapitops.plan.settings.Settings;
import main.java.com.djrapitops.plan.systems.webserver.pagecache.PageCache;
import main.java.com.djrapitops.plan.systems.webserver.pagecache.PageId;
import main.java.com.djrapitops.plan.systems.webserver.response.NotFoundResponse;
import main.java.com.djrapitops.plan.systems.webserver.response.Response;
import main.java.com.djrapitops.plan.systems.webserver.response.api.BadRequestResponse;
@ -165,11 +166,11 @@ public abstract class WebAPI {
};
protected Response success() {
return PageCache.loadPage("success", SuccessResponse::new);
return PageCache.loadPage(PageId.TRUE.id(), SuccessResponse::new);
}
protected Response fail(String reason) {
return PageCache.loadPage("fail", () -> {
return PageCache.loadPage(PageId.FALSE.id(), () -> {
NotFoundResponse notFoundResponse = new NotFoundResponse("");
notFoundResponse.setContent(reason);
return notFoundResponse;
@ -177,7 +178,7 @@ public abstract class WebAPI {
}
protected Response badRequest(String error) {
return PageCache.loadPage(error, () -> new BadRequestResponse(error));
return PageCache.loadPage(PageId.ERROR.of(error), () -> new BadRequestResponse(error));
}
private String parseVariables() {

View File

@ -10,6 +10,7 @@ import main.java.com.djrapitops.plan.api.exceptions.WebAPIException;
import main.java.com.djrapitops.plan.settings.Settings;
import main.java.com.djrapitops.plan.systems.info.InformationManager;
import main.java.com.djrapitops.plan.systems.webserver.pagecache.PageCache;
import main.java.com.djrapitops.plan.systems.webserver.pagecache.PageId;
import main.java.com.djrapitops.plan.systems.webserver.response.AnalysisPageResponse;
import main.java.com.djrapitops.plan.systems.webserver.response.InspectPageResponse;
import main.java.com.djrapitops.plan.systems.webserver.response.Response;
@ -54,14 +55,14 @@ public class PostHtmlWebAPI extends WebAPI {
Map<String, String> map = new HashMap<>();
map.put("networkName", Settings.BUNGEE_NETWORK_NAME.toString());
PageCache.cachePage("inspectPage:" + uuid, () -> new InspectPageResponse(infoManager, UUID.fromString(uuid), StrSubstitutor.replace(html, map)));
PageCache.cachePage(PageId.PLAYER.of(uuid), () -> new InspectPageResponse(infoManager, UUID.fromString(uuid), StrSubstitutor.replace(html, map)));
if (Settings.ANALYSIS_EXPORT.isTrue()) {
HtmlExport.exportPlayer(plugin, UUID.fromString(uuid));
}
break;
case "analysisPage":
String sender = variables.get("sender");
PageCache.cachePage("analysisPage:" + sender, () -> new AnalysisPageResponse(html));
PageCache.cachePage(PageId.SERVER.of(sender), () -> new AnalysisPageResponse(html));
if (Settings.ANALYSIS_EXPORT.isTrue()) {
HtmlExport.exportServer(plugin, UUID.fromString(sender));
}

View File

@ -9,6 +9,7 @@ import com.djrapitops.plugin.api.utility.log.Log;
import com.djrapitops.plugin.task.AbsRunnable;
import main.java.com.djrapitops.plan.settings.Settings;
import main.java.com.djrapitops.plan.systems.webserver.pagecache.PageCache;
import main.java.com.djrapitops.plan.systems.webserver.pagecache.PageId;
import main.java.com.djrapitops.plan.systems.webserver.response.Response;
import main.java.com.djrapitops.plan.utilities.MiscUtils;
@ -73,7 +74,7 @@ public abstract class SpecificExport extends AbsRunnable {
}
protected void exportAvailablePlayerPage(UUID uuid, String name) throws IOException {
Response response = PageCache.loadPage("inspectPage: " + uuid);
Response response = PageCache.loadPage(PageId.PLAYER.of(uuid));
if (response == null) {
return;
}
@ -90,7 +91,7 @@ public abstract class SpecificExport extends AbsRunnable {
protected void exportAvailableServerPage(UUID serverUUID, String serverName) throws IOException {
Response response = PageCache.loadPage("analysisPage:" + serverUUID);
Response response = PageCache.loadPage(PageId.SERVER.of(serverUUID));
if (response == null) {
return;
}