Fixed WebAPI, DevCommand, Improved some WebAPI, New WebAPI.

This commit is contained in:
Rsl1122 2017-09-16 10:53:13 +03:00
parent 3c2c710115
commit fb717387d2
11 changed files with 180 additions and 9 deletions

View File

@ -24,6 +24,7 @@ public enum Settings {
LOG_UNKNOWN_COMMANDS("Data.Commands.LogUnknownCommands"),
COMBINE_COMMAND_ALIASES("Data.Commands.CombineCommandAliases"),
WRITE_NEW_LOCALE("Plugin.WriteNewLocaleFileOnStart"),
DEV_MODE("Plugin.Dev"),
// Integer
WEBSERVER_PORT("WebServer.Port"),

View File

@ -5,6 +5,7 @@ import com.djrapitops.plugin.command.TreeCommand;
import com.djrapitops.plugin.command.defaultcmds.StatusCommand;
import main.java.com.djrapitops.plan.Permissions;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.Settings;
import main.java.com.djrapitops.plan.command.commands.*;
import main.java.com.djrapitops.plan.locale.Locale;
import main.java.com.djrapitops.plan.locale.Msg;
@ -52,5 +53,9 @@ public class PlanCommand extends TreeCommand<Plan> {
commands.add(registerCommand);
commands.add(new WebUserCommand(plugin, registerCommand));
}
if (Settings.DEV_MODE.isTrue()) {
commands.add(new DevCommand(plugin));
}
}
}

View File

@ -0,0 +1,71 @@
/*
* 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.command.commands;
import com.djrapitops.plugin.command.CommandType;
import com.djrapitops.plugin.command.ISender;
import com.djrapitops.plugin.command.SubCommand;
import main.java.com.djrapitops.plan.Plan;
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.WebAPI;
import main.java.com.djrapitops.plan.systems.webserver.webapi.bukkit.InspectWebAPI;
import main.java.com.djrapitops.plan.utilities.Check;
import java.util.UUID;
/**
* //TODO Class Javadoc Comment
*
* @author Rsl1122
*/
public class DevCommand extends SubCommand {
private final Plan plugin;
public DevCommand(Plan plugin) {
super("dev", CommandType.CONSOLE_WITH_ARGUMENTS, "plan.*", "Test Plugin functions not testable with unit tests.", "<feature to test>");
this.plugin = plugin;
}
@Override
public boolean onCommand(ISender sender, String cmd, String[] args) {
if (!Check.isTrue(args.length >= 1, Locale.get(Msg.CMD_FAIL_REQ_ONE_ARG).toString(), sender)) {
return true;
}
String feature = args[0];
switch (feature) {
case "webapi":
if (!Check.isTrue(args.length >= 2, Locale.get(Msg.CMD_FAIL_REQ_ONE_ARG).toString(), sender)) {
return true;
}
if (!webapi(args[1])) {
sender.sendMessage("[Plan] No such API / Exception occurred.");
}
default:
break;
}
return true;
}
private boolean webapi(String method) {
WebAPI api = plugin.getWebServer().getWebAPI().getAPI(method);
if (api == null) {
return false;
}
try {
if (api instanceof InspectWebAPI) {
((InspectWebAPI) api).sendRequest(plugin.getWebServer().getAccessAddress(), Plan.getServerUUID(), UUID.randomUUID());
} else {
api.sendRequest(plugin.getWebServer().getAccessAddress(), Plan.getServerUUID());
}
return true;
} catch (WebAPIException e) {
e.printStackTrace();
}
return false;
}
}

View File

@ -190,7 +190,7 @@ public class WebServer {
}
public String getAccessAddress() {
return getProtocol() + ":/" + HtmlUtils.getIP();
return getProtocol() + "://" + HtmlUtils.getIP();
}
public WebAPIManager getWebAPI() {

View File

@ -13,4 +13,9 @@ public class AnalysisPageResponse extends Response {
super.setHeader("HTTP/1.1 200 OK");
super.setContent(Theme.replaceColors(informationManager.getAnalysisHtml()));
}
public AnalysisPageResponse(String html) {
super.setHeader("HTTP/1.1 200 OK");
super.setContent(Theme.replaceColors(html));
}
}

View File

@ -19,6 +19,12 @@ public class InspectPageResponse extends Response {
setInspectPagePluginsTab(infoManager.getPluginsTabContent(uuid));
}
public InspectPageResponse(InformationManager infoManager, UUID uuid, String html) {
super.setHeader("HTTP/1.1 200 OK");
super.setContent(Theme.replaceColors(html));
setInspectPagePluginsTab(infoManager.getPluginsTabContent(uuid));
}
public void setInspectPagePluginsTab(String inspectPagePluginsTab) {
if (this.inspectPagePluginsTab != null) {
setContent(getContent().replace(this.inspectPagePluginsTab, inspectPagePluginsTab));

View File

@ -15,6 +15,7 @@ import java.io.DataOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
@ -25,6 +26,10 @@ public abstract class WebAPI {
private Map<String, String> variables;
public WebAPI() {
this.variables = new HashMap<>();
}
public abstract Response onResponse(IPlan plugin, Map<String, String> variables);
public void sendRequest(String address, UUID receiverUUID) throws WebAPIException {
@ -59,16 +64,15 @@ public abstract class WebAPI {
int responseCode = connection.getResponseCode();
switch (responseCode) {
case 200:
break;
return;
case 400:
throw new WebAPIException("Bad Request: " + url.toString() + "|" + parameters);
default:
throw new WebAPIException(url.toString() + "| Wrong response code " + responseCode);
}
} catch (IOException e) {
throw new WebAPIConnectionFailException("API connection failed.", e);
throw new WebAPIConnectionFailException("API connection failed. address: " + address, e);
}
throw new WebAPIException("Response entity was null");
}
protected void addVariable(String key, String value) {

View File

@ -16,7 +16,7 @@ import java.util.Map;
import java.util.UUID;
/**
* @author Fuzzlemann
* @author Rsl1122
*/
public class InspectWebAPI extends WebAPI {
@Override
@ -33,6 +33,11 @@ public class InspectWebAPI extends WebAPI {
return PageCache.loadPage("success", SuccessResponse::new);
}
@Override
public void sendRequest(String address, UUID receiverUUID) throws WebAPIException {
throw new IllegalStateException("Wrong method call for this WebAPI, call sendRequest(String, UUID, UUID) instead.");
}
public void sendRequest(String address, UUID receiverUUID, UUID uuid) throws WebAPIException {
addVariable("uuid", uuid.toString());
super.sendRequest(address, receiverUUID);

View File

@ -0,0 +1,22 @@
/*
* 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.webapi.bukkit;
import main.java.com.djrapitops.plan.api.IPlan;
import main.java.com.djrapitops.plan.systems.webserver.response.Response;
import main.java.com.djrapitops.plan.systems.webserver.response.api.JsonResponse;
import main.java.com.djrapitops.plan.systems.webserver.webapi.WebAPI;
import java.util.Map;
/**
* @author Rsl1122
*/
public class MaxPlayersWebAPI extends WebAPI {
@Override
public Response onResponse(IPlan plugin, Map<String, String> variables) {
return new JsonResponse(plugin.getVariable().getMaxPlayers());
}
}

View File

@ -9,7 +9,6 @@ import main.java.com.djrapitops.plan.api.IPlan;
import main.java.com.djrapitops.plan.systems.webserver.response.Response;
import main.java.com.djrapitops.plan.systems.webserver.response.api.JsonResponse;
import main.java.com.djrapitops.plan.systems.webserver.webapi.WebAPI;
import org.bukkit.Server;
import java.util.Map;
@ -19,8 +18,6 @@ import java.util.Map;
public class OnlinePlayersWebAPI extends WebAPI {
@Override
public Response onResponse(IPlan plugin, Map<String, String> variables) {
Server server = ((Plan) plugin).getServer();
return new JsonResponse(server.getOnlinePlayers() + "/" + server.getMaxPlayers());
return new JsonResponse(((Plan) plugin).getServer().getOnlinePlayers());
}
}

View File

@ -0,0 +1,55 @@
/*
* 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.webapi.bungee;
import main.java.com.djrapitops.plan.api.IPlan;
import main.java.com.djrapitops.plan.systems.info.InformationManager;
import main.java.com.djrapitops.plan.systems.webserver.PageCache;
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;
import main.java.com.djrapitops.plan.systems.webserver.response.api.BadRequestResponse;
import main.java.com.djrapitops.plan.systems.webserver.response.api.SuccessResponse;
import main.java.com.djrapitops.plan.systems.webserver.webapi.WebAPI;
import java.util.Map;
import java.util.UUID;
/**
* WebAPI for posting Html pages such as Inspect, players or server pages.
*
* @author Rsl1122
*/
public class PostHtmlWebAPI extends WebAPI {
private final IPlan plugin;
public PostHtmlWebAPI(IPlan plugin) {
this.plugin = plugin;
}
@Override
public Response onResponse(IPlan plugin, Map<String, String> variables) {
try {
String html = variables.get("html");
String target = variables.get("target");
InformationManager infoManager = plugin.getInfoManager();
switch (target) {
case "inspectPage":
String uuid = variables.get("uuid");
PageCache.loadPage("inspectPage:" + uuid, () -> new InspectPageResponse(infoManager, UUID.fromString(uuid), html));
break;
case "analysisPage":
PageCache.loadPage("analysisPage:" + variables.get("sender"), () -> new AnalysisPageResponse(html));
default:
String error = "Faulty Target";
return PageCache.loadPage(error, () -> new BadRequestResponse(error));
}
return PageCache.loadPage("success", SuccessResponse::new);
} catch (NullPointerException e) {
return PageCache.loadPage(e.toString(), () -> new BadRequestResponse(e.toString()));
}
}
}