diff --git a/src/be/jeffcheasey88/peeratcode/Main.java b/src/be/jeffcheasey88/peeratcode/Main.java index f411d35..3cf0bfc 100644 --- a/src/be/jeffcheasey88/peeratcode/Main.java +++ b/src/be/jeffcheasey88/peeratcode/Main.java @@ -5,8 +5,6 @@ import static be.jeffcheasey88.peeratcode.framework.RequestType.OPTIONS; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; -import java.util.ArrayList; -import java.util.List; import java.util.regex.Matcher; import javax.net.ssl.SSLServerSocket; @@ -14,7 +12,6 @@ import javax.net.ssl.SSLServerSocketFactory; import be.jeffcheasey88.peeratcode.framework.Client; import be.jeffcheasey88.peeratcode.framework.HttpReader; -import be.jeffcheasey88.peeratcode.framework.HttpUtil; import be.jeffcheasey88.peeratcode.framework.HttpWriter; import be.jeffcheasey88.peeratcode.framework.Locker; import be.jeffcheasey88.peeratcode.framework.Response; @@ -51,7 +48,7 @@ public class Main{ config.getTokenExpiration()); router.setDefault((matcher, user, reader, writer) -> { - HttpUtil.responseHeaders(writer, 404, "Access-Control-Allow-Origin: *"); + writer.response(404, "Access-Control-Allow-Origin: *"); writer.write("404 not Found.\n"); writer.flush(); writer.close(); @@ -61,7 +58,7 @@ public class Main{ @Route(path = "^(.*)$", type = OPTIONS) @Override public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception { - HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *", + writer.response(200, "Access-Control-Allow-Origin: *", "Access-Control-Allow-Methods: *", "Access-Control-Allow-Headers: *"); } }); diff --git a/src/be/jeffcheasey88/peeratcode/framework/Client.java b/src/be/jeffcheasey88/peeratcode/framework/Client.java index ab2bb18..4c4da80 100644 --- a/src/be/jeffcheasey88/peeratcode/framework/Client.java +++ b/src/be/jeffcheasey88/peeratcode/framework/Client.java @@ -23,13 +23,12 @@ public class Client extends Thread{ @Override public void run(){ - User user = null; - try { + try{ String[] headers = reader.readLine().split("\\s"); System.out.println(Arrays.toString(headers)); reader.readHeaders(); - router.exec(RequestType.valueOf(headers[0]), headers[1], user = isLogin(reader), reader, writer); + router.exec(RequestType.valueOf(headers[0]), headers[1], isLogin(reader), reader, writer); writer.flush(); writer.close(); }catch(Exception e){ @@ -52,7 +51,7 @@ public class Client extends Thread{ JwtClaims jwtClaims = jwtConsumer.processToClaims(auth); return new User(jwtClaims); }catch(Exception e){ - HttpUtil.responseHeaders(writer, 401, "Access-Control-Allow-Origin: *"); + writer.response(401, "Access-Control-Allow-Origin: *"); writer.flush(); writer.close(); throw e; diff --git a/src/be/jeffcheasey88/peeratcode/framework/HttpReader.java b/src/be/jeffcheasey88/peeratcode/framework/HttpReader.java index bd148a2..7239e0e 100644 --- a/src/be/jeffcheasey88/peeratcode/framework/HttpReader.java +++ b/src/be/jeffcheasey88/peeratcode/framework/HttpReader.java @@ -5,13 +5,16 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.Socket; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; -import java.util.Map.Entry; import java.util.regex.Matcher; import java.util.regex.Pattern; -public class HttpReader { +import org.json.simple.parser.JSONParser; + +public class HttpReader{ private static Pattern HEADER_PATTERN = Pattern.compile("^([^:]*):\\s+(.*)$"); @@ -34,7 +37,7 @@ public class HttpReader { this.reader = origin.reader; } - public boolean isClosed() { + public boolean isClosed(){ return this.socket.isClosed(); } @@ -51,23 +54,23 @@ public class HttpReader { return this.headers.get(key.toLowerCase()); } - public int read(byte[] buffer) throws IOException { + public int read(byte[] buffer) throws IOException{ return this.in.read(buffer); } - public int read(char[] buffer) throws IOException { + public int read(char[] buffer) throws IOException{ return this.reader.read(buffer); } - public String readLine() throws IOException { + public String readLine() throws IOException{ return this.reader.readLine(); } - public boolean ready() throws IOException { + public boolean ready() throws IOException{ return this.reader.ready(); } - public int readInt() throws Exception { + public int readInt() throws Exception{ int result = 0; result += this.in.read() << 24; result += this.in.read() << 16; @@ -75,4 +78,61 @@ public class HttpReader { result += this.in.read(); return result; } + + public T readJson() throws Exception{ + String line = ""; + while (ready()){ + char[] c = new char[1]; + read(c); + line += c[0]; + if (c[0] == '}'){ + Object parse; + try { + parse = new JSONParser().parse(line); + if (parse != null) + return (T) parse; + }catch(Exception e){} + } + } + return null; + } + + /* + * +------WebKitFormBoundaryNUjiLBAMuX2dhxU7 +Content-Disposition: form-data; name="answer" + +12 +------WebKitFormBoundaryNUjiLBAMuX2dhxU7 +Content-Disposition: form-data; name="filename" + +webpack.js +------WebKitFormBoundaryNUjiLBAMuX2dhxU7 +Content-Disposition: form-data; name="code_file"; filename="webpack.js" +Content-Type: text/javascript + + +------WebKitFormBoundaryNUjiLBAMuX2dhxU7-- + * + */ + + public List readMultiPartData() throws Exception{ + List list = new ArrayList<>(); + String boundary = getHeader("content-type"); + if(boundary == null) return list; + boundary = boundary.split(";")[1].split("=")[1]; + readLine(); + + while(ready()){ + String line; + while (((line = readLine()) != null) && (line.length() > 0)); + String buffer = ""; + while (((line = readLine()) != null) && (!line.startsWith("--"+boundary))){ + buffer += line; + } + list.add(buffer); + } + + return list; + } } \ No newline at end of file diff --git a/src/be/jeffcheasey88/peeratcode/framework/HttpUtil.java b/src/be/jeffcheasey88/peeratcode/framework/HttpUtil.java deleted file mode 100644 index 3949cd1..0000000 --- a/src/be/jeffcheasey88/peeratcode/framework/HttpUtil.java +++ /dev/null @@ -1,228 +0,0 @@ -package be.jeffcheasey88.peeratcode.framework; - -import java.security.MessageDigest; -import java.util.ArrayList; -import java.util.List; - -import org.json.simple.parser.JSONParser; - -public class HttpUtil{ - - private HttpUtil(){} - - public static void responseHeaders(HttpWriter writer, int code, String... headers) throws Exception{ - writer.write("HTTP/1.1 "+code+codeMessage(code)+"\n"); - for(String header : headers) writer.write(header+"\n"); - writer.write("\n"); - writer.flush(); - StackTraceElement[] e = Thread.currentThread().getStackTrace(); - System.out.println(e[2]+" -> response "+code); - } - - public static List readMultiPartData(HttpReader reader) throws Exception{ - List list = new ArrayList<>(); - System.out.println("read it"); - String boundary = reader.getHeader("content-type"); - if(boundary == null) return list; - boundary = boundary.split(";")[1].split("=")[1]; - System.out.println("boundary "+boundary); - reader.readLine(); - - while(reader.ready()){ - String line; - while (((line = reader.readLine()) != null) && (line.length() > 0)); - String buffer = ""; - while (((line = reader.readLine()) != null) && (!line.startsWith("--"+boundary))){ - buffer += line; - } - list.add(buffer); - } - - return list; - } - - /* - * -------WebKitFormBoundaryNUjiLBAMuX2dhxU7 -Content-Disposition: form-data; name="answer" - -12 -------WebKitFormBoundaryNUjiLBAMuX2dhxU7 -Content-Disposition: form-data; name="filename" - -webpack.js -------WebKitFormBoundaryNUjiLBAMuX2dhxU7 -Content-Disposition: form-data; name="code_file"; filename="webpack.js" -Content-Type: text/javascript - - -------WebKitFormBoundaryNUjiLBAMuX2dhxU7-- - * - */ - - public static void switchToWebSocket(HttpReader reader, HttpWriter writer) throws Exception{ - String key = reader.getHeader("Sec-WebSocket-Key"); - if (key == null) throw new IllegalArgumentException(); - - writer.write("HTTP/1.1 101 Switching Protocols\n"); - writer.write("Connection: Upgrade\n"); - writer.write("Upgrade: websocket\n"); - writer.write("Sec-WebSocket-Accept: " + printBase64Binary(MessageDigest.getInstance("SHA-1") - .digest((key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11").getBytes("UTF-8"))) + "\n"); - writer.write("\n"); - writer.flush(); - } - - public static Object readJson(HttpReader reader) throws Exception{ - String line = ""; - while (reader.ready()){ - char[] c = new char[1]; - reader.read(c); - line += c[0]; - if (c[0] == '}'){ - Object parse; - try { - parse = new JSONParser().parse(line); - if (parse != null) - return parse; - }catch(Exception e){} - } - } - return null; - } - - private static String codeMessage(int paramInt){ - switch (paramInt){ - case 200: - return " OK"; - case 100: - return " Continue"; - case 201: - return " Created"; - case 202: - return " Accepted"; - case 203: - return " Non-Authoritative Information"; - case 204: - return " No Content"; - case 205: - return " Reset Content"; - case 206: - return " Partial Content"; - case 300: - return " Multiple Choices"; - case 301: - return " Moved Permanently"; - case 302: - return " Temporary Redirect"; - case 303: - return " See Other"; - case 304: - return " Not Modified"; - case 305: - return " Use Proxy"; - case 400: - return " Bad Request"; - case 401: - return " Unauthorized"; - case 402: - return " Payment Required"; - case 403: - return " Forbidden"; - case 404: - return " Not Found"; - case 405: - return " Method Not Allowed"; - case 406: - return " Not Acceptable"; - case 407: - return " Proxy Authentication Required"; - case 408: - return " Request Time-Out"; - case 409: - return " Conflict"; - case 410: - return " Gone"; - case 411: - return " Length Required"; - case 412: - return " Precondition Failed"; - case 413: - return " Request Entity Too Large"; - case 414: - return " Request-URI Too Large"; - case 415: - return " Unsupported Media Type"; - case 500: - return " Internal Server Error"; - case 501: - return " Not Implemented"; - case 502: - return " Bad Gateway"; - case 503: - return " Service Unavailable"; - case 504: - return " Gateway Timeout"; - case 505: - return " HTTP Version Not Supported"; - } - return " "; - } - - // From javax.xml.bind.DatatypeConverter - private static String printBase64Binary(byte[] array){ - char[] arrayOfChar = new char[(array.length + 2) / 3 * 4]; - int i = _printBase64Binary(array, 0, array.length, arrayOfChar, 0); - assert i == arrayOfChar.length; - return new String(arrayOfChar); - } - - private static int _printBase64Binary(byte[] paramArrayOfbyte, int paramInt1, int paramInt2, - char[] paramArrayOfchar, int paramInt3){ - int i = paramInt2; - int j; - for (j = paramInt1; i >= 3; j += 3){ - paramArrayOfchar[paramInt3++] = encode(paramArrayOfbyte[j] >> 2); - paramArrayOfchar[paramInt3++] = encode( - (paramArrayOfbyte[j] & 0x3) << 4 | paramArrayOfbyte[j + 1] >> 4 & 0xF); - paramArrayOfchar[paramInt3++] = encode( - (paramArrayOfbyte[j + 1] & 0xF) << 2 | paramArrayOfbyte[j + 2] >> 6 & 0x3); - paramArrayOfchar[paramInt3++] = encode(paramArrayOfbyte[j + 2] & 0x3F); - i -= 3; - } - if (i == 1){ - paramArrayOfchar[paramInt3++] = encode(paramArrayOfbyte[j] >> 2); - paramArrayOfchar[paramInt3++] = encode((paramArrayOfbyte[j] & 0x3) << 4); - paramArrayOfchar[paramInt3++] = '='; - paramArrayOfchar[paramInt3++] = '='; - } - if (i == 2){ - paramArrayOfchar[paramInt3++] = encode(paramArrayOfbyte[j] >> 2); - paramArrayOfchar[paramInt3++] = encode( - (paramArrayOfbyte[j] & 0x3) << 4 | paramArrayOfbyte[j + 1] >> 4 & 0xF); - paramArrayOfchar[paramInt3++] = encode((paramArrayOfbyte[j + 1] & 0xF) << 2); - paramArrayOfchar[paramInt3++] = '='; - } - return paramInt3; - } - - private static char encode(int paramInt){ - return encodeMap[paramInt & 0x3F]; - } - - private static final char[] encodeMap = initEncodeMap(); - - private static char[] initEncodeMap(){ - char[] arrayOfChar = new char[64]; - byte b; - for (b = 0; b < 26; b++) - arrayOfChar[b] = (char) (65 + b); - for (b = 26; b < 52; b++) - arrayOfChar[b] = (char) (97 + b - 26); - for (b = 52; b < 62; b++) - arrayOfChar[b] = (char) (48 + b - 52); - arrayOfChar[62] = '+'; - arrayOfChar[63] = '/'; - return arrayOfChar; - } -} diff --git a/src/be/jeffcheasey88/peeratcode/framework/HttpWriter.java b/src/be/jeffcheasey88/peeratcode/framework/HttpWriter.java index 471c5de..5e2f080 100644 --- a/src/be/jeffcheasey88/peeratcode/framework/HttpWriter.java +++ b/src/be/jeffcheasey88/peeratcode/framework/HttpWriter.java @@ -38,4 +38,64 @@ public class HttpWriter{ public void close() throws IOException{ this.writer.close(); } + + public void response(int code, String... headers) throws Exception{ + write("HTTP/1.1 "+code+codeMessage(code)+"\n"); + for(String header : headers) write(header+"\n"); + write("\n"); + flush(); + StackTraceElement[] e = Thread.currentThread().getStackTrace(); + System.out.println(e[2]+" -> response "+code); + } + + private static String[] HEIGHTBITS = new String[7]; + private static String[] NINEBITS = new String[206]; + + static { + HEIGHTBITS[0] = " OK"; + HEIGHTBITS[1] = " Created"; + HEIGHTBITS[2] = " Accepted"; + HEIGHTBITS[3] = " Non-Authoritative Information"; + HEIGHTBITS[4] = " No Content"; + HEIGHTBITS[5] = " Reset Content"; + HEIGHTBITS[6] = " Partial Content"; + + NINEBITS[0] = " Multiple Choices"; + NINEBITS[1] = " Moved Permanently"; + NINEBITS[2] = " Temporary Redirect"; + NINEBITS[3] = " See Other"; + NINEBITS[4] = " Not Modified"; + NINEBITS[5] = " Use Proxy"; + + NINEBITS[100] = " Bad Request"; + NINEBITS[101] = " Unauthorized"; + NINEBITS[102] = " Payment Required"; + NINEBITS[103] = " Forbidden"; + NINEBITS[104] = " Not Found"; + NINEBITS[105] = " Method Not Allowed"; + NINEBITS[106] = " Not Acceptable"; + NINEBITS[107] = " Proxy Authentication Required"; + NINEBITS[108] = " Request Time-Out"; + NINEBITS[109] = " Conflict"; + NINEBITS[110] = " Gone"; + NINEBITS[111] = " Length Required"; + NINEBITS[112] = " Precondition Failed"; + NINEBITS[113] = " Request Entity Too Large"; + NINEBITS[114] = " Request-URI Too Large"; + NINEBITS[115] = " Unsupported Media Type"; + + NINEBITS[200] = " Internal Server Error"; + NINEBITS[201] = " Not Implemented"; + NINEBITS[202] = " Bad Gateway"; + NINEBITS[203] = " Service Unavailable"; + NINEBITS[204] = " Gateway Timeout"; + NINEBITS[205] = " HTTP Version Not Supported"; + } + + private static String codeMessage(int code){ + if(code == 100) return " Continue"; + if(code >> 8 == 0) return HEIGHTBITS[code-200]; + return NINEBITS[code-300]; + } + } \ No newline at end of file diff --git a/src/be/jeffcheasey88/peeratcode/framework/RequestType.java b/src/be/jeffcheasey88/peeratcode/framework/RequestType.java index 8262f9c..f843ef6 100644 --- a/src/be/jeffcheasey88/peeratcode/framework/RequestType.java +++ b/src/be/jeffcheasey88/peeratcode/framework/RequestType.java @@ -1,6 +1,6 @@ package be.jeffcheasey88.peeratcode.framework; -public enum RequestType { +public enum RequestType{ GET, POST, OPTIONS; diff --git a/src/be/jeffcheasey88/peeratcode/framework/Response.java b/src/be/jeffcheasey88/peeratcode/framework/Response.java index 033a6c1..392c6f3 100644 --- a/src/be/jeffcheasey88/peeratcode/framework/Response.java +++ b/src/be/jeffcheasey88/peeratcode/framework/Response.java @@ -2,7 +2,7 @@ package be.jeffcheasey88.peeratcode.framework; import java.util.regex.Matcher; -public interface Response { +public interface Response{ void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception; diff --git a/src/be/jeffcheasey88/peeratcode/framework/Router.java b/src/be/jeffcheasey88/peeratcode/framework/Router.java index 50d8f3c..432bb5d 100644 --- a/src/be/jeffcheasey88/peeratcode/framework/Router.java +++ b/src/be/jeffcheasey88/peeratcode/framework/Router.java @@ -1,6 +1,7 @@ package be.jeffcheasey88.peeratcode.framework; import java.lang.reflect.Method; +import java.security.MessageDigest; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; @@ -63,11 +64,11 @@ public class Router{ Matcher matcher = this.patterns.get(routes.getKey()).matcher(path); if(matcher.matches()){ if(user == null && routes.getValue().needLogin()){ - HttpUtil.responseHeaders(writer, 401, "Access-Control-Allow-Origin: *"); + writer.response(401, "Access-Control-Allow-Origin: *"); return; } if(routes.getValue().websocket()){ - HttpUtil.switchToWebSocket(reader, writer); + switchToWebSocket(reader, writer); reader = new WebSocketReader(reader); writer = new WebSocketWriter(writer); } @@ -103,4 +104,75 @@ public class Router{ jws.setAlgorithmHeaderValue(AlgorithmIdentifiers.RSA_USING_SHA256); return jws.getCompactSerialization(); } + + private void switchToWebSocket(HttpReader reader, HttpWriter writer) throws Exception{ + String key = reader.getHeader("Sec-WebSocket-Key"); + if (key == null) throw new IllegalArgumentException(); + + writer.write("HTTP/1.1 101 Switching Protocols\n"); + writer.write("Connection: Upgrade\n"); + writer.write("Upgrade: websocket\n"); + writer.write("Sec-WebSocket-Accept: " + printBase64Binary(MessageDigest.getInstance("SHA-1") + .digest((key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11").getBytes("UTF-8"))) + "\n"); + writer.write("\n"); + writer.flush(); + } + + + // From javax.xml.bind.DatatypeConverter + private String printBase64Binary(byte[] array){ + char[] arrayOfChar = new char[(array.length + 2) / 3 * 4]; + int i = _printBase64Binary(array, 0, array.length, arrayOfChar, 0); + assert i == arrayOfChar.length; + return new String(arrayOfChar); + } + + private int _printBase64Binary(byte[] paramArrayOfbyte, int paramInt1, int paramInt2, + char[] paramArrayOfchar, int paramInt3){ + int i = paramInt2; + int j; + for (j = paramInt1; i >= 3; j += 3){ + paramArrayOfchar[paramInt3++] = encode(paramArrayOfbyte[j] >> 2); + paramArrayOfchar[paramInt3++] = encode( + (paramArrayOfbyte[j] & 0x3) << 4 | paramArrayOfbyte[j + 1] >> 4 & 0xF); + paramArrayOfchar[paramInt3++] = encode( + (paramArrayOfbyte[j + 1] & 0xF) << 2 | paramArrayOfbyte[j + 2] >> 6 & 0x3); + paramArrayOfchar[paramInt3++] = encode(paramArrayOfbyte[j + 2] & 0x3F); + i -= 3; + } + if (i == 1){ + paramArrayOfchar[paramInt3++] = encode(paramArrayOfbyte[j] >> 2); + paramArrayOfchar[paramInt3++] = encode((paramArrayOfbyte[j] & 0x3) << 4); + paramArrayOfchar[paramInt3++] = '='; + paramArrayOfchar[paramInt3++] = '='; + } + if (i == 2){ + paramArrayOfchar[paramInt3++] = encode(paramArrayOfbyte[j] >> 2); + paramArrayOfchar[paramInt3++] = encode( + (paramArrayOfbyte[j] & 0x3) << 4 | paramArrayOfbyte[j + 1] >> 4 & 0xF); + paramArrayOfchar[paramInt3++] = encode((paramArrayOfbyte[j + 1] & 0xF) << 2); + paramArrayOfchar[paramInt3++] = '='; + } + return paramInt3; + } + + private char encode(int paramInt){ + return encodeMap[paramInt & 0x3F]; + } + + private static final char[] encodeMap = initEncodeMap(); + + private static char[] initEncodeMap(){ + char[] arrayOfChar = new char[64]; + byte b; + for (b = 0; b < 26; b++) + arrayOfChar[b] = (char) (65 + b); + for (b = 26; b < 52; b++) + arrayOfChar[b] = (char) (97 + b - 26); + for (b = 52; b < 62; b++) + arrayOfChar[b] = (char) (48 + b - 52); + arrayOfChar[62] = '+'; + arrayOfChar[63] = '/'; + return arrayOfChar; + } } \ No newline at end of file diff --git a/src/be/jeffcheasey88/peeratcode/framework/WebSocketReader.java b/src/be/jeffcheasey88/peeratcode/framework/WebSocketReader.java index 20dc9a3..7007402 100644 --- a/src/be/jeffcheasey88/peeratcode/framework/WebSocketReader.java +++ b/src/be/jeffcheasey88/peeratcode/framework/WebSocketReader.java @@ -4,7 +4,7 @@ import java.io.IOException; public class WebSocketReader extends HttpReader{ - public WebSocketReader(HttpReader origin) throws Exception { + public WebSocketReader(HttpReader origin) throws Exception{ super(origin); } diff --git a/src/be/jeffcheasey88/peeratcode/framework/WebSocketWriter.java b/src/be/jeffcheasey88/peeratcode/framework/WebSocketWriter.java index e3fb617..ac093f0 100644 --- a/src/be/jeffcheasey88/peeratcode/framework/WebSocketWriter.java +++ b/src/be/jeffcheasey88/peeratcode/framework/WebSocketWriter.java @@ -4,7 +4,7 @@ import java.io.IOException; public class WebSocketWriter extends HttpWriter{ - public WebSocketWriter(HttpWriter origin) throws Exception { + public WebSocketWriter(HttpWriter origin) throws Exception{ super(origin); } diff --git a/src/be/jeffcheasey88/peeratcode/routes/BadgeDetails.java b/src/be/jeffcheasey88/peeratcode/routes/BadgeDetails.java index a216a85..b96c561 100644 --- a/src/be/jeffcheasey88/peeratcode/routes/BadgeDetails.java +++ b/src/be/jeffcheasey88/peeratcode/routes/BadgeDetails.java @@ -7,7 +7,6 @@ import org.json.simple.JSONObject; import be.jeffcheasey88.peeratcode.bonus.extract.RouteDoc; import be.jeffcheasey88.peeratcode.framework.HttpReader; -import be.jeffcheasey88.peeratcode.framework.HttpUtil; import be.jeffcheasey88.peeratcode.framework.HttpWriter; import be.jeffcheasey88.peeratcode.framework.Response; import be.jeffcheasey88.peeratcode.framework.Route; @@ -38,10 +37,10 @@ public class BadgeDetails implements Response { badgeJSON.put("logo", Base64.getEncoder().encodeToString(badge.getLogo())); badgeJSON.put("level", badge.getLevel()); } - HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *"); + writer.response(200, "Access-Control-Allow-Origin: *"); writer.write(badgeJSON.toJSONString().replace("\\", "")); } else { - HttpUtil.responseHeaders(writer, 400, "Access-Control-Allow-Origin: *"); + writer.response(400, "Access-Control-Allow-Origin: *"); } } diff --git a/src/be/jeffcheasey88/peeratcode/routes/ChapterElement.java b/src/be/jeffcheasey88/peeratcode/routes/ChapterElement.java index f08bd38..22a44f1 100644 --- a/src/be/jeffcheasey88/peeratcode/routes/ChapterElement.java +++ b/src/be/jeffcheasey88/peeratcode/routes/ChapterElement.java @@ -7,7 +7,6 @@ import org.json.simple.JSONObject; import be.jeffcheasey88.peeratcode.bonus.extract.RouteDoc; import be.jeffcheasey88.peeratcode.framework.HttpReader; -import be.jeffcheasey88.peeratcode.framework.HttpUtil; import be.jeffcheasey88.peeratcode.framework.HttpWriter; import be.jeffcheasey88.peeratcode.framework.Response; import be.jeffcheasey88.peeratcode.framework.Route; @@ -50,10 +49,10 @@ public class ChapterElement implements Response { puzzles.add(puzzleJSON); } chapterJSON.put("puzzles", puzzles); - HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *"); + writer.response(200, "Access-Control-Allow-Origin: *"); writer.write(chapterJSON.toJSONString()); } else { - HttpUtil.responseHeaders(writer, 400, "Access-Control-Allow-Origin: *"); + writer.response(400, "Access-Control-Allow-Origin: *"); } } diff --git a/src/be/jeffcheasey88/peeratcode/routes/ChapterList.java b/src/be/jeffcheasey88/peeratcode/routes/ChapterList.java index 72534a0..e7a7420 100644 --- a/src/be/jeffcheasey88/peeratcode/routes/ChapterList.java +++ b/src/be/jeffcheasey88/peeratcode/routes/ChapterList.java @@ -8,7 +8,6 @@ import org.json.simple.JSONObject; import be.jeffcheasey88.peeratcode.bonus.extract.RouteDoc; import be.jeffcheasey88.peeratcode.framework.HttpReader; -import be.jeffcheasey88.peeratcode.framework.HttpUtil; import be.jeffcheasey88.peeratcode.framework.HttpWriter; import be.jeffcheasey88.peeratcode.framework.Response; import be.jeffcheasey88.peeratcode.framework.Route; @@ -42,10 +41,10 @@ public class ChapterList implements Response { chapterJSON.put("endDate", chapter.getEndDate().toString()); chaptersJSON.add(chapterJSON); } - HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *"); + writer.response(200, "Access-Control-Allow-Origin: *"); writer.write(chaptersJSON.toJSONString()); } else { - HttpUtil.responseHeaders(writer, 400, "Access-Control-Allow-Origin: *"); + writer.response(400, "Access-Control-Allow-Origin: *"); } } diff --git a/src/be/jeffcheasey88/peeratcode/routes/Leaderboard.java b/src/be/jeffcheasey88/peeratcode/routes/Leaderboard.java index 211e254..4749058 100644 --- a/src/be/jeffcheasey88/peeratcode/routes/Leaderboard.java +++ b/src/be/jeffcheasey88/peeratcode/routes/Leaderboard.java @@ -9,7 +9,6 @@ import org.json.simple.JSONObject; import be.jeffcheasey88.peeratcode.bonus.extract.RouteDoc; import be.jeffcheasey88.peeratcode.framework.HttpReader; -import be.jeffcheasey88.peeratcode.framework.HttpUtil; import be.jeffcheasey88.peeratcode.framework.HttpWriter; import be.jeffcheasey88.peeratcode.framework.Response; import be.jeffcheasey88.peeratcode.framework.Route; @@ -31,7 +30,7 @@ public class Leaderboard implements Response { @Route(path = "^\\/leaderboard\\/?(\\d+)?$") public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception { - HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *"); + writer.response(200, "Access-Control-Allow-Origin: *"); if (matcher.group(1) != null) { groupsLeaderboard(Integer.parseInt(matcher.group(1)), writer); } else { diff --git a/src/be/jeffcheasey88/peeratcode/routes/Login.java b/src/be/jeffcheasey88/peeratcode/routes/Login.java index e2db12c..378cf3a 100644 --- a/src/be/jeffcheasey88/peeratcode/routes/Login.java +++ b/src/be/jeffcheasey88/peeratcode/routes/Login.java @@ -8,7 +8,6 @@ import org.json.simple.JSONObject; import be.jeffcheasey88.peeratcode.bonus.extract.RouteDoc; import be.jeffcheasey88.peeratcode.framework.HttpReader; -import be.jeffcheasey88.peeratcode.framework.HttpUtil; import be.jeffcheasey88.peeratcode.framework.HttpWriter; import be.jeffcheasey88.peeratcode.framework.Response; import be.jeffcheasey88.peeratcode.framework.Route; @@ -33,22 +32,22 @@ public class Login implements Response { @Route(path = "^\\/login$", type = POST) public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception { if (user != null) { - HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *"); + writer.response(403, "Access-Control-Allow-Origin: *"); return; } - JSONObject informations = (JSONObject) HttpUtil.readJson(reader); + JSONObject informations = reader.readJson(); if (informations != null) { String pseudo = (String) informations.get("pseudo"); String password = (String) informations.get("passwd"); int id; if ((id = databaseRepo.login(pseudo, password)) >= 0) { - HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *", + writer.response(200, "Access-Control-Allow-Origin: *", "Access-Control-Expose-Headers: Authorization", "Authorization: Bearer " + this.router.createAuthUser(id)); return; } } - HttpUtil.responseHeaders(writer, 400, "Access-Control-Allow-Origin: *"); + writer.response(400, "Access-Control-Allow-Origin: *"); } } diff --git a/src/be/jeffcheasey88/peeratcode/routes/PlayerDetails.java b/src/be/jeffcheasey88/peeratcode/routes/PlayerDetails.java index bee6a99..acb0d42 100644 --- a/src/be/jeffcheasey88/peeratcode/routes/PlayerDetails.java +++ b/src/be/jeffcheasey88/peeratcode/routes/PlayerDetails.java @@ -7,7 +7,6 @@ import org.json.simple.JSONObject; import be.jeffcheasey88.peeratcode.bonus.extract.RouteDoc; import be.jeffcheasey88.peeratcode.framework.HttpReader; -import be.jeffcheasey88.peeratcode.framework.HttpUtil; import be.jeffcheasey88.peeratcode.framework.HttpWriter; import be.jeffcheasey88.peeratcode.framework.Response; import be.jeffcheasey88.peeratcode.framework.Route; @@ -52,10 +51,10 @@ public class PlayerDetails implements Response { playerJSON.put("badges", player.getJsonBadges()); if (player.getAvatar() != null) playerJSON.put("avatar", Base64.getEncoder().encodeToString(player.getAvatar())); - HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *"); + writer.response(200, "Access-Control-Allow-Origin: *"); writer.write(playerJSON.toJSONString().replace("\\", "")); } else { - HttpUtil.responseHeaders(writer, 400, "Access-Control-Allow-Origin: *"); + writer.response(400, "Access-Control-Allow-Origin: *"); } } diff --git a/src/be/jeffcheasey88/peeratcode/routes/PuzzleElement.java b/src/be/jeffcheasey88/peeratcode/routes/PuzzleElement.java index fc8bd6b..c78f5b0 100644 --- a/src/be/jeffcheasey88/peeratcode/routes/PuzzleElement.java +++ b/src/be/jeffcheasey88/peeratcode/routes/PuzzleElement.java @@ -7,7 +7,6 @@ import org.json.simple.JSONObject; import be.jeffcheasey88.peeratcode.bonus.extract.RouteDoc; import be.jeffcheasey88.peeratcode.framework.HttpReader; -import be.jeffcheasey88.peeratcode.framework.HttpUtil; import be.jeffcheasey88.peeratcode.framework.HttpWriter; import be.jeffcheasey88.peeratcode.framework.Response; import be.jeffcheasey88.peeratcode.framework.Route; @@ -36,13 +35,13 @@ public class PuzzleElement implements Response { Chapter chapter = this.databaseRepo.getChapter(puzzle); if(chapter.getStartDate() != null){ if(LocalDateTime.now().isBefore(chapter.getStartDate().toLocalDateTime())){ - HttpUtil.responseHeaders(writer, 423, "Access-Control-Allow-Origin: *"); + writer.response(423, "Access-Control-Allow-Origin: *"); return; } } // if(chapter.getEndDate() != null){ // if(LocalDateTime.now().isAfter(chapter.getEndDate().toLocalDateTime())){ -// HttpUtil.responseHeaders(writer, 423, "Access-Control-Allow-Origin: *"); +// writer.response(423, "Access-Control-Allow-Origin: *"); // return; // } // } @@ -59,11 +58,11 @@ public class PuzzleElement implements Response { puzzleJSON.put("tries", completion.getTries()); } if(puzzle.getDepend() > 0) puzzleJSON.put("depend", puzzle.getDepend()); - HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *", "Content-Type: application/json"); + writer.response(200, "Access-Control-Allow-Origin: *", "Content-Type: application/json"); writer.write(puzzleJSON.toJSONString()); } else { - HttpUtil.responseHeaders(writer, 400, "Access-Control-Allow-Origin: *"); + writer.response(400, "Access-Control-Allow-Origin: *"); } } diff --git a/src/be/jeffcheasey88/peeratcode/routes/PuzzleResponse.java b/src/be/jeffcheasey88/peeratcode/routes/PuzzleResponse.java index 6f4365a..e86a406 100644 --- a/src/be/jeffcheasey88/peeratcode/routes/PuzzleResponse.java +++ b/src/be/jeffcheasey88/peeratcode/routes/PuzzleResponse.java @@ -15,7 +15,6 @@ import org.json.simple.JSONObject; import be.jeffcheasey88.peeratcode.bonus.extract.RouteDoc; import be.jeffcheasey88.peeratcode.framework.HttpReader; -import be.jeffcheasey88.peeratcode.framework.HttpUtil; import be.jeffcheasey88.peeratcode.framework.HttpWriter; import be.jeffcheasey88.peeratcode.framework.Locker; import be.jeffcheasey88.peeratcode.framework.Response; @@ -50,13 +49,13 @@ public class PuzzleResponse implements Response { public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception{ ReceivedResponse received = new ReceivedResponse(matcher, reader); if (received.getResponse() == null){ - HttpUtil.responseHeaders(writer, 400, "Access-Control-Allow-Origin: *"); + writer.response(400, "Access-Control-Allow-Origin: *"); return; } //saveSourceCode(received, databaseRepo.getPlayer(user.getId())); JSONObject responseJSON = new JSONObject(); if(this.databaseRepo.getScore(user.getId(), received.getPuzzleId()) > 0){ - HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *"); + writer.response(403, "Access-Control-Allow-Origin: *"); return; } @@ -64,25 +63,25 @@ public class PuzzleResponse implements Response { Chapter chapter = this.databaseRepo.getChapter(currentPuzzle); if(chapter.getStartDate() != null){ if(LocalDateTime.now().isBefore(chapter.getStartDate().toLocalDateTime())){ - HttpUtil.responseHeaders(writer, 423, "Access-Control-Allow-Origin: *"); + writer.response(423, "Access-Control-Allow-Origin: *"); return; } } if(chapter.getEndDate() != null){ if(LocalDateTime.now().isAfter(chapter.getEndDate().toLocalDateTime())){ if(Arrays.equals(currentPuzzle.getSoluce(), received.getResponse())){ - HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *", "Content-Type: application/json"); + writer.response(200, "Access-Control-Allow-Origin: *", "Content-Type: application/json"); JSONObject theoSaisPasJusteRecevoir200 = new JSONObject(); theoSaisPasJusteRecevoir200.put("success", true); writer.write(theoSaisPasJusteRecevoir200.toJSONString()); return; } - HttpUtil.responseHeaders(writer, 423, "Access-Control-Allow-Origin: *"); + writer.response(423, "Access-Control-Allow-Origin: *"); return; } Group group = this.databaseRepo.getPlayerGroup(user.getId(), chapter.getId()); if(group == null){ - HttpUtil.responseHeaders(writer, 423, "Access-Control-Allow-Origin: *"); + writer.response(423, "Access-Control-Allow-Origin: *"); return; } } @@ -91,16 +90,16 @@ public class PuzzleResponse implements Response { Completion completion = databaseRepo.insertOrUpdatePuzzleResponse(received.getPuzzleId(), user.getId(), received.getFileName(), received.getSourceCode(), received.getResponse(), currentPuzzle); if(completion == null){ - HttpUtil.responseHeaders(writer, 400, "Access-Control-Allow-Origin: *"); + writer.response(400, "Access-Control-Allow-Origin: *"); return; } if(completion.getScore() > 0){ - HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *", "Content-Type: application/json"); + writer.response(200, "Access-Control-Allow-Origin: *", "Content-Type: application/json"); responseJSON.put("score", completion.getScore()); responseJSON.put("tries", completion.getTries()); }else{ - HttpUtil.responseHeaders(writer, 406, "Access-Control-Allow-Origin: *", "Content-Type: application/json"); + writer.response(406, "Access-Control-Allow-Origin: *", "Content-Type: application/json"); responseJSON.put("tries", completion.getTries()); } writer.write(responseJSON.toJSONString()); @@ -132,7 +131,7 @@ class ReceivedResponse { public ReceivedResponse(Matcher matcher, HttpReader reader) throws Exception { puzzleId = Integer.parseInt(matcher.group(1)); - List multiPartData = HttpUtil.readMultiPartData(reader); + List multiPartData = reader.readMultiPartData(); if (multiPartData != null && multiPartData.size() > 0) { this.response = multiPartData.get(0).getBytes(); if (multiPartData.size() == 3) { diff --git a/src/be/jeffcheasey88/peeratcode/routes/Register.java b/src/be/jeffcheasey88/peeratcode/routes/Register.java index 207876f..2f3007c 100644 --- a/src/be/jeffcheasey88/peeratcode/routes/Register.java +++ b/src/be/jeffcheasey88/peeratcode/routes/Register.java @@ -11,7 +11,6 @@ import org.json.simple.JSONObject; import be.jeffcheasey88.peeratcode.bonus.extract.RouteDoc; import be.jeffcheasey88.peeratcode.framework.HttpReader; -import be.jeffcheasey88.peeratcode.framework.HttpUtil; import be.jeffcheasey88.peeratcode.framework.HttpWriter; import be.jeffcheasey88.peeratcode.framework.Response; import be.jeffcheasey88.peeratcode.framework.Route; @@ -38,17 +37,17 @@ public class Register implements Response { @Route(path = "^\\/register$", type = POST) public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception{ if (user != null){ - HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *"); + writer.response(403, "Access-Control-Allow-Origin: *"); return; } - JSONObject informations = (JSONObject) HttpUtil.readJson(reader); + JSONObject informations = reader.readJson(); if (informations != null) { boolean allFieldsFilled = informations.containsKey("pseudo") && informations.containsKey("email") && informations.containsKey("passwd") && informations.containsKey("firstname") && informations.containsKey("lastname") && informations.containsKey("description") && informations.containsKey("sgroup") && informations.containsKey("avatar"); if (!allFieldsFilled) { - HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *"); + writer.response(403, "Access-Control-Allow-Origin: *"); return; } String pseudo = (String) informations.get("pseudo"); @@ -66,14 +65,14 @@ public class Register implements Response { int id; if ((id = databaseRepo.register(pseudo, email, password, firstname, lastname, description, group, avatar)) >= 0) { - HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *", + writer.response(200, "Access-Control-Allow-Origin: *", "Access-Control-Expose-Headers: Authorization", "Authorization: Bearer " + this.router.createAuthUser(id)); createFolderToSaveSourceCode(pseudo); return; } } else { - HttpUtil.responseHeaders(writer, 400, "Access-Control-Allow-Origin: *"); + writer.response(400, "Access-Control-Allow-Origin: *"); JSONObject error = new JSONObject(); error.put("username_valid", pseudoAvailable); error.put("email_valid", emailAvailable); @@ -81,7 +80,7 @@ public class Register implements Response { return; } } - HttpUtil.responseHeaders(writer, 400, "Access-Control-Allow-Origin: *"); + writer.response(400, "Access-Control-Allow-Origin: *"); } private void createFolderToSaveSourceCode(String pseudo) throws IOException { diff --git a/src/be/jeffcheasey88/peeratcode/routes/Result.java b/src/be/jeffcheasey88/peeratcode/routes/Result.java index dbd3959..e2fa588 100644 --- a/src/be/jeffcheasey88/peeratcode/routes/Result.java +++ b/src/be/jeffcheasey88/peeratcode/routes/Result.java @@ -4,7 +4,6 @@ import java.util.regex.Matcher; import be.jeffcheasey88.peeratcode.bonus.extract.RouteDoc; import be.jeffcheasey88.peeratcode.framework.HttpReader; -import be.jeffcheasey88.peeratcode.framework.HttpUtil; import be.jeffcheasey88.peeratcode.framework.HttpWriter; import be.jeffcheasey88.peeratcode.framework.Response; import be.jeffcheasey88.peeratcode.framework.Route; @@ -28,9 +27,9 @@ public class Result implements Response { int score = this.repo.getScore(user.getId(), puzzle); if (score < 0) { - HttpUtil.responseHeaders(writer, 425, "Access-Control-Allow-Origin: *"); + writer.response(425, "Access-Control-Allow-Origin: *"); } else { - HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *"); + writer.response(200, "Access-Control-Allow-Origin: *"); writer.write(score + ""); } } diff --git a/src/be/jeffcheasey88/peeratcode/routes/groups/GroupCreate.java b/src/be/jeffcheasey88/peeratcode/routes/groups/GroupCreate.java index 8081cc8..b09f5bd 100644 --- a/src/be/jeffcheasey88/peeratcode/routes/groups/GroupCreate.java +++ b/src/be/jeffcheasey88/peeratcode/routes/groups/GroupCreate.java @@ -5,11 +5,8 @@ import static be.jeffcheasey88.peeratcode.framework.RequestType.POST; import java.time.LocalDateTime; import java.util.regex.Matcher; -import org.json.simple.JSONObject; - import be.jeffcheasey88.peeratcode.bonus.extract.RouteDoc; import be.jeffcheasey88.peeratcode.framework.HttpReader; -import be.jeffcheasey88.peeratcode.framework.HttpUtil; import be.jeffcheasey88.peeratcode.framework.HttpWriter; import be.jeffcheasey88.peeratcode.framework.Locker; import be.jeffcheasey88.peeratcode.framework.Response; @@ -37,12 +34,12 @@ public class GroupCreate implements Response { @Route(path = "^\\/groupCreate$", type = POST, needLogin = true) public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception{ - Group newGroup = new Group((JSONObject) HttpUtil.readJson(reader)); + Group newGroup = new Group(reader.readJson()); if (this.repo.getPlayerGroup(user.getId(), newGroup.getLinkToChapter()) == null) { try { if(this.repo.getGroupId(newGroup) == null) throw new NullPointerException(); - HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *"); + writer.response(403, "Access-Control-Allow-Origin: *"); return; }catch(NullPointerException e){ if(newGroup.getLinkToChapter() != null){ @@ -50,21 +47,21 @@ public class GroupCreate implements Response { if(chapter.getStartDate() != null){ LocalDateTime start = chapter.getStartDate().toLocalDateTime().plusMinutes(this.groupDelay); if(LocalDateTime.now().isAfter(start)){ - HttpUtil.responseHeaders(writer, 423, "Access-Control-Allow-Origin: *"); + writer.response(423, "Access-Control-Allow-Origin: *"); return; } } } if (this.repo.insertGroup(newGroup, user)) { - HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *"); + writer.response(200, "Access-Control-Allow-Origin: *"); locker.setValue(newGroup); } else { - HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *"); + writer.response(403, "Access-Control-Allow-Origin: *"); } } }else { - HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *"); + writer.response(403, "Access-Control-Allow-Origin: *"); } } } diff --git a/src/be/jeffcheasey88/peeratcode/routes/groups/GroupJoin.java b/src/be/jeffcheasey88/peeratcode/routes/groups/GroupJoin.java index a171f81..cf0be76 100644 --- a/src/be/jeffcheasey88/peeratcode/routes/groups/GroupJoin.java +++ b/src/be/jeffcheasey88/peeratcode/routes/groups/GroupJoin.java @@ -5,11 +5,8 @@ import static be.jeffcheasey88.peeratcode.framework.RequestType.POST; import java.time.LocalDateTime; import java.util.regex.Matcher; -import org.json.simple.JSONObject; - import be.jeffcheasey88.peeratcode.bonus.extract.RouteDoc; import be.jeffcheasey88.peeratcode.framework.HttpReader; -import be.jeffcheasey88.peeratcode.framework.HttpUtil; import be.jeffcheasey88.peeratcode.framework.HttpWriter; import be.jeffcheasey88.peeratcode.framework.Locker; import be.jeffcheasey88.peeratcode.framework.Response; @@ -42,16 +39,16 @@ public class GroupJoin implements Response{ @Route(path = "^\\/groupJoin$", type = POST, needLogin = true) public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception{ - Group group = new Group((JSONObject) HttpUtil.readJson(reader)); + Group group = new Group(reader.readJson()); Group userGroup = this.repo.getPlayerGroup(user.getId(), group.getLinkToChapter()); if(group.equals(userGroup)){ - HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *"); + writer.response(403, "Access-Control-Allow-Origin: *"); return; } if(group.getLinkToChapter() == null){ - HttpUtil.responseHeaders(writer, 409, "Access-Control-Allow-Origin: *"); + writer.response(409, "Access-Control-Allow-Origin: *"); writer.write(waitTime); return; } @@ -61,18 +58,18 @@ public class GroupJoin implements Response{ if(chapter.getStartDate() != null){ LocalDateTime start = chapter.getStartDate().toLocalDateTime().plusMinutes(this.groupDelay); if(LocalDateTime.now().isAfter(start)){ - HttpUtil.responseHeaders(writer, 423, "Access-Control-Allow-Origin: *"); + writer.response(423, "Access-Control-Allow-Origin: *"); return; } } } if (this.repo.insertUserInGroup(group, user)) { - HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *"); + writer.response(200, "Access-Control-Allow-Origin: *"); leaderboard.setValue(new Completion(0, 0, 0, null, 0)); } else { - HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *"); + writer.response(403, "Access-Control-Allow-Origin: *"); } } diff --git a/src/be/jeffcheasey88/peeratcode/routes/groups/GroupList.java b/src/be/jeffcheasey88/peeratcode/routes/groups/GroupList.java index e367b00..249e5c8 100644 --- a/src/be/jeffcheasey88/peeratcode/routes/groups/GroupList.java +++ b/src/be/jeffcheasey88/peeratcode/routes/groups/GroupList.java @@ -6,7 +6,6 @@ import org.json.simple.JSONArray; import be.jeffcheasey88.peeratcode.bonus.extract.RouteDoc; import be.jeffcheasey88.peeratcode.framework.HttpReader; -import be.jeffcheasey88.peeratcode.framework.HttpUtil; import be.jeffcheasey88.peeratcode.framework.HttpWriter; import be.jeffcheasey88.peeratcode.framework.Response; import be.jeffcheasey88.peeratcode.framework.Route; @@ -26,7 +25,7 @@ public class GroupList implements Response { @Route(path = "^\\/groups$", needLogin = true) public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception { - HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *"); + writer.response(200, "Access-Control-Allow-Origin: *"); JSONArray result = new JSONArray(); for(Group group : this.repo.getAllGroups()) result.add(group.toJson()); writer.write(result.toJSONString()); diff --git a/src/be/jeffcheasey88/peeratcode/routes/groups/GroupQuit.java b/src/be/jeffcheasey88/peeratcode/routes/groups/GroupQuit.java index dadb3e3..4513fe6 100644 --- a/src/be/jeffcheasey88/peeratcode/routes/groups/GroupQuit.java +++ b/src/be/jeffcheasey88/peeratcode/routes/groups/GroupQuit.java @@ -5,11 +5,8 @@ import static be.jeffcheasey88.peeratcode.framework.RequestType.POST; import java.time.LocalDateTime; import java.util.regex.Matcher; -import org.json.simple.JSONObject; - import be.jeffcheasey88.peeratcode.bonus.extract.RouteDoc; import be.jeffcheasey88.peeratcode.framework.HttpReader; -import be.jeffcheasey88.peeratcode.framework.HttpUtil; import be.jeffcheasey88.peeratcode.framework.HttpWriter; import be.jeffcheasey88.peeratcode.framework.Locker; import be.jeffcheasey88.peeratcode.framework.Response; @@ -40,11 +37,11 @@ public class GroupQuit implements Response{ @Route(path = "^\\/groupQuit$", type = POST, needLogin = true) public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception{ - Group group = new Group((JSONObject) HttpUtil.readJson(reader)); + Group group = new Group(reader.readJson()); Group userGroup = this.repo.getPlayerGroup(user.getId(), group.getLinkToChapter()); if(!group.equals(userGroup)){ - HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *"); + writer.response(403, "Access-Control-Allow-Origin: *"); return; } @@ -53,18 +50,18 @@ public class GroupQuit implements Response{ if(chapter.getStartDate() != null){ LocalDateTime start = chapter.getStartDate().toLocalDateTime().plusMinutes(this.groupDelay); if(LocalDateTime.now().isAfter(start)){ - HttpUtil.responseHeaders(writer, 423, "Access-Control-Allow-Origin: *"); + writer.response(423, "Access-Control-Allow-Origin: *"); return; } } } if (this.repo.leaveGroup(group, user)) { - HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *"); + writer.response(200, "Access-Control-Allow-Origin: *"); leaderboard.setValue(new Completion(0, 0, 0, null, 0)); } else { - HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *"); + writer.response(403, "Access-Control-Allow-Origin: *"); } }