From e794a93bee02a6f0a86b6679cc10bc92087f5b4c Mon Sep 17 00:00:00 2001 From: jeffcheasey88 <66554203+jeffcheasey88@users.noreply.github.com> Date: Tue, 14 Feb 2023 15:00:30 +0100 Subject: [PATCH] out UTF-8 & Update Puzzle Model --- .../peeratcode/model/Puzzle.java | 32 ++++++++++++++++++- .../repository/DatabaseRepository.java | 2 +- .../peeratcode/routes/PuzzleElement.java | 16 ++++++---- .../peeratcode/webserver/HttpWriter.java | 3 +- 4 files changed, 44 insertions(+), 9 deletions(-) diff --git a/src/be/jeffcheasey88/peeratcode/model/Puzzle.java b/src/be/jeffcheasey88/peeratcode/model/Puzzle.java index 2067536..191600a 100644 --- a/src/be/jeffcheasey88/peeratcode/model/Puzzle.java +++ b/src/be/jeffcheasey88/peeratcode/model/Puzzle.java @@ -5,11 +5,17 @@ public class Puzzle { private int id; private String name; private String content; + private byte[] soluce; + private String verify; + private int scoreMax; - public Puzzle(int id, String name, String content) { + public Puzzle(int id, String name, String content, byte[] soluce, String verify, int scoreMax){ this.id = id; this.name = name; this.content = content; + this.soluce = soluce; + this.verify = verify; + this.scoreMax = scoreMax; } public int getId() { @@ -35,6 +41,30 @@ public class Puzzle { public void setContent(String content) { this.content = content; } + + public byte[] getSoluce(){ + return this.soluce; + } + + public void setSoluce(byte[] array){ + this.soluce = array; + } + + public String getVerify(){ + return this.verify; + } + + public void setVerify(String regex){ + this.verify = regex; + } + + private int getScoreMax(){ + return this.scoreMax; + } + + public void setScoreMax(int max){ + this.scoreMax = max; + } @Override public boolean equals(Object object) { diff --git a/src/be/jeffcheasey88/peeratcode/repository/DatabaseRepository.java b/src/be/jeffcheasey88/peeratcode/repository/DatabaseRepository.java index 7238553..c5133b1 100644 --- a/src/be/jeffcheasey88/peeratcode/repository/DatabaseRepository.java +++ b/src/be/jeffcheasey88/peeratcode/repository/DatabaseRepository.java @@ -23,7 +23,7 @@ public class DatabaseRepository { } private Puzzle makePuzzle(ResultSet puzzleResult) throws SQLException { - return new Puzzle(puzzleResult.getInt("id_puzzle"), puzzleResult.getString("name"), puzzleResult.getString("content")); + return new Puzzle(puzzleResult.getInt("id_puzzle"), puzzleResult.getString("name"), puzzleResult.getString("content"), null,"",0); } private Chapter makeChapter(ResultSet chapterResult) throws SQLException { diff --git a/src/be/jeffcheasey88/peeratcode/routes/PuzzleElement.java b/src/be/jeffcheasey88/peeratcode/routes/PuzzleElement.java index ec15de8..9d3ca49 100644 --- a/src/be/jeffcheasey88/peeratcode/routes/PuzzleElement.java +++ b/src/be/jeffcheasey88/peeratcode/routes/PuzzleElement.java @@ -1,15 +1,17 @@ package be.jeffcheasey88.peeratcode.routes; +import java.nio.charset.Charset; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.json.simple.JSONObject; + import be.jeffcheasey88.peeratcode.model.Puzzle; import be.jeffcheasey88.peeratcode.repository.DatabaseRepository; import be.jeffcheasey88.peeratcode.webserver.HttpReader; import be.jeffcheasey88.peeratcode.webserver.HttpUtil; import be.jeffcheasey88.peeratcode.webserver.HttpWriter; import be.jeffcheasey88.peeratcode.webserver.Response; -import org.json.simple.JSONObject; - -import java.util.regex.Matcher; -import java.util.regex.Pattern; public class PuzzleElement implements Response { @@ -21,14 +23,16 @@ public class PuzzleElement implements Response { @Override public void exec(Matcher matcher, HttpReader reader, HttpWriter writer) throws Exception { - HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *"); + HttpUtil.responseHeaders(writer, 200, + "Access-Control-Allow-Origin: *", + "Content-Type: application/json"); Puzzle puzzle = databaseRepo.getPuzzle(extractId(matcher)); if (puzzle != null) { JSONObject puzzleJSON = new JSONObject(); puzzleJSON.put("id", puzzle.getId()); puzzleJSON.put("name", puzzle.getName()); puzzleJSON.put("content", puzzle.getContent()); - writer.write(puzzleJSON.toJSONString()); +; writer.write(puzzleJSON.toJSONString()); } writer.flush(); writer.close(); diff --git a/src/be/jeffcheasey88/peeratcode/webserver/HttpWriter.java b/src/be/jeffcheasey88/peeratcode/webserver/HttpWriter.java index 116647c..5546719 100644 --- a/src/be/jeffcheasey88/peeratcode/webserver/HttpWriter.java +++ b/src/be/jeffcheasey88/peeratcode/webserver/HttpWriter.java @@ -5,6 +5,7 @@ import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.Socket; +import java.nio.charset.StandardCharsets; public class HttpWriter{ @@ -13,7 +14,7 @@ public class HttpWriter{ public HttpWriter(Socket socket) throws Exception{ this.out = socket.getOutputStream(); - this.writer = new BufferedWriter(new OutputStreamWriter(out)); + this.writer = new BufferedWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8)); } public void write(byte[] buffer) throws IOException{