Remove unused method, organize imports and format all files
This commit is contained in:
parent
611b45a5bd
commit
109ab984b3
29 changed files with 650 additions and 690 deletions
|
@ -8,147 +8,152 @@ import java.io.FileWriter;
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
|
|
||||||
public class Configuration {
|
public class Configuration {
|
||||||
|
|
||||||
private String db_host;
|
private String db_host;
|
||||||
private int db_port;
|
private int db_port;
|
||||||
private String db_user;
|
private String db_user;
|
||||||
private String db_database;
|
private String db_database;
|
||||||
private String db_password;
|
private String db_password;
|
||||||
|
|
||||||
private int tcp_port;
|
private int tcp_port;
|
||||||
private boolean use_ssl;
|
private boolean use_ssl;
|
||||||
private String ssl_keystore;
|
private String ssl_keystore;
|
||||||
private String ssl_keystorePasswd;
|
private String ssl_keystorePasswd;
|
||||||
|
|
||||||
private String users_files;
|
private String users_files;
|
||||||
|
|
||||||
private String token_issuer;
|
private String token_issuer;
|
||||||
private int token_expiration;
|
private int token_expiration;
|
||||||
|
|
||||||
private File _file;
|
private File _file;
|
||||||
|
|
||||||
public Configuration(String path){
|
public Configuration(String path) {
|
||||||
this._file = new File(path);
|
this._file = new File(path);
|
||||||
System.out.println("Config on "+_file.getAbsolutePath());
|
System.out.println("Config on " + _file.getAbsolutePath());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void load() throws Exception{
|
public void load() throws Exception {
|
||||||
if(!this._file.exists()) return;
|
if (!this._file.exists())
|
||||||
|
return;
|
||||||
BufferedReader reader = new BufferedReader(new FileReader(this._file));
|
BufferedReader reader = new BufferedReader(new FileReader(this._file));
|
||||||
String line;
|
String line;
|
||||||
while((line = reader.readLine()) != null){
|
while ((line = reader.readLine()) != null) {
|
||||||
String[] split = line.split("=");
|
String[] split = line.split("=");
|
||||||
Field field = getClass().getDeclaredField(split[0]);
|
Field field = getClass().getDeclaredField(split[0]);
|
||||||
if(field == null) continue;
|
if (field == null)
|
||||||
|
continue;
|
||||||
field.setAccessible(true);
|
field.setAccessible(true);
|
||||||
injectValue(field, split[1]);
|
injectValue(field, split[1]);
|
||||||
}
|
}
|
||||||
reader.close();
|
reader.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void injectValue(Field field, String value) throws IllegalAccessException{
|
private void injectValue(Field field, String value) throws IllegalAccessException {
|
||||||
if(field.getType().isPrimitive()){
|
if (field.getType().isPrimitive()) {
|
||||||
switch(field.getType().getName()){
|
switch (field.getType().getName()) {
|
||||||
case "boolean":
|
case "boolean":
|
||||||
field.setBoolean(this, Boolean.parseBoolean(value));
|
field.setBoolean(this, Boolean.parseBoolean(value));
|
||||||
break;
|
break;
|
||||||
case "byte":
|
case "byte":
|
||||||
field.setByte(this, Byte.parseByte(value));
|
field.setByte(this, Byte.parseByte(value));
|
||||||
break;
|
break;
|
||||||
case "char":
|
case "char":
|
||||||
field.setChar(this, value.charAt(0));
|
field.setChar(this, value.charAt(0));
|
||||||
break;
|
break;
|
||||||
case "double":
|
case "double":
|
||||||
field.setDouble(this, Double.parseDouble(value));
|
field.setDouble(this, Double.parseDouble(value));
|
||||||
break;
|
break;
|
||||||
case "float":
|
case "float":
|
||||||
field.setFloat(this, Float.parseFloat(value));
|
field.setFloat(this, Float.parseFloat(value));
|
||||||
break;
|
break;
|
||||||
case "int":
|
case "int":
|
||||||
field.setInt(this, Integer.parseInt(value));
|
field.setInt(this, Integer.parseInt(value));
|
||||||
break;
|
break;
|
||||||
case "long":
|
case "long":
|
||||||
field.setLong(this, Long.parseLong(value));
|
field.setLong(this, Long.parseLong(value));
|
||||||
break;
|
break;
|
||||||
case "short":
|
case "short":
|
||||||
field.setShort(this, Short.parseShort(value));
|
field.setShort(this, Short.parseShort(value));
|
||||||
break;
|
break;
|
||||||
default: throw new IllegalArgumentException(value);
|
default:
|
||||||
|
throw new IllegalArgumentException(value);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(field.getType().equals(String.class)){
|
if (field.getType().equals(String.class)) {
|
||||||
field.set(this, value);
|
field.set(this, value);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
throw new IllegalArgumentException(value);
|
throw new IllegalArgumentException(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void save() throws Exception{
|
public void save() throws Exception {
|
||||||
if(!_file.exists()){
|
if (!_file.exists()) {
|
||||||
File parent = _file.getParentFile();
|
File parent = _file.getParentFile();
|
||||||
if(!parent.exists()) parent.mkdirs();
|
if (!parent.exists())
|
||||||
|
parent.mkdirs();
|
||||||
_file.createNewFile();
|
_file.createNewFile();
|
||||||
}
|
}
|
||||||
Field[] fields = getClass().getDeclaredFields();
|
Field[] fields = getClass().getDeclaredFields();
|
||||||
BufferedWriter writer = new BufferedWriter(new FileWriter(_file));
|
BufferedWriter writer = new BufferedWriter(new FileWriter(_file));
|
||||||
for(Field field : fields){
|
for (Field field : fields) {
|
||||||
field.setAccessible(true);
|
field.setAccessible(true);
|
||||||
if(field.getName().startsWith("_")) continue;
|
if (field.getName().startsWith("_"))
|
||||||
|
continue;
|
||||||
Object value = field.get(this);
|
Object value = field.get(this);
|
||||||
writer.write(field.getName()+"="+value);
|
writer.write(field.getName() + "=" + value);
|
||||||
}
|
}
|
||||||
writer.flush();
|
writer.flush();
|
||||||
writer.close();
|
writer.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getDbHost(){
|
public String getDbHost() {
|
||||||
return this.db_host;
|
return this.db_host;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getDbPort(){
|
public int getDbPort() {
|
||||||
return this.db_port;
|
return this.db_port;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getDbUser(){
|
public String getDbUser() {
|
||||||
return this.db_user;
|
return this.db_user;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getDbDatabase(){
|
public String getDbDatabase() {
|
||||||
return this.db_database;
|
return this.db_database;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getDbPassword(){
|
public String getDbPassword() {
|
||||||
return this.db_password;
|
return this.db_password;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getSslKeystore(){
|
public String getSslKeystore() {
|
||||||
return this.ssl_keystore;
|
return this.ssl_keystore;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getTokenIssuer(){
|
public String getTokenIssuer() {
|
||||||
return this.token_issuer;
|
return this.token_issuer;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getTokenExpiration(){
|
public int getTokenExpiration() {
|
||||||
return this.token_expiration;
|
return this.token_expiration;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getSslKeystorePasswd(){
|
public String getSslKeystorePasswd() {
|
||||||
return this.ssl_keystorePasswd;
|
return this.ssl_keystorePasswd;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getTcpPort(){
|
public int getTcpPort() {
|
||||||
return this.tcp_port;
|
return this.tcp_port;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean useSsl(){
|
public boolean useSsl() {
|
||||||
return this.use_ssl;
|
return this.use_ssl;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getUsersFiles(){
|
public String getUsersFiles() {
|
||||||
if (users_files == null || users_files.trim().isEmpty())
|
if (users_files == null || users_files.trim().isEmpty())
|
||||||
users_files = "/tmp/users_files";
|
users_files = "/tmp/users_files";
|
||||||
return users_files;
|
return users_files;
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -40,9 +40,10 @@ public class Main {
|
||||||
|
|
||||||
Class.forName("com.mysql.cj.jdbc.Driver");
|
Class.forName("com.mysql.cj.jdbc.Driver");
|
||||||
|
|
||||||
Router router = new Router(new DatabaseRepository(config), config.getTokenIssuer(), config.getTokenExpiration());
|
Router router = new Router(new DatabaseRepository(config), config.getTokenIssuer(),
|
||||||
|
config.getTokenExpiration());
|
||||||
|
|
||||||
router.setDefault(new Response(){
|
router.setDefault(new Response() {
|
||||||
@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, 404, "Access-Control-Allow-Origin: *");
|
HttpUtil.responseHeaders(writer, 404, "Access-Control-Allow-Origin: *");
|
||||||
|
@ -51,15 +52,13 @@ public class Main {
|
||||||
writer.close();
|
writer.close();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
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,
|
HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *",
|
||||||
"Access-Control-Allow-Origin: *",
|
"Access-Control-Allow-Methods: *", "Access-Control-Allow-Headers: *");
|
||||||
"Access-Control-Allow-Methods: *",
|
|
||||||
"Access-Control-Allow-Headers: *");
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -79,13 +78,13 @@ public class Main {
|
||||||
router.register(new Leaderboard(router.getDataBase()));
|
router.register(new Leaderboard(router.getDataBase()));
|
||||||
router.register(new PlayerDetails(router.getDataBase()));
|
router.register(new PlayerDetails(router.getDataBase()));
|
||||||
router.register(new BadgeDetails(router.getDataBase()));
|
router.register(new BadgeDetails(router.getDataBase()));
|
||||||
|
|
||||||
router.register(new GroupList(router.getDataBase()));
|
router.register(new GroupList(router.getDataBase()));
|
||||||
router.register(new CreateGroup(router.getDataBase()));
|
router.register(new CreateGroup(router.getDataBase()));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void startWebServer(Configuration config, Router router) throws IOException {
|
private static void startWebServer(Configuration config, Router router) throws IOException {
|
||||||
if (config.useSsl()) {
|
if (config.useSsl()) { // Not needed with the use of a proxy
|
||||||
SSLServerSocket server = null;
|
SSLServerSocket server = null;
|
||||||
try {
|
try {
|
||||||
System.setProperty("javax.net.ssl.keyStore", config.getSslKeystore());
|
System.setProperty("javax.net.ssl.keyStore", config.getSslKeystore());
|
||||||
|
@ -106,17 +105,15 @@ public class Main {
|
||||||
server.close();
|
server.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else {
|
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);
|
RsaJsonWebKey rsaJsonWebKey = RsaJwkGenerator.generateJwk(2048);
|
||||||
Client client = new Client(socket, router, rsaJsonWebKey);
|
Client client = new Client(socket, router, rsaJsonWebKey);
|
||||||
client.start();
|
client.start();
|
||||||
}
|
}
|
||||||
}
|
} catch (Exception e) {
|
||||||
catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,10 +5,6 @@ public class Badge {
|
||||||
private byte[] logo;
|
private byte[] logo;
|
||||||
private int level;
|
private int level;
|
||||||
|
|
||||||
public Badge(String name, int level) {
|
|
||||||
this(name, null, level);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Badge(String name, byte[] logo, int level) {
|
public Badge(String name, byte[] logo, int level) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.logo = logo;
|
this.logo = logo;
|
||||||
|
@ -19,24 +15,11 @@ public class Badge {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setName(String name) {
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public byte[] getLogo() {
|
public byte[] getLogo() {
|
||||||
return logo;
|
return logo;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setLogo(byte[] logo) {
|
|
||||||
this.logo = logo;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getLevel() {
|
public int getLevel() {
|
||||||
return level;
|
return level;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setLevel(int level) {
|
|
||||||
this.level = level;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,10 @@
|
||||||
package be.jeffcheasey88.peeratcode.model;
|
package be.jeffcheasey88.peeratcode.model;
|
||||||
|
|
||||||
import java.sql.Timestamp;
|
import java.sql.Timestamp;
|
||||||
import java.time.LocalDateTime;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class Chapter {
|
public class Chapter {
|
||||||
|
|
||||||
private int id;
|
private int id;
|
||||||
private String name;
|
private String name;
|
||||||
private List<Puzzle> puzzles;
|
private List<Puzzle> puzzles;
|
||||||
|
@ -23,18 +22,10 @@ public class Chapter {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setId(int id) {
|
|
||||||
this.id = id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setName(String name) {
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<Puzzle> getPuzzles() {
|
public List<Puzzle> getPuzzles() {
|
||||||
return puzzles;
|
return puzzles;
|
||||||
}
|
}
|
||||||
|
@ -42,20 +33,22 @@ public class Chapter {
|
||||||
public void setPuzzles(List<Puzzle> puzzles) {
|
public void setPuzzles(List<Puzzle> puzzles) {
|
||||||
this.puzzles = puzzles;
|
this.puzzles = puzzles;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Timestamp getStartDate() {
|
public Timestamp getStartDate() {
|
||||||
return startDate;
|
return startDate;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Timestamp getEndDate() {
|
public Timestamp getEndDate() {
|
||||||
return endDate;
|
return endDate;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object object){
|
public boolean equals(Object object) {
|
||||||
if(this == object) return true;
|
if (this == object)
|
||||||
if(!(object instanceof Chapter)) return false;
|
return true;
|
||||||
return this.id == (((Chapter)object).id);
|
if (!(object instanceof Chapter))
|
||||||
|
return false;
|
||||||
|
return this.id == (((Chapter) object).id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -11,10 +11,6 @@ public class Completion {
|
||||||
public Completion(int playerId, int puzzleId, String fileName, int score) {
|
public Completion(int playerId, int puzzleId, String fileName, int score) {
|
||||||
this(playerId, puzzleId, -1, 1, fileName, score, null);
|
this(playerId, puzzleId, -1, 1, fileName, score, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Completion(int playerId, int puzzleId, int idCompletion, int tries, String fileName, int score) {
|
|
||||||
this(playerId, puzzleId, idCompletion, tries, fileName, score, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Completion(int playerId, int puzzleId, int idCompletion, int tries, String fileName, int score,
|
public Completion(int playerId, int puzzleId, int idCompletion, int tries, String fileName, int score,
|
||||||
byte[] file) {
|
byte[] file) {
|
||||||
|
@ -25,7 +21,7 @@ public class Completion {
|
||||||
this.score = score;
|
this.score = score;
|
||||||
this.code = file;
|
this.code = file;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getPuzzleId() {
|
public int getPuzzleId() {
|
||||||
return puzzleId;
|
return puzzleId;
|
||||||
}
|
}
|
||||||
|
@ -42,9 +38,10 @@ public class Completion {
|
||||||
this.tries++;
|
this.tries++;
|
||||||
updateScore();
|
updateScore();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateScore() {
|
private void updateScore() {
|
||||||
if (tries > 1) {
|
if (tries > 1) {
|
||||||
score = score * (1-((tries-1)/10));
|
score = score * (1 - ((tries - 1) / 10));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
package be.jeffcheasey88.peeratcode.model;
|
package be.jeffcheasey88.peeratcode.model;
|
||||||
|
|
||||||
import java.sql.Timestamp;
|
import java.util.ArrayList;
|
||||||
import java.util.*;
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.SortedSet;
|
||||||
|
import java.util.TreeSet;
|
||||||
|
|
||||||
import org.json.simple.JSONArray;
|
import org.json.simple.JSONArray;
|
||||||
import org.json.simple.JSONObject;
|
import org.json.simple.JSONObject;
|
||||||
|
@ -11,95 +14,83 @@ public class Group implements Comparable<Group> {
|
||||||
private int linkToChapter;
|
private int linkToChapter;
|
||||||
private int linkToPuzzle;
|
private int linkToPuzzle;
|
||||||
private List<Player> players;
|
private List<Player> players;
|
||||||
|
|
||||||
public Group(JSONObject json){
|
public Group(JSONObject json) {
|
||||||
this.name = (String)json.get("name");
|
this.name = (String) json.get("name");
|
||||||
this.linkToChapter = ((Number)json.get("chapter")).intValue();
|
this.linkToChapter = ((Number) json.get("chapter")).intValue();
|
||||||
this.linkToPuzzle = ((Number)json.get("puzzle")).intValue();
|
this.linkToPuzzle = ((Number) json.get("puzzle")).intValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Group(String name, int initChap, int initPuzz) {
|
public Group(String name, int initChap, int initPuzz) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.linkToChapter = initChap;
|
this.linkToChapter = initChap;
|
||||||
this.linkToPuzzle = initPuzz;
|
this.linkToPuzzle = initPuzz;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addPlayer(Player newPlayer) {
|
public void addPlayer(Player newPlayer) {
|
||||||
if (newPlayer != null) {
|
if (newPlayer != null) {
|
||||||
if (players == null)
|
if (players == null)
|
||||||
players = new ArrayList<Player>();
|
players = new ArrayList<Player>();
|
||||||
|
|
||||||
int pPosition = players.indexOf(newPlayer);
|
int pPosition = players.indexOf(newPlayer);
|
||||||
if (pPosition < 0) {
|
if (pPosition < 0) {
|
||||||
players.add(newPlayer);
|
players.add(newPlayer);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
players.get(pPosition).addScore(newPlayer.getTotalScore(), newPlayer.getTotalTries());
|
players.get(pPosition).addScore(newPlayer.getTotalScore(), newPlayer.getTotalTries());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public SortedSet<Player> getPlayers() {
|
|
||||||
return new TreeSet<Player>(players);
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getScore() {
|
public int getScore() {
|
||||||
int score = 0;
|
int score = 0;
|
||||||
|
|
||||||
if (players != null) {
|
if (players != null) {
|
||||||
for (Player p: players) {
|
for (Player p : players) {
|
||||||
score = score + p.getTotalScore();
|
score = score + p.getTotalScore();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return score;
|
return score;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getTries() {
|
public int getTries() {
|
||||||
int tries = 0;
|
int tries = 0;
|
||||||
|
|
||||||
if (players != null) {
|
if (players != null) {
|
||||||
for (Player p: players) {
|
for (Player p : players) {
|
||||||
tries = tries + p.getTotalTries();
|
tries = tries + p.getTotalTries();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return tries;
|
return tries;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setName(String name) {
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getLinkToChapter() {
|
public int getLinkToChapter() {
|
||||||
return linkToChapter;
|
return linkToChapter;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setLinkToChapter(int linkToChapter) {
|
|
||||||
this.linkToChapter = linkToChapter;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getLinkToPuzzle() {
|
public int getLinkToPuzzle() {
|
||||||
return linkToPuzzle;
|
return linkToPuzzle;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setLinkToPuzzle(int linkToPuzzle) {
|
|
||||||
this.linkToPuzzle = linkToPuzzle;
|
|
||||||
}
|
|
||||||
|
|
||||||
public JSONObject toJson() {
|
public JSONObject toJson() {
|
||||||
return this.toJson(null);
|
return this.toJson(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public JSONObject toJson(Integer rank) {
|
public JSONObject toJson(Integer rank) {
|
||||||
JSONObject groupJSON = new JSONObject();
|
JSONObject groupJSON = new JSONObject();
|
||||||
groupJSON.put("name", name);
|
groupJSON.put("name", name);
|
||||||
if (rank != null) groupJSON.put("rank", rank);
|
if (rank != null)
|
||||||
else if (linkToChapter > 0) groupJSON.put("chapter", linkToChapter);
|
groupJSON.put("rank", rank);
|
||||||
else if (linkToPuzzle > 0) groupJSON.put("puzzle", linkToPuzzle);
|
else if (linkToChapter > 0)
|
||||||
|
groupJSON.put("chapter", linkToChapter);
|
||||||
|
else if (linkToPuzzle > 0)
|
||||||
|
groupJSON.put("puzzle", linkToPuzzle);
|
||||||
if (players != null) {
|
if (players != null) {
|
||||||
JSONArray groupsPlayerJSON = new JSONArray();
|
JSONArray groupsPlayerJSON = new JSONArray();
|
||||||
for (Player p:players) {
|
for (Player p : players) {
|
||||||
JSONObject playerJSON = new JSONObject();
|
JSONObject playerJSON = new JSONObject();
|
||||||
playerJSON.put("pseudo", p.getPseudo());
|
playerJSON.put("pseudo", p.getPseudo());
|
||||||
playerJSON.put("score", p.getTotalScore());
|
playerJSON.put("score", p.getTotalScore());
|
||||||
|
@ -143,6 +134,5 @@ public class Group implements Comparable<Group> {
|
||||||
Group other = (Group) obj;
|
Group other = (Group) obj;
|
||||||
return Objects.equals(name, other.name);
|
return Objects.equals(name, other.name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -72,11 +72,6 @@ public class Player implements Comparable<Player> {
|
||||||
return groups;
|
return groups;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* SEE SET_TAGS IN PUZZLE
|
|
||||||
*
|
|
||||||
* @return DEATH
|
|
||||||
*/
|
|
||||||
public JSONArray getJsonGroups() {
|
public JSONArray getJsonGroups() {
|
||||||
if (groups != null) {
|
if (groups != null) {
|
||||||
JSONArray groupsJSON = new JSONArray();
|
JSONArray groupsJSON = new JSONArray();
|
||||||
|
@ -146,11 +141,6 @@ public class Player implements Comparable<Player> {
|
||||||
return badges;
|
return badges;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* SEE SET_TAGS IN PUZZLE
|
|
||||||
*
|
|
||||||
* @return DEATH
|
|
||||||
*/
|
|
||||||
public JSONArray getJsonBadges() {
|
public JSONArray getJsonBadges() {
|
||||||
if (badges == null)
|
if (badges == null)
|
||||||
return null;
|
return null;
|
||||||
|
|
|
@ -8,7 +8,7 @@ import org.json.simple.JSONArray;
|
||||||
import org.json.simple.JSONObject;
|
import org.json.simple.JSONObject;
|
||||||
|
|
||||||
public class Puzzle {
|
public class Puzzle {
|
||||||
|
|
||||||
private int id;
|
private int id;
|
||||||
private String name;
|
private String name;
|
||||||
private String content;
|
private String content;
|
||||||
|
@ -18,10 +18,8 @@ public class Puzzle {
|
||||||
private Set<String> tags;
|
private Set<String> tags;
|
||||||
private int depend;
|
private int depend;
|
||||||
|
|
||||||
public Puzzle(int id, String name, String content, byte[] soluce, String verify, int scoreMax, String tags){
|
public Puzzle(int id, String name, String content, byte[] soluce, String verify, int scoreMax, String tags,
|
||||||
this(id, name, content, soluce, verify, scoreMax, tags, -1);
|
int depend) {
|
||||||
}
|
|
||||||
public Puzzle(int id, String name, String content, byte[] soluce, String verify, int scoreMax, String tags, int depend){
|
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.content = content;
|
this.content = content;
|
||||||
|
@ -36,90 +34,61 @@ public class Puzzle {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setId(int id) {
|
|
||||||
this.id = id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setName(String name) {
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getContent() {
|
public String getContent() {
|
||||||
return content;
|
return content;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setContent(String content) {
|
public byte[] getSoluce() {
|
||||||
this.content = content;
|
|
||||||
}
|
|
||||||
|
|
||||||
public byte[] getSoluce(){
|
|
||||||
return this.soluce;
|
return this.soluce;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setSoluce(byte[] array){
|
public int getScoreMax() {
|
||||||
this.soluce = array;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getVerify(){
|
|
||||||
return this.verify;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setVerify(String regex){
|
|
||||||
this.verify = regex;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getScoreMax(){
|
|
||||||
return this.scoreMax;
|
return this.scoreMax;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setScoreMax(int max){
|
public Set<String> getTags() {
|
||||||
this.scoreMax = max;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Set<String> getTags(){
|
|
||||||
return this.tags;
|
return this.tags;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* DO NOT EVER EVER SHOW TO MISTER LUDWIG XD
|
* DO NOT EVER EVER SHOW TO MISTER LUDWIG XD
|
||||||
|
*
|
||||||
* @return DEATH
|
* @return DEATH
|
||||||
*/
|
*/
|
||||||
public JSONArray getJsonTags() {
|
public JSONArray getJsonTags() {
|
||||||
if (tags == null)
|
if (tags == null)
|
||||||
return null;
|
return null;
|
||||||
JSONArray tagsJSON = new JSONArray();
|
JSONArray tagsJSON = new JSONArray();
|
||||||
for (String tag: tags) {
|
for (String tag : tags) {
|
||||||
JSONObject tagJSON = new JSONObject();
|
JSONObject tagJSON = new JSONObject();
|
||||||
tagJSON.put("name", tag);
|
tagJSON.put("name", tag);
|
||||||
tagsJSON.add(tagJSON);
|
tagsJSON.add(tagJSON);
|
||||||
}
|
}
|
||||||
return tagsJSON;
|
return tagsJSON;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setTags(String tags){
|
public void setTags(String tags) {
|
||||||
if (tags == null || tags.isEmpty())
|
if (tags == null || tags.isEmpty())
|
||||||
this.tags = null;
|
this.tags = null;
|
||||||
else
|
else
|
||||||
this.tags = new HashSet<String>(Arrays.asList(tags.split(",")));
|
this.tags = new HashSet<String>(Arrays.asList(tags.split(",")));
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getDepend(){
|
public int getDepend() {
|
||||||
return this.depend;
|
return this.depend;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setDepend(int depend){
|
|
||||||
this.depend = depend;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object object) {
|
public boolean equals(Object object) {
|
||||||
if(this == object) return true;
|
if (this == object)
|
||||||
if(!(object instanceof Puzzle)) return false;
|
return true;
|
||||||
return this.id == (((Puzzle)object).id);
|
if (!(object instanceof Puzzle))
|
||||||
|
return false;
|
||||||
|
return this.id == (((Puzzle) object).id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -5,49 +5,72 @@ import java.sql.PreparedStatement;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
|
||||||
public enum DatabaseQuery {
|
public enum DatabaseQuery {
|
||||||
|
// PUZZLES
|
||||||
SPECIFIC_PUZZLE_QUERY("SELECT p.*, np.origin, GROUP_CONCAT(t.name) AS tags FROM puzzles p LEFT JOIN nextPart np ON p.id_puzzle = np.next LEFT JOIN containsTags ct ON ct.fk_puzzle = p.id_puzzle LEFT JOIN tags t ON t.id_tag = ct.fk_tag WHERE p.id_puzzle = ? GROUP BY p.id_puzzle"),
|
SPECIFIC_PUZZLE_QUERY(
|
||||||
|
"SELECT p.*, np.origin, GROUP_CONCAT(t.name) AS tags FROM puzzles p LEFT JOIN nextPart np ON p.id_puzzle = np.next LEFT JOIN containsTags ct ON ct.fk_puzzle = p.id_puzzle LEFT JOIN tags t ON t.id_tag = ct.fk_tag WHERE p.id_puzzle = ? GROUP BY p.id_puzzle"),
|
||||||
|
PUZZLES_IN_CHAPTER_QUERY(
|
||||||
|
"SELECT p.*, GROUP_CONCAT(t.name) AS tags FROM puzzles p LEFT JOIN containsTags ct ON ct.fk_puzzle = p.id_puzzle LEFT JOIN tags t ON t.id_tag = ct.fk_tag WHERE fk_chapter = ? GROUP BY p.id_puzzle"),
|
||||||
|
|
||||||
|
// CHAPTERS
|
||||||
SPECIFIC_CHAPTER_QUERY("SELECT * FROM chapters WHERE id_chapter = ?"),
|
SPECIFIC_CHAPTER_QUERY("SELECT * FROM chapters WHERE id_chapter = ?"),
|
||||||
PUZZLES_IN_CHAPTER_QUERY("SELECT p.*, GROUP_CONCAT(t.name) AS tags FROM puzzles p LEFT JOIN containsTags ct ON ct.fk_puzzle = p.id_puzzle LEFT JOIN tags t ON t.id_tag = ct.fk_tag WHERE fk_chapter = ? GROUP BY p.id_puzzle"),
|
|
||||||
ALL_CHAPTERS_QUERY("SELECT * FROM chapters WHERE id_chapter > 0"),
|
ALL_CHAPTERS_QUERY("SELECT * FROM chapters WHERE id_chapter > 0"),
|
||||||
ALL_GROUPS("SELCT * FROM groups"),
|
|
||||||
|
// GROUPS
|
||||||
ALL_PLAYERS_FOR_LEADERBOARD("select p.*, scores.*, g.* from players p ,(SELECT fk_player, SUM(c.score) AS score, COUNT(c.id_completion) AS completions, SUM(c.tries) AS tries, rank() over(ORDER BY score DESC) AS rank FROM completions c GROUP BY c.fk_player) AS scores LEFT JOIN containsGroups cg ON scores.fk_player = cg.fk_player LEFT JOIN groups g ON cg.fk_group = g.id_group WHERE p.id_player = scores.fk_player ORDER BY g.fk_chapter, g.fk_puzzle"),
|
ALL_GROUPS("SELCT * FROM groups"), INSERT_GROUP("INSERT INTO groups (name, fk_chapter, fk_puzzle) VALUES (?,?,?)"),
|
||||||
ALL_GROUP_FOR_CHAPTER_LEADERBOARD("SELECT g.*, pl.pseudo, co.score, co.tries FROM groups g LEFT JOIN containsGroups cg ON g.id_group = cg.fk_group LEFT JOIN players pl ON cg.fk_player = pl.id_player LEFT JOIN completions co ON pl.id_player = co.fk_player WHERE fk_chapter = ? AND (co.fk_puzzle IN (SELECT id_puzzle FROM puzzles puz WHERE puz.fk_chapter = g.fk_chapter) OR co.score IS NULL);"),
|
UPDATE_COMPLETION(
|
||||||
|
"UPDATE completions SET tries = ?, filename = ?, score = ? WHERE fk_puzzle = ? AND fk_player = ?"),
|
||||||
|
|
||||||
|
// LEADERBOARD
|
||||||
|
ALL_PLAYERS_FOR_LEADERBOARD(
|
||||||
|
"select p.*, scores.*, g.* from players p ,(SELECT fk_player, SUM(c.score) AS score, COUNT(c.id_completion) AS completions, SUM(c.tries) AS tries, rank() over(ORDER BY score DESC) AS rank FROM completions c GROUP BY c.fk_player) AS scores LEFT JOIN containsGroups cg ON scores.fk_player = cg.fk_player LEFT JOIN groups g ON cg.fk_group = g.id_group WHERE p.id_player = scores.fk_player ORDER BY g.fk_chapter, g.fk_puzzle"),
|
||||||
|
ALL_GROUP_FOR_CHAPTER_LEADERBOARD(
|
||||||
|
"SELECT g.*, pl.pseudo, co.score, co.tries FROM groups g LEFT JOIN containsGroups cg ON g.id_group = cg.fk_group LEFT JOIN players pl ON cg.fk_player = pl.id_player LEFT JOIN completions co ON pl.id_player = co.fk_player WHERE fk_chapter = ? AND (co.fk_puzzle IN (SELECT id_puzzle FROM puzzles puz WHERE puz.fk_chapter = g.fk_chapter) OR co.score IS NULL);"),
|
||||||
|
|
||||||
|
// REGISTER
|
||||||
CHECK_PSEUDO_AVAILABLE_QUERY("SELECT * FROM players WHERE pseudo = ?"),
|
CHECK_PSEUDO_AVAILABLE_QUERY("SELECT * FROM players WHERE pseudo = ?"),
|
||||||
CHECK_EMAIL_AVAILABLE_QUERY("SELECT * FROM players WHERE email = ?"),
|
CHECK_EMAIL_AVAILABLE_QUERY("SELECT * FROM players WHERE email = ?"),
|
||||||
REGISTER_QUERY("INSERT INTO players (pseudo, email, passwd, firstname, lastname, description, avatar) VALUES (?, ?, ?, ?, ?, ?, ?)"),
|
REGISTER_QUERY(
|
||||||
REGISTER_PLAYER_IN_EXISTING_GROUP("INSERT INTO containsGroups (fk_player, fk_group) VALUES (?, (SELECT id_group FROM groups WHERE name = ?));"),
|
"INSERT INTO players (pseudo, email, passwd, firstname, lastname, description, avatar) VALUES (?, ?, ?, ?, ?, ?, ?)"),
|
||||||
|
REGISTER_PLAYER_IN_EXISTING_GROUP(
|
||||||
|
"INSERT INTO containsGroups (fk_player, fk_group) VALUES (?, (SELECT id_group FROM groups WHERE name = ?));"),
|
||||||
|
|
||||||
|
// LOGIN
|
||||||
CHECK_PASSWORD("SELECT id_player, passwd FROM players WHERE pseudo=?"),
|
CHECK_PASSWORD("SELECT id_player, passwd FROM players WHERE pseudo=?"),
|
||||||
|
|
||||||
|
// COMPLETIONS
|
||||||
|
GET_COMPLETION(
|
||||||
|
"SELECT id_completion, tries, fileName, score FROM completions WHERE fk_puzzle = ? AND fk_player = ?"),
|
||||||
|
INSERT_COMPLETION(
|
||||||
|
"INSERT INTO completions (fk_puzzle, fk_player, tries, code, fileName, score) values (?, ?, ?, ?, ?, ?)"),
|
||||||
SCORE("SELECT score FROM completions WHERE fk_player = ? AND fk_puzzle = ?"),
|
SCORE("SELECT score FROM completions WHERE fk_player = ? AND fk_puzzle = ?"),
|
||||||
GET_COMPLETION("SELECT id_completion, tries, fileName, score FROM completions WHERE fk_puzzle = ? AND fk_player = ?"),
|
|
||||||
|
// PLAYERS
|
||||||
GET_PLAYER_SIMPLE("SELECT pseudo, email, firstname, lastname, description FROM players WHERE id_player = ?"),
|
GET_PLAYER_SIMPLE("SELECT pseudo, email, firstname, lastname, description FROM players WHERE id_player = ?"),
|
||||||
GET_PLAYER_DETAILS("SELECT p.*, scores.score, scores.completions, scores.tries, scores.rank, g.* FROM players p, (SELECT fk_player, SUM(c.score) AS score, COUNT(c.id_completion) AS completions, SUM(c.tries) AS tries, rank() over(ORDER BY score DESC) AS rank FROM completions c GROUP BY c.fk_player) AS scores LEFT JOIN containsGroups cg ON scores.fk_player = cg.fk_player LEFT JOIN groups g ON cg.fk_group = g.id_group WHERE p.id_player = scores.fk_player AND "),
|
GET_PLAYER_DETAILS(
|
||||||
GET_PLAYER_DETAILS_BY_ID(GET_PLAYER_DETAILS," p.id_player = ? ORDER BY g.fk_chapter, g.fk_puzzle;"),
|
"SELECT p.*, scores.score, scores.completions, scores.tries, scores.rank, g.* FROM players p, (SELECT fk_player, SUM(c.score) AS score, COUNT(c.id_completion) AS completions, SUM(c.tries) AS tries, rank() over(ORDER BY score DESC) AS rank FROM completions c GROUP BY c.fk_player) AS scores LEFT JOIN containsGroups cg ON scores.fk_player = cg.fk_player LEFT JOIN groups g ON cg.fk_group = g.id_group WHERE p.id_player = scores.fk_player AND "),
|
||||||
GET_PLAYER_DETAILS_BY_PSEUDO(GET_PLAYER_DETAILS,"p.pseudo = ? ORDER BY g.fk_chapter, g.fk_puzzle;"),
|
GET_PLAYER_DETAILS_BY_ID(GET_PLAYER_DETAILS, " p.id_player = ? ORDER BY g.fk_chapter, g.fk_puzzle;"),
|
||||||
GET_BADGE("SELECT * FROM badges WHERE id_badge = ?"),
|
GET_PLAYER_DETAILS_BY_PSEUDO(GET_PLAYER_DETAILS, "p.pseudo = ? ORDER BY g.fk_chapter, g.fk_puzzle;"),
|
||||||
GET_BADGES_OF_PLAYER("SELECT * FROM badges b LEFT JOIN containsBadges cb ON cb.fk_badge = b.id_badge WHERE cb.fk_player = ?"),
|
|
||||||
INSERT_COMPLETION("INSERT INTO completions (fk_puzzle, fk_player, tries, code, fileName, score) values (?, ?, ?, ?, ?, ?)"),
|
// BADGES
|
||||||
INSERT_GROUP("INSERT INTO groups (name, fk_chapter, fk_puzzle) VALUES (?,?,?)"),
|
GET_BADGE("SELECT * FROM badges WHERE id_badge = ?"), GET_BADGES_OF_PLAYER(
|
||||||
UPDATE_COMPLETION("UPDATE completions SET tries = ?, filename = ?, score = ? WHERE fk_puzzle = ? AND fk_player = ?");
|
"SELECT * FROM badges b LEFT JOIN containsBadges cb ON cb.fk_badge = b.id_badge WHERE cb.fk_player = ?");
|
||||||
|
|
||||||
private String request;
|
private String request;
|
||||||
|
|
||||||
DatabaseQuery(DatabaseQuery parent, String request){
|
DatabaseQuery(DatabaseQuery parent, String request) {
|
||||||
this.request = parent.request+request;
|
this.request = parent.request + request;
|
||||||
}
|
}
|
||||||
|
|
||||||
DatabaseQuery(String request){
|
DatabaseQuery(String request) {
|
||||||
this.request = request;
|
this.request = request;
|
||||||
}
|
}
|
||||||
|
|
||||||
public PreparedStatement prepare(Connection con) throws SQLException{
|
public PreparedStatement prepare(Connection con) throws SQLException {
|
||||||
return con.prepareStatement(this.request);
|
return con.prepareStatement(this.request);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString(){
|
public String toString() {
|
||||||
return this.request;
|
return this.request;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,10 @@
|
||||||
package be.jeffcheasey88.peeratcode.routes;
|
package be.jeffcheasey88.peeratcode.routes;
|
||||||
|
|
||||||
|
import java.util.Base64;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
|
||||||
|
import org.json.simple.JSONObject;
|
||||||
|
|
||||||
import be.jeffcheasey88.peeratcode.model.Badge;
|
import be.jeffcheasey88.peeratcode.model.Badge;
|
||||||
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
||||||
import be.jeffcheasey88.peeratcode.webserver.HttpReader;
|
import be.jeffcheasey88.peeratcode.webserver.HttpReader;
|
||||||
|
@ -9,11 +14,6 @@ import be.jeffcheasey88.peeratcode.webserver.Response;
|
||||||
import be.jeffcheasey88.peeratcode.webserver.Route;
|
import be.jeffcheasey88.peeratcode.webserver.Route;
|
||||||
import be.jeffcheasey88.peeratcode.webserver.User;
|
import be.jeffcheasey88.peeratcode.webserver.User;
|
||||||
|
|
||||||
import org.json.simple.JSONObject;
|
|
||||||
|
|
||||||
import java.util.Base64;
|
|
||||||
import java.util.regex.Matcher;
|
|
||||||
|
|
||||||
public class BadgeDetails implements Response {
|
public class BadgeDetails implements Response {
|
||||||
|
|
||||||
private final DatabaseRepository databaseRepo;
|
private final DatabaseRepository databaseRepo;
|
||||||
|
@ -32,12 +32,12 @@ public class BadgeDetails implements Response {
|
||||||
JSONObject badgeJSON = new JSONObject();
|
JSONObject badgeJSON = new JSONObject();
|
||||||
if (badge != null) {
|
if (badge != null) {
|
||||||
badgeJSON.put("name", badge.getName());
|
badgeJSON.put("name", badge.getName());
|
||||||
if(badge.getLogo() != null) badgeJSON.put("logo", Base64.getEncoder().encodeToString(badge.getLogo()));
|
if (badge.getLogo() != null)
|
||||||
|
badgeJSON.put("logo", Base64.getEncoder().encodeToString(badge.getLogo()));
|
||||||
badgeJSON.put("level", badge.getLevel());
|
badgeJSON.put("level", badge.getLevel());
|
||||||
}
|
}
|
||||||
writer.write(badgeJSON.toJSONString().replace("\\", ""));
|
writer.write(badgeJSON.toJSONString().replace("\\", ""));
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
HttpUtil.responseHeaders(writer, 400, "Access-Control-Allow-Origin: *");
|
HttpUtil.responseHeaders(writer, 400, "Access-Control-Allow-Origin: *");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,10 @@
|
||||||
package be.jeffcheasey88.peeratcode.routes;
|
package be.jeffcheasey88.peeratcode.routes;
|
||||||
|
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
|
||||||
|
import org.json.simple.JSONArray;
|
||||||
|
import org.json.simple.JSONObject;
|
||||||
|
|
||||||
import be.jeffcheasey88.peeratcode.model.Chapter;
|
import be.jeffcheasey88.peeratcode.model.Chapter;
|
||||||
import be.jeffcheasey88.peeratcode.model.Puzzle;
|
import be.jeffcheasey88.peeratcode.model.Puzzle;
|
||||||
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
||||||
|
@ -10,11 +15,6 @@ import be.jeffcheasey88.peeratcode.webserver.Response;
|
||||||
import be.jeffcheasey88.peeratcode.webserver.Route;
|
import be.jeffcheasey88.peeratcode.webserver.Route;
|
||||||
import be.jeffcheasey88.peeratcode.webserver.User;
|
import be.jeffcheasey88.peeratcode.webserver.User;
|
||||||
|
|
||||||
import org.json.simple.JSONArray;
|
|
||||||
import org.json.simple.JSONObject;
|
|
||||||
|
|
||||||
import java.util.regex.Matcher;
|
|
||||||
|
|
||||||
public class ChapterElement implements Response {
|
public class ChapterElement implements Response {
|
||||||
|
|
||||||
private final DatabaseRepository databaseRepo;
|
private final DatabaseRepository databaseRepo;
|
||||||
|
@ -32,14 +32,17 @@ public class ChapterElement implements Response {
|
||||||
JSONObject chapterJSON = new JSONObject();
|
JSONObject chapterJSON = new JSONObject();
|
||||||
chapterJSON.put("id", chapter.getId());
|
chapterJSON.put("id", chapter.getId());
|
||||||
chapterJSON.put("name", chapter.getName());
|
chapterJSON.put("name", chapter.getName());
|
||||||
if (chapter.getStartDate() != null) chapterJSON.put("startDate", chapter.getStartDate().toString());
|
if (chapter.getStartDate() != null)
|
||||||
if (chapter.getEndDate() != null) chapterJSON.put("endDate", chapter.getEndDate().toString());
|
chapterJSON.put("startDate", chapter.getStartDate().toString());
|
||||||
|
if (chapter.getEndDate() != null)
|
||||||
|
chapterJSON.put("endDate", chapter.getEndDate().toString());
|
||||||
JSONArray puzzles = new JSONArray();
|
JSONArray puzzles = new JSONArray();
|
||||||
for (Puzzle puzzle : chapter.getPuzzles()) {
|
for (Puzzle puzzle : chapter.getPuzzles()) {
|
||||||
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());
|
||||||
if (puzzle.getTags() != null) puzzleJSON.put("tags", puzzle.getJsonTags());
|
if (puzzle.getTags() != null)
|
||||||
|
puzzleJSON.put("tags", puzzle.getJsonTags());
|
||||||
puzzles.add(puzzleJSON);
|
puzzles.add(puzzleJSON);
|
||||||
}
|
}
|
||||||
chapterJSON.put("puzzles", puzzles);
|
chapterJSON.put("puzzles", puzzles);
|
||||||
|
|
|
@ -34,8 +34,10 @@ public class ChapterList implements Response {
|
||||||
JSONObject chapterJSON = new JSONObject();
|
JSONObject chapterJSON = new JSONObject();
|
||||||
chapterJSON.put("id", chapter.getId());
|
chapterJSON.put("id", chapter.getId());
|
||||||
chapterJSON.put("name", chapter.getName());
|
chapterJSON.put("name", chapter.getName());
|
||||||
if (chapter.getStartDate() != null) chapterJSON.put("startDate", chapter.getStartDate().toString());
|
if (chapter.getStartDate() != null)
|
||||||
if (chapter.getEndDate() != null) chapterJSON.put("endDate", chapter.getEndDate().toString());
|
chapterJSON.put("startDate", chapter.getStartDate().toString());
|
||||||
|
if (chapter.getEndDate() != null)
|
||||||
|
chapterJSON.put("endDate", chapter.getEndDate().toString());
|
||||||
chaptersJSON.add(chapterJSON);
|
chaptersJSON.add(chapterJSON);
|
||||||
}
|
}
|
||||||
writer.write(chaptersJSON.toJSONString());
|
writer.write(chaptersJSON.toJSONString());
|
||||||
|
|
|
@ -31,7 +31,7 @@ public class Leaderboard implements Response {
|
||||||
@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: *");
|
||||||
if (matcher.group(1) != null){
|
if (matcher.group(1) != null) {
|
||||||
groupsLeaderboard(Integer.parseInt(matcher.group(1)), writer);
|
groupsLeaderboard(Integer.parseInt(matcher.group(1)), writer);
|
||||||
} else {
|
} else {
|
||||||
playersLeaderboard(writer);
|
playersLeaderboard(writer);
|
||||||
|
@ -40,17 +40,19 @@ public class Leaderboard implements Response {
|
||||||
|
|
||||||
private void groupsLeaderboard(int chapterId, HttpWriter writer) throws IOException {
|
private void groupsLeaderboard(int chapterId, HttpWriter writer) throws IOException {
|
||||||
Chapter chInfo = databaseRepo.getChapter(chapterId);
|
Chapter chInfo = databaseRepo.getChapter(chapterId);
|
||||||
|
|
||||||
SortedSet<Group> allGroupsForChapter = databaseRepo.getAllGroupForChapterLeaderboard(chapterId);
|
SortedSet<Group> allGroupsForChapter = databaseRepo.getAllGroupForChapterLeaderboard(chapterId);
|
||||||
JSONObject leaderboardJSON = new JSONObject();
|
JSONObject leaderboardJSON = new JSONObject();
|
||||||
if (chInfo.getStartDate() != null) leaderboardJSON.put("start_date", chInfo.getStartDate().toString());
|
if (chInfo.getStartDate() != null)
|
||||||
if (chInfo.getEndDate() != null) leaderboardJSON.put("end_date", chInfo.getEndDate().toString());
|
leaderboardJSON.put("start_date", chInfo.getStartDate().toString());
|
||||||
|
if (chInfo.getEndDate() != null)
|
||||||
|
leaderboardJSON.put("end_date", chInfo.getEndDate().toString());
|
||||||
JSONArray groupsJSON = new JSONArray();
|
JSONArray groupsJSON = new JSONArray();
|
||||||
if (allGroupsForChapter != null) {
|
if (allGroupsForChapter != null) {
|
||||||
int rank = 1;
|
int rank = 1;
|
||||||
int sameRankCount = 1;
|
int sameRankCount = 1;
|
||||||
Group previousGroup = null;
|
Group previousGroup = null;
|
||||||
for (Group g: allGroupsForChapter) {
|
for (Group g : allGroupsForChapter) {
|
||||||
if (previousGroup != null) {
|
if (previousGroup != null) {
|
||||||
if (g.compareTo(previousGroup) == 0) {
|
if (g.compareTo(previousGroup) == 0) {
|
||||||
sameRankCount++;
|
sameRankCount++;
|
||||||
|
@ -74,8 +76,10 @@ public class Leaderboard implements Response {
|
||||||
for (Player player : allPlayers) {
|
for (Player player : allPlayers) {
|
||||||
JSONObject playerJSON = new JSONObject();
|
JSONObject playerJSON = new JSONObject();
|
||||||
playerJSON.put("pseudo", player.getPseudo());
|
playerJSON.put("pseudo", player.getPseudo());
|
||||||
if (player.getGroups() != null) playerJSON.put("groups", player.getJsonGroups());
|
if (player.getGroups() != null)
|
||||||
if(player.getAvatar() != null) playerJSON.put("avatar", Base64.getEncoder().encodeToString(player.getAvatar()));
|
playerJSON.put("groups", player.getJsonGroups());
|
||||||
|
if (player.getAvatar() != null)
|
||||||
|
playerJSON.put("avatar", Base64.getEncoder().encodeToString(player.getAvatar()));
|
||||||
playerJSON.put("rank", player.getRank());
|
playerJSON.put("rank", player.getRank());
|
||||||
playerJSON.put("score", player.getTotalScore());
|
playerJSON.put("score", player.getTotalScore());
|
||||||
playerJSON.put("completions", player.getTotalCompletion());
|
playerJSON.put("completions", player.getTotalCompletion());
|
||||||
|
|
|
@ -1,12 +1,7 @@
|
||||||
package be.jeffcheasey88.peeratcode.routes;
|
package be.jeffcheasey88.peeratcode.routes;
|
||||||
|
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
|
||||||
|
|
||||||
import org.jose4j.jwk.RsaJsonWebKey;
|
|
||||||
import org.jose4j.jws.AlgorithmIdentifiers;
|
|
||||||
import org.jose4j.jws.JsonWebSignature;
|
|
||||||
import org.jose4j.jwt.JwtClaims;
|
|
||||||
import org.json.simple.JSONObject;
|
import org.json.simple.JSONObject;
|
||||||
|
|
||||||
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
||||||
|
@ -23,7 +18,7 @@ public class Login implements Response {
|
||||||
private DatabaseRepository databaseRepo;
|
private DatabaseRepository databaseRepo;
|
||||||
private Router router;
|
private Router router;
|
||||||
|
|
||||||
public Login(DatabaseRepository databaseRepo, Router router){
|
public Login(DatabaseRepository databaseRepo, Router router) {
|
||||||
this.databaseRepo = databaseRepo;
|
this.databaseRepo = databaseRepo;
|
||||||
this.router = router;
|
this.router = router;
|
||||||
}
|
}
|
||||||
|
@ -31,8 +26,8 @@ public class Login implements Response {
|
||||||
@Route(path = "^\\/login$", type = "POST")
|
@Route(path = "^\\/login$", type = "POST")
|
||||||
@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 {
|
||||||
if(user != null){
|
if (user != null) {
|
||||||
HttpUtil.responseHeaders(writer, 403,"Access-Control-Allow-Origin: *");
|
HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
JSONObject informations = (JSONObject) HttpUtil.readJson(reader);
|
JSONObject informations = (JSONObject) HttpUtil.readJson(reader);
|
||||||
|
@ -40,11 +35,10 @@ public class Login implements Response {
|
||||||
String pseudo = (String) informations.get("pseudo");
|
String pseudo = (String) informations.get("pseudo");
|
||||||
String password = (String) informations.get("passwd");
|
String password = (String) informations.get("passwd");
|
||||||
int id;
|
int id;
|
||||||
if ((id = databaseRepo.login(pseudo, password)) >= 0){
|
if ((id = databaseRepo.login(pseudo, password)) >= 0) {
|
||||||
HttpUtil.responseHeaders(writer, 200,
|
HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *",
|
||||||
"Access-Control-Allow-Origin: *",
|
|
||||||
"Access-Control-Expose-Headers: Authorization",
|
"Access-Control-Expose-Headers: Authorization",
|
||||||
"Authorization: Bearer "+this.router.createAuthUser(id));
|
"Authorization: Bearer " + this.router.createAuthUser(id));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,7 +26,7 @@ public class PlayerDetails implements Response {
|
||||||
@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 {
|
||||||
Player player;
|
Player player;
|
||||||
if (matcher.group(1) != null){
|
if (matcher.group(1) != null) {
|
||||||
player = databaseRepo.getPlayerDetails(matcher.group(1));
|
player = databaseRepo.getPlayerDetails(matcher.group(1));
|
||||||
} else {
|
} else {
|
||||||
player = databaseRepo.getPlayerDetails(user.getId());
|
player = databaseRepo.getPlayerDetails(user.getId());
|
||||||
|
@ -39,13 +39,16 @@ public class PlayerDetails implements Response {
|
||||||
playerJSON.put("firstname", player.getFirstname());
|
playerJSON.put("firstname", player.getFirstname());
|
||||||
playerJSON.put("lastname", player.getLastname());
|
playerJSON.put("lastname", player.getLastname());
|
||||||
playerJSON.put("description", player.getDescription());
|
playerJSON.put("description", player.getDescription());
|
||||||
if (player.getGroups() != null) playerJSON.put("groups", player.getJsonGroups());
|
if (player.getGroups() != null)
|
||||||
|
playerJSON.put("groups", player.getJsonGroups());
|
||||||
playerJSON.put("rank", player.getRank());
|
playerJSON.put("rank", player.getRank());
|
||||||
playerJSON.put("score", player.getTotalScore());
|
playerJSON.put("score", player.getTotalScore());
|
||||||
playerJSON.put("completions", player.getTotalCompletion());
|
playerJSON.put("completions", player.getTotalCompletion());
|
||||||
playerJSON.put("tries", player.getTotalTries());
|
playerJSON.put("tries", player.getTotalTries());
|
||||||
if (player.getBadges() != null) playerJSON.put("badges", player.getJsonBadges());
|
if (player.getBadges() != null)
|
||||||
if(player.getAvatar() != null) playerJSON.put("avatar", Base64.getEncoder().encodeToString(player.getAvatar()));
|
playerJSON.put("badges", player.getJsonBadges());
|
||||||
|
if (player.getAvatar() != null)
|
||||||
|
playerJSON.put("avatar", Base64.getEncoder().encodeToString(player.getAvatar()));
|
||||||
writer.write(playerJSON.toJSONString().replace("\\", ""));
|
writer.write(playerJSON.toJSONString().replace("\\", ""));
|
||||||
} else {
|
} else {
|
||||||
HttpUtil.responseHeaders(writer, 400, "Access-Control-Allow-Origin: *");
|
HttpUtil.responseHeaders(writer, 400, "Access-Control-Allow-Origin: *");
|
||||||
|
|
|
@ -1,11 +1,7 @@
|
||||||
package be.jeffcheasey88.peeratcode.routes;
|
package be.jeffcheasey88.peeratcode.routes;
|
||||||
|
|
||||||
import java.nio.charset.Charset;
|
|
||||||
import java.util.Set;
|
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
|
||||||
|
|
||||||
import org.jose4j.json.internal.json_simple.JSONArray;
|
|
||||||
import org.json.simple.JSONObject;
|
import org.json.simple.JSONObject;
|
||||||
|
|
||||||
import be.jeffcheasey88.peeratcode.model.Puzzle;
|
import be.jeffcheasey88.peeratcode.model.Puzzle;
|
||||||
|
@ -35,8 +31,10 @@ public class PuzzleElement implements Response {
|
||||||
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());
|
||||||
if (puzzle.getTags() != null) puzzleJSON.put("tags", puzzle.getJsonTags());
|
if (puzzle.getTags() != null)
|
||||||
if (puzzle.getDepend() > 0) puzzleJSON.put("depend", puzzle.getDepend());
|
puzzleJSON.put("tags", puzzle.getJsonTags());
|
||||||
|
if (puzzle.getDepend() > 0)
|
||||||
|
puzzleJSON.put("depend", puzzle.getDepend());
|
||||||
writer.write(puzzleJSON.toJSONString());
|
writer.write(puzzleJSON.toJSONString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -54,15 +54,16 @@ public class PuzzleResponse implements Response {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void saveSourceCode(ReceivedResponse received, Player player) throws IOException {
|
private void saveSourceCode(ReceivedResponse received, Player player) throws IOException {
|
||||||
Path path = Paths.get(String.format("%s/%s/puz%04d-%s", usersFilesPath, player.getPseudo(), received.getPuzzleId(), received.getFileName()));
|
Path path = Paths.get(String.format("%s/%s/puz%04d-%s", usersFilesPath, player.getPseudo(),
|
||||||
|
received.getPuzzleId(), received.getFileName()));
|
||||||
Files.write(path, received.getSourceCode());
|
Files.write(path, received.getSourceCode());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class ReceivedResponse {
|
class ReceivedResponse {
|
||||||
|
|
||||||
private int puzzleId;
|
private int puzzleId;
|
||||||
private byte[] response;
|
private byte[] response;
|
||||||
private String fileName;
|
private String fileName;
|
||||||
|
@ -73,7 +74,7 @@ class ReceivedResponse {
|
||||||
public ReceivedResponse(Matcher matcher, HttpReader reader) throws Exception {
|
public ReceivedResponse(Matcher matcher, HttpReader reader) throws Exception {
|
||||||
this.reader = reader;
|
this.reader = reader;
|
||||||
puzzleId = Integer.parseInt(matcher.group(1));
|
puzzleId = Integer.parseInt(matcher.group(1));
|
||||||
|
|
||||||
List<String> multiPartData = HttpUtil.readMultiPartData(reader);
|
List<String> multiPartData = HttpUtil.readMultiPartData(reader);
|
||||||
this.response = multiPartData.get(0).getBytes();
|
this.response = multiPartData.get(0).getBytes();
|
||||||
this.fileName = multiPartData.get(1);
|
this.fileName = multiPartData.get(1);
|
||||||
|
|
|
@ -4,11 +4,9 @@ import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
|
||||||
|
|
||||||
import org.json.simple.JSONObject;
|
import org.json.simple.JSONObject;
|
||||||
|
|
||||||
import be.jeffcheasey88.peeratcode.model.Player;
|
|
||||||
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;
|
||||||
|
@ -33,8 +31,8 @@ public class Register implements Response {
|
||||||
@Route(path = "^\\/register$", type = "POST")
|
@Route(path = "^\\/register$", type = "POST")
|
||||||
@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 {
|
||||||
if(user != null){
|
if (user != null) {
|
||||||
HttpUtil.responseHeaders(writer, 403,"Access-Control-Allow-Origin: *");
|
HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
JSONObject informations = (JSONObject) HttpUtil.readJson(reader);
|
JSONObject informations = (JSONObject) HttpUtil.readJson(reader);
|
||||||
|
@ -62,8 +60,7 @@ public class Register implements Response {
|
||||||
int id;
|
int id;
|
||||||
if ((id = databaseRepo.register(pseudo, email, password, firstname, lastname, description, group,
|
if ((id = databaseRepo.register(pseudo, email, password, firstname, lastname, description, group,
|
||||||
avatar)) >= 0) {
|
avatar)) >= 0) {
|
||||||
HttpUtil.responseHeaders(writer, 200,
|
HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *",
|
||||||
"Access-Control-Allow-Origin: *",
|
|
||||||
"Access-Control-Expose-Headers: Authorization",
|
"Access-Control-Expose-Headers: Authorization",
|
||||||
"Authorization: Bearer " + this.router.createAuthUser(id));
|
"Authorization: Bearer " + this.router.createAuthUser(id));
|
||||||
createFolderToSaveSourceCode(pseudo);
|
createFolderToSaveSourceCode(pseudo);
|
||||||
|
@ -82,7 +79,7 @@ public class Register implements Response {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createFolderToSaveSourceCode(String pseudo) throws IOException {
|
private void createFolderToSaveSourceCode(String pseudo) throws IOException {
|
||||||
|
|
||||||
Files.createDirectories(Paths.get(String.format("%s/%s", usersFilesPath, pseudo)));
|
Files.createDirectories(Paths.get(String.format("%s/%s", usersFilesPath, pseudo)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package be.jeffcheasey88.peeratcode.routes;
|
package be.jeffcheasey88.peeratcode.routes;
|
||||||
|
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
|
||||||
|
|
||||||
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
||||||
import be.jeffcheasey88.peeratcode.webserver.HttpReader;
|
import be.jeffcheasey88.peeratcode.webserver.HttpReader;
|
||||||
|
@ -11,25 +10,25 @@ import be.jeffcheasey88.peeratcode.webserver.Response;
|
||||||
import be.jeffcheasey88.peeratcode.webserver.Route;
|
import be.jeffcheasey88.peeratcode.webserver.Route;
|
||||||
import be.jeffcheasey88.peeratcode.webserver.User;
|
import be.jeffcheasey88.peeratcode.webserver.User;
|
||||||
|
|
||||||
public class Result implements Response{
|
public class Result implements Response {
|
||||||
|
|
||||||
private DatabaseRepository repo;
|
private DatabaseRepository repo;
|
||||||
|
|
||||||
public Result(DatabaseRepository repo){
|
public Result(DatabaseRepository repo) {
|
||||||
this.repo = repo;
|
this.repo = repo;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Route(path = "^\\/result\\/(\\d+)$", needLogin = true)
|
@Route(path = "^\\/result\\/(\\d+)$", needLogin = true)
|
||||||
@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 {
|
||||||
int puzzle = Integer.parseInt(matcher.group(1));
|
int puzzle = Integer.parseInt(matcher.group(1));
|
||||||
|
|
||||||
int score = this.repo.getScore(user.getId(), puzzle);
|
int score = this.repo.getScore(user.getId(), puzzle);
|
||||||
if(score < 0) {
|
if (score < 0) {
|
||||||
HttpUtil.responseHeaders(writer, 425, "Access-Control-Allow-Origin: *");
|
HttpUtil.responseHeaders(writer, 425, "Access-Control-Allow-Origin: *");
|
||||||
}else{
|
} else {
|
||||||
HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *");
|
HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *");
|
||||||
writer.write(score+"");
|
writer.write(score + "");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -13,11 +13,11 @@ import be.jeffcheasey88.peeratcode.webserver.Response;
|
||||||
import be.jeffcheasey88.peeratcode.webserver.Route;
|
import be.jeffcheasey88.peeratcode.webserver.Route;
|
||||||
import be.jeffcheasey88.peeratcode.webserver.User;
|
import be.jeffcheasey88.peeratcode.webserver.User;
|
||||||
|
|
||||||
public class CreateGroup implements Response{
|
public class CreateGroup implements Response {
|
||||||
|
|
||||||
private DatabaseRepository repo;
|
private DatabaseRepository repo;
|
||||||
|
|
||||||
public CreateGroup(DatabaseRepository repo){
|
public CreateGroup(DatabaseRepository repo) {
|
||||||
this.repo = repo;
|
this.repo = repo;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,10 +25,10 @@ public class CreateGroup implements Response{
|
||||||
@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.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)))) {
|
||||||
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: *");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,6 @@ package be.jeffcheasey88.peeratcode.routes.groups;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
|
|
||||||
import org.json.simple.JSONArray;
|
import org.json.simple.JSONArray;
|
||||||
import org.json.simple.JSONObject;
|
|
||||||
|
|
||||||
import be.jeffcheasey88.peeratcode.model.Group;
|
import be.jeffcheasey88.peeratcode.model.Group;
|
||||||
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
||||||
|
@ -14,11 +13,11 @@ import be.jeffcheasey88.peeratcode.webserver.Response;
|
||||||
import be.jeffcheasey88.peeratcode.webserver.Route;
|
import be.jeffcheasey88.peeratcode.webserver.Route;
|
||||||
import be.jeffcheasey88.peeratcode.webserver.User;
|
import be.jeffcheasey88.peeratcode.webserver.User;
|
||||||
|
|
||||||
public class GroupList implements Response{
|
public class GroupList implements Response {
|
||||||
|
|
||||||
private DatabaseRepository repo;
|
private DatabaseRepository repo;
|
||||||
|
|
||||||
public GroupList(DatabaseRepository repo){
|
public GroupList(DatabaseRepository repo) {
|
||||||
this.repo = repo;
|
this.repo = repo;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,7 +26,8 @@ public class GroupList implements Response{
|
||||||
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()) result.add(group.toJson());
|
for (Group group : this.repo.getAllGroups())
|
||||||
|
result.add(group.toJson());
|
||||||
writer.write(result.toJSONString());
|
writer.write(result.toJSONString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,20 +5,19 @@ import java.util.Arrays;
|
||||||
|
|
||||||
import org.jose4j.jwa.AlgorithmConstraints.ConstraintType;
|
import org.jose4j.jwa.AlgorithmConstraints.ConstraintType;
|
||||||
import org.jose4j.jwk.RsaJsonWebKey;
|
import org.jose4j.jwk.RsaJsonWebKey;
|
||||||
import org.jose4j.jwk.RsaJwkGenerator;
|
|
||||||
import org.jose4j.jws.AlgorithmIdentifiers;
|
import org.jose4j.jws.AlgorithmIdentifiers;
|
||||||
import org.jose4j.jwt.JwtClaims;
|
import org.jose4j.jwt.JwtClaims;
|
||||||
import org.jose4j.jwt.consumer.JwtConsumer;
|
import org.jose4j.jwt.consumer.JwtConsumer;
|
||||||
import org.jose4j.jwt.consumer.JwtConsumerBuilder;
|
import org.jose4j.jwt.consumer.JwtConsumerBuilder;
|
||||||
|
|
||||||
public class Client extends Thread{
|
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;
|
private RsaJsonWebKey key; // Really needed ?
|
||||||
|
|
||||||
public Client(Socket socket, Router router, RsaJsonWebKey key) throws Exception{
|
public Client(Socket socket, Router router, RsaJsonWebKey key) 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;
|
||||||
|
@ -26,7 +25,7 @@ public class Client extends Thread{
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run(){
|
public void run() {
|
||||||
try {
|
try {
|
||||||
String[] headers = reader.readLine().split("\\s");
|
String[] headers = reader.readLine().split("\\s");
|
||||||
System.out.println(Arrays.toString(headers));
|
System.out.println(Arrays.toString(headers));
|
||||||
|
@ -34,27 +33,24 @@ public class Client extends Thread{
|
||||||
router.exec(headers[0], headers[1], isLogin(reader), reader, writer);
|
router.exec(headers[0], headers[1], isLogin(reader), reader, writer);
|
||||||
writer.flush();
|
writer.flush();
|
||||||
writer.close();
|
writer.close();
|
||||||
} catch (Exception e){
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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) return null;
|
if (auth == null)
|
||||||
|
return null;
|
||||||
try {
|
try {
|
||||||
JwtConsumer jwtConsumer = new JwtConsumerBuilder()
|
JwtConsumer jwtConsumer = new JwtConsumerBuilder().setRequireExpirationTime()
|
||||||
.setRequireExpirationTime()
|
.setAllowedClockSkewInSeconds(30).setExpectedIssuer(this.router.getTokenIssuer())
|
||||||
.setAllowedClockSkewInSeconds(30)
|
.setVerificationKey(this.router.getWebKey().getKey())
|
||||||
.setExpectedIssuer(this.router.getTokenIssuer())
|
.setJwsAlgorithmConstraints(ConstraintType.PERMIT, AlgorithmIdentifiers.RSA_USING_SHA256).build();
|
||||||
.setVerificationKey(this.router.getWebKey().getKey())
|
|
||||||
.setJwsAlgorithmConstraints(
|
JwtClaims jwtClaims = jwtConsumer.processToClaims(auth);
|
||||||
ConstraintType.PERMIT, AlgorithmIdentifiers.RSA_USING_SHA256)
|
return new User(jwtClaims);
|
||||||
.build();
|
} catch (Exception e) {
|
||||||
|
|
||||||
JwtClaims jwtClaims = jwtConsumer.processToClaims(auth);
|
|
||||||
return new User(jwtClaims);
|
|
||||||
}catch(Exception e){
|
|
||||||
HttpUtil.responseHeaders(writer, 401, "Access-Control-Allow-Origin: *");
|
HttpUtil.responseHeaders(writer, 401, "Access-Control-Allow-Origin: *");
|
||||||
writer.flush();
|
writer.flush();
|
||||||
writer.close();
|
writer.close();
|
||||||
|
|
|
@ -7,43 +7,43 @@ import java.io.InputStreamReader;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
|
|
||||||
public class HttpReader {
|
public class HttpReader {
|
||||||
|
|
||||||
private Socket socket;
|
private Socket socket;
|
||||||
private InputStream in;
|
private InputStream in;
|
||||||
private BufferedReader reader;
|
private BufferedReader reader;
|
||||||
|
|
||||||
public HttpReader(Socket socket) throws Exception{
|
public HttpReader(Socket socket) throws Exception {
|
||||||
this.socket = socket;
|
this.socket = socket;
|
||||||
this.in = socket.getInputStream();
|
this.in = socket.getInputStream();
|
||||||
this.reader = new BufferedReader(new InputStreamReader(in));
|
this.reader = new BufferedReader(new InputStreamReader(in));
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isClosed(){
|
public boolean isClosed() {
|
||||||
return this.socket.isClosed();
|
return this.socket.isClosed();
|
||||||
}
|
}
|
||||||
|
|
||||||
public int read(byte[] buffer) throws IOException{
|
public int read(byte[] buffer) throws IOException {
|
||||||
return this.in.read(buffer);
|
return this.in.read(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int read(char[] buffer) throws IOException {
|
public int read(char[] buffer) throws IOException {
|
||||||
return this.reader.read(buffer);
|
return this.reader.read(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String readLine() throws IOException{
|
public String readLine() throws IOException {
|
||||||
return this.reader.readLine();
|
return this.reader.readLine();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean ready() throws IOException{
|
public boolean ready() throws IOException {
|
||||||
return this.reader.ready();
|
return this.reader.ready();
|
||||||
}
|
}
|
||||||
|
|
||||||
public int readInt() throws Exception{
|
public int readInt() throws Exception {
|
||||||
int result = 0;
|
int result = 0;
|
||||||
result+=this.in.read() << 24;
|
result += this.in.read() << 24;
|
||||||
result+=this.in.read() << 16;
|
result += this.in.read() << 16;
|
||||||
result+=this.in.read() << 8;
|
result += this.in.read() << 8;
|
||||||
result+=this.in.read();
|
result += this.in.read();
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -9,326 +9,338 @@ import java.util.regex.Pattern;
|
||||||
import org.json.simple.parser.JSONParser;
|
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{
|
|
||||||
writer.write("HTTP/1.1 "+code+" "+codeMessage(code)+"\n");
|
public static void responseHeaders(HttpWriter writer, int code, String... headers) throws Exception {
|
||||||
for(String header : headers) writer.write(header+"\n");
|
writer.write("HTTP/1.1 " + code + " " + codeMessage(code) + "\n");
|
||||||
|
for (String header : headers)
|
||||||
|
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 {
|
||||||
List<String> list = new ArrayList<>();
|
List<String> list = new ArrayList<>();
|
||||||
|
|
||||||
reader.readLine();
|
reader.readLine();
|
||||||
|
|
||||||
while(reader.ready()){
|
while (reader.ready()) {
|
||||||
String line;
|
String line;
|
||||||
while(((line = reader.readLine()) != null) && (line.length() > 0)){
|
while (((line = reader.readLine()) != null) && (line.length() > 0)) {
|
||||||
|
|
||||||
}
|
}
|
||||||
String buffer = "";
|
String buffer = "";
|
||||||
while(((line = reader.readLine()) != null) && (!line.startsWith("------WebKitFormBoundary"))){
|
while (((line = reader.readLine()) != null) && (!line.startsWith("------WebKitFormBoundary"))) {
|
||||||
buffer+=line;
|
buffer += line;
|
||||||
}
|
}
|
||||||
list.add(buffer);
|
list.add(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
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) throw new IllegalArgumentException();
|
if (key == null)
|
||||||
|
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");
|
||||||
writer.write("Upgrade: websocket\n");
|
writer.write("Upgrade: websocket\n");
|
||||||
writer.write("Sec-WebSocket-Accept: "+
|
writer.write("Sec-WebSocket-Accept: " + printBase64Binary(MessageDigest.getInstance("SHA-1")
|
||||||
printBase64Binary(
|
.digest((key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11").getBytes("UTF-8"))) + "\n");
|
||||||
MessageDigest.getInstance("SHA-1").
|
|
||||||
digest((key+"258EAFA5-E914-47DA-95CA-C5AB0DC85B11").getBytes("UTF-8")))+"\n");
|
|
||||||
writer.write("\n");
|
writer.write("\n");
|
||||||
writer.flush();
|
writer.flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Pattern WEBSOCKET_KEY = Pattern.compile("Sec-WebSocket-Key: (.*)");
|
private static Pattern WEBSOCKET_KEY = Pattern.compile("Sec-WebSocket-Key: (.*)");
|
||||||
|
|
||||||
public static String readWebSocketKey(HttpReader reader) throws Exception {
|
public static String readWebSocketKey(HttpReader reader) throws Exception {
|
||||||
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()) key = matcher.group(1);
|
if (matcher.matches())
|
||||||
|
key = matcher.group(1);
|
||||||
}
|
}
|
||||||
return key;
|
return key;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Pattern AUTORIZATION = Pattern.compile("Authorization: Bearer (.*)");
|
private static Pattern AUTORIZATION = Pattern.compile("Authorization: Bearer (.*)");
|
||||||
|
|
||||||
public static String readAuthorization(HttpReader reader) throws Exception {
|
public static String readAuthorization(HttpReader reader) throws Exception {
|
||||||
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)) {
|
||||||
Matcher matcher = AUTORIZATION.matcher(line);
|
Matcher matcher = AUTORIZATION.matcher(line);
|
||||||
if(matcher.matches()){
|
if (matcher.matches()) {
|
||||||
key = matcher.group(1);
|
key = matcher.group(1);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return key;
|
return key;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Object readJson(HttpReader reader) throws Exception{
|
public static Object readJson(HttpReader reader) throws Exception {
|
||||||
String line = "";
|
String line = "";
|
||||||
while(reader.ready()){
|
while (reader.ready()) {
|
||||||
char[] c = new char[1];
|
char[] c = new char[1];
|
||||||
reader.read(c);
|
reader.read(c);
|
||||||
line+=c[0];
|
line += c[0];
|
||||||
if(c[0] == '}'){
|
if (c[0] == '}') {
|
||||||
Object parse;
|
Object parse;
|
||||||
try {
|
try {
|
||||||
parse = new JSONParser().parse(line);
|
parse = new JSONParser().parse(line);
|
||||||
if(parse != null) return parse;
|
if (parse != null)
|
||||||
}catch(Exception e){}
|
return parse;
|
||||||
|
} catch (Exception e) {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
//I found this code on StackOverFlow !!!!! (and the write too)
|
// I found this code on StackOverFlow !!!!! (and the write too)
|
||||||
public static String readWebSocket(HttpReader reader) throws Exception{
|
public static String readWebSocket(HttpReader reader) throws Exception {
|
||||||
int buffLenth = 1024;
|
int buffLenth = 1024;
|
||||||
int len = 0;
|
int len = 0;
|
||||||
byte[] b = new byte[buffLenth];
|
byte[] b = new byte[buffLenth];
|
||||||
//rawIn is a Socket.getInputStream();
|
// rawIn is a Socket.getInputStream();
|
||||||
while(true){
|
while (true) {
|
||||||
len = reader.read(b);
|
len = reader.read(b);
|
||||||
if(len!=-1){
|
if (len != -1) {
|
||||||
byte rLength = 0;
|
byte rLength = 0;
|
||||||
int rMaskIndex = 2;
|
int rMaskIndex = 2;
|
||||||
int rDataStart = 0;
|
int rDataStart = 0;
|
||||||
//b[0] is always text in my case so no need to check;
|
// b[0] is always text in my case so no need to check;
|
||||||
byte data = b[1];
|
byte data = b[1];
|
||||||
byte op = (byte) 127;
|
byte op = (byte) 127;
|
||||||
rLength = (byte) (data & op);
|
rLength = (byte) (data & op);
|
||||||
|
|
||||||
if(rLength==(byte)126) rMaskIndex=4;
|
if (rLength == (byte) 126)
|
||||||
if(rLength==(byte)127) rMaskIndex=10;
|
rMaskIndex = 4;
|
||||||
|
if (rLength == (byte) 127)
|
||||||
|
rMaskIndex = 10;
|
||||||
|
|
||||||
byte[] masks = new byte[4];
|
byte[] masks = new byte[4];
|
||||||
|
|
||||||
int j=0;
|
int j = 0;
|
||||||
int i=0;
|
int i = 0;
|
||||||
for(i=rMaskIndex;i<(rMaskIndex+4);i++){
|
for (i = rMaskIndex; i < (rMaskIndex + 4); i++) {
|
||||||
masks[j] = b[i];
|
masks[j] = b[i];
|
||||||
j++;
|
j++;
|
||||||
}
|
}
|
||||||
|
|
||||||
rDataStart = rMaskIndex + 4;
|
rDataStart = rMaskIndex + 4;
|
||||||
|
|
||||||
int messLen = len - rDataStart;
|
int messLen = len - rDataStart;
|
||||||
|
|
||||||
byte[] message = new byte[messLen];
|
byte[] message = new byte[messLen];
|
||||||
|
|
||||||
for(i=rDataStart, j=0; i<len; i++, j++){
|
for (i = rDataStart, j = 0; i < len; i++, j++) {
|
||||||
message[j] = (byte) (b[i] ^ masks[j % 4]);
|
message[j] = (byte) (b[i] ^ masks[j % 4]);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new String(message);
|
|
||||||
|
|
||||||
}else break;
|
return new String(message);
|
||||||
}
|
|
||||||
return null;
|
} else
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void sendWebSocket(HttpWriter writer, String message) throws Exception{
|
public static void sendWebSocket(HttpWriter writer, String message) throws Exception {
|
||||||
byte[] rawData = message.getBytes();
|
byte[] rawData = message.getBytes();
|
||||||
|
|
||||||
int frameCount = 0;
|
int frameCount = 0;
|
||||||
byte[] frame = new byte[10];
|
byte[] frame = new byte[10];
|
||||||
|
|
||||||
frame[0] = (byte) 129;
|
frame[0] = (byte) 129;
|
||||||
|
|
||||||
if(rawData.length <= 125){
|
if (rawData.length <= 125) {
|
||||||
frame[1] = (byte) rawData.length;
|
frame[1] = (byte) rawData.length;
|
||||||
frameCount = 2;
|
frameCount = 2;
|
||||||
}else if(rawData.length >= 126 && rawData.length <= 65535){
|
} else if (rawData.length >= 126 && rawData.length <= 65535) {
|
||||||
frame[1] = (byte) 126;
|
frame[1] = (byte) 126;
|
||||||
int len = rawData.length;
|
int len = rawData.length;
|
||||||
frame[2] = (byte)((len >> 8 ) & (byte)255);
|
frame[2] = (byte) ((len >> 8) & (byte) 255);
|
||||||
frame[3] = (byte)(len & (byte)255);
|
frame[3] = (byte) (len & (byte) 255);
|
||||||
frameCount = 4;
|
frameCount = 4;
|
||||||
}else{
|
} else {
|
||||||
frame[1] = (byte) 127;
|
frame[1] = (byte) 127;
|
||||||
int len = rawData.length;
|
int len = rawData.length;
|
||||||
frame[2] = (byte)((len >> 56 ) & (byte)255);
|
frame[2] = (byte) ((len >> 56) & (byte) 255);
|
||||||
frame[3] = (byte)((len >> 48 ) & (byte)255);
|
frame[3] = (byte) ((len >> 48) & (byte) 255);
|
||||||
frame[4] = (byte)((len >> 40 ) & (byte)255);
|
frame[4] = (byte) ((len >> 40) & (byte) 255);
|
||||||
frame[5] = (byte)((len >> 32 ) & (byte)255);
|
frame[5] = (byte) ((len >> 32) & (byte) 255);
|
||||||
frame[6] = (byte)((len >> 24 ) & (byte)255);
|
frame[6] = (byte) ((len >> 24) & (byte) 255);
|
||||||
frame[7] = (byte)((len >> 16 ) & (byte)255);
|
frame[7] = (byte) ((len >> 16) & (byte) 255);
|
||||||
frame[8] = (byte)((len >> 8 ) & (byte)255);
|
frame[8] = (byte) ((len >> 8) & (byte) 255);
|
||||||
frame[9] = (byte)(len & (byte)255);
|
frame[9] = (byte) (len & (byte) 255);
|
||||||
frameCount = 10;
|
frameCount = 10;
|
||||||
}
|
}
|
||||||
|
|
||||||
int bLength = frameCount + rawData.length;
|
int bLength = frameCount + rawData.length;
|
||||||
|
|
||||||
byte[] reply = new byte[bLength];
|
byte[] reply = new byte[bLength];
|
||||||
|
|
||||||
int bLim = 0;
|
int bLim = 0;
|
||||||
for(int i=0; i<frameCount;i++){
|
for (int i = 0; i < frameCount; i++) {
|
||||||
reply[bLim] = frame[i];
|
reply[bLim] = frame[i];
|
||||||
bLim++;
|
bLim++;
|
||||||
}
|
}
|
||||||
for(int i=0; i<rawData.length;i++){
|
for (int i = 0; i < rawData.length; i++) {
|
||||||
reply[bLim] = rawData[i];
|
reply[bLim] = rawData[i];
|
||||||
bLim++;
|
bLim++;
|
||||||
}
|
}
|
||||||
|
|
||||||
writer.write(reply);
|
writer.write(reply);
|
||||||
writer.flush();
|
writer.flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String codeMessage(int paramInt) {
|
private static String codeMessage(int paramInt) {
|
||||||
switch (paramInt) {
|
switch (paramInt) {
|
||||||
case 200:
|
case 200:
|
||||||
return " OK";
|
return " OK";
|
||||||
case 100:
|
case 100:
|
||||||
return " Continue";
|
return " Continue";
|
||||||
case 201:
|
case 201:
|
||||||
return " Created";
|
return " Created";
|
||||||
case 202:
|
case 202:
|
||||||
return " Accepted";
|
return " Accepted";
|
||||||
case 203:
|
case 203:
|
||||||
return " Non-Authoritative Information";
|
return " Non-Authoritative Information";
|
||||||
case 204:
|
case 204:
|
||||||
return " No Content";
|
return " No Content";
|
||||||
case 205:
|
case 205:
|
||||||
return " Reset Content";
|
return " Reset Content";
|
||||||
case 206:
|
case 206:
|
||||||
return " Partial Content";
|
return " Partial Content";
|
||||||
case 300:
|
case 300:
|
||||||
return " Multiple Choices";
|
return " Multiple Choices";
|
||||||
case 301:
|
case 301:
|
||||||
return " Moved Permanently";
|
return " Moved Permanently";
|
||||||
case 302:
|
case 302:
|
||||||
return " Temporary Redirect";
|
return " Temporary Redirect";
|
||||||
case 303:
|
case 303:
|
||||||
return " See Other";
|
return " See Other";
|
||||||
case 304:
|
case 304:
|
||||||
return " Not Modified";
|
return " Not Modified";
|
||||||
case 305:
|
case 305:
|
||||||
return " Use Proxy";
|
return " Use Proxy";
|
||||||
case 400:
|
case 400:
|
||||||
return " Bad Request";
|
return " Bad Request";
|
||||||
case 401:
|
case 401:
|
||||||
return " Unauthorized";
|
return " Unauthorized";
|
||||||
case 402:
|
case 402:
|
||||||
return " Payment Required";
|
return " Payment Required";
|
||||||
case 403:
|
case 403:
|
||||||
return " Forbidden";
|
return " Forbidden";
|
||||||
case 404:
|
case 404:
|
||||||
return " Not Found";
|
return " Not Found";
|
||||||
case 405:
|
case 405:
|
||||||
return " Method Not Allowed";
|
return " Method Not Allowed";
|
||||||
case 406:
|
case 406:
|
||||||
return " Not Acceptable";
|
return " Not Acceptable";
|
||||||
case 407:
|
case 407:
|
||||||
return " Proxy Authentication Required";
|
return " Proxy Authentication Required";
|
||||||
case 408:
|
case 408:
|
||||||
return " Request Time-Out";
|
return " Request Time-Out";
|
||||||
case 409:
|
case 409:
|
||||||
return " Conflict";
|
return " Conflict";
|
||||||
case 410:
|
case 410:
|
||||||
return " Gone";
|
return " Gone";
|
||||||
case 411:
|
case 411:
|
||||||
return " Length Required";
|
return " Length Required";
|
||||||
case 412:
|
case 412:
|
||||||
return " Precondition Failed";
|
return " Precondition Failed";
|
||||||
case 413:
|
case 413:
|
||||||
return " Request Entity Too Large";
|
return " Request Entity Too Large";
|
||||||
case 414:
|
case 414:
|
||||||
return " Request-URI Too Large";
|
return " Request-URI Too Large";
|
||||||
case 415:
|
case 415:
|
||||||
return " Unsupported Media Type";
|
return " Unsupported Media Type";
|
||||||
case 500:
|
case 500:
|
||||||
return " Internal Server Error";
|
return " Internal Server Error";
|
||||||
case 501:
|
case 501:
|
||||||
return " Not Implemented";
|
return " Not Implemented";
|
||||||
case 502:
|
case 502:
|
||||||
return " Bad Gateway";
|
return " Bad Gateway";
|
||||||
case 503:
|
case 503:
|
||||||
return " Service Unavailable";
|
return " Service Unavailable";
|
||||||
case 504:
|
case 504:
|
||||||
return " Gateway Timeout";
|
return " Gateway Timeout";
|
||||||
case 505:
|
case 505:
|
||||||
return " HTTP Version Not Supported";
|
return " HTTP Version Not Supported";
|
||||||
}
|
}
|
||||||
return " ";
|
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) {
|
// From javax.xml.bind.DatatypeConverter
|
||||||
int i = paramInt2;
|
private static String printBase64Binary(byte[] array) {
|
||||||
int j;
|
char[] arrayOfChar = new char[(array.length + 2) / 3 * 4];
|
||||||
for (j = paramInt1; i >= 3; j += 3) {
|
int i = _printBase64Binary(array, 0, array.length, arrayOfChar, 0);
|
||||||
paramArrayOfchar[paramInt3++] = encode(paramArrayOfbyte[j] >> 2);
|
assert i == arrayOfChar.length;
|
||||||
paramArrayOfchar[paramInt3++] = encode((paramArrayOfbyte[j] & 0x3) << 4 | paramArrayOfbyte[j + 1] >> 4 & 0xF);
|
return new String(arrayOfChar);
|
||||||
paramArrayOfchar[paramInt3++] = encode((paramArrayOfbyte[j + 1] & 0xF) << 2 | paramArrayOfbyte[j + 2] >> 6 & 0x3);
|
}
|
||||||
paramArrayOfchar[paramInt3++] = encode(paramArrayOfbyte[j + 2] & 0x3F);
|
|
||||||
i -= 3;
|
private static int _printBase64Binary(byte[] paramArrayOfbyte, int paramInt1, int paramInt2,
|
||||||
}
|
char[] paramArrayOfchar, int paramInt3) {
|
||||||
if (i == 1) {
|
int i = paramInt2;
|
||||||
paramArrayOfchar[paramInt3++] = encode(paramArrayOfbyte[j] >> 2);
|
int j;
|
||||||
paramArrayOfchar[paramInt3++] = encode((paramArrayOfbyte[j] & 0x3) << 4);
|
for (j = paramInt1; i >= 3; j += 3) {
|
||||||
paramArrayOfchar[paramInt3++] = '=';
|
paramArrayOfchar[paramInt3++] = encode(paramArrayOfbyte[j] >> 2);
|
||||||
paramArrayOfchar[paramInt3++] = '=';
|
paramArrayOfchar[paramInt3++] = encode(
|
||||||
}
|
(paramArrayOfbyte[j] & 0x3) << 4 | paramArrayOfbyte[j + 1] >> 4 & 0xF);
|
||||||
if (i == 2) {
|
paramArrayOfchar[paramInt3++] = encode(
|
||||||
paramArrayOfchar[paramInt3++] = encode(paramArrayOfbyte[j] >> 2);
|
(paramArrayOfbyte[j + 1] & 0xF) << 2 | paramArrayOfbyte[j + 2] >> 6 & 0x3);
|
||||||
paramArrayOfchar[paramInt3++] = encode((paramArrayOfbyte[j] & 0x3) << 4 | paramArrayOfbyte[j + 1] >> 4 & 0xF);
|
paramArrayOfchar[paramInt3++] = encode(paramArrayOfbyte[j + 2] & 0x3F);
|
||||||
paramArrayOfchar[paramInt3++] = encode((paramArrayOfbyte[j + 1] & 0xF) << 2);
|
i -= 3;
|
||||||
paramArrayOfchar[paramInt3++] = '=';
|
}
|
||||||
}
|
if (i == 1) {
|
||||||
return paramInt3;
|
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) {
|
private static char encode(int paramInt) {
|
||||||
return encodeMap[paramInt & 0x3F];
|
return encodeMap[paramInt & 0x3F];
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final char[] encodeMap = initEncodeMap();
|
private static final char[] encodeMap = initEncodeMap();
|
||||||
|
|
||||||
private static char[] initEncodeMap() {
|
private static char[] initEncodeMap() {
|
||||||
char[] arrayOfChar = new char[64];
|
char[] arrayOfChar = new char[64];
|
||||||
byte b;
|
byte b;
|
||||||
for (b = 0; b < 26; b++)
|
for (b = 0; b < 26; b++)
|
||||||
arrayOfChar[b] = (char)(65 + b);
|
arrayOfChar[b] = (char) (65 + b);
|
||||||
for (b = 26; b < 52; b++)
|
for (b = 26; b < 52; b++)
|
||||||
arrayOfChar[b] = (char)(97 + b - 26);
|
arrayOfChar[b] = (char) (97 + b - 26);
|
||||||
for (b = 52; b < 62; b++)
|
for (b = 52; b < 62; b++)
|
||||||
arrayOfChar[b] = (char)(48 + b - 52);
|
arrayOfChar[b] = (char) (48 + b - 52);
|
||||||
arrayOfChar[62] = '+';
|
arrayOfChar[62] = '+';
|
||||||
arrayOfChar[63] = '/';
|
arrayOfChar[63] = '/';
|
||||||
return arrayOfChar;
|
return arrayOfChar;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,30 +7,30 @@ import java.io.OutputStreamWriter;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
public class HttpWriter{
|
public class HttpWriter {
|
||||||
|
|
||||||
private OutputStream out;
|
private OutputStream out;
|
||||||
private BufferedWriter writer;
|
private BufferedWriter writer;
|
||||||
|
|
||||||
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, StandardCharsets.UTF_8));
|
this.writer = new BufferedWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void write(byte[] buffer) throws IOException{
|
public void write(byte[] buffer) throws IOException {
|
||||||
this.out.write(buffer);
|
this.out.write(buffer);
|
||||||
this.out.flush();
|
this.out.flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void write(String message) throws IOException{
|
public void write(String message) throws IOException {
|
||||||
this.writer.write(message);
|
this.writer.write(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void flush() throws IOException{
|
public void flush() throws IOException {
|
||||||
this.writer.flush();
|
this.writer.flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void close() throws IOException{
|
public void close() throws IOException {
|
||||||
this.writer.close();
|
this.writer.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,10 +1,9 @@
|
||||||
package be.jeffcheasey88.peeratcode.webserver;
|
package be.jeffcheasey88.peeratcode.webserver;
|
||||||
|
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
|
||||||
|
|
||||||
public interface Response{
|
public interface Response {
|
||||||
|
|
||||||
|
void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception;
|
||||||
|
|
||||||
void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception ;
|
|
||||||
|
|
||||||
}
|
}
|
|
@ -8,9 +8,11 @@ import java.lang.annotation.Target;
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
@Target(ElementType.METHOD)
|
@Target(ElementType.METHOD)
|
||||||
public @interface Route {
|
public @interface Route {
|
||||||
|
|
||||||
String path() default "^.*$";
|
String path() default "^.*$";
|
||||||
|
|
||||||
String type() default "GET";
|
String type() default "GET";
|
||||||
|
|
||||||
boolean needLogin() default false;
|
boolean needLogin() default false;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,8 +16,8 @@ import org.jose4j.lang.JoseException;
|
||||||
|
|
||||||
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
||||||
|
|
||||||
public class Router{
|
public class Router {
|
||||||
|
|
||||||
private Map<Response, Route> responses;
|
private Map<Response, Route> responses;
|
||||||
private Map<Response, Pattern> patterns;
|
private Map<Response, Pattern> patterns;
|
||||||
private Response noFileFound;
|
private Response noFileFound;
|
||||||
|
@ -25,73 +25,76 @@ public class Router{
|
||||||
private DatabaseRepository repo;
|
private DatabaseRepository repo;
|
||||||
private String token_issuer;
|
private String token_issuer;
|
||||||
private int token_expiration;
|
private int token_expiration;
|
||||||
|
|
||||||
public Router(DatabaseRepository repo, String token_issuer, int token_expiration) throws Exception{
|
public Router(DatabaseRepository repo, String token_issuer, int token_expiration) throws Exception {
|
||||||
this.repo = repo;
|
this.repo = repo;
|
||||||
this.token_issuer = token_issuer;
|
this.token_issuer = token_issuer;
|
||||||
this.token_expiration = token_expiration;
|
this.token_expiration = token_expiration;
|
||||||
this.responses = new HashMap<>();
|
this.responses = new HashMap<>();
|
||||||
this.patterns = new HashMap<>();
|
this.patterns = new HashMap<>();
|
||||||
this.rsaJsonWebKey = RsaJwkGenerator.generateJwk(2048);
|
this.rsaJsonWebKey = RsaJwkGenerator.generateJwk(2048);
|
||||||
}
|
}
|
||||||
|
|
||||||
public DatabaseRepository getDataBase(){
|
public DatabaseRepository getDataBase() {
|
||||||
return this.repo;
|
return this.repo;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void register(Response response){
|
public void register(Response response) {
|
||||||
try {
|
try {
|
||||||
Method method = response.getClass().getDeclaredMethod("exec", Response.class.getDeclaredMethods()[0].getParameterTypes());
|
Method method = response.getClass().getDeclaredMethod("exec",
|
||||||
|
Response.class.getDeclaredMethods()[0].getParameterTypes());
|
||||||
Route route = method.getAnnotation(Route.class);
|
Route route = method.getAnnotation(Route.class);
|
||||||
|
|
||||||
this.responses.put(response, route);
|
this.responses.put(response, route);
|
||||||
this.patterns.put(response, Pattern.compile(route.path()));
|
this.patterns.put(response, Pattern.compile(route.path()));
|
||||||
} catch (Exception e){
|
} catch (Exception e) {
|
||||||
throw new IllegalArgumentException(e);
|
throw new IllegalArgumentException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setDefault(Response response){
|
public void setDefault(Response response) {
|
||||||
this.noFileFound = response;
|
this.noFileFound = response;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void exec(String type, String path, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
public void exec(String type, String path, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
||||||
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()) return;
|
if (user == null && routes.getValue().needLogin())
|
||||||
|
return;
|
||||||
routes.getKey().exec(matcher, user, reader, writer);
|
routes.getKey().exec(matcher, user, reader, writer);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(noFileFound != null) noFileFound.exec(null, user, reader, writer);
|
if (noFileFound != null)
|
||||||
|
noFileFound.exec(null, user, reader, writer);
|
||||||
}
|
}
|
||||||
|
|
||||||
public RsaJsonWebKey getWebKey(){
|
public RsaJsonWebKey getWebKey() {
|
||||||
return this.rsaJsonWebKey;
|
return this.rsaJsonWebKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getTokenIssuer(){
|
public String getTokenIssuer() {
|
||||||
return this.token_issuer;
|
return this.token_issuer;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String createAuthUser(int id) throws JoseException{
|
public String createAuthUser(int id) throws JoseException {
|
||||||
JwtClaims claims = new JwtClaims();
|
JwtClaims claims = new JwtClaims();
|
||||||
claims.setIssuer(token_issuer); // who creates the token and signs it
|
claims.setIssuer(token_issuer); // who creates the token and signs it
|
||||||
claims.setExpirationTimeMinutesInTheFuture(token_expiration);
|
claims.setExpirationTimeMinutesInTheFuture(token_expiration);
|
||||||
claims.setGeneratedJwtId(); // a unique identifier for the token
|
claims.setGeneratedJwtId(); // a unique identifier for the token
|
||||||
claims.setIssuedAtToNow(); // when the token was issued/created (now)
|
claims.setIssuedAtToNow(); // when the token was issued/created (now)
|
||||||
claims.setNotBeforeMinutesInThePast(2); // time before which the token is not yet valid (2 minutes ago)
|
claims.setNotBeforeMinutesInThePast(2); // time before which the token is not yet valid (2 minutes ago)
|
||||||
|
|
||||||
claims.setClaim("id", id);
|
claims.setClaim("id", id);
|
||||||
|
|
||||||
JsonWebSignature jws = new JsonWebSignature();
|
JsonWebSignature jws = new JsonWebSignature();
|
||||||
jws.setPayload(claims.toJson());
|
jws.setPayload(claims.toJson());
|
||||||
jws.setKey(rsaJsonWebKey.getPrivateKey());
|
jws.setKey(rsaJsonWebKey.getPrivateKey());
|
||||||
jws.setKeyIdHeaderValue(rsaJsonWebKey.getKeyId());
|
jws.setKeyIdHeaderValue(rsaJsonWebKey.getKeyId());
|
||||||
jws.setAlgorithmHeaderValue(AlgorithmIdentifiers.RSA_USING_SHA256);
|
jws.setAlgorithmHeaderValue(AlgorithmIdentifiers.RSA_USING_SHA256);
|
||||||
return jws.getCompactSerialization();
|
return jws.getCompactSerialization();
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -5,12 +5,12 @@ import org.jose4j.jwt.JwtClaims;
|
||||||
public class User {
|
public class User {
|
||||||
|
|
||||||
private int id;
|
private int id;
|
||||||
|
|
||||||
public User(JwtClaims jwtClaims){
|
public User(JwtClaims jwtClaims) {
|
||||||
this.id = ((Long)jwtClaims.getClaimValue("id")).intValue();
|
this.id = ((Long) jwtClaims.getClaimValue("id")).intValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getId(){
|
public int getId() {
|
||||||
return this.id;
|
return this.id;
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Add table
Reference in a new issue