211 lines
7.4 KiB
Java
211 lines
7.4 KiB
Java
|
package xyz.etztech.minecraftmanager;
|
||
|
|
||
|
import com.google.gson.JsonArray;
|
||
|
import com.google.gson.JsonElement;
|
||
|
import com.google.gson.JsonObject;
|
||
|
import com.google.gson.JsonParser;
|
||
|
import org.apache.commons.lang.StringUtils;
|
||
|
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.HttpGet;
|
||
|
import org.apache.http.client.methods.HttpPost;
|
||
|
import org.apache.http.impl.client.HttpClients;
|
||
|
import org.apache.http.message.BasicNameValuePair;
|
||
|
import org.bukkit.Bukkit;
|
||
|
|
||
|
import java.io.BufferedReader;
|
||
|
import java.io.InputStream;
|
||
|
import java.io.InputStreamReader;
|
||
|
import java.net.URL;
|
||
|
import java.util.ArrayList;
|
||
|
import java.util.HashMap;
|
||
|
import java.util.List;
|
||
|
import java.util.Map;
|
||
|
|
||
|
public class MCMAPI {
|
||
|
private static String url;
|
||
|
private static String api;
|
||
|
|
||
|
/**
|
||
|
* Turns on testing mode
|
||
|
* @param url The override Django API URL
|
||
|
* @param api The override Django API password
|
||
|
*/
|
||
|
public static void test(String url, String api) {
|
||
|
MCMAPI.url = url;
|
||
|
MCMAPI.api = api;
|
||
|
}
|
||
|
|
||
|
|
||
|
public static JsonArray queryModel(String model, Map<String, String> filters) {
|
||
|
// Querying a model
|
||
|
String djangoUrl = getDjangoUrl();
|
||
|
djangoUrl += "model/";
|
||
|
|
||
|
try {
|
||
|
String get = GET(djangoUrl + model + "/", filters);
|
||
|
JsonParser jsonParser = new JsonParser();
|
||
|
return (JsonArray)jsonParser.parse(get);
|
||
|
} catch (Exception ex) {
|
||
|
log("Could not connect to Django. Is the web server running?");
|
||
|
}
|
||
|
return new JsonArray();
|
||
|
}
|
||
|
|
||
|
public static JsonObject postApplication(Application application) {
|
||
|
String djangoUrl = getDjangoUrl() + "plugin/application/";
|
||
|
String post = POST(djangoUrl, application.getForm());
|
||
|
JsonParser jsonParser = new JsonParser();
|
||
|
return (JsonObject) jsonParser.parse(post);
|
||
|
}
|
||
|
|
||
|
public static JsonObject postApplicationAction(String id, boolean accepted, String username) {
|
||
|
String djangoUrl = getDjangoUrl() + "plugin/application_action/";
|
||
|
Map<String, String> data = new HashMap<>();
|
||
|
data.put("application_id", id);
|
||
|
data.put("action", accepted ? "True" : "False");
|
||
|
data.put("username", username);
|
||
|
String post = POST(djangoUrl, data);
|
||
|
JsonParser jsonParser = new JsonParser();
|
||
|
return (JsonObject) jsonParser.parse(post);
|
||
|
}
|
||
|
|
||
|
public static JsonObject postApplicationClear(String id) {
|
||
|
String djangoUrl = getDjangoUrl() + "plugin/application_clear/";
|
||
|
Map<String, String> data = new HashMap<>();
|
||
|
data.put("application_id", id);
|
||
|
String post = POST(djangoUrl, data);
|
||
|
JsonParser jsonParser = new JsonParser();
|
||
|
return (JsonObject) jsonParser.parse(post);
|
||
|
}
|
||
|
|
||
|
public static JsonObject postLogin(String username, String uuid, String ip) {
|
||
|
String djangoUrl = getDjangoUrl() + "plugin/login/";
|
||
|
Map<String, String> data = new HashMap<>();
|
||
|
data.put("username", username);
|
||
|
data.put("uuid", uuid);
|
||
|
data.put("ip", ip);
|
||
|
String post = POST(djangoUrl, data);
|
||
|
JsonParser jsonParser = new JsonParser();
|
||
|
return (JsonObject) jsonParser.parse(post);
|
||
|
}
|
||
|
|
||
|
public static JsonObject postTicket(String uuid, String message, String x, String y, String z, String world) {
|
||
|
String djangoUrl = getDjangoUrl() + "plugin/ticket/";
|
||
|
Map<String, String> data = new HashMap<>();
|
||
|
data.put("uuid", uuid);
|
||
|
data.put("message", message);
|
||
|
data.put("x", x);
|
||
|
data.put("y", y);
|
||
|
data.put("z", z);
|
||
|
data.put("world", world);
|
||
|
String post = POST(djangoUrl, data);
|
||
|
JsonParser jsonParser = new JsonParser();
|
||
|
return (JsonObject) jsonParser.parse(post);
|
||
|
}
|
||
|
|
||
|
public static JsonObject getPassword(String uuid) {
|
||
|
String djangoUrl = getDjangoUrl() + "plugin/register/";
|
||
|
Map<String, String> data = new HashMap<>();
|
||
|
data.put("uuid", uuid);
|
||
|
String post = POST(djangoUrl, data);
|
||
|
JsonParser jsonParser = new JsonParser();
|
||
|
return (JsonObject) jsonParser.parse(post);
|
||
|
}
|
||
|
|
||
|
private static void log(String message) {
|
||
|
try {
|
||
|
Bukkit.getConsoleSender().sendMessage(message);
|
||
|
} catch (Exception ex) {
|
||
|
System.out.println(message);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private static String GET(String url) {
|
||
|
return GET(url, new HashMap<String, String>());
|
||
|
}
|
||
|
|
||
|
private static String GET(String url, Map<String, String> data) {
|
||
|
if (StringUtils.isNotEmpty(MCMAPI.api)) {
|
||
|
data.put("api", MCMAPI.api);
|
||
|
} else {
|
||
|
data.put("api", MinecraftManager.config.getString("django.api"));
|
||
|
}
|
||
|
StringBuffer result = new StringBuffer();
|
||
|
ArrayList<String> query = new ArrayList<>();
|
||
|
for (String key : data.keySet()) {
|
||
|
query.add(key + "=" + data.get(key));
|
||
|
}
|
||
|
String dataQuery = StringUtils.join(query, "&");
|
||
|
try {
|
||
|
HttpClient client = HttpClients.createDefault();
|
||
|
HttpGet get = new HttpGet(url + "?" + dataQuery);
|
||
|
HttpResponse response = client.execute(get);
|
||
|
HttpEntity entity = response.getEntity();
|
||
|
|
||
|
InputStream input = entity.getContent();
|
||
|
BufferedReader in = new BufferedReader(new InputStreamReader(input));
|
||
|
String inputLine;
|
||
|
while ((inputLine = in.readLine()) != null) {
|
||
|
result.append(inputLine);
|
||
|
}
|
||
|
input.close();
|
||
|
} catch (Exception ex) {
|
||
|
log("GET request failed. (" + url + ")");
|
||
|
}
|
||
|
|
||
|
return result.toString();
|
||
|
}
|
||
|
|
||
|
private static String POST(String url) {
|
||
|
return POST(url, new HashMap<String, String>());
|
||
|
}
|
||
|
|
||
|
private static String POST(String url, Map<String, String> data) {
|
||
|
StringBuffer result = new StringBuffer();
|
||
|
if (StringUtils.isNotEmpty(MCMAPI.api)) {
|
||
|
data.put("api", MCMAPI.api);
|
||
|
} else {
|
||
|
data.put("api", MinecraftManager.config.getString("django.api"));
|
||
|
}
|
||
|
try {
|
||
|
HttpClient client = HttpClients.createDefault();
|
||
|
HttpPost post = new HttpPost(url);
|
||
|
List<NameValuePair> params = new ArrayList<>();
|
||
|
for (String key : data.keySet()) {
|
||
|
params.add(new BasicNameValuePair(key, data.get(key)));
|
||
|
}
|
||
|
post.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
|
||
|
HttpResponse response = client.execute(post);
|
||
|
HttpEntity entity = response.getEntity();
|
||
|
|
||
|
InputStream input = entity.getContent();
|
||
|
BufferedReader in = new BufferedReader(new InputStreamReader(input));
|
||
|
String inputLine;
|
||
|
while ((inputLine = in.readLine()) != null) {
|
||
|
result.append(inputLine);
|
||
|
}
|
||
|
input.close();
|
||
|
} catch (Exception ex) {
|
||
|
log("POST request failed. (" + url + ")");
|
||
|
}
|
||
|
|
||
|
return result.toString();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @return The Django API URL ending with a slash
|
||
|
*/
|
||
|
private static String getDjangoUrl() {
|
||
|
if (StringUtils.isNotEmpty(MCMAPI.url)) {
|
||
|
return MCMAPI.url;
|
||
|
} else {
|
||
|
String configUrl = MinecraftManager.config.getString("django.url");
|
||
|
return configUrl.endsWith("/") ? configUrl : configUrl + "/";
|
||
|
}
|
||
|
}
|
||
|
}
|