Fixed some WebAPI responses leading to infinite loops.

This commit is contained in:
Rsl1122 2017-11-10 10:22:33 +02:00
parent 209208eb51
commit 88d891333c
11 changed files with 102 additions and 43 deletions

View File

@ -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 main.java.com.djrapitops.plan.api.exceptions;
/**
* Group of WebAPIExceptions that can be considered a failed connection state on some occasions.
*
* @author Rsl1122
*/
public class WebAPIFailException extends WebAPIException {
public WebAPIFailException() {
}
public WebAPIFailException(String message) {
super(message);
}
public WebAPIFailException(String message, Throwable cause) {
super(message, cause);
}
public WebAPIFailException(Throwable cause) {
super(cause);
}
}

View File

@ -9,7 +9,7 @@ package main.java.com.djrapitops.plan.api.exceptions;
*
* @author Rsl1122
*/
public class WebAPIForbiddenException extends WebAPIException {
public class WebAPIForbiddenException extends WebAPIFailException {
public WebAPIForbiddenException(String url) {
super("Forbidden: " + url);
}

View File

@ -0,0 +1,16 @@
/*
* 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.api.exceptions;
/**
* Thrown when WebAPI returns 404, usually when response is supposed to be false.
*
* @author Rsl1122
*/
public class WebAPIInternalErrorException extends WebAPIFailException {
public WebAPIInternalErrorException() {
super("Internal Error occurred on receiving server");
}
}

View File

@ -9,7 +9,7 @@ package main.java.com.djrapitops.plan.api.exceptions;
*
* @author Rsl1122
*/
public class WebAPINotFoundException extends WebAPIException {
public class WebAPINotFoundException extends WebAPIFailException {
public WebAPINotFoundException() {
super("Not Found");
}

View File

@ -12,7 +12,6 @@ import main.java.com.djrapitops.plan.api.exceptions.WebAPIException;
import main.java.com.djrapitops.plan.locale.Locale;
import main.java.com.djrapitops.plan.locale.Msg;
import main.java.com.djrapitops.plan.systems.webserver.webapi.bungee.RequestSetupWebAPI;
import main.java.com.djrapitops.plan.systems.webserver.webapi.universal.PingWebAPI;
import main.java.com.djrapitops.plan.utilities.Condition;
/**
@ -68,7 +67,7 @@ public class ManageSetupCommand extends SubCommand {
Config config = plugin.getMainConfig();
config.set(Settings.BUNGEE_OVERRIDE_STANDALONE_MODE.getPath(), false);
config.set(Settings.BUNGEE_COPY_CONFIG.getPath(), true);
plugin.getWebServer().getWebAPI().getAPI(PingWebAPI.class).sendRequest(address);
// plugin.getWebServer().getWebAPI().getAPI(PingWebAPI.class).sendRequest(address);
plugin.getWebServer().getWebAPI().getAPI(RequestSetupWebAPI.class).sendRequest(address);
sender.sendMessage("§eConnection successful, Plan may restart in a few seconds, if it doesn't something has gone wrong.");
} catch (WebAPIException e) {

View File

@ -10,7 +10,7 @@ import main.java.com.djrapitops.plan.Settings;
import main.java.com.djrapitops.plan.api.exceptions.ParseException;
import main.java.com.djrapitops.plan.api.exceptions.WebAPIConnectionFailException;
import main.java.com.djrapitops.plan.api.exceptions.WebAPIException;
import main.java.com.djrapitops.plan.api.exceptions.WebAPIForbiddenException;
import main.java.com.djrapitops.plan.api.exceptions.WebAPIFailException;
import main.java.com.djrapitops.plan.command.commands.AnalyzeCommand;
import main.java.com.djrapitops.plan.data.AnalysisData;
import main.java.com.djrapitops.plan.data.additional.HookHandler;
@ -83,8 +83,8 @@ public class BukkitInformationManager extends InformationManager {
} else if (usingAnotherWebServer) {
try {
getWebAPI().getAPI(AnalyzeWebAPI.class).sendRequest(webServerAddress, serverUUID);
} catch (WebAPIForbiddenException ignored) {
/* Do nothing */
} catch (WebAPIFailException e) {
Log.error("Failed to request Analysis refresh from Bungee.");
} catch (WebAPIException e) {
attemptConnection();
refreshAnalysis(serverUUID);
@ -98,8 +98,8 @@ public class BukkitInformationManager extends InformationManager {
if (usingAnotherWebServer) {
try {
getWebAPI().getAPI(PostHtmlWebAPI.class).sendInspectHtml(webServerAddress, uuid, getPlayerHtml(uuid));
} catch (WebAPIForbiddenException ignored) {
/* Do nothing */
} catch (WebAPIFailException e) {
Log.error("Failed to request Inspect from Bungee.");
} catch (WebAPIException e) {
attemptConnection();
cachePlayer(uuid);
@ -123,8 +123,8 @@ public class BukkitInformationManager extends InformationManager {
if (usingAnotherWebServer && !origin.equals(RequestInspectPluginsTabBukkitWebAPI.class)) {
try {
getWebAPI().getAPI(RequestPluginsTabWebAPI.class).sendRequest(webServerAddress, uuid);
} catch (WebAPIForbiddenException ignored) {
/* Do nothing */
} catch (WebAPIFailException e) {
Log.error("Failed send Player Plugins tab contents to BungeeCord.");
} catch (WebAPIException e) {
attemptConnection();
cacheInspectPluginsTab(uuid, origin);
@ -143,8 +143,8 @@ public class BukkitInformationManager extends InformationManager {
if (usingAnotherWebServer) {
try {
getWebAPI().getAPI(PostInspectPluginsTabWebAPI.class).sendPluginsTab(webServerAddress, uuid, contents);
} catch (WebAPIForbiddenException ignored) {
/* Do nothing */
} catch (WebAPIFailException e) {
Log.error("Failed send Player HTML to BungeeCord.");
} catch (WebAPIException e) {
attemptConnection();
cacheInspectPluginsTab(uuid, contents);
@ -169,8 +169,8 @@ public class BukkitInformationManager extends InformationManager {
if (usingAnotherWebServer) {
try {
return getWebAPI().getAPI(IsCachedWebAPI.class).isInspectCached(webServerAddress, uuid);
} catch (WebAPIForbiddenException ignored) {
/* Do nothing */
} catch (WebAPIFailException e) {
Log.error("Failed check Bungee Player Cache status.");
} catch (WebAPIException e) {
attemptConnection();
return isCached(uuid);
@ -187,8 +187,8 @@ public class BukkitInformationManager extends InformationManager {
if (usingAnotherWebServer) {
try {
return getWebAPI().getAPI(IsCachedWebAPI.class).isAnalysisCached(webServerAddress, serverUUID);
} catch (WebAPIForbiddenException ignored) {
/* Do nothing */
} catch (WebAPIFailException e) {
Log.error("Failed check Bungee Analysis Cache status.");
} catch (WebAPIException e) {
attemptConnection();
return isAnalysisCached(serverUUID);
@ -248,8 +248,8 @@ public class BukkitInformationManager extends InformationManager {
getWebAPI().getAPI(AnalysisReadyWebAPI.class).sendRequest(webServerAddress, serverUUID);
updateNetworkPageContent();
return;
} catch (WebAPIForbiddenException ignored) {
/* Do nothing */
} catch (WebAPIFailException ignored) {
Log.error("Failed to notify Bungee of Analysis Completion.");
} catch (WebAPIException e) {
attemptConnection();
}
@ -265,8 +265,8 @@ public class BukkitInformationManager extends InformationManager {
if (usingAnotherWebServer) {
try {
getWebAPI().getAPI(PostHtmlWebAPI.class).sendAnalysisHtml(webServerAddress, html);
} catch (WebAPIForbiddenException ignored) {
/* Do nothing */
} catch (WebAPIFailException e) {
Log.error("Failed to send Analysis HTML to Bungee Server.");
} catch (WebAPIException e) {
attemptConnection();
cacheAnalysisHtml(html);
@ -337,7 +337,7 @@ public class BukkitInformationManager extends InformationManager {
if (usingAnotherWebServer) {
try {
getWebAPI().getAPI(PostNetworkPageContentWebAPI.class).sendNetworkContent(webServerAddress, HtmlStructure.createServerContainer(plugin));
} catch (WebAPIForbiddenException ignored) {
} catch (WebAPIFailException ignored) {
/* Do nothing */
} catch (WebAPIException ignored) {
attemptConnection();

View File

@ -136,32 +136,34 @@ public class BungeeServerInfoManager {
onlineServers.add(server.getUuid());
}
public void serverConnected(UUID serverUUID) {
public boolean serverConnected(UUID serverUUID) {
if (plugin.getServerUuid().equals(serverUUID)) {
return;
return false;
}
Log.info("Received a connection from a Bukkit server..");
if (onlineServers.contains(serverUUID)) {
sendConfigSettings(serverUUID);
return;
return true;
}
try {
Optional<ServerInfo> serverInfo = db.getServerTable().getServerInfo(serverUUID);
serverInfo.ifPresent(server -> {
Log.info("Server Info found from DB: " + server.getName());
RunnableFactory.createNew("BukkitConnectionTask: " + server.getName(), new AbsRunnable() {
@Override
public void run() {
attemptConnection(server);
sendConfigSettings(serverUUID);
this.cancel();
}
}).runTaskLaterAsynchronously(TimeAmount.SECOND.ticks() * 3L);
if (serverInfo.isPresent()) {
ServerInfo server = serverInfo.get();
Log.info("Server Info found from DB: " + server.getName());
RunnableFactory.createNew("BukkitConnectionTask: " + server.getName(), new AbsRunnable() {
@Override
public void run() {
attemptConnection(server);
sendConfigSettings(serverUUID);
this.cancel();
}
);
}).runTaskLaterAsynchronously(TimeAmount.SECOND.ticks() * 3L);
return true;
}
} catch (SQLException e) {
Log.toLog(this.getClass().getName(), e);
}
return false;
}
public Collection<ServerInfo> getOnlineBukkitServers() {

View File

@ -52,6 +52,8 @@ public class WebServer {
StaticHolder.saveInstance(APIRequestHandler.class, plugin.getClass());
StaticHolder.saveInstance(RequestHandler.class, plugin.getClass());
StaticHolder.saveInstance(ResponseHandler.class, plugin.getClass());
StaticHolder.saveInstance(APIResponseHandler.class, plugin.getClass());
}
private void registerWebAPIs() {

View File

@ -8,10 +8,7 @@ import com.djrapitops.plugin.api.utility.log.Log;
import com.djrapitops.plugin.utilities.Verify;
import main.java.com.djrapitops.plan.Settings;
import main.java.com.djrapitops.plan.api.IPlan;
import main.java.com.djrapitops.plan.api.exceptions.WebAPIConnectionFailException;
import main.java.com.djrapitops.plan.api.exceptions.WebAPIException;
import main.java.com.djrapitops.plan.api.exceptions.WebAPIForbiddenException;
import main.java.com.djrapitops.plan.api.exceptions.WebAPINotFoundException;
import main.java.com.djrapitops.plan.api.exceptions.*;
import main.java.com.djrapitops.plan.systems.webserver.PageCache;
import main.java.com.djrapitops.plan.systems.webserver.response.NotFoundResponse;
import main.java.com.djrapitops.plan.systems.webserver.response.Response;
@ -112,15 +109,18 @@ public abstract class WebAPI {
}
int responseCode = connection.getResponseCode();
Log.debug("Response: " + responseCode);
switch (responseCode) {
case 200:
return;
case 400:
throw new WebAPIException("Bad Request: " + url.toString() + "|" + parameters);
throw new WebAPIFailException("Bad Request: " + url.toString() + "|" + parameters);
case 403:
throw new WebAPIForbiddenException(url.toString());
case 404:
throw new WebAPINotFoundException();
case 500:
throw new WebAPIInternalErrorException();
default:
throw new WebAPIException(url.toString() + "| Wrong response code " + responseCode);
}

View File

@ -4,6 +4,7 @@
*/
package main.java.com.djrapitops.plan.systems.webserver.webapi.bungee;
import com.djrapitops.plugin.api.utility.log.Log;
import main.java.com.djrapitops.plan.Settings;
import main.java.com.djrapitops.plan.api.IPlan;
import main.java.com.djrapitops.plan.api.exceptions.WebAPIException;
@ -33,7 +34,16 @@ public class PostHtmlWebAPI extends WebAPI {
if (htmlVariable == null) {
return badRequest("Html was null");
}
String html = "<!DOCTYPE html>" + htmlVariable.split("<!DOCTYPE html>", 2)[1];
if (!htmlVariable.startsWith("<!DOCTYPE html>")) {
String[] split = htmlVariable.split("<!DOCTYPE html>", 2);
if (split.length <= 1) {
Log.debug(htmlVariable);
return badRequest("Html did not start with <!DOCTYPE html>");
}
htmlVariable = "<!DOCTYPE html>" + split[1];
}
String html = htmlVariable;
String target = variables.get("target");
InformationManager infoManager = plugin.getInfoManager();
switch (target) {

View File

@ -27,7 +27,9 @@ public class PingWebAPI extends WebAPI {
@Override
public Response onRequest(IPlan plugin, Map<String, String> variables) {
if (Check.isBungeeAvailable()) {
((PlanBungee) plugin).getServerInfoManager().serverConnected(UUID.fromString(variables.get("sender")));
if (!((PlanBungee) plugin).getServerInfoManager().serverConnected(UUID.fromString(variables.get("sender")))) {
return fail("Server info not found from the database");
}
} else if (!plugin.getInfoManager().isUsingAnotherWebServer()) {
try {
String webAddress = variables.get("webAddress");