From eac490f040977430eb3e626b9afc0470f4e2b956 Mon Sep 17 00:00:00 2001 From: jeffcheasey88 <66554203+jeffcheasey88@users.noreply.github.com> Date: Sun, 9 Apr 2023 23:03:08 +0200 Subject: [PATCH] Little edit --- src/be/jeffcheasey88/peeratcode/Main.java | 4 +-- .../peeratcode/framework/Router.java | 19 +++++++------- .../peeratcode/routes/PuzzleResponse.java | 25 ++++++++----------- 3 files changed, 21 insertions(+), 27 deletions(-) diff --git a/src/be/jeffcheasey88/peeratcode/Main.java b/src/be/jeffcheasey88/peeratcode/Main.java index 587b1de..9b377ed 100644 --- a/src/be/jeffcheasey88/peeratcode/Main.java +++ b/src/be/jeffcheasey88/peeratcode/Main.java @@ -34,8 +34,8 @@ import be.jeffcheasey88.peeratcode.routes.groups.GroupJoin; import be.jeffcheasey88.peeratcode.routes.groups.GroupList; import be.jeffcheasey88.peeratcode.routes.groups.GroupQuit; -public class Main { - public static void main(String[] args) throws Exception { +public class Main{ + public static void main(String[] args) throws Exception{ Configuration config = new Configuration("config.txt"); config.load(); diff --git a/src/be/jeffcheasey88/peeratcode/framework/Router.java b/src/be/jeffcheasey88/peeratcode/framework/Router.java index 669c57e..5167028 100644 --- a/src/be/jeffcheasey88/peeratcode/framework/Router.java +++ b/src/be/jeffcheasey88/peeratcode/framework/Router.java @@ -18,7 +18,7 @@ import be.jeffcheasey88.peeratcode.repository.DatabaseRepository; public class Router{ - private Map responses; + private Map> responses; private Map patterns; private Response noFileFound; private RsaJsonWebKey rsaJsonWebKey; @@ -31,6 +31,7 @@ public class Router{ this.token_issuer = token_issuer; this.token_expiration = token_expiration; this.responses = new HashMap<>(); + for(RequestType type : RequestType.values()) this.responses.put(type, new HashMap<>()); this.patterns = new HashMap<>(); this.rsaJsonWebKey = RsaJwkGenerator.generateJwk(2048); } @@ -45,7 +46,7 @@ public class Router{ Response.class.getDeclaredMethods()[0].getParameterTypes()); Route route = method.getAnnotation(Route.class); - this.responses.put(response, route); + this.responses.get(route.type()).put(response, route); this.patterns.put(response, Pattern.compile(route.path())); }catch(Exception e){ throw new IllegalArgumentException(e); @@ -58,14 +59,12 @@ public class Router{ public void exec(RequestType type, String path, User user, HttpReader reader, HttpWriter writer) throws Exception{ if(type == null) return; - for(Entry routes : this.responses.entrySet()){ - if(routes.getValue().type().equals(type)){ - Matcher matcher = this.patterns.get(routes.getKey()).matcher(path); - if(matcher.matches()){ - if(user == null && routes.getValue().needLogin()) return; - routes.getKey().exec(matcher, user, reader, writer); - return; - } + for(Entry routes : this.responses.get(type).entrySet()){ + Matcher matcher = this.patterns.get(routes.getKey()).matcher(path); + if(matcher.matches()){ + if(user == null && routes.getValue().needLogin()) return; + routes.getKey().exec(matcher, user, reader, writer); + return; } } if(noFileFound != null) noFileFound.exec(null, user, reader, writer); diff --git a/src/be/jeffcheasey88/peeratcode/routes/PuzzleResponse.java b/src/be/jeffcheasey88/peeratcode/routes/PuzzleResponse.java index fe4ae46..2872ee6 100644 --- a/src/be/jeffcheasey88/peeratcode/routes/PuzzleResponse.java +++ b/src/be/jeffcheasey88/peeratcode/routes/PuzzleResponse.java @@ -6,7 +6,6 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.Arrays; import java.util.List; import java.util.regex.Matcher; @@ -33,12 +32,7 @@ public class PuzzleResponse implements Response { } @Route(path = "^\\/puzzleResponse\\/([0-9]+)$", type = POST, needLogin = true) - public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception { - if (user == null) { - HttpUtil.responseHeaders(writer, 401, "Access-Control-Allow-Origin: *"); - return; - } - + public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception{ ReceivedResponse received = new ReceivedResponse(matcher, reader); saveSourceCode(received, databaseRepo.getPlayer(user.getId())); @@ -46,27 +40,28 @@ public class PuzzleResponse implements Response { Puzzle currentPuzzle = databaseRepo.getPuzzle(received.getPuzzleId()); Completion completion = databaseRepo.insertOrUpdatePuzzleResponse(received.getPuzzleId(), user.getId(), received.getFileName(), received.getSourceCode(), received.getResponse(), currentPuzzle); - if (completion.getScore() > 0) { + if(completion == null){ + HttpUtil.responseHeaders(writer, 400, "Access-Control-Allow-Origin: *"); + return; + } + if(completion.getScore() > 0){ HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *", "Content-Type: application/json"); responseJSON.put("score", completion.getScore()); responseJSON.put("tries", completion.getTries()); - } else if (completion != null) { + }else{ HttpUtil.responseHeaders(writer, 406, "Access-Control-Allow-Origin: *", "Content-Type: application/json"); responseJSON.put("tries", completion.getTries()); - } else { - HttpUtil.responseHeaders(writer, 400, "Access-Control-Allow-Origin: *"); - return; } writer.write(responseJSON.toJSONString()); } - private void saveSourceCode(ReceivedResponse received, Player player) { - try { + private void saveSourceCode(ReceivedResponse received, Player player){ + try{ Path path = Paths.get(String.format("%s/%s/puz%04d-%s", usersFilesPath, player.getPseudo(), received.getPuzzleId(), received.getFileName())); Files.write(path, received.getSourceCode()); - } catch (IOException e) { + }catch (IOException e){ System.err.println("ERREUR D'ENREGISTREMENT DU CODE POUR PUZZLE_RESPONSE : " + e.toString()); }