Skip to content

Commit 7578c5c

Browse files
committed
Handle default route (get and post)
1 parent b3cf1d5 commit 7578c5c

File tree

1 file changed

+82
-43
lines changed

1 file changed

+82
-43
lines changed

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

Lines changed: 82 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -139,60 +139,99 @@ public void run() {
139139
logger.info("Route: " + route);
140140
}
141141

142+
// Default route.
143+
// This is a special case.
144+
if (route.equals("")) {
145+
// Handle GET, just in case they try to go to the URL.
146+
if (method.equals("GET")) {
147+
// Send a "I'm alive!" response
148+
respondOk(printWriter, "{\"success\": true}");
149+
} else {
150+
// Handle POST as described in the config.
151+
// They essentially pass what would be in the route
152+
// Requires "request", "key", and "username"/"uuid"
153+
154+
respondOk(printWriter, buildResponse(method, route, params, printWriter,
155+
true, params.getOrDefault("request", "")));
156+
}
157+
// We're done here.
158+
return;
159+
}
160+
142161
// Check if route exists
143162
if (config.contains("routes." + route)) {
144-
// Initialize some parameters
145-
String key = "routes." + route + ".";
146-
String configMethod = config.getString(key + "method");
147-
String authkey = config.getString("authkey");
148-
boolean keyRequired = method.equals("POST");
149-
150-
// Ensure the method matches the route itself
151-
if (!method.equals(configMethod)) {
152-
respondNotAllowed(printWriter);
153-
return;
154-
}
163+
// Build the request and send it off!
164+
respondOk(printWriter, buildResponse(method, route, params, printWriter, false, null));
165+
} else {
166+
// Doesn't exist. Try again!
167+
respondNotFound(printWriter);
168+
}
169+
} catch (IOException e) {
170+
e.printStackTrace();
171+
}
172+
}
155173

156-
// If auth key is specified, override keyRequired.
157-
if (config.contains(key + ".authkey")) {
158-
keyRequired = config.getBoolean(key + ".authkey");
159-
}
174+
private boolean isUnauthorized(String key) {
175+
return !key.equals(config.getString("authkey"));
176+
}
160177

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-
}
178+
private String buildResponse(String method, String route, Map<String, String> params, PrintWriter printWriter,
179+
boolean root, String request) /* Root request-specific params */ {
180+
// By default, POST requires a key.
181+
boolean keyRequired = method.equals("POST");
166182

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-
}
183+
// Don't do this stuff for the root route.
184+
if (!root) {
185+
// Get the config key for this route
186+
String key = "routes." + route + ".";
181187

182-
// Build the response by letting PAPI parse the placeholders
183-
String response = PlaceholderAPI.setPlaceholders(player, config.getString(key + "response"));
188+
// Ensure the method matches the route itself
189+
if (!method.equals(config.getString(key + "method"))) {
190+
respondNotAllowed(printWriter);
191+
return null;
192+
}
184193

185-
// Wrap the response
186-
String json = "{\"success\": true, \"response\": " + response + "}";
194+
// If auth key is specified, override keyRequired.
195+
if (config.contains(key + ".authkey")) {
196+
keyRequired = config.getBoolean(key + ".authkey");
197+
}
198+
199+
// Get the request to fill in from the config.
200+
request = config.getString(key + "response");
201+
}
202+
203+
// Check if a key is required, and if the key is specified
204+
if (keyRequired && isUnauthorized(params.getOrDefault("key", ""))) {
205+
respondUnauthorized(printWriter);
206+
return null;
207+
}
187208

188-
// Send it off!
189-
respondOk(printWriter, json);
209+
// Set the player, depending on the request.
210+
OfflinePlayer player;
211+
if (method.equals("POST")) {
212+
if (params.containsKey("uuid")) {
213+
player = Bukkit.getOfflinePlayer(UUID.fromString(params.get("uuid")));
214+
} else if (params.containsKey("username")) {
215+
player = Bukkit.getPlayer(params.get("username"));
190216
} else {
191-
respondNotFound(printWriter);
217+
respondBadRequest(printWriter);
218+
return null;
192219
}
193-
} catch (IOException e) {
194-
e.printStackTrace();
220+
} else {
221+
player = Bukkit.getOfflinePlayer(UUID.randomUUID());
195222
}
223+
224+
// Build the response by letting PAPI parse the placeholders
225+
String response = PlaceholderAPI.setPlaceholders(player, request);
226+
227+
// Wrap the response
228+
String json = "{\"success\": true, \"response\": \"" + response + "\"}";
229+
230+
if (config.getBoolean("debug")) {
231+
logger.info("Response is " + json);
232+
}
233+
234+
return json;
196235
}
197236

198237
/**

0 commit comments

Comments
 (0)