1616import java .nio .charset .StandardCharsets ;
1717import java .util .Base64 ;
1818import java .util .List ;
19- import java .util .concurrent .CompletableFuture ;
2019
2120public 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}
0 commit comments