out UTF-8 & Update Puzzle Model

This commit is contained in:
jeffcheasey88 2023-02-14 15:00:30 +01:00
parent 1835ee6662
commit e794a93bee
4 changed files with 44 additions and 9 deletions

View file

@ -5,11 +5,17 @@ public class Puzzle {
private int id; private int id;
private String name; private String name;
private String content; 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.id = id;
this.name = name; this.name = name;
this.content = content; this.content = content;
this.soluce = soluce;
this.verify = verify;
this.scoreMax = scoreMax;
} }
public int getId() { public int getId() {
@ -36,6 +42,30 @@ public class Puzzle {
this.content = 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 @Override
public boolean equals(Object object) { public boolean equals(Object object) {
if(this == object) return true; if(this == object) return true;

View file

@ -23,7 +23,7 @@ public class DatabaseRepository {
} }
private Puzzle makePuzzle(ResultSet puzzleResult) throws SQLException { 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 { private Chapter makeChapter(ResultSet chapterResult) throws SQLException {

View file

@ -1,15 +1,17 @@
package be.jeffcheasey88.peeratcode.routes; 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.model.Puzzle;
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository; import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
import be.jeffcheasey88.peeratcode.webserver.HttpReader; import be.jeffcheasey88.peeratcode.webserver.HttpReader;
import be.jeffcheasey88.peeratcode.webserver.HttpUtil; import be.jeffcheasey88.peeratcode.webserver.HttpUtil;
import be.jeffcheasey88.peeratcode.webserver.HttpWriter; import be.jeffcheasey88.peeratcode.webserver.HttpWriter;
import be.jeffcheasey88.peeratcode.webserver.Response; 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 { public class PuzzleElement implements Response {
@ -21,14 +23,16 @@ public class PuzzleElement implements Response {
@Override @Override
public void exec(Matcher matcher, HttpReader reader, HttpWriter writer) throws Exception { 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)); Puzzle puzzle = databaseRepo.getPuzzle(extractId(matcher));
if (puzzle != null) { if (puzzle != null) {
JSONObject puzzleJSON = new JSONObject(); JSONObject puzzleJSON = new JSONObject();
puzzleJSON.put("id", puzzle.getId()); puzzleJSON.put("id", puzzle.getId());
puzzleJSON.put("name", puzzle.getName()); puzzleJSON.put("name", puzzle.getName());
puzzleJSON.put("content", puzzle.getContent()); puzzleJSON.put("content", puzzle.getContent());
writer.write(puzzleJSON.toJSONString()); ; writer.write(puzzleJSON.toJSONString());
} }
writer.flush(); writer.flush();
writer.close(); writer.close();

View file

@ -5,6 +5,7 @@ import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.net.Socket; import java.net.Socket;
import java.nio.charset.StandardCharsets;
public class HttpWriter{ public class HttpWriter{
@ -13,7 +14,7 @@ public class HttpWriter{
public HttpWriter(Socket socket) throws Exception{ public HttpWriter(Socket socket) throws Exception{
this.out = socket.getOutputStream(); 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{ public void write(byte[] buffer) throws IOException{