Skip to content

Commit ca1e08e

Browse files
authored
Merge pull request #15 from gnmyt/fixes/response-handler
🐛 Bug-Fix in the ResponseHandler
2 parents 1db7477 + 4f0a936 commit ca1e08e

File tree

16 files changed

+147
-175
lines changed

16 files changed

+147
-175
lines changed

src/main/java/de/gnmyt/mcdash/MinecraftDashboard.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
1414
import java.io.IOException;
1515
import java.net.InetSocketAddress;
1616
import java.util.concurrent.Executors;
17+
import java.util.concurrent.ScheduledExecutorService;
1718

1819
public class MinecraftDashboard extends JavaPlugin {
1920

21+
private static final ScheduledExecutorService executor = Executors.newScheduledThreadPool(4);
2022
private static ConfigurationManager config;
2123
private static BackupController backupController;
2224
private static AccountManager accountManager;
@@ -140,4 +142,12 @@ public static AccountManager getAccountManager() {
140142
public static BackupController getBackupController() {
141143
return backupController;
142144
}
145+
146+
/**
147+
* Gets the executor
148+
* @return the executor
149+
*/
150+
public static ScheduledExecutorService getExecutor() {
151+
return executor;
152+
}
143153
}

src/main/java/de/gnmyt/mcdash/api/handler/DefaultHandler.java

Lines changed: 62 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,8 @@
1616
import java.nio.charset.StandardCharsets;
1717
import java.util.Base64;
1818
import java.util.List;
19-
import java.util.concurrent.CompletableFuture;
2019

2120
public abstract class DefaultHandler implements HttpHandler {
22-
23-
private Request request = null;
24-
private ResponseController controller = null;
25-
2621
public ConfigurationManager manager = MinecraftDashboard.getDashboardConfig();
2722
public AccountManager accountManager = MinecraftDashboard.getAccountManager();
2823

@@ -40,39 +35,37 @@ public String path() {
4035
*/
4136
@Override
4237
public void handle(HttpExchange exchange) {
38+
MinecraftDashboard.getExecutor().execute(() -> {
39+
Request request = prepareRequest(exchange, true);
40+
ResponseController controller = new ResponseController(exchange);
41+
42+
List<String> authHeader = request.getHeaders().get("Authorization");
43+
if (authHeader == null) {
44+
controller.code(400).message("You need to provide your credentials");
45+
return;
46+
}
4347

44-
request = prepareRequest(exchange, true);
45-
46-
controller = new ResponseController(exchange);
47-
48-
List<String> authHeader = request.getHeaders().get("Authorization");
49-
50-
if (authHeader == null) {
51-
controller.code(400).message("You need to provide your credentials");
52-
return;
53-
}
54-
55-
String[] authCredentials;
56-
57-
try {
58-
authCredentials = new String(Base64.getDecoder().decode(authHeader.get(0)
59-
.replace("Basic ", ""))).split(":");
60-
} catch (Exception e) {
61-
controller.code(400).message("You need to provide your credentials");
62-
return;
63-
}
48+
String[] authCredentials;
49+
try {
50+
authCredentials = new String(Base64.getDecoder().decode(authHeader.get(0)
51+
.replace("Basic ", ""))).split(":");
52+
} catch (Exception e) {
53+
controller.code(400).message("You need to provide your credentials");
54+
return;
55+
}
6456

65-
if (authCredentials.length != 2) {
66-
controller.code(400).message("You need to provide your credentials");
67-
return;
68-
}
57+
if (authCredentials.length != 2) {
58+
controller.code(400).message("You need to provide your credentials");
59+
return;
60+
}
6961

70-
if (!accountManager.isValidPassword(authCredentials[0], authCredentials[1])) {
71-
controller.code(401).message("The provided credentials are invalid");
72-
return;
73-
}
62+
if (!accountManager.isValidPassword(authCredentials[0], authCredentials[1])) {
63+
controller.code(401).message("The provided credentials are invalid");
64+
return;
65+
}
7466

75-
CompletableFuture.runAsync(() -> execute(request, controller));
67+
execute(request, controller);
68+
});
7669
}
7770

7871
/**
@@ -166,7 +159,6 @@ public void register() {
166159
* @return The prepared request
167160
*/
168161
protected Request prepareRequest(HttpExchange exchange, boolean writeBody) {
169-
170162
StringWriter writer = new StringWriter();
171163

172164
if (writeBody) {
@@ -204,21 +196,23 @@ public void runSync(Runnable runnable) {
204196
}
205197

206198
/**
207-
* Gets an string from the body
199+
* Gets a string from the body
200+
* @param request The request object from the HttpExchange
208201
* @param name The name of the value you want to get
209202
* @return the value (string)
210203
*/
211-
public String getStringFromBody(String name) {
204+
public String getStringFromBody(Request request, String name) {
212205
return request.getBody().get(name);
213206
}
214207

215208
/**
216209
* Gets an integer from the body
210+
* @param request The request object from the HttpExchange
217211
* @param name The name of the value you want to get
218212
* @return the value (integer)
219213
*/
220-
public Integer getIntegerFromBody(String name) {
221-
String value = getStringFromBody(name);
214+
public Integer getIntegerFromBody(Request request, String name) {
215+
String value = getStringFromBody(request, name);
222216
try {
223217
return Integer.parseInt(value);
224218
} catch (NumberFormatException e) {
@@ -227,53 +221,49 @@ public Integer getIntegerFromBody(String name) {
227221
}
228222

229223
/**
230-
* Gets an boolean from the body
224+
* Gets a boolean from the body
225+
* @param request The request object from the HttpExchange
231226
* @param name The name of the value you want to get
232227
* @return the value (boolean)
233228
*/
234-
public Boolean getBooleanFromBody(String name) {
235-
return Boolean.parseBoolean(getStringFromBody(name));
229+
public Boolean getBooleanFromBody(Request request, String name) {
230+
return Boolean.parseBoolean(getStringFromBody(request, name));
236231
}
237232

238233
/**
239-
* Gets an string from the query
234+
* Gets a string from the query
235+
* @param request The request object from the HttpExchange
240236
* @param name The name of the value you want to get
241237
* @return the value (string)
242238
*/
243-
public String getStringFromQuery(String name) {
239+
public String getStringFromQuery(Request request, String name) {
244240
return request.getQuery().get(name);
245241
}
246242

247243
/**
248244
* Gets an integer from the query
245+
* @param request The request object from the HttpExchange
249246
* @param name The name of the value you want to get
250247
* @return the value (integer)
251248
*/
252-
public Integer getIntegerFromQuery(String name) {
253-
String value = getStringFromQuery(name);
249+
public Integer getIntegerFromQuery(Request request, String name) {
250+
String value = getStringFromQuery(request, name);
254251
try {
255252
return Integer.parseInt(value);
256253
} catch (Exception e) {
257254
return null;
258255
}
259256
}
260257

261-
/**
262-
* Gets a boolean from the query
263-
* @param name The name of the value you want to get
264-
* @return the value (boolean)
265-
*/
266-
public Boolean getBooleanFromQuery(String name) {
267-
return Boolean.parseBoolean(getStringFromQuery(name));
268-
}
269-
270258
/**
271259
* Checks if a string is in the body
260+
* @param request The request object from the HttpExchange
261+
* @param controller The response controller from the HttpExchange
272262
* @param name The name of the value you want to check
273263
* @return <code>true</code> if the string is in the body, otherwise <code>false</code>
274264
*/
275-
public boolean isStringInBody(String name) {
276-
String value = getStringFromBody(name);
265+
public boolean isStringInBody(Request request, ResponseController controller, String name) {
266+
String value = getStringFromBody(request, name);
277267
if (value == null || value.isEmpty()) {
278268
controller.code(400).messageFormat("You need to provide %s in your request body", name);
279269
return false;
@@ -282,13 +272,15 @@ public boolean isStringInBody(String name) {
282272
}
283273

284274
/**
285-
* Checks if a integer is in the body
275+
* Checks if an integer is in the body
276+
* @param request The request object from the HttpExchange
277+
* @param controller The response controller from the HttpExchange
286278
* @param name The name of the value you want to check
287279
* @return <code>true</code> if the integer is in the body, otherwise <code>false</code>
288280
*/
289-
public boolean isIntegerInBody(String name) {
290-
if (!isStringInBody(name)) return false;
291-
Integer value = getIntegerFromBody(name);
281+
public boolean isIntegerInBody(Request request, ResponseController controller, String name) {
282+
if (!isStringInBody(request, controller, name)) return false;
283+
Integer value = getIntegerFromBody(request, name);
292284
if (value == null) {
293285
controller.code(400).messageFormat("%s must be an integer", name);
294286
}
@@ -297,12 +289,14 @@ public boolean isIntegerInBody(String name) {
297289

298290
/**
299291
* Checks if a boolean is in the body
292+
* @param request The request object from the HttpExchange
293+
* @param controller The response controller from the HttpExchange
300294
* @param name The name of the value you want to check
301295
* @return <code>true</code> if the boolean is in the body, otherwise <code>false</code>
302296
*/
303-
public boolean isBooleanInBody(String name) {
304-
if (!isStringInBody(name)) return false;
305-
String value = getStringFromBody(name);
297+
public boolean isBooleanInBody(Request request, ResponseController controller, String name) {
298+
if (!isStringInBody(request, controller, name)) return false;
299+
String value = getStringFromBody(request, name);
306300
try {
307301
if (value.equals("true") || value.equals("false")) return true;
308302
throw new Exception();
@@ -314,47 +308,18 @@ public boolean isBooleanInBody(String name) {
314308

315309
/**
316310
* Checks if a string is in the query
311+
* @param request The request object from the HttpExchange
312+
* @param controller The response controller from the HttpExchange
317313
* @param name The name of the value you want to check
318314
* @return <code>true</code> if the string is in the query, otherwise <code>false</code>
319315
*/
320-
public boolean isStringInQuery(String name) {
321-
String value = getStringFromQuery(name);
316+
public boolean isStringInQuery(Request request, ResponseController controller, String name) {
317+
String value = getStringFromQuery(request, name);
322318
if (value == null || value.isEmpty()) {
323319
controller.code(400).messageFormat("You need to provide %s in your request query", name);
324320
return false;
325321
}
326322
return true;
327323
}
328324

329-
/**
330-
* Checks if a integer is in the query
331-
* @param name The name of the value you want to check
332-
* @return <code>true</code> if the integer is in the query, otherwise <code>false</code>
333-
*/
334-
public boolean isIntegerInQuery(String name) {
335-
if (!isStringInQuery(name)) return false;
336-
Integer value = getIntegerFromQuery(name);
337-
if (value == null) {
338-
controller.code(400).messageFormat("%s must be an integer", name);
339-
}
340-
return value != null;
341-
}
342-
343-
/**
344-
* Checks if a boolean is in the query
345-
* @param name The name of the value you want to check
346-
* @return <code>true</code> if the boolean is in the query, otherwise <code>false</code>
347-
*/
348-
public Boolean isBooleanInQuery(String name) {
349-
if (!isStringInQuery(name)) return false;
350-
String value = getStringFromQuery(name);
351-
try {
352-
if (value.equals("true") || value.equals("false")) return true;
353-
throw new Exception();
354-
} catch (Exception e) {
355-
controller.code(400).messageFormat("%s must be an boolean", name);
356-
return false;
357-
}
358-
}
359-
360325
}

src/main/java/de/gnmyt/mcdash/api/http/ResponseController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public void send() {
158158
os.write(bs);
159159
}
160160
os.close();
161-
} catch (IOException e) {
161+
} catch (IOException ignored) {
162162
}
163163
}
164164

src/main/java/de/gnmyt/mcdash/panel/routes/ConsoleRoute.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ public String path() {
2424
@Override
2525
public void get(Request request, ResponseController response) throws Exception {
2626

27-
int startLine = getIntegerFromQuery("startLine") != null ? getIntegerFromQuery("startLine") : 1;
28-
int limit = getIntegerFromQuery("limit") != null ? getIntegerFromQuery("limit") : 500;
27+
int startLine = getIntegerFromQuery(request, "startLine") != null ? getIntegerFromQuery(request, "startLine") : 1;
28+
int limit = getIntegerFromQuery(request, "limit") != null ? getIntegerFromQuery(request, "limit") : 500;
2929

3030
Object[] lines = Files.lines(Paths.get("logs//latest.log"))
3131
.skip(startLine)
@@ -47,11 +47,11 @@ public void get(Request request, ResponseController response) throws Exception {
4747
*/
4848
@Override
4949
public void post(Request request, ResponseController response) {
50-
if (!isStringInBody("command")) return;
50+
if (!isStringInBody(request, response, "command")) return;
5151

52-
Bukkit.getLogger().warning("Executing command \"" + getStringFromBody("command") + "\"..");
52+
Bukkit.getLogger().warning("Executing command \"" + getStringFromBody(request, "command") + "\"..");
5353

54-
runSync(() -> Bukkit.dispatchCommand(Bukkit.getConsoleSender(), getStringFromBody("command")));
54+
runSync(() -> Bukkit.dispatchCommand(Bukkit.getConsoleSender(), getStringFromBody(request, "command")));
5555

5656
response.message("Action executed.");
5757
}

src/main/java/de/gnmyt/mcdash/panel/routes/action/WhitelistRoute.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ public void get(Request request, ResponseController response) throws Exception {
2929
*/
3030
@Override
3131
public void patch(Request request, ResponseController response) throws Exception {
32-
if (!isBooleanInBody("status")) return;
32+
if (!isBooleanInBody(request, response, "status")) return;
3333

3434
runSync(() -> {
35-
Bukkit.setWhitelist(getBooleanFromBody("status"));
36-
response.message("Whitelist successfully " + (getBooleanFromBody("status") ? "enabled" : "disabled"));
35+
Bukkit.setWhitelist(getBooleanFromBody(request, "status"));
36+
response.message("Whitelist successfully " + (getBooleanFromBody(request, "status") ? "enabled" : "disabled"));
3737
});
3838
}
3939
}

src/main/java/de/gnmyt/mcdash/panel/routes/backups/BackupDownloadRoute.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ public String path() {
2424
*/
2525
@Override
2626
public void get(Request request, ResponseController response) throws Exception {
27-
if (!isStringInQuery("backup_id")) return;
27+
if (!isStringInQuery(request, response, "backup_id")) return;
2828

29-
String backupId = getStringFromQuery("backup_id");
29+
String backupId = getStringFromQuery(request, "backup_id");
3030

3131
if (!controller.backupExists(backupId)) {
3232
response.code(404).message("Backup not found");

src/main/java/de/gnmyt/mcdash/panel/routes/backups/BackupRestoreRoute.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ public String path() {
2323
*/
2424
@Override
2525
public void post(Request request, ResponseController response) throws Exception {
26-
if (!isStringInBody("backup_id")) return;
27-
if (!isBooleanInBody("halt")) return;
26+
if (!isStringInBody(request, response, "backup_id")) return;
27+
if (!isBooleanInBody(request, response, "halt")) return;
2828

29-
String backupId = getStringFromBody("backup_id");
30-
boolean restart = getBooleanFromBody("halt");
29+
String backupId = getStringFromBody(request, "backup_id");
30+
boolean restart = getBooleanFromBody(request, "halt");
3131

3232
if (!controller.backupExists(backupId)) {
3333
response.code(404).message("Backup not found");

0 commit comments

Comments
 (0)