Compare commits
2 commits
e119911fef
...
fa6f0774a1
Author | SHA1 | Date | |
---|---|---|---|
|
fa6f0774a1 | ||
|
0a22f55e4e |
24 changed files with 237 additions and 127 deletions
|
@ -1,5 +1,7 @@
|
||||||
package be.jeffcheasey88.peeratcode;
|
package be.jeffcheasey88.peeratcode;
|
||||||
|
|
||||||
|
import static be.jeffcheasey88.peeratcode.framework.RequestType.OPTIONS;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.ServerSocket;
|
import java.net.ServerSocket;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
|
@ -8,9 +10,6 @@ import java.util.regex.Matcher;
|
||||||
import javax.net.ssl.SSLServerSocket;
|
import javax.net.ssl.SSLServerSocket;
|
||||||
import javax.net.ssl.SSLServerSocketFactory;
|
import javax.net.ssl.SSLServerSocketFactory;
|
||||||
|
|
||||||
import org.jose4j.jwk.RsaJsonWebKey;
|
|
||||||
import org.jose4j.jwk.RsaJwkGenerator;
|
|
||||||
|
|
||||||
import be.jeffcheasey88.peeratcode.framework.Client;
|
import be.jeffcheasey88.peeratcode.framework.Client;
|
||||||
import be.jeffcheasey88.peeratcode.framework.HttpReader;
|
import be.jeffcheasey88.peeratcode.framework.HttpReader;
|
||||||
import be.jeffcheasey88.peeratcode.framework.HttpUtil;
|
import be.jeffcheasey88.peeratcode.framework.HttpUtil;
|
||||||
|
@ -31,7 +30,9 @@ import be.jeffcheasey88.peeratcode.routes.PuzzleResponse;
|
||||||
import be.jeffcheasey88.peeratcode.routes.Register;
|
import be.jeffcheasey88.peeratcode.routes.Register;
|
||||||
import be.jeffcheasey88.peeratcode.routes.Result;
|
import be.jeffcheasey88.peeratcode.routes.Result;
|
||||||
import be.jeffcheasey88.peeratcode.routes.groups.CreateGroup;
|
import be.jeffcheasey88.peeratcode.routes.groups.CreateGroup;
|
||||||
|
import be.jeffcheasey88.peeratcode.routes.groups.GroupJoin;
|
||||||
import be.jeffcheasey88.peeratcode.routes.groups.GroupList;
|
import be.jeffcheasey88.peeratcode.routes.groups.GroupList;
|
||||||
|
import be.jeffcheasey88.peeratcode.routes.groups.GroupQuit;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
|
@ -54,7 +55,7 @@ public class Main {
|
||||||
});
|
});
|
||||||
|
|
||||||
router.register(new Response() {
|
router.register(new Response() {
|
||||||
@Route(path = "^(.*)$", type = "OPTIONS")
|
@Route(path = "^(.*)$", type = OPTIONS)
|
||||||
@Override
|
@Override
|
||||||
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
||||||
HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *",
|
HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *",
|
||||||
|
@ -81,6 +82,8 @@ public class Main {
|
||||||
|
|
||||||
router.register(new GroupList(router.getDataBase()));
|
router.register(new GroupList(router.getDataBase()));
|
||||||
router.register(new CreateGroup(router.getDataBase()));
|
router.register(new CreateGroup(router.getDataBase()));
|
||||||
|
router.register(new GroupJoin(router.getDataBase()));
|
||||||
|
router.register(new GroupQuit(router.getDataBase()));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void startWebServer(Configuration config, Router router) throws IOException {
|
private static void startWebServer(Configuration config, Router router) throws IOException {
|
||||||
|
@ -95,7 +98,7 @@ public class Main {
|
||||||
|
|
||||||
while (!server.isClosed()) {
|
while (!server.isClosed()) {
|
||||||
Socket socket = server.accept();
|
Socket socket = server.accept();
|
||||||
Client client = new Client(socket, router, RsaJwkGenerator.generateJwk(2048));
|
Client client = new Client(socket, router);
|
||||||
client.start();
|
client.start();
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
@ -109,8 +112,7 @@ public class Main {
|
||||||
try (ServerSocket server = new ServerSocket(config.getTcpPort())) {
|
try (ServerSocket server = new ServerSocket(config.getTcpPort())) {
|
||||||
while (!server.isClosed()) {
|
while (!server.isClosed()) {
|
||||||
Socket socket = server.accept();
|
Socket socket = server.accept();
|
||||||
RsaJsonWebKey rsaJsonWebKey = RsaJwkGenerator.generateJwk(2048);
|
Client client = new Client(socket, router);
|
||||||
Client client = new Client(socket, router, rsaJsonWebKey);
|
|
||||||
client.start();
|
client.start();
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|
|
@ -15,13 +15,11 @@ public class Client extends Thread {
|
||||||
private HttpReader reader;
|
private HttpReader reader;
|
||||||
private HttpWriter writer;
|
private HttpWriter writer;
|
||||||
private Router router;
|
private Router router;
|
||||||
private RsaJsonWebKey key; // Really needed ?
|
|
||||||
|
|
||||||
public Client(Socket socket, Router router, RsaJsonWebKey key) throws Exception {
|
public Client(Socket socket, Router router) throws Exception{
|
||||||
this.reader = new HttpReader(socket);
|
this.reader = new HttpReader(socket);
|
||||||
this.writer = new HttpWriter(socket);
|
this.writer = new HttpWriter(socket);
|
||||||
this.router = router;
|
this.router = router;
|
||||||
this.key = key;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -30,7 +28,7 @@ public class Client extends Thread {
|
||||||
String[] headers = reader.readLine().split("\\s");
|
String[] headers = reader.readLine().split("\\s");
|
||||||
System.out.println(Arrays.toString(headers));
|
System.out.println(Arrays.toString(headers));
|
||||||
|
|
||||||
router.exec(headers[0], headers[1], isLogin(reader), reader, writer);
|
router.exec(RequestType.valueOf(headers[0]), headers[1], isLogin(reader), reader, writer);
|
||||||
writer.flush();
|
writer.flush();
|
||||||
writer.close();
|
writer.close();
|
||||||
}catch(Exception e){
|
}catch(Exception e){
|
||||||
|
@ -40,11 +38,12 @@ public class Client extends Thread {
|
||||||
|
|
||||||
private User isLogin(HttpReader reader) throws Exception{
|
private User isLogin(HttpReader reader) throws Exception{
|
||||||
String auth = HttpUtil.readAuthorization(reader);
|
String auth = HttpUtil.readAuthorization(reader);
|
||||||
if (auth == null)
|
if(auth == null) return null;
|
||||||
return null;
|
|
||||||
try{
|
try{
|
||||||
JwtConsumer jwtConsumer = new JwtConsumerBuilder().setRequireExpirationTime()
|
JwtConsumer jwtConsumer = new JwtConsumerBuilder()
|
||||||
.setAllowedClockSkewInSeconds(30).setExpectedIssuer(this.router.getTokenIssuer())
|
.setRequireExpirationTime()
|
||||||
|
.setAllowedClockSkewInSeconds(30)
|
||||||
|
.setExpectedIssuer(this.router.getTokenIssuer())
|
||||||
.setVerificationKey(this.router.getWebKey().getKey())
|
.setVerificationKey(this.router.getWebKey().getKey())
|
||||||
.setJwsAlgorithmConstraints(ConstraintType.PERMIT, AlgorithmIdentifiers.RSA_USING_SHA256).build();
|
.setJwsAlgorithmConstraints(ConstraintType.PERMIT, AlgorithmIdentifiers.RSA_USING_SHA256).build();
|
||||||
|
|
||||||
|
|
|
@ -10,21 +10,18 @@ import org.json.simple.parser.JSONParser;
|
||||||
|
|
||||||
public class HttpUtil{
|
public class HttpUtil{
|
||||||
|
|
||||||
private HttpUtil() {
|
private HttpUtil(){}
|
||||||
}
|
|
||||||
|
|
||||||
public static void responseHeaders(HttpWriter writer, int code, String... headers) throws Exception{
|
public static void responseHeaders(HttpWriter writer, int code, String... headers) throws Exception{
|
||||||
writer.write("HTTP/1.1 " + code + " " + codeMessage(code) + "\n");
|
writer.write("HTTP/1.1 " + code + " " + codeMessage(code) + "\n");
|
||||||
for (String header : headers)
|
for(String header : headers) writer.write(header + "\n");
|
||||||
writer.write(header + "\n");
|
|
||||||
writer.write("\n");
|
writer.write("\n");
|
||||||
writer.flush();
|
writer.flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void skipHeaders(HttpReader reader) throws Exception{
|
public static void skipHeaders(HttpReader reader) throws Exception{
|
||||||
String line;
|
String line;
|
||||||
while (((line = reader.readLine()) != null) && (line.length() > 0))
|
while(((line = reader.readLine()) != null) && (line.length() > 0));
|
||||||
;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<String> readMultiPartData(HttpReader reader) throws Exception{
|
public static List<String> readMultiPartData(HttpReader reader) throws Exception{
|
||||||
|
@ -49,8 +46,7 @@ public class HttpUtil {
|
||||||
|
|
||||||
public static void switchToWebSocket(HttpReader reader, HttpWriter writer) throws Exception{
|
public static void switchToWebSocket(HttpReader reader, HttpWriter writer) throws Exception{
|
||||||
String key = readWebSocketKey(reader);
|
String key = readWebSocketKey(reader);
|
||||||
if (key == null)
|
if (key == null) throw new IllegalArgumentException();
|
||||||
throw new IllegalArgumentException();
|
|
||||||
|
|
||||||
writer.write("HTTP/1.1 101 Switching Protocols\n");
|
writer.write("HTTP/1.1 101 Switching Protocols\n");
|
||||||
writer.write("Connection: Upgrade\n");
|
writer.write("Connection: Upgrade\n");
|
||||||
|
@ -67,12 +63,9 @@ public class HttpUtil {
|
||||||
String line;
|
String line;
|
||||||
String key = null;
|
String key = null;
|
||||||
while(((line = reader.readLine()) != null) && (line.length() > 0)){
|
while(((line = reader.readLine()) != null) && (line.length() > 0)){
|
||||||
if (key != null) {
|
if(key != null) continue;
|
||||||
continue;
|
|
||||||
}
|
|
||||||
Matcher matcher = WEBSOCKET_KEY.matcher(line);
|
Matcher matcher = WEBSOCKET_KEY.matcher(line);
|
||||||
if (matcher.matches())
|
if(matcher.matches()) key = matcher.group(1);
|
||||||
key = matcher.group(1);
|
|
||||||
}
|
}
|
||||||
return key;
|
return key;
|
||||||
}
|
}
|
||||||
|
@ -104,8 +97,7 @@ public class HttpUtil {
|
||||||
parse = new JSONParser().parse(line);
|
parse = new JSONParser().parse(line);
|
||||||
if (parse != null)
|
if (parse != null)
|
||||||
return parse;
|
return parse;
|
||||||
} catch (Exception e) {
|
}catch(Exception e){}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
package be.jeffcheasey88.peeratcode.framework;
|
||||||
|
|
||||||
|
public enum RequestType {
|
||||||
|
|
||||||
|
GET, POST, OPTIONS;
|
||||||
|
|
||||||
|
}
|
|
@ -11,7 +11,7 @@ public @interface Route {
|
||||||
|
|
||||||
String path() default "^.*$";
|
String path() default "^.*$";
|
||||||
|
|
||||||
String type() default "GET";
|
RequestType type() default RequestType.GET;
|
||||||
|
|
||||||
boolean needLogin() default false;
|
boolean needLogin() default false;
|
||||||
|
|
||||||
|
|
|
@ -56,20 +56,19 @@ public class Router {
|
||||||
this.noFileFound = response;
|
this.noFileFound = response;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void exec(String type, String path, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
public void exec(RequestType type, String path, User user, HttpReader reader, HttpWriter writer) throws Exception{
|
||||||
|
if(type == null) return;
|
||||||
for(Entry<Response, Route> routes : this.responses.entrySet()){
|
for(Entry<Response, Route> routes : this.responses.entrySet()){
|
||||||
if(routes.getValue().type().equals(type)){
|
if(routes.getValue().type().equals(type)){
|
||||||
Matcher matcher = this.patterns.get(routes.getKey()).matcher(path);
|
Matcher matcher = this.patterns.get(routes.getKey()).matcher(path);
|
||||||
if(matcher.matches()){
|
if(matcher.matches()){
|
||||||
if (user == null && routes.getValue().needLogin())
|
if(user == null && routes.getValue().needLogin()) return;
|
||||||
return;
|
|
||||||
routes.getKey().exec(matcher, user, reader, writer);
|
routes.getKey().exec(matcher, user, reader, writer);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (noFileFound != null)
|
if(noFileFound != null) noFileFound.exec(null, user, reader, writer);
|
||||||
noFileFound.exec(null, user, reader, writer);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public RsaJsonWebKey getWebKey(){
|
public RsaJsonWebKey getWebKey(){
|
||||||
|
|
|
@ -16,7 +16,11 @@ public enum DatabaseQuery {
|
||||||
ALL_CHAPTERS_QUERY("SELECT * FROM chapters WHERE id_chapter > 0"),
|
ALL_CHAPTERS_QUERY("SELECT * FROM chapters WHERE id_chapter > 0"),
|
||||||
|
|
||||||
// GROUPS
|
// GROUPS
|
||||||
ALL_GROUPS("SELCT * FROM groups"), INSERT_GROUP("INSERT INTO groups (name, fk_chapter, fk_puzzle) VALUES (?,?,?)"),
|
ALL_GROUPS("SELECT * FROM groups"),
|
||||||
|
GET_GROUP_ID_BY_DATA("SELECT id_group FROM groups WHERE name = ? AND fk_chapter = ? AND fk_puzzle = ?"),
|
||||||
|
INSERT_GROUP("INSERT INTO groups (name, fk_chapter, fk_puzzle) VALUES (?,?,?)"),
|
||||||
|
INSERT_PLAYER_IN_GROUP("INSERT INTO containsGroups (fk_player, fk_group) VALUES (?,?)"),
|
||||||
|
LEAVE_GROUP("DELETE FROM containsGroups WHERE fk_player = ? AND fk_group = ?"),
|
||||||
|
|
||||||
// LEADERBOARD
|
// LEADERBOARD
|
||||||
ALL_PLAYERS_FOR_LEADERBOARD(
|
ALL_PLAYERS_FOR_LEADERBOARD(
|
||||||
|
|
|
@ -16,6 +16,7 @@ import com.password4j.Hash;
|
||||||
import com.password4j.Password;
|
import com.password4j.Password;
|
||||||
|
|
||||||
import be.jeffcheasey88.peeratcode.Configuration;
|
import be.jeffcheasey88.peeratcode.Configuration;
|
||||||
|
import be.jeffcheasey88.peeratcode.framework.User;
|
||||||
import be.jeffcheasey88.peeratcode.model.Badge;
|
import be.jeffcheasey88.peeratcode.model.Badge;
|
||||||
import be.jeffcheasey88.peeratcode.model.Chapter;
|
import be.jeffcheasey88.peeratcode.model.Chapter;
|
||||||
import be.jeffcheasey88.peeratcode.model.Completion;
|
import be.jeffcheasey88.peeratcode.model.Completion;
|
||||||
|
@ -487,16 +488,53 @@ public class DatabaseRepository {
|
||||||
statement.executeUpdate();
|
statement.executeUpdate();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean insertGroup(Group group) {
|
public boolean insertGroup(Group group, User creator){
|
||||||
try {
|
try {
|
||||||
ensureConnection();
|
ensureConnection();
|
||||||
PreparedStatement statement = DatabaseQuery.INSERT_GROUP.prepare(this.con);
|
PreparedStatement statement = DatabaseQuery.INSERT_GROUP.prepare(this.con);
|
||||||
statement.setString(1, group.getName());
|
statement.setString(1, group.getName());
|
||||||
statement.setInt(2, group.getLinkToChapter());
|
statement.setInt(2, group.getLinkToChapter());
|
||||||
statement.setInt(3, group.getLinkToPuzzle());
|
statement.setInt(3, group.getLinkToPuzzle());
|
||||||
return statement.executeUpdate() >= 0;
|
if(statement.executeUpdate() >= 0) return insertUserInGroup(group, creator);
|
||||||
} catch (Exception e) {
|
} catch (Exception e){}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private int getGroupId(Group group) throws Exception{
|
||||||
|
ensureConnection();
|
||||||
|
PreparedStatement stmt = DatabaseQuery.GET_GROUP_ID_BY_DATA.prepare(this.con);
|
||||||
|
stmt.setString(1, group.getName());
|
||||||
|
stmt.setInt(2, group.getLinkToChapter());
|
||||||
|
stmt.setInt(3, group.getLinkToPuzzle());
|
||||||
|
|
||||||
|
ResultSet result = stmt.executeQuery();
|
||||||
|
if(result.next()) return result.getInt("id_group");
|
||||||
|
throw new NullPointerException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean insertUserInGroup(Group group, User user){
|
||||||
|
try {
|
||||||
|
int id = getGroupId(group);
|
||||||
|
PreparedStatement stmt = DatabaseQuery.INSERT_PLAYER_IN_GROUP.prepare(this.con);
|
||||||
|
|
||||||
|
stmt.setInt(1, user.getId());
|
||||||
|
stmt.setInt(2, id);
|
||||||
|
|
||||||
|
return stmt.executeUpdate() >= 0;
|
||||||
|
}catch(Exception e){}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean leaveGroup(Group group, User user){
|
||||||
|
try {
|
||||||
|
int id = getGroupId(group);
|
||||||
|
PreparedStatement stmt = DatabaseQuery.LEAVE_GROUP.prepare(this.con);
|
||||||
|
|
||||||
|
stmt.setInt(1, user.getId());
|
||||||
|
stmt.setInt(2, id);
|
||||||
|
|
||||||
|
return stmt.executeUpdate() >= 0;
|
||||||
|
}catch(Exception e){}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -23,7 +23,6 @@ public class BadgeDetails implements Response {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Route(path = "^\\/badge\\/([0-9]+)$", needLogin = true)
|
@Route(path = "^\\/badge\\/([0-9]+)$", needLogin = true)
|
||||||
@Override
|
|
||||||
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
||||||
if (matcher.groupCount() > 0) {
|
if (matcher.groupCount() > 0) {
|
||||||
int badgeId = Integer.parseInt(matcher.group(1));
|
int badgeId = Integer.parseInt(matcher.group(1));
|
||||||
|
|
|
@ -24,7 +24,6 @@ public class ChapterElement implements Response {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Route(path = "^\\/chapter\\/([0-9]+)$", needLogin = true)
|
@Route(path = "^\\/chapter\\/([0-9]+)$", needLogin = true)
|
||||||
@Override
|
|
||||||
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
||||||
Chapter chapter = databaseRepo.getChapter(extractId(matcher));
|
Chapter chapter = databaseRepo.getChapter(extractId(matcher));
|
||||||
if (chapter != null) {
|
if (chapter != null) {
|
||||||
|
|
|
@ -24,7 +24,6 @@ public class ChapterList implements Response {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Route(path = "^\\/chapters$", needLogin = true)
|
@Route(path = "^\\/chapters$", needLogin = true)
|
||||||
@Override
|
|
||||||
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
||||||
List<Chapter> allChapters = databaseRepo.getAllChapters();
|
List<Chapter> allChapters = databaseRepo.getAllChapters();
|
||||||
if (allChapters != null) {
|
if (allChapters != null) {
|
||||||
|
|
|
@ -28,7 +28,6 @@ public class Leaderboard implements Response {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Route(path = "^\\/leaderboard\\/?(\\d+)?$")
|
@Route(path = "^\\/leaderboard\\/?(\\d+)?$")
|
||||||
@Override
|
|
||||||
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
||||||
HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *");
|
HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *");
|
||||||
if (matcher.group(1) != null) {
|
if (matcher.group(1) != null) {
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package be.jeffcheasey88.peeratcode.routes;
|
package be.jeffcheasey88.peeratcode.routes;
|
||||||
|
|
||||||
|
import static be.jeffcheasey88.peeratcode.framework.RequestType.POST;
|
||||||
|
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
|
|
||||||
import org.json.simple.JSONObject;
|
import org.json.simple.JSONObject;
|
||||||
|
@ -23,8 +25,7 @@ public class Login implements Response {
|
||||||
this.router = router;
|
this.router = router;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Route(path = "^\\/login$", type = "POST")
|
@Route(path = "^\\/login$", type = POST)
|
||||||
@Override
|
|
||||||
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
||||||
if (user != null) {
|
if (user != null) {
|
||||||
HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *");
|
HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *");
|
||||||
|
|
|
@ -23,7 +23,6 @@ public class PlayerDetails implements Response {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Route(path = "^\\/player\\/?(.+)?$", needLogin = true)
|
@Route(path = "^\\/player\\/?(.+)?$", needLogin = true)
|
||||||
@Override
|
|
||||||
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
||||||
Player player;
|
Player player;
|
||||||
if (matcher.group(1) != null) {
|
if (matcher.group(1) != null) {
|
||||||
|
|
|
@ -22,7 +22,6 @@ public class PuzzleElement implements Response {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Route(path = "^\\/puzzle\\/([0-9]+)$", needLogin = true)
|
@Route(path = "^\\/puzzle\\/([0-9]+)$", needLogin = true)
|
||||||
@Override
|
|
||||||
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
||||||
Puzzle puzzle = databaseRepo.getPuzzle(extractId(matcher));
|
Puzzle puzzle = databaseRepo.getPuzzle(extractId(matcher));
|
||||||
if (puzzle != null) {
|
if (puzzle != null) {
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package be.jeffcheasey88.peeratcode.routes;
|
package be.jeffcheasey88.peeratcode.routes;
|
||||||
|
|
||||||
|
import static be.jeffcheasey88.peeratcode.framework.RequestType.POST;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
@ -30,8 +32,7 @@ public class PuzzleResponse implements Response {
|
||||||
usersFilesPath = initUsersFilesPath;
|
usersFilesPath = initUsersFilesPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Route(path = "^\\/puzzleResponse\\/([0-9]+)$", type = "POST")
|
@Route(path = "^\\/puzzleResponse\\/([0-9]+)$", type = POST, needLogin = true)
|
||||||
@Override
|
|
||||||
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
||||||
if (user == null) {
|
if (user == null) {
|
||||||
HttpUtil.responseHeaders(writer, 401, "Access-Control-Allow-Origin: *");
|
HttpUtil.responseHeaders(writer, 401, "Access-Control-Allow-Origin: *");
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package be.jeffcheasey88.peeratcode.routes;
|
package be.jeffcheasey88.peeratcode.routes;
|
||||||
|
|
||||||
|
import static be.jeffcheasey88.peeratcode.framework.RequestType.POST;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
|
@ -28,8 +30,7 @@ public class Register implements Response {
|
||||||
usersFilesPath = initUsersFilesPath;
|
usersFilesPath = initUsersFilesPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Route(path = "^\\/register$", type = "POST")
|
@Route(path = "^\\/register$", type = POST)
|
||||||
@Override
|
|
||||||
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
||||||
if (user != null) {
|
if (user != null) {
|
||||||
HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *");
|
HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *");
|
||||||
|
|
|
@ -19,7 +19,6 @@ public class Result implements Response {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Route(path = "^\\/result\\/(\\d+)$", needLogin = true)
|
@Route(path = "^\\/result\\/(\\d+)$", needLogin = true)
|
||||||
@Override
|
|
||||||
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
||||||
int puzzle = Integer.parseInt(matcher.group(1));
|
int puzzle = Integer.parseInt(matcher.group(1));
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package be.jeffcheasey88.peeratcode.routes.groups;
|
package be.jeffcheasey88.peeratcode.routes.groups;
|
||||||
|
|
||||||
|
import static be.jeffcheasey88.peeratcode.framework.RequestType.POST;
|
||||||
|
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
|
|
||||||
import org.json.simple.JSONObject;
|
import org.json.simple.JSONObject;
|
||||||
|
@ -21,12 +23,11 @@ public class CreateGroup implements Response {
|
||||||
this.repo = repo;
|
this.repo = repo;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Route(path = "^\\/groupCreate$", type = "POST", needLogin = true)
|
@Route(path = "^\\/groupCreate$", type = POST, needLogin = true)
|
||||||
@Override
|
|
||||||
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
||||||
HttpUtil.skipHeaders(reader);
|
HttpUtil.skipHeaders(reader);
|
||||||
|
|
||||||
if (this.repo.insertGroup(new Group((JSONObject) HttpUtil.readJson(reader)))) {
|
if (this.repo.insertGroup(new Group((JSONObject) HttpUtil.readJson(reader)), user)) {
|
||||||
HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *");
|
HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *");
|
||||||
} else {
|
} else {
|
||||||
HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *");
|
HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *");
|
||||||
|
|
37
src/be/jeffcheasey88/peeratcode/routes/groups/GroupJoin.java
Normal file
37
src/be/jeffcheasey88/peeratcode/routes/groups/GroupJoin.java
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
package be.jeffcheasey88.peeratcode.routes.groups;
|
||||||
|
|
||||||
|
import static be.jeffcheasey88.peeratcode.framework.RequestType.POST;
|
||||||
|
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
|
||||||
|
import org.json.simple.JSONObject;
|
||||||
|
|
||||||
|
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;
|
||||||
|
import be.jeffcheasey88.peeratcode.framework.User;
|
||||||
|
import be.jeffcheasey88.peeratcode.model.Group;
|
||||||
|
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
||||||
|
|
||||||
|
public class GroupJoin implements Response{
|
||||||
|
|
||||||
|
private DatabaseRepository repo;
|
||||||
|
|
||||||
|
public GroupJoin(DatabaseRepository repo){
|
||||||
|
this.repo = repo;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Route(path = "^\\/groupJoin$", type = POST, needLogin = true)
|
||||||
|
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
||||||
|
HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *");
|
||||||
|
|
||||||
|
if (this.repo.insertUserInGroup(new Group((JSONObject) HttpUtil.readJson(reader)), user)) {
|
||||||
|
HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *");
|
||||||
|
} else {
|
||||||
|
HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -22,12 +22,10 @@ public class GroupList implements Response {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Route(path = "^\\/groups$", needLogin = true)
|
@Route(path = "^\\/groups$", needLogin = true)
|
||||||
@Override
|
|
||||||
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
||||||
HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *");
|
HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *");
|
||||||
JSONArray result = new JSONArray();
|
JSONArray result = new JSONArray();
|
||||||
for (Group group : this.repo.getAllGroups())
|
for(Group group : this.repo.getAllGroups()) result.add(group.toJson());
|
||||||
result.add(group.toJson());
|
|
||||||
writer.write(result.toJSONString());
|
writer.write(result.toJSONString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
37
src/be/jeffcheasey88/peeratcode/routes/groups/GroupQuit.java
Normal file
37
src/be/jeffcheasey88/peeratcode/routes/groups/GroupQuit.java
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
package be.jeffcheasey88.peeratcode.routes.groups;
|
||||||
|
|
||||||
|
import static be.jeffcheasey88.peeratcode.framework.RequestType.POST;
|
||||||
|
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
|
||||||
|
import org.json.simple.JSONObject;
|
||||||
|
|
||||||
|
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;
|
||||||
|
import be.jeffcheasey88.peeratcode.framework.User;
|
||||||
|
import be.jeffcheasey88.peeratcode.model.Group;
|
||||||
|
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
||||||
|
|
||||||
|
public class GroupQuit implements Response{
|
||||||
|
|
||||||
|
private DatabaseRepository repo;
|
||||||
|
|
||||||
|
public GroupQuit(DatabaseRepository repo){
|
||||||
|
this.repo = repo;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Route(path = "^\\/groupQuit$", type = POST, needLogin = true)
|
||||||
|
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
||||||
|
HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *");
|
||||||
|
|
||||||
|
if (this.repo.leaveGroup(new Group((JSONObject) HttpUtil.readJson(reader)), user)) {
|
||||||
|
HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *");
|
||||||
|
} else {
|
||||||
|
HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue