mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2025-01-15 12:41:36 +01:00
Implementation to WebServer for the new InfoRequest system
This commit is contained in:
parent
a6b5b25d9a
commit
60257a3004
@ -1,7 +1,7 @@
|
||||
package com.djrapitops.plan.system.database.databases.operation;
|
||||
|
||||
import com.djrapitops.plan.api.exceptions.database.DBException;
|
||||
import com.djrapitops.plan.system.PlanSystem;
|
||||
import com.djrapitops.plan.system.info.server.ServerInfo;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@ -14,6 +14,8 @@ public interface CheckOperations {
|
||||
boolean doesWebUserExists(String username) throws DBException;
|
||||
|
||||
default boolean isPlayerRegisteredOnThisServer(UUID player) throws DBException {
|
||||
return isPlayerRegistered(player, PlanSystem.getInstance().getServerInfo().getUuid());
|
||||
return isPlayerRegistered(player, ServerInfo.getServerUUID());
|
||||
}
|
||||
|
||||
boolean isServerInDatabase(UUID serverUUID) throws DBException;
|
||||
}
|
||||
|
@ -1,22 +1,84 @@
|
||||
package com.djrapitops.plan.system.info.connection;
|
||||
|
||||
import com.djrapitops.plan.api.exceptions.connection.WebException;
|
||||
import com.djrapitops.plan.api.exceptions.connection.*;
|
||||
import com.djrapitops.plan.api.exceptions.database.DBException;
|
||||
import com.djrapitops.plan.system.database.databases.Database;
|
||||
import com.djrapitops.plan.system.info.request.InfoRequest;
|
||||
import com.djrapitops.plan.system.info.request.RequestSetupRequest;
|
||||
import com.djrapitops.plan.system.webserver.Request;
|
||||
import com.djrapitops.plan.system.webserver.response.Response;
|
||||
import com.djrapitops.plan.utilities.NullCheck;
|
||||
import com.djrapitops.plugin.utilities.Verify;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class ConnectionIn {
|
||||
|
||||
|
||||
private final Map<String, String> variables;
|
||||
private final InfoRequest infoRequest;
|
||||
|
||||
public ConnectionIn(Map<String, String> variables, InfoRequest infoRequest) {
|
||||
public ConnectionIn(Request httpRequest, InfoRequest infoRequest) throws WebException {
|
||||
Verify.nullCheck(httpRequest, infoRequest);
|
||||
|
||||
Map<String, String> variables = readVariables(httpRequest);
|
||||
checkAuthentication(variables);
|
||||
|
||||
this.variables = variables;
|
||||
this.infoRequest = infoRequest;
|
||||
}
|
||||
|
||||
private void checkAuthentication(Map<String, String> variables) throws WebException {
|
||||
String sender = variables.get("sender");
|
||||
NullCheck.check(sender, new BadRequestException("Sender ('sender') was not included."));
|
||||
UUID serverUUID = UUID.fromString(sender);
|
||||
|
||||
try {
|
||||
if (!Database.getActive().check().isServerInDatabase(serverUUID)) {
|
||||
if (infoRequest instanceof RequestSetupRequest) {
|
||||
return;
|
||||
}
|
||||
throw new UnauthorizedServerException(sender + "Sender was not found from database");
|
||||
}
|
||||
} catch (DBException e) {
|
||||
throw new TransferDatabaseException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, String> readVariables(Request request) throws WebException {
|
||||
String requestBody = readRequestBody(request.getRequestBody());
|
||||
String[] variables = requestBody.split(";&variable;");
|
||||
|
||||
return Arrays.stream(variables)
|
||||
.map(variable -> variable.split("=", 2))
|
||||
.filter(splitVariables -> splitVariables.length == 2)
|
||||
.collect(Collectors.toMap(splitVariables -> splitVariables[0], splitVariables -> splitVariables[1], (a, b) -> b));
|
||||
}
|
||||
|
||||
public Response handleRequest() throws WebException {
|
||||
return infoRequest.handleRequest(variables);
|
||||
}
|
||||
|
||||
private String readRequestBody(InputStream in) throws WebFailException {
|
||||
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
|
||||
byte[] bytes;
|
||||
|
||||
byte[] buf = new byte[4096];
|
||||
for (int n = in.read(buf); n > 0; n = in.read(buf)) {
|
||||
out.write(buf, 0, n);
|
||||
}
|
||||
|
||||
bytes = out.toByteArray();
|
||||
|
||||
return new String(bytes, StandardCharsets.UTF_8);
|
||||
} catch (IOException e) {
|
||||
throw new WebFailException("Exception while reading Request.", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -8,8 +8,7 @@ import com.djrapitops.plan.api.exceptions.connection.NoServersException;
|
||||
import com.djrapitops.plan.api.exceptions.connection.WebException;
|
||||
import com.djrapitops.plan.system.SubSystem;
|
||||
import com.djrapitops.plan.system.info.InfoSystem;
|
||||
import com.djrapitops.plan.system.info.request.CacheInspectPageRequest;
|
||||
import com.djrapitops.plan.system.info.request.InfoRequest;
|
||||
import com.djrapitops.plan.system.info.request.*;
|
||||
import com.djrapitops.plan.system.info.server.Server;
|
||||
import com.djrapitops.plan.utilities.NullCheck;
|
||||
|
||||
@ -44,14 +43,23 @@ public abstract class ConnectionSystem implements SubSystem {
|
||||
return connectionSystem;
|
||||
}
|
||||
|
||||
public InfoRequest getInfoRequest(String name) {
|
||||
return dataRequests.get(name.toLowerCase());
|
||||
}
|
||||
|
||||
private Map<String, InfoRequest> loadDataRequests() {
|
||||
Map<String, InfoRequest> requests = new HashMap<>();
|
||||
putRequest(requests, CacheInspectPageRequest.createHandler());
|
||||
putRequest(requests, CacheAnalysisPageRequest.createHandler());
|
||||
putRequest(requests, CacheNetworkPageContentRequest.createHandler());
|
||||
|
||||
putRequest(requests, GenerateAnalysisPageRequest.createHandler());
|
||||
putRequest(requests, GenerateInspectPageRequest.createHandler());
|
||||
return requests;
|
||||
}
|
||||
|
||||
private void putRequest(Map<String, InfoRequest> requests, InfoRequest request) {
|
||||
requests.put(request.getClass().getSimpleName(), request);
|
||||
requests.put(request.getClass().getSimpleName().toLowerCase(), request);
|
||||
}
|
||||
|
||||
protected abstract Server selectServerForRequest(InfoRequest infoRequest) throws NoServersException;
|
||||
|
@ -35,6 +35,7 @@ public class CacheAnalysisPageRequest implements InfoRequest {
|
||||
}
|
||||
|
||||
public CacheAnalysisPageRequest(UUID serverUUID, String html) {
|
||||
Verify.nullCheck(serverUUID, html);
|
||||
this.serverUUID = serverUUID;
|
||||
this.html = html;
|
||||
}
|
||||
|
@ -35,6 +35,7 @@ public class CacheInspectPageRequest implements InfoRequest {
|
||||
}
|
||||
|
||||
public CacheInspectPageRequest(UUID player, String html) {
|
||||
Verify.nullCheck(player, html);
|
||||
this.player = player;
|
||||
this.html = html;
|
||||
}
|
||||
|
@ -14,6 +14,7 @@ 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.NetworkPageContent;
|
||||
import com.djrapitops.plugin.utilities.Verify;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
@ -29,10 +30,16 @@ public class CacheNetworkPageContentRequest implements InfoRequest {
|
||||
private final String html;
|
||||
|
||||
public CacheNetworkPageContentRequest(UUID serverUUID, String html) {
|
||||
Verify.nullCheck(serverUUID, html);
|
||||
this.serverUUID = serverUUID;
|
||||
this.html = html;
|
||||
}
|
||||
|
||||
private CacheNetworkPageContentRequest() {
|
||||
serverUUID = null;
|
||||
html = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void placeDataToDatabase() throws WebException {
|
||||
try {
|
||||
@ -70,4 +77,8 @@ public class CacheNetworkPageContentRequest implements InfoRequest {
|
||||
|
||||
return DefaultResponses.SUCCESS.get();
|
||||
}
|
||||
|
||||
public static CacheNetworkPageContentRequest createHandler() {
|
||||
return new CacheNetworkPageContentRequest();
|
||||
}
|
||||
}
|
@ -11,6 +11,7 @@ import com.djrapitops.plan.system.info.server.ServerInfo;
|
||||
import com.djrapitops.plan.system.webserver.pages.DefaultResponses;
|
||||
import com.djrapitops.plan.system.webserver.response.Response;
|
||||
import com.djrapitops.plan.utilities.NullCheck;
|
||||
import com.djrapitops.plugin.utilities.Verify;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
@ -23,9 +24,13 @@ import java.util.UUID;
|
||||
public class GenerateAnalysisPageRequest extends InfoRequestWithVariables {
|
||||
|
||||
public GenerateAnalysisPageRequest(UUID serverUUID) {
|
||||
Verify.nullCheck(serverUUID);
|
||||
variables.put("server", serverUUID.toString());
|
||||
}
|
||||
|
||||
private GenerateAnalysisPageRequest() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void placeDataToDatabase() {
|
||||
// No data required in a Generate request
|
||||
@ -53,4 +58,8 @@ public class GenerateAnalysisPageRequest extends InfoRequestWithVariables {
|
||||
// TODO Perform Analysis & get HTML
|
||||
return null;
|
||||
}
|
||||
|
||||
public static GenerateAnalysisPageRequest createHandler() {
|
||||
return new GenerateAnalysisPageRequest();
|
||||
}
|
||||
}
|
@ -12,9 +12,10 @@ 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.pages.DefaultResponses;
|
||||
import com.djrapitops.plan.system.webserver.pages.parsing.InspectPage;
|
||||
import com.djrapitops.plan.system.webserver.response.Response;
|
||||
import com.djrapitops.plan.systems.info.parsing.InspectPage;
|
||||
import com.djrapitops.plan.utilities.NullCheck;
|
||||
import com.djrapitops.plugin.utilities.Verify;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
@ -26,10 +27,18 @@ import java.util.UUID;
|
||||
*/
|
||||
public class GenerateInspectPageRequest extends InfoRequestWithVariables {
|
||||
|
||||
private GenerateInspectPageRequest() {
|
||||
}
|
||||
|
||||
public GenerateInspectPageRequest(UUID uuid) {
|
||||
Verify.nullCheck(uuid);
|
||||
variables.put("player", uuid.toString());
|
||||
}
|
||||
|
||||
public static GenerateInspectPageRequest createHandler() {
|
||||
return new GenerateInspectPageRequest();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void placeDataToDatabase() {
|
||||
// No data required in a Generate request
|
||||
@ -52,7 +61,9 @@ public class GenerateInspectPageRequest extends InfoRequestWithVariables {
|
||||
|
||||
private String getHtml(UUID uuid) throws WebException {
|
||||
try {
|
||||
|
||||
return new InspectPage(uuid).toHtml();
|
||||
|
||||
} catch (ParseException e) {
|
||||
Throwable cause = e.getCause();
|
||||
if (cause instanceof DBException) {
|
||||
|
@ -0,0 +1,28 @@
|
||||
/*
|
||||
* 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.info.request;
|
||||
|
||||
import com.djrapitops.plan.system.webserver.response.Response;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* InfoRequest for /plan m setup command.
|
||||
*
|
||||
* @author Rsl1122
|
||||
*/
|
||||
// TODO
|
||||
public class RequestSetupRequest extends InfoRequestWithVariables {
|
||||
|
||||
@Override
|
||||
public void placeDataToDatabase() {
|
||||
// Not Required with setup request.
|
||||
}
|
||||
|
||||
@Override
|
||||
public Response handleRequest(Map<String, String> variables) {
|
||||
return null;
|
||||
}
|
||||
}
|
@ -48,7 +48,9 @@ public class ResponseHandler extends TreePageHandler {
|
||||
}
|
||||
|
||||
public void registerWebAPIPages() {
|
||||
// TODO WebAPIPageHandler
|
||||
registerPage("api", new InfoRequestPageHandler());
|
||||
|
||||
// TODO Remove redundant comment after implementing replacements
|
||||
// private void registerWebAPIs() {
|
||||
// webAPI.registerNewAPI(
|
||||
// new AnalysisReadyWebAPI(),
|
||||
|
@ -0,0 +1,40 @@
|
||||
/*
|
||||
* 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.webserver.pages;
|
||||
|
||||
import com.djrapitops.plan.api.exceptions.connection.NotFoundException;
|
||||
import com.djrapitops.plan.api.exceptions.connection.WebException;
|
||||
import com.djrapitops.plan.system.info.connection.ConnectionIn;
|
||||
import com.djrapitops.plan.system.info.connection.ConnectionSystem;
|
||||
import com.djrapitops.plan.system.info.request.InfoRequest;
|
||||
import com.djrapitops.plan.system.webserver.Request;
|
||||
import com.djrapitops.plan.system.webserver.response.Response;
|
||||
import com.djrapitops.plan.utilities.NullCheck;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* PageHandler for /info/requestname pages.
|
||||
* <p>
|
||||
* Used for answering info requests by other servers.
|
||||
*
|
||||
* @author Rsl1122
|
||||
*/
|
||||
public class InfoRequestPageHandler extends PageHandler {
|
||||
|
||||
@Override
|
||||
public Response getResponse(Request request, List<String> target) throws WebException {
|
||||
if (target.isEmpty()) {
|
||||
return DefaultResponses.NOT_FOUND.get();
|
||||
}
|
||||
|
||||
String requestName = target.get(0);
|
||||
InfoRequest infoRequest = ConnectionSystem.getInstance().getInfoRequest(requestName);
|
||||
|
||||
NullCheck.check(infoRequest, new NotFoundException("Info Request has not been registered."));
|
||||
|
||||
return new ConnectionIn(request, infoRequest).handleRequest();
|
||||
}
|
||||
}
|
@ -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.systems.info.parsing;
|
||||
package com.djrapitops.plan.system.webserver.pages.parsing;
|
||||
|
||||
import com.djrapitops.plan.Plan;
|
||||
import com.djrapitops.plan.PlanBungee;
|
@ -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.systems.info.parsing;
|
||||
package com.djrapitops.plan.system.webserver.pages.parsing;
|
||||
|
||||
import com.djrapitops.plan.PlanPlugin;
|
||||
import com.djrapitops.plan.api.exceptions.ParseException;
|
@ -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.systems.info.parsing;
|
||||
package com.djrapitops.plan.system.webserver.pages.parsing;
|
||||
|
||||
import com.djrapitops.plan.PlanBungee;
|
||||
import com.djrapitops.plan.api.exceptions.ParseException;
|
@ -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.systems.info.parsing;
|
||||
package com.djrapitops.plan.system.webserver.pages.parsing;
|
||||
|
||||
import com.djrapitops.plan.api.exceptions.ParseException;
|
||||
|
@ -144,7 +144,7 @@ public abstract class WebAPI {
|
||||
case 403:
|
||||
throw new ForbiddenException(url.toString());
|
||||
case 404:
|
||||
throw new NotFoundException();
|
||||
throw new NotFoundException("");
|
||||
case 500:
|
||||
throw new InternalErrorException();
|
||||
default:
|
||||
|
@ -21,6 +21,8 @@ import com.djrapitops.plan.system.processing.processors.Processor;
|
||||
import com.djrapitops.plan.system.settings.Settings;
|
||||
import com.djrapitops.plan.system.webserver.WebServer;
|
||||
import com.djrapitops.plan.system.webserver.WebServerSystem;
|
||||
import com.djrapitops.plan.system.webserver.pages.parsing.AnalysisPage;
|
||||
import com.djrapitops.plan.system.webserver.pages.parsing.InspectPage;
|
||||
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;
|
||||
@ -34,8 +36,6 @@ import com.djrapitops.plan.system.webserver.webapi.bukkit.AnalysisReadyWebAPI;
|
||||
import com.djrapitops.plan.system.webserver.webapi.bukkit.AnalyzeWebAPI;
|
||||
import com.djrapitops.plan.system.webserver.webapi.bukkit.RequestInspectPluginsTabBukkitWebAPI;
|
||||
import com.djrapitops.plan.system.webserver.webapi.bungee.*;
|
||||
import com.djrapitops.plan.systems.info.parsing.AnalysisPage;
|
||||
import com.djrapitops.plan.systems.info.parsing.InspectPage;
|
||||
import com.djrapitops.plan.utilities.MiscUtils;
|
||||
import com.djrapitops.plan.utilities.analysis.Analysis;
|
||||
import com.djrapitops.plan.utilities.file.export.HtmlExport;
|
||||
|
@ -14,6 +14,7 @@ import com.djrapitops.plan.system.cache.DataCache;
|
||||
import com.djrapitops.plan.system.info.server.BungeeServerInfo;
|
||||
import com.djrapitops.plan.system.info.server.Server;
|
||||
import com.djrapitops.plan.system.settings.Settings;
|
||||
import com.djrapitops.plan.system.webserver.pages.parsing.NetworkPage;
|
||||
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;
|
||||
@ -27,7 +28,6 @@ import com.djrapitops.plan.system.webserver.webapi.bukkit.AnalyzeWebAPI;
|
||||
import com.djrapitops.plan.system.webserver.webapi.bukkit.InspectWebAPI;
|
||||
import com.djrapitops.plan.system.webserver.webapi.bukkit.IsOnlineWebAPI;
|
||||
import com.djrapitops.plan.system.webserver.webapi.bungee.RequestPluginsTabWebAPI;
|
||||
import com.djrapitops.plan.systems.info.parsing.NetworkPage;
|
||||
import com.djrapitops.plan.utilities.file.export.HtmlExport;
|
||||
import com.djrapitops.plan.utilities.html.HtmlStructure;
|
||||
import com.djrapitops.plugin.api.utility.log.ErrorLogger;
|
||||
|
Loading…
Reference in New Issue
Block a user