Skip to content

Commit e71f41b

Browse files
committed
Support for POST + authkey and add a debug config
1 parent fb869d6 commit e71f41b

4 files changed

Lines changed: 79 additions & 11 deletions

File tree

src/main/java/pw/chew/jsonrestapi/JSONRestAPI.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@
55
import pw.chew.jsonrestapi.commands.ReloadCommand;
66

77
public final class JSONRestAPI extends JavaPlugin {
8-
// Files
9-
private static FileConfiguration config;
108

119
@Override
1210
public void onEnable() {
1311
// Get and save config
14-
config = this.getConfig();
12+
FileConfiguration config = this.getConfig();
1513
config.addDefault("port", 6548);
14+
config.addDefault("debug", false);
1615
config.addDefault("authkey", "CHANGE_ME_PLEASE");
1716
config.options().copyDefaults(true);
1817
saveDefaultConfig();
@@ -22,7 +21,7 @@ public void onEnable() {
2221
server.setDaemon(true);
2322
server.start();
2423

25-
this.getCommand("jrareload").setExecutor(new ReloadCommand(config, this));
24+
this.getCommand("jrareload").setExecutor(new ReloadCommand(this));
2625

2726
this.getLogger().info("Listening on port " + config.getInt("port"));
2827
}

src/main/java/pw/chew/jsonrestapi/RestServer.java

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import me.clip.placeholderapi.PlaceholderAPI;
44
import org.bukkit.Bukkit;
5+
import org.bukkit.OfflinePlayer;
56
import org.bukkit.configuration.file.FileConfiguration;
67

78
import java.io.BufferedReader;
@@ -12,8 +13,10 @@
1213
import java.net.ServerSocket;
1314
import java.net.Socket;
1415
import java.util.Arrays;
16+
import java.util.HashMap;
1517
import java.util.List;
1618
import java.util.Locale;
19+
import java.util.Map;
1720
import java.util.UUID;
1821
import java.util.logging.Logger;
1922

@@ -103,32 +106,86 @@ public void run() {
103106
return;
104107
}
105108

109+
// Debug message
110+
if (config.getBoolean("debug")) {
111+
logger.info("Received request: " + request);
112+
}
113+
114+
// Get parameters and route
106115
String route = request.substring(initialSplit + 1, routeSplit);
116+
Map<String, String> params = new HashMap<>();
107117
int routeQuerySplit = route.indexOf('?');
108118
if (routeQuerySplit != -1) {
109119
route = route.substring(0, routeQuerySplit);
120+
String paramString = request.split(" ")[1].split("\\?")[1];
121+
String[] paramSplit = paramString.split("&");
122+
for (String param : paramSplit) {
123+
String key = param.split("=")[0];
124+
String value = param.split("=")[1];
125+
126+
// Debug message
127+
if (config.getBoolean("debug")) {
128+
logger.info("Added parameter " + key + " with value " + value);
129+
}
130+
params.put(key, value);
131+
}
110132
}
111133

112134
// Remove initial slash
113135
route = route.replaceFirst("/", "");
114136

115-
logger.info("Received request with route: " + route);
137+
// Debug message
138+
if (config.getBoolean("debug")) {
139+
logger.info("Route: " + route);
140+
}
116141

117142
// Check if route exists
118143
if (config.contains("routes." + route)) {
144+
// Initialize some parameters
119145
String key = "routes." + route + ".";
120146
String configMethod = config.getString(key + "method");
147+
String authkey = config.getString("authkey");
148+
boolean keyRequired = method.equals("POST");
121149

150+
// Ensure the method matches the route itself
122151
if (!method.equals(configMethod)) {
123152
respondNotAllowed(printWriter);
124153
return;
125154
}
126155

127-
String response = PlaceholderAPI.setPlaceholders(Bukkit.getOfflinePlayer(UUID.randomUUID()), config.getString(key + "response"));
156+
// If auth key is specified, override keyRequired.
157+
if (config.contains(key + ".authkey")) {
158+
keyRequired = config.getBoolean(key + ".authkey");
159+
}
160+
161+
// Check if a key is required, and if the key is specified
162+
if (keyRequired && !authkey.equals(params.getOrDefault("key", ""))) {
163+
respondUnauthorized(printWriter);
164+
return;
165+
}
166+
167+
// Set the player, depending on the request.
168+
OfflinePlayer player;
169+
if (method.equals("POST")) {
170+
if (params.containsKey("uuid")) {
171+
player = Bukkit.getOfflinePlayer(UUID.fromString(params.get("uuid")));
172+
} else if (params.containsKey("username")) {
173+
player = Bukkit.getPlayer(params.get("username"));
174+
} else {
175+
respondBadRequest(printWriter);
176+
return;
177+
}
178+
} else {
179+
player = Bukkit.getOfflinePlayer(UUID.randomUUID());
180+
}
181+
182+
// Build the response by letting PAPI parse the placeholders
183+
String response = PlaceholderAPI.setPlaceholders(player, config.getString(key + "response"));
128184

129185
// Wrap the response
130186
String json = "{\"success\": true, \"response\": " + response + "}";
131187

188+
// Send it off!
132189
respondOk(printWriter, json);
133190
} else {
134191
respondNotFound(printWriter);
@@ -165,6 +222,15 @@ private void respondNotFound(PrintWriter writer) {
165222
writer.printf("HTTP/1.2 404 Not Found%n%n");
166223
}
167224

225+
/**
226+
* Responds to the HTTP request with a not found header.
227+
*
228+
* @param writer The {@link PrintWriter} to print the response to.
229+
*/
230+
private void respondUnauthorized(PrintWriter writer) {
231+
writer.printf("HTTP/1.2 401 Unauthorized%n%n");
232+
}
233+
168234
/**
169235
* Responds to the HTTP request with an OK header.
170236
*

src/main/java/pw/chew/jsonrestapi/commands/ReloadCommand.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,18 @@
99
import pw.chew.jsonrestapi.RestServer;
1010

1111
public class ReloadCommand implements CommandExecutor {
12-
public static FileConfiguration config;
1312
public final JSONRestAPI plugin;
1413

15-
public ReloadCommand(FileConfiguration baseConfig, JSONRestAPI plugin) {
16-
config = baseConfig;
14+
public ReloadCommand(JSONRestAPI plugin) {
1715
this.plugin = plugin;
1816
}
1917

2018
@Override
2119
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
2220
sender.sendMessage("Reloading the config.");
2321
plugin.reloadConfig();
24-
config = plugin.getConfig();
2522
// Tell the server about the new config.
26-
RestServer.updateConfig(config);
23+
RestServer.updateConfig(plugin.getConfig());
2724
sender.sendMessage("Configuration reloaded!");
2825
return true;
2926
}

src/main/resources/config.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ port: 6548
1717
## Only takes effect if authkey: true in the routes below, plus the root route.
1818
authkey: CHANGE_ME_PLEASE
1919

20+
# Debug mode
21+
## Adds some useful debug output for developing.
22+
## Includes the full request string, parameters, and route itself.
23+
## Most of the time no one cares about this, but you might.
24+
debug: false
25+
2026
# Route setup
2127
## By default, the root route accept a raw POST value for any info not provided here.
2228
## You can set up specific routes below.

0 commit comments

Comments
 (0)