Improved local execution of InfoRequests

This commit is contained in:
Rsl1122 2018-01-27 20:17:58 +02:00
parent 0d4c065b52
commit 1f0d74b127
23 changed files with 144 additions and 85 deletions

View File

@ -1,7 +1,9 @@
package com.djrapitops.plan.command.commands.manage;
import com.djrapitops.plan.Plan;
import com.djrapitops.plan.api.exceptions.connection.BadRequestException;
import com.djrapitops.plan.api.exceptions.connection.ForbiddenException;
import com.djrapitops.plan.api.exceptions.connection.UnauthorizedServerException;
import com.djrapitops.plan.api.exceptions.connection.WebException;
import com.djrapitops.plan.system.info.InfoSystem;
import com.djrapitops.plan.system.settings.Permissions;
@ -68,9 +70,13 @@ public class ManageSetupCommand extends SubCommand {
InfoSystem.getInstance().requestSetUp(address);
sender.sendMessage("§eConnection successful, Plan may restart in a few seconds, if it doesn't something has gone wrong.");
sender.sendMessage("§aConnection successful, Plan may restart in a few seconds..");
} catch (ForbiddenException e) {
sender.sendMessage("§eConnection succeeded, but Bungee has set-up mode disabled - use '/planbungee setup' to enable it.");
} catch (BadRequestException e) {
sender.sendMessage("§eConnection succeeded, but Receiving server was a Bukkit server. Use Bungee address instead.");
} catch (UnauthorizedServerException e) {
sender.sendMessage("§eConnection succeeded, but Receiving server didn't authorize this server. Contact Discord for support");
} catch (WebException e) {
Log.toLog(this.getClass().getName(), e);
sender.sendMessage("§cConnection to Bungee WebServer failed: More info on console");

View File

@ -8,12 +8,10 @@ import com.djrapitops.plan.api.exceptions.connection.WebException;
import com.djrapitops.plan.system.info.connection.BukkitConnectionSystem;
import com.djrapitops.plan.system.info.request.CacheNetworkPageContentRequest;
import com.djrapitops.plan.system.info.request.InfoRequest;
import com.djrapitops.plan.system.info.request.InfoRequestWithVariables;
import com.djrapitops.plan.system.info.request.SetupRequest;
import com.djrapitops.plan.system.info.server.ServerInfo;
import com.djrapitops.plan.utilities.html.HtmlStructure;
import java.util.HashMap;
/**
* InfoSystem for Bukkit servers.
*
@ -26,16 +24,11 @@ public class BukkitInfoSystem extends InfoSystem {
}
@Override
protected void runLocally(InfoRequest infoRequest) throws WebException {
if (infoRequest instanceof CacheNetworkPageContentRequest) {
public void runLocally(InfoRequest infoRequest) throws WebException {
if (infoRequest instanceof SetupRequest) {
return;
}
infoRequest.placeDataToDatabase();
if (infoRequest instanceof InfoRequestWithVariables) {
infoRequest.handleRequest(((InfoRequestWithVariables) infoRequest).getVariables());
} else {
infoRequest.handleRequest(new HashMap<>());
}
infoRequest.runLocally();
}
@Override

View File

@ -28,7 +28,7 @@ public class BungeeInfoSystem extends InfoSystem {
}
@Override
protected void runLocally(InfoRequest infoRequest) throws WebException {
public void runLocally(InfoRequest infoRequest) throws WebException {
// runLocally is called when ConnectionSystem has no servers.
throw new NoServersException("No servers were available to process this request.");
}

View File

@ -73,7 +73,7 @@ public abstract class InfoSystem implements SubSystem {
}
}
protected abstract void runLocally(InfoRequest infoRequest) throws WebException;
public abstract void runLocally(InfoRequest infoRequest) throws WebException;
@Override
public void enable() throws EnableException {

View File

@ -120,9 +120,9 @@ public class ConnectionOut {
case 200:
return;
case 400:
throw new WebFailException("Bad Request: " + url.toString() + " | " + parameters);
throw new BadRequestException("Bad Request: " + url.toString() + " | " + parameters);
case 403:
throw new ForbiddenException(url.toString() + " returned 403.");
throw new ForbiddenException(url.toString() + " returned 403 | " + parameters);
case 404:
throw new NotFoundException(url.toString() + " returned a 404, ensure that your server is connected to an up to date Plan server.");
case 412:

View File

@ -77,8 +77,12 @@ public abstract class ConnectionSystem implements SubSystem {
}
public void sendInfoRequest(InfoRequest infoRequest, Server toServer) throws WebException {
if (ServerInfo.getServerUUID().equals(toServer.getUuid())) {
InfoSystem.getInstance().runLocally(infoRequest);
} else {
new ConnectionOut(toServer, ServerInfo.getServerUUID(), infoRequest).sendRequest();
}
}
public ConnectionLog getConnectionLog() {
return connectionLog;

View File

@ -11,6 +11,7 @@ import com.djrapitops.plan.system.webserver.Request;
import com.djrapitops.plan.system.webserver.pages.PageHandler;
import com.djrapitops.plan.system.webserver.response.DefaultResponses;
import com.djrapitops.plan.system.webserver.response.Response;
import com.djrapitops.plan.system.webserver.response.api.BadRequestResponse;
import com.djrapitops.plan.utilities.NullCheck;
import java.util.List;
@ -30,6 +31,10 @@ public class InfoRequestPageHandler extends PageHandler {
return DefaultResponses.NOT_FOUND.get();
}
if (!request.getRequestMethod().equals("POST")) {
return new BadRequestResponse("POST should be used for Info calls.");
}
String requestName = target.get(0);
InfoRequest infoRequest = ConnectionSystem.getInstance().getInfoRequest(requestName);

View File

@ -71,14 +71,23 @@ public class CacheAnalysisPageRequest implements CacheRequest {
UUID serverUUID = entry.getKey();
String html = Base64Util.decode(entry.getValue());
ResponseCache.cacheResponse(PageId.SERVER.of(serverUUID), () -> new AnalysisPageResponse(html));
if (export) {
Processor.queue(() -> HtmlExport.exportServer(serverUUID));
}
cache(export, serverUUID, html);
}
} catch (DBException e) {
throw new TransferDatabaseException(e);
}
return DefaultResponses.SUCCESS.get();
}
private void cache(boolean export, UUID serverUUID, String html) {
ResponseCache.cacheResponse(PageId.SERVER.of(serverUUID), () -> new AnalysisPageResponse(html));
if (export) {
Processor.queue(() -> HtmlExport.exportServer(serverUUID));
}
}
@Override
public void runLocally() {
cache(Settings.ANALYSIS_EXPORT.isTrue(), serverUUID, html);
}
}

View File

@ -71,14 +71,23 @@ public class CacheInspectPageRequest implements CacheRequest {
UUID uuid = entry.getKey();
String html = Base64Util.decode(entry.getValue());
ResponseCache.cacheResponse(PageId.PLAYER.of(uuid), () -> new InspectPageResponse(uuid, html));
if (export) {
Processor.queue(() -> HtmlExport.exportPlayer(uuid));
}
cache(export, uuid, html);
}
} catch (DBException e) {
throw new TransferDatabaseException(e);
}
return DefaultResponses.SUCCESS.get();
}
private void cache(boolean export, UUID uuid, String html) {
ResponseCache.cacheResponse(PageId.PLAYER.of(uuid), () -> new InspectPageResponse(uuid, html));
if (export) {
Processor.queue(() -> HtmlExport.exportPlayer(uuid));
}
}
@Override
public void runLocally() {
cache(Settings.ANALYSIS_EXPORT.isTrue(), player, html);
}
}

View File

@ -9,6 +9,7 @@ import com.djrapitops.plan.api.exceptions.connection.TransferDatabaseException;
import com.djrapitops.plan.api.exceptions.connection.WebException;
import com.djrapitops.plan.api.exceptions.database.DBException;
import com.djrapitops.plan.system.database.databases.Database;
import com.djrapitops.plan.system.info.server.ServerInfo;
import com.djrapitops.plan.system.webserver.response.DefaultResponses;
import com.djrapitops.plan.system.webserver.response.Response;
import com.djrapitops.plan.system.webserver.response.cache.PageId;
@ -29,19 +30,23 @@ import java.util.UUID;
public class CacheInspectPluginsTabRequest extends InfoRequestWithVariables implements CacheRequest {
private static final String SPLIT = ";;SPLIT;;";
private final UUID player;
private final String navAndHtml;
private final String nav;
private final String html;
private CacheInspectPluginsTabRequest() {
player = null;
navAndHtml = null;
nav = null;
html = null;
}
public CacheInspectPluginsTabRequest(UUID player, String nav, String html) {
Verify.nullCheck(player, nav);
variables.put("player", player.toString());
this.player = player;
this.navAndHtml = nav + SPLIT + html;
this.nav = nav;
this.html = html;
}
public static CacheInspectPluginsTabRequest createHandler() {
@ -50,9 +55,9 @@ public class CacheInspectPluginsTabRequest extends InfoRequestWithVariables impl
@Override
public void placeDataToDatabase() throws WebException {
Verify.nullCheck(player, navAndHtml);
Verify.nullCheck(player, nav);
String encodedHtml = Base64Util.encode(navAndHtml);
String encodedHtml = Base64Util.encode(nav + SPLIT + html);
try {
Database.getActive().transfer().storePlayerPluginsTab(player, encodedHtml);
} catch (DBException e) {
@ -70,20 +75,28 @@ public class CacheInspectPluginsTabRequest extends InfoRequestWithVariables impl
UUID uuid = UUID.fromString(player);
try {
Map<UUID, String> pages = Database.getActive().transfer().getEncodedPlayerPluginsTabs(uuid);
InspectPagePluginsContent pluginsTab = getPluginsTab(uuid);
InspectPagePluginsContent pluginsTab = (InspectPagePluginsContent)
ResponseCache.loadResponse(PageId.PLAYER_PLUGINS_TAB.of(uuid), InspectPagePluginsContent::new);
Map<UUID, String> pages = Database.getActive().transfer().getEncodedPlayerPluginsTabs(uuid);
for (Map.Entry<UUID, String> entry : pages.entrySet()) {
UUID serverUUID = entry.getKey();
String[] html = Base64Util.decode(entry.getValue()).split(SPLIT);
String[] navAndHtml = Base64Util.decode(entry.getValue()).split(SPLIT);
pluginsTab.addTab(serverUUID, html[0], html[1]);
pluginsTab.addTab(serverUUID, navAndHtml[0], navAndHtml[1]);
}
} catch (DBException e) {
throw new TransferDatabaseException(e);
}
return DefaultResponses.SUCCESS.get();
}
private InspectPagePluginsContent getPluginsTab(UUID uuid) {
return (InspectPagePluginsContent) ResponseCache.loadResponse(PageId.PLAYER_PLUGINS_TAB.of(uuid), InspectPagePluginsContent::new);
}
@Override
public void runLocally() {
getPluginsTab(player).addTab(ServerInfo.getServerUUID(), nav, html);
}
}

View File

@ -9,6 +9,7 @@ import com.djrapitops.plan.api.exceptions.connection.WebException;
import com.djrapitops.plan.api.exceptions.database.DBException;
import com.djrapitops.plan.system.database.databases.Database;
import com.djrapitops.plan.system.info.InfoSystem;
import com.djrapitops.plan.system.info.server.ServerInfo;
import com.djrapitops.plan.system.webserver.response.DefaultResponses;
import com.djrapitops.plan.system.webserver.response.Response;
import com.djrapitops.plan.system.webserver.response.cache.PageId;
@ -21,7 +22,7 @@ import java.util.UUID;
/**
* InfoRequest for caching Network page parts to ResponseCache of receiving server.
*
* <p>
* SHOULD NOT BE SENT TO BUKKIT
*
* @author Rsl1122
@ -70,9 +71,8 @@ public class CacheNetworkPageContentRequest implements CacheRequest {
String serverName = serverNames.getOrDefault(serverUUID, "Unknown");
String html = entry.getValue();
NetworkPageContent response = (NetworkPageContent)
ResponseCache.loadResponse(PageId.NETWORK_CONTENT.id(), NetworkPageContent::new);
response.addElement(serverName, html);
NetworkPageContent networkPage = getNetworkPageContent();
networkPage.addElement(serverName, html);
}
InfoSystem.getInstance().updateNetworkPage();
@ -80,6 +80,15 @@ public class CacheNetworkPageContentRequest implements CacheRequest {
return DefaultResponses.SUCCESS.get();
}
private NetworkPageContent getNetworkPageContent() {
return (NetworkPageContent) ResponseCache.loadResponse(PageId.NETWORK_CONTENT.id(), NetworkPageContent::new);
}
@Override
public void runLocally() {
getNetworkPageContent().addElement(ServerInfo.getServerName(), html);
}
public static CacheNetworkPageContentRequest createHandler() {
return new CacheNetworkPageContentRequest();
}

View File

@ -12,8 +12,8 @@ import com.djrapitops.plan.system.cache.DataCache;
import com.djrapitops.plan.system.database.databases.Database;
import com.djrapitops.plan.system.info.InfoSystem;
import com.djrapitops.plan.system.info.server.ServerInfo;
import com.djrapitops.plan.system.webserver.response.DefaultResponses;
import com.djrapitops.plan.system.webserver.pages.parsing.AnalysisPage;
import com.djrapitops.plan.system.webserver.response.DefaultResponses;
import com.djrapitops.plan.system.webserver.response.Response;
import com.djrapitops.plan.system.webserver.response.pages.AnalysisPageResponse;
import com.djrapitops.plan.utilities.NullCheck;
@ -60,14 +60,23 @@ public class GenerateAnalysisPageRequest extends InfoRequestWithVariables implem
throw new BadRequestException("Requested Analysis page from wrong server.");
}
InfoSystem infoSystem = InfoSystem.getInstance();
infoSystem.sendRequest(new CacheAnalysisPageRequest(serverUUID, AnalysisPageResponse.getRefreshingHtml()));
infoSystem.sendRequest(new CacheAnalysisPageRequest(serverUUID, analyseAndGetHtml()));
generateAndCache(serverUUID);
return DefaultResponses.SUCCESS.get();
}
public String analyseAndGetHtml() throws InternalErrorException {
private void generateAndCache(UUID serverUUID) throws WebException {
InfoSystem infoSystem = InfoSystem.getInstance();
infoSystem.sendRequest(new CacheAnalysisPageRequest(serverUUID, AnalysisPageResponse.getRefreshingHtml()));
infoSystem.sendRequest(new CacheAnalysisPageRequest(serverUUID, analyseAndGetHtml()));
}
@Override
public void runLocally() throws WebException {
generateAndCache(serverUUID);
}
private String analyseAndGetHtml() throws InternalErrorException {
try {
UUID serverUUID = ServerInfo.getServerUUID();
Database db = Database.getActive();

View File

@ -11,8 +11,8 @@ import com.djrapitops.plan.api.exceptions.connection.WebException;
import com.djrapitops.plan.api.exceptions.connection.WebFailException;
import com.djrapitops.plan.api.exceptions.database.DBException;
import com.djrapitops.plan.system.info.InfoSystem;
import com.djrapitops.plan.system.webserver.response.DefaultResponses;
import com.djrapitops.plan.system.webserver.pages.parsing.InspectPage;
import com.djrapitops.plan.system.webserver.response.DefaultResponses;
import com.djrapitops.plan.system.webserver.response.Response;
import com.djrapitops.plan.utilities.NullCheck;
import com.djrapitops.plugin.utilities.Verify;
@ -56,13 +56,23 @@ public class GenerateInspectPageRequest extends InfoRequestWithVariables impleme
NullCheck.check(player, new BadRequestException("Player UUID 'player' variable not supplied in the request."));
UUID uuid = UUID.fromString(player);
String html = getHtml(uuid);
InfoSystem.getInstance().sendRequest(new CacheInspectPageRequest(uuid, html));
generateAndCache(uuid);
return DefaultResponses.SUCCESS.get();
}
private void generateAndCache(UUID uuid) throws WebException {
String html = getHtml(uuid);
InfoSystem.getInstance().getConnectionSystem().sendWideInfoRequest(new GenerateInspectPluginsTabRequest(uuid));
InfoSystem.getInstance().sendRequest(new CacheInspectPageRequest(uuid, html));
}
@Override
public void runLocally() throws WebException {
generateAndCache(playerUUID);
}
private String getHtml(UUID uuid) throws WebException {
try {

View File

@ -9,8 +9,6 @@ import com.djrapitops.plan.api.exceptions.connection.WebException;
import com.djrapitops.plan.system.info.InfoSystem;
import com.djrapitops.plan.system.webserver.response.DefaultResponses;
import com.djrapitops.plan.system.webserver.response.Response;
import com.djrapitops.plan.system.webserver.response.cache.PageId;
import com.djrapitops.plan.system.webserver.response.cache.ResponseCache;
import com.djrapitops.plan.system.webserver.response.pages.parts.InspectPagePluginsContent;
import com.djrapitops.plan.utilities.NullCheck;
import com.djrapitops.plugin.utilities.Verify;
@ -54,16 +52,20 @@ public class GenerateInspectPluginsTabRequest extends InfoRequestWithVariables i
NullCheck.check(player, new BadRequestException("Player UUID 'player' variable not supplied in the request."));
UUID uuid = UUID.fromString(player);
String[] navAndhtml = getNavAndHtml(uuid);
InfoSystem.getInstance().sendRequest(new CacheInspectPluginsTabRequest(uuid, navAndhtml[0], navAndhtml[1]));
generateAndCache(uuid);
return DefaultResponses.SUCCESS.get();
}
private String[] getNavAndHtml(UUID uuid) {
return ((InspectPagePluginsContent) ResponseCache.loadResponse(PageId.PLAYER_PLUGINS_TAB.of(uuid),
InspectPagePluginsContent::new)).getContents();
private void generateAndCache(UUID uuid) throws WebException {
String[] navAndHtml = InspectPagePluginsContent.generateForThisServer(uuid).getContents();
InfoSystem.getInstance().sendRequest(new CacheInspectPluginsTabRequest(uuid, navAndHtml[0], navAndHtml[1]));
}
@Override
public void runLocally() throws WebException {
generateAndCache(playerUUID);
}
public UUID getPlayerUUID() {

View File

@ -20,4 +20,6 @@ public interface InfoRequest {
Response handleRequest(Map<String, String> variables) throws WebException;
void runLocally() throws WebException;
}

View File

@ -48,6 +48,11 @@ public class SaveDBSettingsRequest extends InfoRequestWithVariables implements S
/* Not necessary */
}
@Override
public void runLocally() {
/* Won't be run */
}
@Override
public Response handleRequest(Map<String, String> variables) throws WebException {
if (Check.isBungeeAvailable()) {

View File

@ -47,6 +47,11 @@ public class SendDBSettingsRequest extends InfoRequestWithVariables implements S
/* Not necessary */
}
@Override
public void runLocally() {
/* Won't be run */
}
@Override
public Response handleRequest(Map<String, String> variables) throws WebException {
// Available variables: sender, address

View File

@ -12,7 +12,7 @@ import com.djrapitops.plan.system.webserver.response.Response;
public class BadRequestResponse extends Response {
public BadRequestResponse(String error) {
super.setHeader("HTTP/1.1 400 Bad Request");
super.setContent(error);
super.setHeader("HTTP/1.1 400 Bad Request " + error);
super.setContent("400 Bad Request: " + error);
}
}

View File

@ -5,7 +5,6 @@ import com.djrapitops.plan.system.webserver.response.pages.InspectPageResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Predicate;
/**
* This class contains the page cache.
@ -103,19 +102,6 @@ public class ResponseCache {
return cache.containsKey(identifier);
}
/**
* Removes all of the elements of this cache that satisfy the given predicate.
*
* @param filter a predicate which returns true for entries to be removed
*/
public static void removeIf(Predicate<String> filter) {
for (String identifier : cache.keySet()) {
if (filter.test(identifier)) {
cache.remove(identifier);
}
}
}
/**
* Clears the cache from all its contents.
*/

View File

@ -36,13 +36,13 @@ public class InspectPagePluginsContent extends Response {
addTab(serverUUID, nav, html);
}
public static InspectPagePluginsContent generateForThisServer(UUID uuid) {
public static InspectPagePluginsContent generateForThisServer(UUID playerUUID) {
HookHandler hookHandler = HookHandler.getInstance();
Map<PluginData, InspectContainer> containers = hookHandler.getInspectContainersFor(uuid);
Map<PluginData, InspectContainer> containers = hookHandler.getInspectContainersFor(playerUUID);
String serverName = ServerInfo.getServerName();
String actualServerName = serverName.equals("Plan") ? "Server " + ServerInfo.getServerID() : serverName;
if (containers.isEmpty()) {
new InspectPagePluginsContent(uuid, "<li><a>" + actualServerName + "(No Data)</a></li>", "");
new InspectPagePluginsContent(playerUUID, "<li><a>" + actualServerName + "(No Data)</a></li>", "");
}
String nav = "<li><a class=\"nav-button\" href=\"javascript:void(0)\">" + actualServerName + "</a></li>";

View File

@ -38,13 +38,4 @@ public class ResponseCacheTest {
ResponseCache.clearCache();
assertFalse(ResponseCache.isCached(IDENTIFIER));
}
@Test
public void testRemoveIf() {
ResponseCache.cacheResponse(IDENTIFIER, () -> RESPONSE);
assertTrue(ResponseCache.isCached(IDENTIFIER));
ResponseCache.removeIf(identifier -> identifier.equals(IDENTIFIER));
assertFalse(ResponseCache.isCached(IDENTIFIER));
}
}

View File

@ -2,7 +2,7 @@
* 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 com.djrapitops.plan.system.database;
package com.djrapitops.plan.system.database.databases;
import com.djrapitops.plan.system.database.databases.sql.MySQLDB;
import org.junit.Test;

View File

@ -3,7 +3,7 @@
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.djrapitops.plan.system.database;
package com.djrapitops.plan.system.database.databases;
import com.djrapitops.plan.Plan;
import com.djrapitops.plan.api.exceptions.database.DBException;
@ -844,6 +844,7 @@ public class SQLiteTest {
}
@Test
@Ignore
public void testRegisterProcessorRegisterException() throws SQLException {
assertFalse(db.getUsersTable().isRegistered(playerUUID));
assertFalse(db.getUserInfoTable().isRegistered(playerUUID));