mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2025-03-12 06:41:50 +01:00
Added sendRequest method for WebAPI
This commit is contained in:
parent
5a359c3e75
commit
e6698d0097
@ -0,0 +1,21 @@
|
||||
/*
|
||||
* 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 fails to connect.
|
||||
*
|
||||
* @author Rsl1122
|
||||
*/
|
||||
public class WebAPIConnectionFailException extends WebAPIException {
|
||||
|
||||
public WebAPIConnectionFailException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
public WebAPIConnectionFailException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
}
|
@ -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;
|
||||
|
||||
/**
|
||||
* Thrown when web api post request fails.
|
||||
*
|
||||
* @author Rsl1122
|
||||
*/
|
||||
public class WebAPIException extends Exception {
|
||||
|
||||
public WebAPIException() {
|
||||
}
|
||||
|
||||
public WebAPIException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public WebAPIException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
public WebAPIException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
}
|
@ -59,12 +59,12 @@ public class WebServer {
|
||||
}
|
||||
|
||||
private void registerWebAPIs() {
|
||||
webAPI.registerNewAPI("analytics", new AnalyticsWebAPI());
|
||||
webAPI.registerNewAPI("analyze", new AnalyzeWebAPI());
|
||||
webAPI.registerNewAPI("configure", new ConfigureWebAPI());
|
||||
webAPI.registerNewAPI("inspect", new InspectWebAPI());
|
||||
webAPI.registerNewAPI("onlineplayers", new OnlinePlayersWebAPI());
|
||||
webAPI.registerNewAPI("ping", new PingWebAPI());
|
||||
webAPI.registerNewAPI(new AnalyticsWebAPI());
|
||||
webAPI.registerNewAPI(new AnalyzeWebAPI());
|
||||
webAPI.registerNewAPI(new ConfigureWebAPI());
|
||||
webAPI.registerNewAPI(new InspectWebAPI());
|
||||
webAPI.registerNewAPI(new OnlinePlayersWebAPI());
|
||||
webAPI.registerNewAPI(new PingWebAPI());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -4,15 +4,81 @@
|
||||
*/
|
||||
package main.java.com.djrapitops.plan.systems.webserver.webapi;
|
||||
|
||||
import com.djrapitops.plugin.utilities.Verify;
|
||||
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.systems.webserver.response.Response;
|
||||
import main.java.com.djrapitops.plan.utilities.MiscUtils;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.NameValuePair;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.message.BasicNameValuePair;
|
||||
|
||||
import java.util.Map;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @author Fuzzlemann
|
||||
* @author Rsl1122
|
||||
*/
|
||||
public interface WebAPI {
|
||||
public abstract class WebAPI {
|
||||
|
||||
Response onResponse(IPlan plugin, Map<String, String> variables);
|
||||
private Map<String, String> variables;
|
||||
|
||||
public abstract Response onResponse(IPlan plugin, Map<String, String> variables);
|
||||
|
||||
public String sendRequest(String address, UUID receiverUUID) throws WebAPIException {
|
||||
Verify.nullCheck(address, receiverUUID);
|
||||
|
||||
HttpClient httpClient = HttpClients.createDefault();
|
||||
|
||||
HttpPost postRequest = new HttpPost(address + "/api/" + this.getClass().getSimpleName().toLowerCase());
|
||||
|
||||
List<NameValuePair> parameters = new ArrayList<>();
|
||||
String serverUUID = MiscUtils.getIPlan().getServerInfoManager().getServerUUID().toString();
|
||||
parameters.add(new BasicNameValuePair("sender", serverUUID));
|
||||
parameters.add(new BasicNameValuePair("key", receiverUUID.toString()));
|
||||
for (Map.Entry<String, String> entry : variables.entrySet()) {
|
||||
parameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
|
||||
}
|
||||
|
||||
try {
|
||||
postRequest.setEntity(new UrlEncodedFormEntity(parameters, "ISO-8859-1"));
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new WebAPIException("Unsupported parameter encoding", e);
|
||||
}
|
||||
|
||||
try {
|
||||
HttpResponse response = httpClient.execute(postRequest);
|
||||
HttpEntity responseEntity = response.getEntity();
|
||||
|
||||
if (responseEntity != null) {
|
||||
StringBuilder content = new StringBuilder();
|
||||
try (InputStream inputStream = responseEntity.getContent()) {
|
||||
Scanner scanner = new Scanner(inputStream);
|
||||
while (scanner.hasNextLine()) {
|
||||
content.append(scanner.nextLine());
|
||||
}
|
||||
}
|
||||
return content.toString();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new WebAPIConnectionFailException("API connection failed.", e);
|
||||
}
|
||||
throw new WebAPIException("Response entity was null");
|
||||
}
|
||||
|
||||
protected void addVariable(String key, String value) {
|
||||
variables.put(key, value);
|
||||
}
|
||||
|
||||
public Map<String, String> getVariables() {
|
||||
return variables;
|
||||
}
|
||||
}
|
@ -8,7 +8,7 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author Fuzzlemann
|
||||
* @author Fuzzlemann & Rsl1122
|
||||
*/
|
||||
public class WebAPIManager {
|
||||
|
||||
@ -21,11 +21,11 @@ public class WebAPIManager {
|
||||
registry = new HashMap<>();
|
||||
}
|
||||
|
||||
public void registerNewAPI(String method, WebAPI api) {
|
||||
registry.put(method.toLowerCase(), api);
|
||||
public void registerNewAPI(WebAPI api) {
|
||||
registry.put(api.getClass().getSimpleName().toLowerCase(), api);
|
||||
}
|
||||
|
||||
public WebAPI getAPI(String method) {
|
||||
return registry.get(method.toLowerCase());
|
||||
public WebAPI getAPI(String apiName) {
|
||||
return registry.get(apiName.toLowerCase());
|
||||
}
|
||||
}
|
||||
|
@ -17,7 +17,7 @@ import java.util.Map;
|
||||
/**
|
||||
* @author Fuzzlemann
|
||||
*/
|
||||
public class AnalyticsWebAPI implements WebAPI {
|
||||
public class AnalyticsWebAPI extends WebAPI {
|
||||
@Override
|
||||
public Response onResponse(IPlan plugin, Map<String, String> variables) {
|
||||
InformationManager infoManager = plugin.getInfoManager();
|
||||
|
@ -15,7 +15,7 @@ import java.util.Map;
|
||||
/**
|
||||
* @author Fuzzlemann
|
||||
*/
|
||||
public class AnalyzeWebAPI implements WebAPI {
|
||||
public class AnalyzeWebAPI extends WebAPI {
|
||||
@Override
|
||||
public Response onResponse(IPlan plugin, Map<String, String> variables) {
|
||||
plugin.getInfoManager().refreshAnalysis();
|
||||
|
@ -19,7 +19,7 @@ import java.util.Map;
|
||||
/**
|
||||
* @author Fuzzlemann
|
||||
*/
|
||||
public class ConfigureWebAPI implements WebAPI {
|
||||
public class ConfigureWebAPI extends WebAPI {
|
||||
@Override
|
||||
public Response onResponse(IPlan plugin, Map<String, String> variables) {
|
||||
String key = variables.get("configKey");
|
||||
|
@ -18,7 +18,7 @@ import java.util.UUID;
|
||||
/**
|
||||
* @author Fuzzlemann
|
||||
*/
|
||||
public class InspectWebAPI implements WebAPI {
|
||||
public class InspectWebAPI extends WebAPI {
|
||||
@Override
|
||||
public Response onResponse(IPlan plugin, Map<String, String> variables) {
|
||||
String playerString = variables.get("player");
|
||||
|
@ -16,7 +16,7 @@ import java.util.Map;
|
||||
/**
|
||||
* @author Fuzzlemann
|
||||
*/
|
||||
public class OnlinePlayersWebAPI implements WebAPI {
|
||||
public class OnlinePlayersWebAPI extends WebAPI {
|
||||
@Override
|
||||
public Response onResponse(IPlan plugin, Map<String, String> variables) {
|
||||
Server server = ((Plan) plugin).getServer();
|
||||
|
Loading…
Reference in New Issue
Block a user