merging
This commit is contained in:
commit
0585e073e5
30 changed files with 859 additions and 694 deletions
|
@ -8,139 +8,152 @@ import java.io.FileWriter;
|
|||
import java.lang.reflect.Field;
|
||||
|
||||
public class Configuration {
|
||||
|
||||
|
||||
private String db_host;
|
||||
private int db_port;
|
||||
private String db_user;
|
||||
private String db_database;
|
||||
private String db_password;
|
||||
|
||||
|
||||
private int tcp_port;
|
||||
private boolean use_ssl;
|
||||
private String ssl_keystore;
|
||||
private String ssl_keystorePasswd;
|
||||
|
||||
private String users_files;
|
||||
|
||||
private String token_issuer;
|
||||
private int token_expiration;
|
||||
|
||||
|
||||
private File _file;
|
||||
|
||||
public Configuration(String path){
|
||||
|
||||
public Configuration(String 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{
|
||||
if(!this._file.exists()) return;
|
||||
|
||||
public void load() throws Exception {
|
||||
if (!this._file.exists())
|
||||
return;
|
||||
BufferedReader reader = new BufferedReader(new FileReader(this._file));
|
||||
String line;
|
||||
while((line = reader.readLine()) != null){
|
||||
while ((line = reader.readLine()) != null) {
|
||||
String[] split = line.split("=");
|
||||
Field field = getClass().getDeclaredField(split[0]);
|
||||
if(field == null) continue;
|
||||
if (field == null)
|
||||
continue;
|
||||
field.setAccessible(true);
|
||||
injectValue(field, split[1]);
|
||||
}
|
||||
reader.close();
|
||||
}
|
||||
|
||||
private void injectValue(Field field, String value) throws IllegalAccessException{
|
||||
if(field.getType().isPrimitive()){
|
||||
switch(field.getType().getName()){
|
||||
case "boolean":
|
||||
field.setBoolean(this, Boolean.parseBoolean(value));
|
||||
break;
|
||||
case "byte":
|
||||
field.setByte(this, Byte.parseByte(value));
|
||||
break;
|
||||
case "char":
|
||||
field.setChar(this, value.charAt(0));
|
||||
break;
|
||||
case "double":
|
||||
field.setDouble(this, Double.parseDouble(value));
|
||||
break;
|
||||
case "float":
|
||||
field.setFloat(this, Float.parseFloat(value));
|
||||
break;
|
||||
case "int":
|
||||
field.setInt(this, Integer.parseInt(value));
|
||||
break;
|
||||
case "long":
|
||||
field.setLong(this, Long.parseLong(value));
|
||||
break;
|
||||
case "short":
|
||||
field.setShort(this, Short.parseShort(value));
|
||||
break;
|
||||
default: throw new IllegalArgumentException(value);
|
||||
|
||||
private void injectValue(Field field, String value) throws IllegalAccessException {
|
||||
if (field.getType().isPrimitive()) {
|
||||
switch (field.getType().getName()) {
|
||||
case "boolean":
|
||||
field.setBoolean(this, Boolean.parseBoolean(value));
|
||||
break;
|
||||
case "byte":
|
||||
field.setByte(this, Byte.parseByte(value));
|
||||
break;
|
||||
case "char":
|
||||
field.setChar(this, value.charAt(0));
|
||||
break;
|
||||
case "double":
|
||||
field.setDouble(this, Double.parseDouble(value));
|
||||
break;
|
||||
case "float":
|
||||
field.setFloat(this, Float.parseFloat(value));
|
||||
break;
|
||||
case "int":
|
||||
field.setInt(this, Integer.parseInt(value));
|
||||
break;
|
||||
case "long":
|
||||
field.setLong(this, Long.parseLong(value));
|
||||
break;
|
||||
case "short":
|
||||
field.setShort(this, Short.parseShort(value));
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException(value);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if(field.getType().equals(String.class)){
|
||||
if (field.getType().equals(String.class)) {
|
||||
field.set(this, value);
|
||||
return;
|
||||
}
|
||||
throw new IllegalArgumentException(value);
|
||||
}
|
||||
|
||||
public void save() throws Exception{
|
||||
if(!_file.exists()){
|
||||
|
||||
public void save() throws Exception {
|
||||
if (!_file.exists()) {
|
||||
File parent = _file.getParentFile();
|
||||
if(!parent.exists()) parent.mkdirs();
|
||||
if (!parent.exists())
|
||||
parent.mkdirs();
|
||||
_file.createNewFile();
|
||||
}
|
||||
Field[] fields = getClass().getDeclaredFields();
|
||||
BufferedWriter writer = new BufferedWriter(new FileWriter(_file));
|
||||
for(Field field : fields){
|
||||
for (Field field : fields) {
|
||||
field.setAccessible(true);
|
||||
if(field.getName().startsWith("_")) continue;
|
||||
if (field.getName().startsWith("_"))
|
||||
continue;
|
||||
Object value = field.get(this);
|
||||
writer.write(field.getName()+"="+value);
|
||||
writer.write(field.getName() + "=" + value);
|
||||
}
|
||||
writer.flush();
|
||||
writer.close();
|
||||
}
|
||||
|
||||
public String getDbHost(){
|
||||
|
||||
public String getDbHost() {
|
||||
return this.db_host;
|
||||
}
|
||||
|
||||
public int getDbPort(){
|
||||
|
||||
public int getDbPort() {
|
||||
return this.db_port;
|
||||
}
|
||||
|
||||
public String getDbUser(){
|
||||
|
||||
public String getDbUser() {
|
||||
return this.db_user;
|
||||
}
|
||||
|
||||
public String getDbDatabase(){
|
||||
|
||||
public String getDbDatabase() {
|
||||
return this.db_database;
|
||||
}
|
||||
|
||||
public String getDbPassword(){
|
||||
|
||||
public String getDbPassword() {
|
||||
return this.db_password;
|
||||
}
|
||||
|
||||
public String getSslKeystore(){
|
||||
|
||||
public String getSslKeystore() {
|
||||
return this.ssl_keystore;
|
||||
}
|
||||
|
||||
public String getTokenIssuer(){
|
||||
|
||||
public String getTokenIssuer() {
|
||||
return this.token_issuer;
|
||||
}
|
||||
|
||||
public int getTokenExpiration(){
|
||||
|
||||
public int getTokenExpiration() {
|
||||
return this.token_expiration;
|
||||
}
|
||||
|
||||
public String getSslKeystorePasswd(){
|
||||
|
||||
public String getSslKeystorePasswd() {
|
||||
return this.ssl_keystorePasswd;
|
||||
}
|
||||
|
||||
public int getTcpPort(){
|
||||
|
||||
public int getTcpPort() {
|
||||
return this.tcp_port;
|
||||
}
|
||||
|
||||
public boolean useSsl(){
|
||||
|
||||
public boolean useSsl() {
|
||||
return this.use_ssl;
|
||||
}
|
||||
}
|
||||
|
||||
public String getUsersFiles() {
|
||||
if (users_files == null || users_files.trim().isEmpty())
|
||||
users_files = "/tmp/users_files";
|
||||
return users_files;
|
||||
}
|
||||
}
|
|
@ -40,9 +40,10 @@ public class Main {
|
|||
|
||||
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
|
||||
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
||||
HttpUtil.responseHeaders(writer, 404, "Access-Control-Allow-Origin: *");
|
||||
|
@ -51,41 +52,39 @@ public class Main {
|
|||
writer.close();
|
||||
}
|
||||
});
|
||||
|
||||
router.register(new Response(){
|
||||
|
||||
router.register(new Response() {
|
||||
@Route(path = "^(.*)$", type = "OPTIONS")
|
||||
@Override
|
||||
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
||||
HttpUtil.responseHeaders(writer, 200,
|
||||
"Access-Control-Allow-Origin: *",
|
||||
"Access-Control-Allow-Methods: *",
|
||||
"Access-Control-Allow-Headers: *");
|
||||
HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *",
|
||||
"Access-Control-Allow-Methods: *", "Access-Control-Allow-Headers: *");
|
||||
}
|
||||
});
|
||||
|
||||
initRoutes(router);
|
||||
initRoutes(router, config);
|
||||
|
||||
startWebServer(config, router);
|
||||
}
|
||||
|
||||
private static void initRoutes(Router router) {
|
||||
private static void initRoutes(Router router, Configuration config) {
|
||||
router.register(new ChapterElement(router.getDataBase()));
|
||||
router.register(new ChapterList(router.getDataBase()));
|
||||
router.register(new PuzzleElement(router.getDataBase()));
|
||||
router.register(new Register(router.getDataBase(), router));
|
||||
router.register(new Register(router.getDataBase(), router, config.getUsersFiles()));
|
||||
router.register(new Login(router.getDataBase(), router));
|
||||
router.register(new Result(router.getDataBase()));
|
||||
router.register(new PuzzleResponse(router.getDataBase()));
|
||||
router.register(new PuzzleResponse(router.getDataBase(), config.getUsersFiles()));
|
||||
router.register(new Leaderboard(router.getDataBase()));
|
||||
router.register(new PlayerDetails(router.getDataBase()));
|
||||
router.register(new BadgeDetails(router.getDataBase()));
|
||||
|
||||
|
||||
router.register(new GroupList(router.getDataBase()));
|
||||
router.register(new CreateGroup(router.getDataBase()));
|
||||
}
|
||||
|
||||
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;
|
||||
try {
|
||||
System.setProperty("javax.net.ssl.keyStore", config.getSslKeystore());
|
||||
|
@ -106,17 +105,15 @@ public class Main {
|
|||
server.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
try (ServerSocket server = new ServerSocket(config.getTcpPort())){
|
||||
while(!server.isClosed()){
|
||||
} else {
|
||||
try (ServerSocket server = new ServerSocket(config.getTcpPort())) {
|
||||
while (!server.isClosed()) {
|
||||
Socket socket = server.accept();
|
||||
RsaJsonWebKey rsaJsonWebKey = RsaJwkGenerator.generateJwk(2048);
|
||||
Client client = new Client(socket, router, rsaJsonWebKey);
|
||||
client.start();
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,20 +5,19 @@ import java.util.Arrays;
|
|||
|
||||
import org.jose4j.jwa.AlgorithmConstraints.ConstraintType;
|
||||
import org.jose4j.jwk.RsaJsonWebKey;
|
||||
import org.jose4j.jwk.RsaJwkGenerator;
|
||||
import org.jose4j.jws.AlgorithmIdentifiers;
|
||||
import org.jose4j.jwt.JwtClaims;
|
||||
import org.jose4j.jwt.consumer.JwtConsumer;
|
||||
import org.jose4j.jwt.consumer.JwtConsumerBuilder;
|
||||
|
||||
public class Client extends Thread{
|
||||
|
||||
public class Client extends Thread {
|
||||
|
||||
private HttpReader reader;
|
||||
private HttpWriter writer;
|
||||
private Router router;
|
||||
private RsaJsonWebKey key;
|
||||
|
||||
public Client(Socket socket, Router router, RsaJsonWebKey key) throws Exception{
|
||||
private RsaJsonWebKey key; // Really needed ?
|
||||
|
||||
public Client(Socket socket, Router router, RsaJsonWebKey key) throws Exception {
|
||||
this.reader = new HttpReader(socket);
|
||||
this.writer = new HttpWriter(socket);
|
||||
this.router = router;
|
||||
|
@ -26,7 +25,7 @@ public class Client extends Thread{
|
|||
}
|
||||
|
||||
@Override
|
||||
public void run(){
|
||||
public void run() {
|
||||
try {
|
||||
String[] headers = reader.readLine().split("\\s");
|
||||
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);
|
||||
writer.flush();
|
||||
writer.close();
|
||||
} catch (Exception e){
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private User isLogin(HttpReader reader) throws Exception{
|
||||
|
||||
private User isLogin(HttpReader reader) throws Exception {
|
||||
String auth = HttpUtil.readAuthorization(reader);
|
||||
if(auth == null) return null;
|
||||
if (auth == null)
|
||||
return null;
|
||||
try {
|
||||
JwtConsumer jwtConsumer = new JwtConsumerBuilder()
|
||||
.setRequireExpirationTime()
|
||||
.setAllowedClockSkewInSeconds(30)
|
||||
.setExpectedIssuer(this.router.getTokenIssuer())
|
||||
.setVerificationKey(this.router.getWebKey().getKey())
|
||||
.setJwsAlgorithmConstraints(
|
||||
ConstraintType.PERMIT, AlgorithmIdentifiers.RSA_USING_SHA256)
|
||||
.build();
|
||||
|
||||
JwtClaims jwtClaims = jwtConsumer.processToClaims(auth);
|
||||
return new User(jwtClaims);
|
||||
}catch(Exception e){
|
||||
JwtConsumer jwtConsumer = new JwtConsumerBuilder().setRequireExpirationTime()
|
||||
.setAllowedClockSkewInSeconds(30).setExpectedIssuer(this.router.getTokenIssuer())
|
||||
.setVerificationKey(this.router.getWebKey().getKey())
|
||||
.setJwsAlgorithmConstraints(ConstraintType.PERMIT, AlgorithmIdentifiers.RSA_USING_SHA256).build();
|
||||
|
||||
JwtClaims jwtClaims = jwtConsumer.processToClaims(auth);
|
||||
return new User(jwtClaims);
|
||||
} catch (Exception e) {
|
||||
HttpUtil.responseHeaders(writer, 401, "Access-Control-Allow-Origin: *");
|
||||
writer.flush();
|
||||
writer.close();
|
||||
|
|
|
@ -7,43 +7,43 @@ import java.io.InputStreamReader;
|
|||
import java.net.Socket;
|
||||
|
||||
public class HttpReader {
|
||||
|
||||
|
||||
private Socket socket;
|
||||
private InputStream in;
|
||||
private BufferedReader reader;
|
||||
|
||||
public HttpReader(Socket socket) throws Exception{
|
||||
|
||||
public HttpReader(Socket socket) throws Exception {
|
||||
this.socket = socket;
|
||||
this.in = socket.getInputStream();
|
||||
this.reader = new BufferedReader(new InputStreamReader(in));
|
||||
}
|
||||
|
||||
public boolean isClosed(){
|
||||
|
||||
public boolean isClosed() {
|
||||
return this.socket.isClosed();
|
||||
}
|
||||
|
||||
public int read(byte[] buffer) throws IOException{
|
||||
|
||||
public int read(byte[] buffer) throws IOException {
|
||||
return this.in.read(buffer);
|
||||
}
|
||||
|
||||
|
||||
public int read(char[] buffer) throws IOException {
|
||||
return this.reader.read(buffer);
|
||||
}
|
||||
|
||||
public String readLine() throws IOException{
|
||||
|
||||
public String readLine() throws IOException {
|
||||
return this.reader.readLine();
|
||||
}
|
||||
|
||||
public boolean ready() throws IOException{
|
||||
|
||||
public boolean ready() throws IOException {
|
||||
return this.reader.ready();
|
||||
}
|
||||
|
||||
public int readInt() throws Exception{
|
||||
int result = 0;
|
||||
result+=this.in.read() << 24;
|
||||
result+=this.in.read() << 16;
|
||||
result+=this.in.read() << 8;
|
||||
result+=this.in.read();
|
||||
return result;
|
||||
}
|
||||
|
||||
public int readInt() throws Exception {
|
||||
int result = 0;
|
||||
result += this.in.read() << 24;
|
||||
result += this.in.read() << 16;
|
||||
result += this.in.read() << 8;
|
||||
result += this.in.read();
|
||||
return result;
|
||||
}
|
||||
}
|
|
@ -9,326 +9,338 @@ import java.util.regex.Pattern;
|
|||
import org.json.simple.parser.JSONParser;
|
||||
|
||||
public class HttpUtil {
|
||||
|
||||
private HttpUtil(){}
|
||||
|
||||
public static void responseHeaders(HttpWriter writer, int code, String... headers) throws Exception{
|
||||
writer.write("HTTP/1.1 "+code+" "+codeMessage(code)+"\n");
|
||||
for(String header : headers) writer.write(header+"\n");
|
||||
|
||||
private HttpUtil() {
|
||||
}
|
||||
|
||||
public static void responseHeaders(HttpWriter writer, int code, String... headers) throws Exception {
|
||||
writer.write("HTTP/1.1 " + code + " " + codeMessage(code) + "\n");
|
||||
for (String header : headers)
|
||||
writer.write(header + "\n");
|
||||
writer.write("\n");
|
||||
writer.flush();
|
||||
}
|
||||
|
||||
public static void skipHeaders(HttpReader reader) throws Exception{
|
||||
|
||||
public static void skipHeaders(HttpReader reader) throws Exception {
|
||||
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<>();
|
||||
|
||||
|
||||
reader.readLine();
|
||||
|
||||
while(reader.ready()){
|
||||
|
||||
while (reader.ready()) {
|
||||
String line;
|
||||
while(((line = reader.readLine()) != null) && (line.length() > 0)){
|
||||
|
||||
while (((line = reader.readLine()) != null) && (line.length() > 0)) {
|
||||
|
||||
}
|
||||
String buffer = "";
|
||||
while(((line = reader.readLine()) != null) && (!line.startsWith("------WebKitFormBoundary"))){
|
||||
buffer+=line;
|
||||
while (((line = reader.readLine()) != null) && (!line.startsWith("------WebKitFormBoundary"))) {
|
||||
buffer += line;
|
||||
}
|
||||
list.add(buffer);
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
if(key == null) throw new IllegalArgumentException();
|
||||
|
||||
if (key == null)
|
||||
throw new IllegalArgumentException();
|
||||
|
||||
writer.write("HTTP/1.1 101 Switching Protocols\n");
|
||||
writer.write("Connection: Upgrade\n");
|
||||
writer.write("Upgrade: websocket\n");
|
||||
writer.write("Sec-WebSocket-Accept: "+
|
||||
printBase64Binary(
|
||||
MessageDigest.getInstance("SHA-1").
|
||||
digest((key+"258EAFA5-E914-47DA-95CA-C5AB0DC85B11").getBytes("UTF-8")))+"\n");
|
||||
writer.write("Sec-WebSocket-Accept: " + printBase64Binary(MessageDigest.getInstance("SHA-1")
|
||||
.digest((key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11").getBytes("UTF-8"))) + "\n");
|
||||
writer.write("\n");
|
||||
writer.flush();
|
||||
}
|
||||
|
||||
|
||||
private static Pattern WEBSOCKET_KEY = Pattern.compile("Sec-WebSocket-Key: (.*)");
|
||||
|
||||
|
||||
public static String readWebSocketKey(HttpReader reader) throws Exception {
|
||||
String line;
|
||||
String key = null;
|
||||
while(((line = reader.readLine()) != null) && (line.length() > 0)){
|
||||
if(key != null){
|
||||
while (((line = reader.readLine()) != null) && (line.length() > 0)) {
|
||||
if (key != null) {
|
||||
continue;
|
||||
}
|
||||
Matcher matcher = WEBSOCKET_KEY.matcher(line);
|
||||
if(matcher.matches()) key = matcher.group(1);
|
||||
if (matcher.matches())
|
||||
key = matcher.group(1);
|
||||
}
|
||||
return key;
|
||||
}
|
||||
|
||||
|
||||
private static Pattern AUTORIZATION = Pattern.compile("Authorization: Bearer (.*)");
|
||||
|
||||
|
||||
public static String readAuthorization(HttpReader reader) throws Exception {
|
||||
String line;
|
||||
String key = null;
|
||||
while(((line = reader.readLine()) != null) && (line.length() > 0)){
|
||||
while (((line = reader.readLine()) != null) && (line.length() > 0)) {
|
||||
Matcher matcher = AUTORIZATION.matcher(line);
|
||||
if(matcher.matches()){
|
||||
if (matcher.matches()) {
|
||||
key = matcher.group(1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return key;
|
||||
}
|
||||
|
||||
public static Object readJson(HttpReader reader) throws Exception{
|
||||
|
||||
public static Object readJson(HttpReader reader) throws Exception {
|
||||
String line = "";
|
||||
while(reader.ready()){
|
||||
while (reader.ready()) {
|
||||
char[] c = new char[1];
|
||||
reader.read(c);
|
||||
line+=c[0];
|
||||
if(c[0] == '}'){
|
||||
line += c[0];
|
||||
if (c[0] == '}') {
|
||||
Object parse;
|
||||
try {
|
||||
parse = new JSONParser().parse(line);
|
||||
if(parse != null) return parse;
|
||||
}catch(Exception e){}
|
||||
if (parse != null)
|
||||
return parse;
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
//I found this code on StackOverFlow !!!!! (and the write too)
|
||||
public static String readWebSocket(HttpReader reader) throws Exception{
|
||||
|
||||
// I found this code on StackOverFlow !!!!! (and the write too)
|
||||
public static String readWebSocket(HttpReader reader) throws Exception {
|
||||
int buffLenth = 1024;
|
||||
int len = 0;
|
||||
byte[] b = new byte[buffLenth];
|
||||
//rawIn is a Socket.getInputStream();
|
||||
while(true){
|
||||
len = reader.read(b);
|
||||
if(len!=-1){
|
||||
byte rLength = 0;
|
||||
int rMaskIndex = 2;
|
||||
int rDataStart = 0;
|
||||
//b[0] is always text in my case so no need to check;
|
||||
byte data = b[1];
|
||||
byte op = (byte) 127;
|
||||
rLength = (byte) (data & op);
|
||||
int len = 0;
|
||||
byte[] b = new byte[buffLenth];
|
||||
// rawIn is a Socket.getInputStream();
|
||||
while (true) {
|
||||
len = reader.read(b);
|
||||
if (len != -1) {
|
||||
byte rLength = 0;
|
||||
int rMaskIndex = 2;
|
||||
int rDataStart = 0;
|
||||
// b[0] is always text in my case so no need to check;
|
||||
byte data = b[1];
|
||||
byte op = (byte) 127;
|
||||
rLength = (byte) (data & op);
|
||||
|
||||
if(rLength==(byte)126) rMaskIndex=4;
|
||||
if(rLength==(byte)127) rMaskIndex=10;
|
||||
if (rLength == (byte) 126)
|
||||
rMaskIndex = 4;
|
||||
if (rLength == (byte) 127)
|
||||
rMaskIndex = 10;
|
||||
|
||||
byte[] masks = new byte[4];
|
||||
byte[] masks = new byte[4];
|
||||
|
||||
int j=0;
|
||||
int i=0;
|
||||
for(i=rMaskIndex;i<(rMaskIndex+4);i++){
|
||||
masks[j] = b[i];
|
||||
j++;
|
||||
}
|
||||
int j = 0;
|
||||
int i = 0;
|
||||
for (i = rMaskIndex; i < (rMaskIndex + 4); i++) {
|
||||
masks[j] = b[i];
|
||||
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++){
|
||||
message[j] = (byte) (b[i] ^ masks[j % 4]);
|
||||
}
|
||||
|
||||
return new String(message);
|
||||
for (i = rDataStart, j = 0; i < len; i++, j++) {
|
||||
message[j] = (byte) (b[i] ^ masks[j % 4]);
|
||||
}
|
||||
|
||||
}else break;
|
||||
}
|
||||
return null;
|
||||
return new String(message);
|
||||
|
||||
} 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();
|
||||
|
||||
int frameCount = 0;
|
||||
byte[] frame = new byte[10];
|
||||
int frameCount = 0;
|
||||
byte[] frame = new byte[10];
|
||||
|
||||
frame[0] = (byte) 129;
|
||||
frame[0] = (byte) 129;
|
||||
|
||||
if(rawData.length <= 125){
|
||||
frame[1] = (byte) rawData.length;
|
||||
frameCount = 2;
|
||||
}else if(rawData.length >= 126 && rawData.length <= 65535){
|
||||
frame[1] = (byte) 126;
|
||||
int len = rawData.length;
|
||||
frame[2] = (byte)((len >> 8 ) & (byte)255);
|
||||
frame[3] = (byte)(len & (byte)255);
|
||||
frameCount = 4;
|
||||
}else{
|
||||
frame[1] = (byte) 127;
|
||||
int len = rawData.length;
|
||||
frame[2] = (byte)((len >> 56 ) & (byte)255);
|
||||
frame[3] = (byte)((len >> 48 ) & (byte)255);
|
||||
frame[4] = (byte)((len >> 40 ) & (byte)255);
|
||||
frame[5] = (byte)((len >> 32 ) & (byte)255);
|
||||
frame[6] = (byte)((len >> 24 ) & (byte)255);
|
||||
frame[7] = (byte)((len >> 16 ) & (byte)255);
|
||||
frame[8] = (byte)((len >> 8 ) & (byte)255);
|
||||
frame[9] = (byte)(len & (byte)255);
|
||||
frameCount = 10;
|
||||
}
|
||||
if (rawData.length <= 125) {
|
||||
frame[1] = (byte) rawData.length;
|
||||
frameCount = 2;
|
||||
} else if (rawData.length >= 126 && rawData.length <= 65535) {
|
||||
frame[1] = (byte) 126;
|
||||
int len = rawData.length;
|
||||
frame[2] = (byte) ((len >> 8) & (byte) 255);
|
||||
frame[3] = (byte) (len & (byte) 255);
|
||||
frameCount = 4;
|
||||
} else {
|
||||
frame[1] = (byte) 127;
|
||||
int len = rawData.length;
|
||||
frame[2] = (byte) ((len >> 56) & (byte) 255);
|
||||
frame[3] = (byte) ((len >> 48) & (byte) 255);
|
||||
frame[4] = (byte) ((len >> 40) & (byte) 255);
|
||||
frame[5] = (byte) ((len >> 32) & (byte) 255);
|
||||
frame[6] = (byte) ((len >> 24) & (byte) 255);
|
||||
frame[7] = (byte) ((len >> 16) & (byte) 255);
|
||||
frame[8] = (byte) ((len >> 8) & (byte) 255);
|
||||
frame[9] = (byte) (len & (byte) 255);
|
||||
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;
|
||||
for(int i=0; i<frameCount;i++){
|
||||
reply[bLim] = frame[i];
|
||||
bLim++;
|
||||
}
|
||||
for(int i=0; i<rawData.length;i++){
|
||||
reply[bLim] = rawData[i];
|
||||
bLim++;
|
||||
}
|
||||
|
||||
writer.write(reply);
|
||||
writer.flush();
|
||||
int bLim = 0;
|
||||
for (int i = 0; i < frameCount; i++) {
|
||||
reply[bLim] = frame[i];
|
||||
bLim++;
|
||||
}
|
||||
for (int i = 0; i < rawData.length; i++) {
|
||||
reply[bLim] = rawData[i];
|
||||
bLim++;
|
||||
}
|
||||
|
||||
writer.write(reply);
|
||||
writer.flush();
|
||||
}
|
||||
|
||||
private static String codeMessage(int paramInt) {
|
||||
switch (paramInt) {
|
||||
case 200:
|
||||
return " OK";
|
||||
case 100:
|
||||
return " Continue";
|
||||
case 201:
|
||||
return " Created";
|
||||
case 202:
|
||||
return " Accepted";
|
||||
case 203:
|
||||
return " Non-Authoritative Information";
|
||||
case 204:
|
||||
return " No Content";
|
||||
case 205:
|
||||
return " Reset Content";
|
||||
case 206:
|
||||
return " Partial Content";
|
||||
case 300:
|
||||
return " Multiple Choices";
|
||||
case 301:
|
||||
return " Moved Permanently";
|
||||
case 302:
|
||||
return " Temporary Redirect";
|
||||
case 303:
|
||||
return " See Other";
|
||||
case 304:
|
||||
return " Not Modified";
|
||||
case 305:
|
||||
return " Use Proxy";
|
||||
case 400:
|
||||
return " Bad Request";
|
||||
case 401:
|
||||
return " Unauthorized";
|
||||
case 402:
|
||||
return " Payment Required";
|
||||
case 403:
|
||||
return " Forbidden";
|
||||
case 404:
|
||||
return " Not Found";
|
||||
case 405:
|
||||
return " Method Not Allowed";
|
||||
case 406:
|
||||
return " Not Acceptable";
|
||||
case 407:
|
||||
return " Proxy Authentication Required";
|
||||
case 408:
|
||||
return " Request Time-Out";
|
||||
case 409:
|
||||
return " Conflict";
|
||||
case 410:
|
||||
return " Gone";
|
||||
case 411:
|
||||
return " Length Required";
|
||||
case 412:
|
||||
return " Precondition Failed";
|
||||
case 413:
|
||||
return " Request Entity Too Large";
|
||||
case 414:
|
||||
return " Request-URI Too Large";
|
||||
case 415:
|
||||
return " Unsupported Media Type";
|
||||
case 500:
|
||||
return " Internal Server Error";
|
||||
case 501:
|
||||
return " Not Implemented";
|
||||
case 502:
|
||||
return " Bad Gateway";
|
||||
case 503:
|
||||
return " Service Unavailable";
|
||||
case 504:
|
||||
return " Gateway Timeout";
|
||||
case 505:
|
||||
return " HTTP Version Not Supported";
|
||||
}
|
||||
return " ";
|
||||
}
|
||||
|
||||
|
||||
//From javax.xml.bind.DatatypeConverter
|
||||
private static String printBase64Binary(byte[] array){
|
||||
char[] arrayOfChar = new char[(array.length + 2) / 3 * 4];
|
||||
int i = _printBase64Binary(array, 0, array.length, arrayOfChar, 0);
|
||||
assert i == arrayOfChar.length;
|
||||
return new String(arrayOfChar);
|
||||
switch (paramInt) {
|
||||
case 200:
|
||||
return " OK";
|
||||
case 100:
|
||||
return " Continue";
|
||||
case 201:
|
||||
return " Created";
|
||||
case 202:
|
||||
return " Accepted";
|
||||
case 203:
|
||||
return " Non-Authoritative Information";
|
||||
case 204:
|
||||
return " No Content";
|
||||
case 205:
|
||||
return " Reset Content";
|
||||
case 206:
|
||||
return " Partial Content";
|
||||
case 300:
|
||||
return " Multiple Choices";
|
||||
case 301:
|
||||
return " Moved Permanently";
|
||||
case 302:
|
||||
return " Temporary Redirect";
|
||||
case 303:
|
||||
return " See Other";
|
||||
case 304:
|
||||
return " Not Modified";
|
||||
case 305:
|
||||
return " Use Proxy";
|
||||
case 400:
|
||||
return " Bad Request";
|
||||
case 401:
|
||||
return " Unauthorized";
|
||||
case 402:
|
||||
return " Payment Required";
|
||||
case 403:
|
||||
return " Forbidden";
|
||||
case 404:
|
||||
return " Not Found";
|
||||
case 405:
|
||||
return " Method Not Allowed";
|
||||
case 406:
|
||||
return " Not Acceptable";
|
||||
case 407:
|
||||
return " Proxy Authentication Required";
|
||||
case 408:
|
||||
return " Request Time-Out";
|
||||
case 409:
|
||||
return " Conflict";
|
||||
case 410:
|
||||
return " Gone";
|
||||
case 411:
|
||||
return " Length Required";
|
||||
case 412:
|
||||
return " Precondition Failed";
|
||||
case 413:
|
||||
return " Request Entity Too Large";
|
||||
case 414:
|
||||
return " Request-URI Too Large";
|
||||
case 415:
|
||||
return " Unsupported Media Type";
|
||||
case 500:
|
||||
return " Internal Server Error";
|
||||
case 501:
|
||||
return " Not Implemented";
|
||||
case 502:
|
||||
return " Bad Gateway";
|
||||
case 503:
|
||||
return " Service Unavailable";
|
||||
case 504:
|
||||
return " Gateway Timeout";
|
||||
case 505:
|
||||
return " HTTP Version Not Supported";
|
||||
}
|
||||
return " ";
|
||||
}
|
||||
|
||||
private static int _printBase64Binary(byte[] paramArrayOfbyte, int paramInt1, int paramInt2, char[] paramArrayOfchar, int paramInt3) {
|
||||
int i = paramInt2;
|
||||
int j;
|
||||
for (j = paramInt1; i >= 3; j += 3) {
|
||||
paramArrayOfchar[paramInt3++] = encode(paramArrayOfbyte[j] >> 2);
|
||||
paramArrayOfchar[paramInt3++] = encode((paramArrayOfbyte[j] & 0x3) << 4 | paramArrayOfbyte[j + 1] >> 4 & 0xF);
|
||||
paramArrayOfchar[paramInt3++] = encode((paramArrayOfbyte[j + 1] & 0xF) << 2 | paramArrayOfbyte[j + 2] >> 6 & 0x3);
|
||||
paramArrayOfchar[paramInt3++] = encode(paramArrayOfbyte[j + 2] & 0x3F);
|
||||
i -= 3;
|
||||
}
|
||||
if (i == 1) {
|
||||
paramArrayOfchar[paramInt3++] = encode(paramArrayOfbyte[j] >> 2);
|
||||
paramArrayOfchar[paramInt3++] = encode((paramArrayOfbyte[j] & 0x3) << 4);
|
||||
paramArrayOfchar[paramInt3++] = '=';
|
||||
paramArrayOfchar[paramInt3++] = '=';
|
||||
}
|
||||
if (i == 2) {
|
||||
paramArrayOfchar[paramInt3++] = encode(paramArrayOfbyte[j] >> 2);
|
||||
paramArrayOfchar[paramInt3++] = encode((paramArrayOfbyte[j] & 0x3) << 4 | paramArrayOfbyte[j + 1] >> 4 & 0xF);
|
||||
paramArrayOfchar[paramInt3++] = encode((paramArrayOfbyte[j + 1] & 0xF) << 2);
|
||||
paramArrayOfchar[paramInt3++] = '=';
|
||||
}
|
||||
return paramInt3;
|
||||
}
|
||||
|
||||
|
||||
// From javax.xml.bind.DatatypeConverter
|
||||
private static String printBase64Binary(byte[] array) {
|
||||
char[] arrayOfChar = new char[(array.length + 2) / 3 * 4];
|
||||
int i = _printBase64Binary(array, 0, array.length, arrayOfChar, 0);
|
||||
assert i == arrayOfChar.length;
|
||||
return new String(arrayOfChar);
|
||||
}
|
||||
|
||||
private static int _printBase64Binary(byte[] paramArrayOfbyte, int paramInt1, int paramInt2,
|
||||
char[] paramArrayOfchar, int paramInt3) {
|
||||
int i = paramInt2;
|
||||
int j;
|
||||
for (j = paramInt1; i >= 3; j += 3) {
|
||||
paramArrayOfchar[paramInt3++] = encode(paramArrayOfbyte[j] >> 2);
|
||||
paramArrayOfchar[paramInt3++] = encode(
|
||||
(paramArrayOfbyte[j] & 0x3) << 4 | paramArrayOfbyte[j + 1] >> 4 & 0xF);
|
||||
paramArrayOfchar[paramInt3++] = encode(
|
||||
(paramArrayOfbyte[j + 1] & 0xF) << 2 | paramArrayOfbyte[j + 2] >> 6 & 0x3);
|
||||
paramArrayOfchar[paramInt3++] = encode(paramArrayOfbyte[j + 2] & 0x3F);
|
||||
i -= 3;
|
||||
}
|
||||
if (i == 1) {
|
||||
paramArrayOfchar[paramInt3++] = encode(paramArrayOfbyte[j] >> 2);
|
||||
paramArrayOfchar[paramInt3++] = encode((paramArrayOfbyte[j] & 0x3) << 4);
|
||||
paramArrayOfchar[paramInt3++] = '=';
|
||||
paramArrayOfchar[paramInt3++] = '=';
|
||||
}
|
||||
if (i == 2) {
|
||||
paramArrayOfchar[paramInt3++] = encode(paramArrayOfbyte[j] >> 2);
|
||||
paramArrayOfchar[paramInt3++] = encode(
|
||||
(paramArrayOfbyte[j] & 0x3) << 4 | paramArrayOfbyte[j + 1] >> 4 & 0xF);
|
||||
paramArrayOfchar[paramInt3++] = encode((paramArrayOfbyte[j + 1] & 0xF) << 2);
|
||||
paramArrayOfchar[paramInt3++] = '=';
|
||||
}
|
||||
return paramInt3;
|
||||
}
|
||||
|
||||
private static char encode(int paramInt) {
|
||||
return encodeMap[paramInt & 0x3F];
|
||||
}
|
||||
return encodeMap[paramInt & 0x3F];
|
||||
}
|
||||
|
||||
private static final char[] encodeMap = initEncodeMap();
|
||||
|
||||
|
||||
private static char[] initEncodeMap() {
|
||||
char[] arrayOfChar = new char[64];
|
||||
byte b;
|
||||
for (b = 0; b < 26; b++)
|
||||
arrayOfChar[b] = (char)(65 + b);
|
||||
for (b = 26; b < 52; b++)
|
||||
arrayOfChar[b] = (char)(97 + b - 26);
|
||||
for (b = 52; b < 62; b++)
|
||||
arrayOfChar[b] = (char)(48 + b - 52);
|
||||
arrayOfChar[62] = '+';
|
||||
arrayOfChar[63] = '/';
|
||||
return arrayOfChar;
|
||||
}
|
||||
char[] arrayOfChar = new char[64];
|
||||
byte b;
|
||||
for (b = 0; b < 26; b++)
|
||||
arrayOfChar[b] = (char) (65 + b);
|
||||
for (b = 26; b < 52; b++)
|
||||
arrayOfChar[b] = (char) (97 + b - 26);
|
||||
for (b = 52; b < 62; b++)
|
||||
arrayOfChar[b] = (char) (48 + b - 52);
|
||||
arrayOfChar[62] = '+';
|
||||
arrayOfChar[63] = '/';
|
||||
return arrayOfChar;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,30 +7,30 @@ import java.io.OutputStreamWriter;
|
|||
import java.net.Socket;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class HttpWriter{
|
||||
|
||||
public class HttpWriter {
|
||||
|
||||
private OutputStream out;
|
||||
private BufferedWriter writer;
|
||||
|
||||
public HttpWriter(Socket socket) throws Exception{
|
||||
|
||||
public HttpWriter(Socket socket) throws Exception {
|
||||
this.out = socket.getOutputStream();
|
||||
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.flush();
|
||||
}
|
||||
|
||||
public void write(String message) throws IOException{
|
||||
public void write(String message) throws IOException {
|
||||
this.writer.write(message);
|
||||
}
|
||||
|
||||
public void flush() throws IOException{
|
||||
|
||||
public void flush() throws IOException {
|
||||
this.writer.flush();
|
||||
}
|
||||
|
||||
public void close() throws IOException{
|
||||
|
||||
public void close() throws IOException {
|
||||
this.writer.close();
|
||||
}
|
||||
}
|
|
@ -1,10 +1,9 @@
|
|||
package be.jeffcheasey88.peeratcode.framework;
|
||||
|
||||
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)
|
||||
@Target(ElementType.METHOD)
|
||||
public @interface Route {
|
||||
|
||||
|
||||
String path() default "^.*$";
|
||||
|
||||
String type() default "GET";
|
||||
|
||||
boolean needLogin() default false;
|
||||
|
||||
}
|
||||
|
|
|
@ -16,8 +16,8 @@ import org.jose4j.lang.JoseException;
|
|||
|
||||
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
||||
|
||||
public class Router{
|
||||
|
||||
public class Router {
|
||||
|
||||
private Map<Response, Route> responses;
|
||||
private Map<Response, Pattern> patterns;
|
||||
private Response noFileFound;
|
||||
|
@ -25,73 +25,76 @@ public class Router{
|
|||
private DatabaseRepository repo;
|
||||
private String token_issuer;
|
||||
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.token_issuer = token_issuer;
|
||||
this.token_issuer = token_issuer;
|
||||
this.token_expiration = token_expiration;
|
||||
this.responses = new HashMap<>();
|
||||
this.patterns = new HashMap<>();
|
||||
this.rsaJsonWebKey = RsaJwkGenerator.generateJwk(2048);
|
||||
}
|
||||
|
||||
public DatabaseRepository getDataBase(){
|
||||
|
||||
public DatabaseRepository getDataBase() {
|
||||
return this.repo;
|
||||
}
|
||||
|
||||
public void register(Response response){
|
||||
|
||||
public void register(Response response) {
|
||||
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);
|
||||
|
||||
|
||||
this.responses.put(response, route);
|
||||
this.patterns.put(response, Pattern.compile(route.path()));
|
||||
} catch (Exception e){
|
||||
} catch (Exception e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public void setDefault(Response response){
|
||||
|
||||
public void setDefault(Response response) {
|
||||
this.noFileFound = response;
|
||||
}
|
||||
|
||||
public void exec(String type, String path, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
||||
for(Entry<Response, Route> routes : this.responses.entrySet()){
|
||||
if(routes.getValue().type().equals(type)){
|
||||
for (Entry<Response, Route> routes : this.responses.entrySet()) {
|
||||
if (routes.getValue().type().equals(type)) {
|
||||
Matcher matcher = this.patterns.get(routes.getKey()).matcher(path);
|
||||
if(matcher.matches()){
|
||||
if(user == null && routes.getValue().needLogin()) return;
|
||||
if (matcher.matches()) {
|
||||
if (user == null && routes.getValue().needLogin())
|
||||
return;
|
||||
routes.getKey().exec(matcher, user, reader, writer);
|
||||
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;
|
||||
}
|
||||
|
||||
public String getTokenIssuer(){
|
||||
|
||||
public String getTokenIssuer() {
|
||||
return this.token_issuer;
|
||||
}
|
||||
|
||||
public String createAuthUser(int id) throws JoseException{
|
||||
|
||||
public String createAuthUser(int id) throws JoseException {
|
||||
JwtClaims claims = new JwtClaims();
|
||||
claims.setIssuer(token_issuer); // who creates the token and signs it
|
||||
claims.setExpirationTimeMinutesInTheFuture(token_expiration);
|
||||
claims.setGeneratedJwtId(); // a unique identifier for the token
|
||||
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.setClaim("id", id);
|
||||
|
||||
JsonWebSignature jws = new JsonWebSignature();
|
||||
jws.setPayload(claims.toJson());
|
||||
jws.setKey(rsaJsonWebKey.getPrivateKey());
|
||||
jws.setKeyIdHeaderValue(rsaJsonWebKey.getKeyId());
|
||||
jws.setAlgorithmHeaderValue(AlgorithmIdentifiers.RSA_USING_SHA256);
|
||||
return jws.getCompactSerialization();
|
||||
claims.setIssuer(token_issuer); // who creates the token and signs it
|
||||
claims.setExpirationTimeMinutesInTheFuture(token_expiration);
|
||||
claims.setGeneratedJwtId(); // a unique identifier for the token
|
||||
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.setClaim("id", id);
|
||||
|
||||
JsonWebSignature jws = new JsonWebSignature();
|
||||
jws.setPayload(claims.toJson());
|
||||
jws.setKey(rsaJsonWebKey.getPrivateKey());
|
||||
jws.setKeyIdHeaderValue(rsaJsonWebKey.getKeyId());
|
||||
jws.setAlgorithmHeaderValue(AlgorithmIdentifiers.RSA_USING_SHA256);
|
||||
return jws.getCompactSerialization();
|
||||
}
|
||||
}
|
|
@ -5,12 +5,12 @@ import org.jose4j.jwt.JwtClaims;
|
|||
public class User {
|
||||
|
||||
private int id;
|
||||
|
||||
public User(JwtClaims jwtClaims){
|
||||
this.id = ((Long)jwtClaims.getClaimValue("id")).intValue();
|
||||
|
||||
public User(JwtClaims jwtClaims) {
|
||||
this.id = ((Long) jwtClaims.getClaimValue("id")).intValue();
|
||||
}
|
||||
|
||||
public int getId(){
|
||||
|
||||
public int getId() {
|
||||
return this.id;
|
||||
}
|
||||
}
|
|
@ -9,10 +9,6 @@ public class Badge {
|
|||
private byte[] logo;
|
||||
private int level;
|
||||
|
||||
public Badge(String name, int level) {
|
||||
this(name, null, level);
|
||||
}
|
||||
|
||||
public Badge(String name, byte[] logo, int level) {
|
||||
this.name = name;
|
||||
this.logo = logo;
|
||||
|
@ -23,24 +19,11 @@ public class Badge {
|
|||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public byte[] getLogo() {
|
||||
return logo;
|
||||
}
|
||||
|
||||
public void setLogo(byte[] logo) {
|
||||
this.logo = logo;
|
||||
}
|
||||
|
||||
public int getLevel() {
|
||||
return level;
|
||||
}
|
||||
|
||||
public void setLevel(int level) {
|
||||
this.level = level;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,11 +1,10 @@
|
|||
package be.jeffcheasey88.peeratcode.model;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
public class Chapter {
|
||||
|
||||
|
||||
private int id;
|
||||
private String name;
|
||||
private List<Puzzle> puzzles;
|
||||
|
@ -23,18 +22,10 @@ public class Chapter {
|
|||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public List<Puzzle> getPuzzles() {
|
||||
return puzzles;
|
||||
}
|
||||
|
@ -42,20 +33,22 @@ public class Chapter {
|
|||
public void setPuzzles(List<Puzzle> puzzles) {
|
||||
this.puzzles = puzzles;
|
||||
}
|
||||
|
||||
|
||||
public Timestamp getStartDate() {
|
||||
return startDate;
|
||||
}
|
||||
|
||||
|
||||
public Timestamp getEndDate() {
|
||||
return endDate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object object){
|
||||
if(this == object) return true;
|
||||
if(!(object instanceof Chapter)) return false;
|
||||
return this.id == (((Chapter)object).id);
|
||||
public boolean equals(Object object) {
|
||||
if (this == object)
|
||||
return true;
|
||||
if (!(object instanceof Chapter))
|
||||
return false;
|
||||
return this.id == (((Chapter) object).id);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -11,10 +11,6 @@ public class Completion {
|
|||
public Completion(int playerId, int puzzleId, String fileName, int score) {
|
||||
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,
|
||||
byte[] file) {
|
||||
|
@ -25,7 +21,7 @@ public class Completion {
|
|||
this.score = score;
|
||||
this.code = file;
|
||||
}
|
||||
|
||||
|
||||
public int getPuzzleId() {
|
||||
return puzzleId;
|
||||
}
|
||||
|
@ -42,9 +38,10 @@ public class Completion {
|
|||
this.tries++;
|
||||
updateScore();
|
||||
}
|
||||
|
||||
private void updateScore() {
|
||||
if (tries > 1) {
|
||||
score = score * (1-((tries-1)/10));
|
||||
score = score * (1 - ((tries - 1) / 10));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,53 +1,138 @@
|
|||
package be.jeffcheasey88.peeratcode.model;
|
||||
|
||||
import java.util.ArrayList;
|
||||
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.JSONObject;
|
||||
|
||||
public class Group {
|
||||
public class Group implements Comparable<Group> {
|
||||
private String name;
|
||||
private int linkToChapter;
|
||||
private int linkToPuzzle;
|
||||
|
||||
public Group(JSONObject json){
|
||||
this.name = (String)json.get("name");
|
||||
this.linkToChapter = ((Number)json.get("chapter")).intValue();
|
||||
this.linkToPuzzle = ((Number)json.get("puzzle")).intValue();
|
||||
private List<Player> players;
|
||||
|
||||
public Group(JSONObject json) {
|
||||
this.name = (String) json.get("name");
|
||||
this.linkToChapter = ((Number) json.get("chapter")).intValue();
|
||||
this.linkToPuzzle = ((Number) json.get("puzzle")).intValue();
|
||||
}
|
||||
|
||||
|
||||
public Group(String name, int initChap, int initPuzz) {
|
||||
this.name = name;
|
||||
this.linkToChapter = initChap;
|
||||
this.linkToPuzzle = initPuzz;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
public void addPlayer(Player newPlayer) {
|
||||
if (newPlayer != null) {
|
||||
if (players == null)
|
||||
players = new ArrayList<Player>();
|
||||
|
||||
int pPosition = players.indexOf(newPlayer);
|
||||
if (pPosition < 0) {
|
||||
players.add(newPlayer);
|
||||
} else {
|
||||
players.get(pPosition).addScore(newPlayer.getTotalScore(), newPlayer.getTotalTries());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
public int getScore() {
|
||||
int score = 0;
|
||||
|
||||
if (players != null) {
|
||||
for (Player p : players) {
|
||||
score = score + p.getTotalScore();
|
||||
}
|
||||
}
|
||||
return score;
|
||||
}
|
||||
|
||||
public int getTries() {
|
||||
int tries = 0;
|
||||
|
||||
if (players != null) {
|
||||
for (Player p : players) {
|
||||
tries = tries + p.getTotalTries();
|
||||
}
|
||||
}
|
||||
return tries;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public int getLinkToChapter() {
|
||||
return linkToChapter;
|
||||
}
|
||||
|
||||
public void setLinkToChapter(int linkToChapter) {
|
||||
this.linkToChapter = linkToChapter;
|
||||
}
|
||||
|
||||
public int getLinkToPuzzle() {
|
||||
return linkToPuzzle;
|
||||
}
|
||||
|
||||
public void setLinkToPuzzle(int linkToPuzzle) {
|
||||
this.linkToPuzzle = linkToPuzzle;
|
||||
public JSONObject toJson() {
|
||||
return this.toJson(null);
|
||||
}
|
||||
|
||||
public JSONObject toJson() {
|
||||
public JSONObject toJson(Integer rank) {
|
||||
JSONObject groupJSON = new JSONObject();
|
||||
groupJSON.put("name", name);
|
||||
if (linkToChapter > 0) groupJSON.put("chapter", linkToChapter);
|
||||
if (linkToPuzzle > 0) groupJSON.put("puzzle", linkToPuzzle);
|
||||
if (rank != null)
|
||||
groupJSON.put("rank", rank);
|
||||
else if (linkToChapter > 0)
|
||||
groupJSON.put("chapter", linkToChapter);
|
||||
else if (linkToPuzzle > 0)
|
||||
groupJSON.put("puzzle", linkToPuzzle);
|
||||
if (players != null) {
|
||||
JSONArray groupsPlayerJSON = new JSONArray();
|
||||
for (Player p : players) {
|
||||
JSONObject playerJSON = new JSONObject();
|
||||
playerJSON.put("pseudo", p.getPseudo());
|
||||
playerJSON.put("score", p.getTotalScore());
|
||||
playerJSON.put("completion", p.getTotalCompletion());
|
||||
playerJSON.put("tries", p.getTotalTries());
|
||||
groupsPlayerJSON.add(playerJSON);
|
||||
}
|
||||
groupJSON.put("players", groupsPlayerJSON);
|
||||
}
|
||||
return groupJSON;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(Group arg0) {
|
||||
int comparo = arg0.getScore() - getScore();
|
||||
if (comparo == 0) {
|
||||
comparo = players.size() - arg0.players.size();
|
||||
if (comparo == 0) {
|
||||
comparo = getTries() - arg0.getTries();
|
||||
if (comparo == 0) {
|
||||
comparo = name.compareTo(arg0.name);
|
||||
}
|
||||
}
|
||||
}
|
||||
return comparo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
Group other = (Group) obj;
|
||||
return Objects.equals(name, other.name);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -9,8 +9,6 @@ import org.json.simple.JSONArray;
|
|||
import org.json.simple.JSONObject;
|
||||
|
||||
public class Player implements Comparable<Player> {
|
||||
public static final String PATH_TO_CODE = "/home/%s/peer-at-source/";
|
||||
|
||||
private String pseudo;
|
||||
private String email;
|
||||
private String firstname;
|
||||
|
@ -38,6 +36,18 @@ public class Player implements Comparable<Player> {
|
|||
totalTries = 0;
|
||||
}
|
||||
|
||||
public Player(String pseudo, int score, int tries) {
|
||||
// For groups leaderboard
|
||||
this.pseudo = pseudo;
|
||||
totalScore = score;
|
||||
totalTries = tries;
|
||||
if (totalTries > 0)
|
||||
totalCompletion = totalTries;
|
||||
else
|
||||
totalCompletion = 0;
|
||||
email = ""; // TO make compareTo and equals works as usual
|
||||
}
|
||||
|
||||
public String getPseudo() {
|
||||
return this.pseudo;
|
||||
}
|
||||
|
@ -62,11 +72,6 @@ public class Player implements Comparable<Player> {
|
|||
return groups;
|
||||
}
|
||||
|
||||
/**
|
||||
* SEE SET_TAGS IN PUZZLE
|
||||
*
|
||||
* @return DEATH
|
||||
*/
|
||||
public JSONArray getJsonGroups() {
|
||||
if (groups != null) {
|
||||
JSONArray groupsJSON = new JSONArray();
|
||||
|
@ -94,10 +99,6 @@ public class Player implements Comparable<Player> {
|
|||
avatar = newAvatar;
|
||||
}
|
||||
|
||||
public String getPathToSourceCode() {
|
||||
return String.format(PATH_TO_CODE, pseudo);
|
||||
}
|
||||
|
||||
public int getRank() {
|
||||
return rank;
|
||||
}
|
||||
|
@ -114,6 +115,12 @@ public class Player implements Comparable<Player> {
|
|||
this.totalScore = totalScore;
|
||||
}
|
||||
|
||||
public void addScore(int addScore, int tries) {
|
||||
totalScore = totalScore + addScore;
|
||||
totalTries = totalTries + tries;
|
||||
totalCompletion++;
|
||||
}
|
||||
|
||||
public int getTotalCompletion() {
|
||||
return totalCompletion;
|
||||
}
|
||||
|
@ -134,11 +141,6 @@ public class Player implements Comparable<Player> {
|
|||
return badges;
|
||||
}
|
||||
|
||||
/**
|
||||
* SEE SET_TAGS IN PUZZLE
|
||||
*
|
||||
* @return DEATH
|
||||
*/
|
||||
public JSONArray getJsonBadges() {
|
||||
if (badges == null)
|
||||
return null;
|
||||
|
|
|
@ -8,7 +8,7 @@ import org.json.simple.JSONArray;
|
|||
import org.json.simple.JSONObject;
|
||||
|
||||
public class Puzzle {
|
||||
|
||||
|
||||
private int id;
|
||||
private String name;
|
||||
private String content;
|
||||
|
@ -18,10 +18,8 @@ public class Puzzle {
|
|||
private Set<String> tags;
|
||||
private int depend;
|
||||
|
||||
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);
|
||||
}
|
||||
public Puzzle(int id, String name, String content, byte[] soluce, String verify, int scoreMax, String tags, int depend){
|
||||
public Puzzle(int id, String name, String content, byte[] soluce, String verify, int scoreMax, String tags,
|
||||
int depend) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.content = content;
|
||||
|
@ -36,90 +34,61 @@ public class Puzzle {
|
|||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getContent() {
|
||||
return content;
|
||||
}
|
||||
|
||||
public void setContent(String content) {
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
public byte[] getSoluce(){
|
||||
public byte[] getSoluce() {
|
||||
return this.soluce;
|
||||
}
|
||||
|
||||
public void setSoluce(byte[] array){
|
||||
this.soluce = array;
|
||||
}
|
||||
|
||||
public String getVerify(){
|
||||
return this.verify;
|
||||
}
|
||||
|
||||
public void setVerify(String regex){
|
||||
this.verify = regex;
|
||||
}
|
||||
|
||||
public int getScoreMax(){
|
||||
|
||||
public int getScoreMax() {
|
||||
return this.scoreMax;
|
||||
}
|
||||
|
||||
public void setScoreMax(int max){
|
||||
this.scoreMax = max;
|
||||
}
|
||||
|
||||
public Set<String> getTags(){
|
||||
|
||||
public Set<String> getTags() {
|
||||
return this.tags;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* DO NOT EVER EVER SHOW TO MISTER LUDWIG XD
|
||||
*
|
||||
* @return DEATH
|
||||
*/
|
||||
public JSONArray getJsonTags() {
|
||||
if (tags == null)
|
||||
return null;
|
||||
JSONArray tagsJSON = new JSONArray();
|
||||
for (String tag: tags) {
|
||||
for (String tag : tags) {
|
||||
JSONObject tagJSON = new JSONObject();
|
||||
tagJSON.put("name", tag);
|
||||
tagsJSON.add(tagJSON);
|
||||
}
|
||||
return tagsJSON;
|
||||
}
|
||||
|
||||
public void setTags(String tags){
|
||||
|
||||
public void setTags(String tags) {
|
||||
if (tags == null || tags.isEmpty())
|
||||
this.tags = null;
|
||||
else
|
||||
this.tags = new HashSet<String>(Arrays.asList(tags.split(",")));
|
||||
}
|
||||
|
||||
public int getDepend(){
|
||||
|
||||
public int getDepend() {
|
||||
return this.depend;
|
||||
}
|
||||
|
||||
public void setDepend(int depend){
|
||||
this.depend = depend;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object object) {
|
||||
if(this == object) return true;
|
||||
if(!(object instanceof Puzzle)) return false;
|
||||
return this.id == (((Puzzle)object).id);
|
||||
if (this == object)
|
||||
return true;
|
||||
if (!(object instanceof Puzzle))
|
||||
return false;
|
||||
return this.id == (((Puzzle) object).id);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -5,46 +5,72 @@ import java.sql.PreparedStatement;
|
|||
import java.sql.SQLException;
|
||||
|
||||
public enum DatabaseQuery {
|
||||
|
||||
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
|
||||
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 = ?"),
|
||||
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_GROUPS("SELCT * FROM 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"),
|
||||
|
||||
// GROUPS
|
||||
ALL_GROUPS("SELCT * FROM groups"), INSERT_GROUP("INSERT INTO groups (name, fk_chapter, fk_puzzle) VALUES (?,?,?)"),
|
||||
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_EMAIL_AVAILABLE_QUERY("SELECT * FROM players WHERE email = ?"),
|
||||
REGISTER_QUERY("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 = ?));"),
|
||||
REGISTER_QUERY(
|
||||
"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=?"),
|
||||
|
||||
// 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 = ?"),
|
||||
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_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_BY_ID(GET_PLAYER_DETAILS," p.id_player = ? ORDER BY g.fk_chapter, g.fk_puzzle;"),
|
||||
GET_PLAYER_DETAILS_BY_PSEUDO(GET_PLAYER_DETAILS,"p.pseudo = ? ORDER BY g.fk_chapter, g.fk_puzzle;"),
|
||||
GET_BADGE("SELECT * FROM badges WHERE id_badge = ?"),
|
||||
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 (?, ?, ?, ?, ?, ?)"),
|
||||
INSERT_GROUP("INSERT INTO groups (name, fk_chapter, fk_puzzle) VALUES (?,?,?)"),
|
||||
UPDATE_COMPLETION("UPDATE completions SET tries = ?, filename = ?, score = ? WHERE fk_puzzle = ? AND fk_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_BY_ID(GET_PLAYER_DETAILS, " p.id_player = ? ORDER BY g.fk_chapter, g.fk_puzzle;"),
|
||||
GET_PLAYER_DETAILS_BY_PSEUDO(GET_PLAYER_DETAILS, "p.pseudo = ? ORDER BY g.fk_chapter, g.fk_puzzle;"),
|
||||
|
||||
// BADGES
|
||||
GET_BADGE("SELECT * FROM badges WHERE id_badge = ?"), GET_BADGES_OF_PLAYER(
|
||||
"SELECT * FROM badges b LEFT JOIN containsBadges cb ON cb.fk_badge = b.id_badge WHERE cb.fk_player = ?");
|
||||
|
||||
private String request;
|
||||
|
||||
DatabaseQuery(DatabaseQuery parent, String request){
|
||||
this.request = parent.request+request;
|
||||
|
||||
DatabaseQuery(DatabaseQuery parent, String request) {
|
||||
this.request = parent.request + request;
|
||||
}
|
||||
|
||||
DatabaseQuery(String request){
|
||||
|
||||
DatabaseQuery(String request) {
|
||||
this.request = request;
|
||||
}
|
||||
|
||||
public PreparedStatement prepare(Connection con) throws SQLException{
|
||||
|
||||
public PreparedStatement prepare(Connection con) throws SQLException {
|
||||
return con.prepareStatement(this.request);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString(){
|
||||
public String toString() {
|
||||
return this.request;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -82,6 +82,9 @@ public class DatabaseRepository {
|
|||
private Group makeGroup(ResultSet result) throws SQLException {
|
||||
return new Group(result.getString("name"), result.getInt("fk_chapter"), result.getInt("fk_puzzle"));
|
||||
}
|
||||
private Player makeGroupPlayer(ResultSet result) throws SQLException {
|
||||
return new Player(result.getString("pseudo"), result.getInt("score"), result.getInt("tries"));
|
||||
}
|
||||
|
||||
private Badge makeBadge(ResultSet rs) throws SQLException {
|
||||
return new Badge(rs.getString("name"), rs.getBytes("logo"), rs.getInt("level"));
|
||||
|
@ -91,8 +94,9 @@ public class DatabaseRepository {
|
|||
// Found on StackOverflow
|
||||
ResultSetMetaData rsmd = rs.getMetaData();
|
||||
int columns = rsmd.getColumnCount();
|
||||
for(int x = 1; x <= columns; x++){
|
||||
if(columnName.equals(rsmd.getColumnName(x))) return true;
|
||||
for (int x = 1; x <= columns; x++) {
|
||||
if (columnName.equals(rsmd.getColumnName(x)))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
@ -238,6 +242,33 @@ public class DatabaseRepository {
|
|||
return null;
|
||||
}
|
||||
|
||||
public SortedSet<Group> getAllGroupForChapterLeaderboard(int chapterId) {
|
||||
try {
|
||||
ensureConnection();
|
||||
PreparedStatement groupsStmt = DatabaseQuery.ALL_GROUP_FOR_CHAPTER_LEADERBOARD.prepare(this.con);
|
||||
groupsStmt.setInt(1, chapterId);
|
||||
ResultSet result = groupsStmt.executeQuery();
|
||||
List<Group> groups = new ArrayList<Group>();
|
||||
Group tmpGroup;
|
||||
while (result.next()) {
|
||||
tmpGroup = makeGroup(result);
|
||||
if (tmpGroup != null) {
|
||||
int gPosition = groups.indexOf(tmpGroup);
|
||||
if (gPosition < 0) {
|
||||
tmpGroup.addPlayer(makeGroupPlayer(result));
|
||||
groups.add(tmpGroup);
|
||||
} else {
|
||||
groups.get(gPosition).addPlayer(makeGroupPlayer(result));
|
||||
}
|
||||
}
|
||||
}
|
||||
return new TreeSet<Group>(groups);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Badge getBadge(int badgeId) {
|
||||
try {
|
||||
ensureConnection();
|
||||
|
@ -380,7 +411,8 @@ public class DatabaseRepository {
|
|||
ResultSet inserted = playerStatement.getGeneratedKeys();
|
||||
if (inserted.next()) {
|
||||
int newPlayerId = inserted.getInt(1);
|
||||
try (PreparedStatement containsGroupsStatement = con.prepareStatement(DatabaseQuery.REGISTER_PLAYER_IN_EXISTING_GROUP.toString())) {
|
||||
try (PreparedStatement containsGroupsStatement = con
|
||||
.prepareStatement(DatabaseQuery.REGISTER_PLAYER_IN_EXISTING_GROUP.toString())) {
|
||||
containsGroupsStatement.setInt(1, newPlayerId);
|
||||
containsGroupsStatement.setString(2, sgroup);
|
||||
containsGroupsStatement.executeUpdate();
|
||||
|
@ -390,10 +422,9 @@ public class DatabaseRepository {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (SQLException e) {
|
||||
con.rollback();
|
||||
con.setAutoCommit(true);
|
||||
} catch (SQLException e) {
|
||||
con.rollback();
|
||||
con.setAutoCommit(true);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
|
@ -453,8 +484,8 @@ public class DatabaseRepository {
|
|||
statement.setInt(6, newCompletion.getScore());
|
||||
statement.executeUpdate();
|
||||
}
|
||||
|
||||
public boolean insertGroup(Group group){
|
||||
|
||||
public boolean insertGroup(Group group) {
|
||||
try {
|
||||
ensureConnection();
|
||||
PreparedStatement statement = DatabaseQuery.INSERT_GROUP.prepare(this.con);
|
||||
|
@ -462,7 +493,8 @@ public class DatabaseRepository {
|
|||
statement.setInt(2, group.getLinkToChapter());
|
||||
statement.setInt(3, group.getLinkToPuzzle());
|
||||
return statement.executeUpdate() >= 0;
|
||||
}catch(Exception e){}
|
||||
} catch (Exception e) {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,10 @@
|
|||
package be.jeffcheasey88.peeratcode.routes;
|
||||
|
||||
import java.util.Base64;
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
import be.jeffcheasey88.peeratcode.framework.HttpReader;
|
||||
import be.jeffcheasey88.peeratcode.framework.HttpUtil;
|
||||
import be.jeffcheasey88.peeratcode.framework.HttpWriter;
|
||||
|
@ -9,11 +14,6 @@ import be.jeffcheasey88.peeratcode.framework.User;
|
|||
import be.jeffcheasey88.peeratcode.model.Badge;
|
||||
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
||||
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
import java.util.Base64;
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
public class BadgeDetails implements Response {
|
||||
|
||||
private final DatabaseRepository databaseRepo;
|
||||
|
@ -22,22 +22,22 @@ public class BadgeDetails implements Response {
|
|||
this.databaseRepo = databaseRepo;
|
||||
}
|
||||
|
||||
@Route(path = "^\\/badge\\/([0-9]+)$")
|
||||
@Route(path = "^\\/badge\\/([0-9]+)$", needLogin = true)
|
||||
@Override
|
||||
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
||||
HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *");
|
||||
if (matcher.groupCount() > 0) {
|
||||
int badgeId = Integer.parseInt(matcher.group(1));
|
||||
Badge badge = databaseRepo.getBadge(badgeId);
|
||||
JSONObject badgeJSON = new JSONObject();
|
||||
if (badge != null) {
|
||||
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());
|
||||
}
|
||||
HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *");
|
||||
writer.write(badgeJSON.toJSONString().replace("\\", ""));
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
HttpUtil.responseHeaders(writer, 400, "Access-Control-Allow-Origin: *");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,10 @@
|
|||
package be.jeffcheasey88.peeratcode.routes;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
import be.jeffcheasey88.peeratcode.framework.HttpReader;
|
||||
import be.jeffcheasey88.peeratcode.framework.HttpUtil;
|
||||
import be.jeffcheasey88.peeratcode.framework.HttpWriter;
|
||||
|
@ -10,11 +15,6 @@ import be.jeffcheasey88.peeratcode.model.Chapter;
|
|||
import be.jeffcheasey88.peeratcode.model.Puzzle;
|
||||
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
||||
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
public class ChapterElement implements Response {
|
||||
|
||||
private final DatabaseRepository databaseRepo;
|
||||
|
@ -23,27 +23,32 @@ public class ChapterElement implements Response {
|
|||
this.databaseRepo = databaseRepo;
|
||||
}
|
||||
|
||||
@Route(path = "^\\/chapter\\/([0-9]+)$")
|
||||
@Route(path = "^\\/chapter\\/([0-9]+)$", needLogin = true)
|
||||
@Override
|
||||
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
||||
HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *");
|
||||
Chapter chapter = databaseRepo.getChapter(extractId(matcher));
|
||||
if (chapter != null) {
|
||||
JSONObject chapterJSON = new JSONObject();
|
||||
chapterJSON.put("id", chapter.getId());
|
||||
chapterJSON.put("name", chapter.getName());
|
||||
if (chapter.getStartDate() != null) chapterJSON.put("startDate", chapter.getStartDate().toString());
|
||||
if (chapter.getEndDate() != null) chapterJSON.put("endDate", chapter.getEndDate().toString());
|
||||
if (chapter.getStartDate() != null)
|
||||
chapterJSON.put("startDate", chapter.getStartDate().toString());
|
||||
if (chapter.getEndDate() != null)
|
||||
chapterJSON.put("endDate", chapter.getEndDate().toString());
|
||||
JSONArray puzzles = new JSONArray();
|
||||
for (Puzzle puzzle : chapter.getPuzzles()) {
|
||||
JSONObject puzzleJSON = new JSONObject();
|
||||
puzzleJSON.put("id", puzzle.getId());
|
||||
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);
|
||||
}
|
||||
chapterJSON.put("puzzles", puzzles);
|
||||
HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *");
|
||||
writer.write(chapterJSON.toJSONString());
|
||||
} else {
|
||||
HttpUtil.responseHeaders(writer, 400, "Access-Control-Allow-Origin: *");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -23,10 +23,9 @@ public class ChapterList implements Response {
|
|||
this.databaseRepo = databaseRepo;
|
||||
}
|
||||
|
||||
@Route(path = "^\\/chapters$")
|
||||
@Route(path = "^\\/chapters$", needLogin = true)
|
||||
@Override
|
||||
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
||||
HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *");
|
||||
List<Chapter> allChapters = databaseRepo.getAllChapters();
|
||||
if (allChapters != null) {
|
||||
JSONArray chaptersJSON = new JSONArray();
|
||||
|
@ -34,11 +33,16 @@ public class ChapterList implements Response {
|
|||
JSONObject chapterJSON = new JSONObject();
|
||||
chapterJSON.put("id", chapter.getId());
|
||||
chapterJSON.put("name", chapter.getName());
|
||||
if (chapter.getStartDate() != null) chapterJSON.put("startDate", chapter.getStartDate().toString());
|
||||
if (chapter.getEndDate() != null) chapterJSON.put("endDate", chapter.getEndDate().toString());
|
||||
if (chapter.getStartDate() != null)
|
||||
chapterJSON.put("startDate", chapter.getStartDate().toString());
|
||||
if (chapter.getEndDate() != null)
|
||||
chapterJSON.put("endDate", chapter.getEndDate().toString());
|
||||
chaptersJSON.add(chapterJSON);
|
||||
}
|
||||
HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *");
|
||||
writer.write(chaptersJSON.toJSONString());
|
||||
} else {
|
||||
HttpUtil.responseHeaders(writer, 400, "Access-Control-Allow-Origin: *");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package be.jeffcheasey88.peeratcode.routes;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Base64;
|
||||
import java.util.SortedSet;
|
||||
import java.util.regex.Matcher;
|
||||
|
@ -13,6 +14,8 @@ import be.jeffcheasey88.peeratcode.framework.HttpWriter;
|
|||
import be.jeffcheasey88.peeratcode.framework.Response;
|
||||
import be.jeffcheasey88.peeratcode.framework.Route;
|
||||
import be.jeffcheasey88.peeratcode.framework.User;
|
||||
import be.jeffcheasey88.peeratcode.model.Chapter;
|
||||
import be.jeffcheasey88.peeratcode.model.Group;
|
||||
import be.jeffcheasey88.peeratcode.model.Player;
|
||||
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
||||
|
||||
|
@ -24,18 +27,59 @@ public class Leaderboard implements Response {
|
|||
this.databaseRepo = databaseRepo;
|
||||
}
|
||||
|
||||
@Route(path = "^\\/leaderboard$")
|
||||
@Route(path = "^\\/leaderboard\\/?(\\d+)?$")
|
||||
@Override
|
||||
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
||||
HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *");
|
||||
if (matcher.group(1) != null) {
|
||||
groupsLeaderboard(Integer.parseInt(matcher.group(1)), writer);
|
||||
} else {
|
||||
playersLeaderboard(writer);
|
||||
}
|
||||
}
|
||||
|
||||
private void groupsLeaderboard(int chapterId, HttpWriter writer) throws IOException {
|
||||
Chapter chInfo = databaseRepo.getChapter(chapterId);
|
||||
|
||||
SortedSet<Group> allGroupsForChapter = databaseRepo.getAllGroupForChapterLeaderboard(chapterId);
|
||||
JSONObject leaderboardJSON = new JSONObject();
|
||||
if (chInfo.getStartDate() != null)
|
||||
leaderboardJSON.put("start_date", chInfo.getStartDate().toString());
|
||||
if (chInfo.getEndDate() != null)
|
||||
leaderboardJSON.put("end_date", chInfo.getEndDate().toString());
|
||||
JSONArray groupsJSON = new JSONArray();
|
||||
if (allGroupsForChapter != null) {
|
||||
int rank = 1;
|
||||
int sameRankCount = 1;
|
||||
Group previousGroup = null;
|
||||
for (Group g : allGroupsForChapter) {
|
||||
if (previousGroup != null) {
|
||||
if (g.compareTo(previousGroup) == 0) {
|
||||
sameRankCount++;
|
||||
} else {
|
||||
rank = rank + sameRankCount;
|
||||
sameRankCount = 1;
|
||||
}
|
||||
}
|
||||
groupsJSON.add(g.toJson(rank));
|
||||
previousGroup = g;
|
||||
}
|
||||
}
|
||||
leaderboardJSON.put("groups", groupsJSON);
|
||||
writer.write(leaderboardJSON.toJSONString().replace("\\", ""));
|
||||
}
|
||||
|
||||
private void playersLeaderboard(HttpWriter writer) throws IOException {
|
||||
SortedSet<Player> allPlayers = databaseRepo.getAllPlayerForLeaderboard();
|
||||
JSONArray playersJSON = new JSONArray();
|
||||
if (allPlayers != null) {
|
||||
for (Player player : allPlayers) {
|
||||
JSONObject playerJSON = new JSONObject();
|
||||
playerJSON.put("pseudo", player.getPseudo());
|
||||
if (player.getGroups() != null) playerJSON.put("groups", player.getJsonGroups());
|
||||
if(player.getAvatar() != null) playerJSON.put("avatar", Base64.getEncoder().encodeToString(player.getAvatar()));
|
||||
if (player.getGroups() != null)
|
||||
playerJSON.put("groups", player.getJsonGroups());
|
||||
if (player.getAvatar() != null)
|
||||
playerJSON.put("avatar", Base64.getEncoder().encodeToString(player.getAvatar()));
|
||||
playerJSON.put("rank", player.getRank());
|
||||
playerJSON.put("score", player.getTotalScore());
|
||||
playerJSON.put("completions", player.getTotalCompletion());
|
||||
|
|
|
@ -1,12 +1,7 @@
|
|||
package be.jeffcheasey88.peeratcode.routes;
|
||||
|
||||
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 be.jeffcheasey88.peeratcode.framework.HttpReader;
|
||||
|
@ -23,7 +18,7 @@ public class Login implements Response {
|
|||
private DatabaseRepository databaseRepo;
|
||||
private Router router;
|
||||
|
||||
public Login(DatabaseRepository databaseRepo, Router router){
|
||||
public Login(DatabaseRepository databaseRepo, Router router) {
|
||||
this.databaseRepo = databaseRepo;
|
||||
this.router = router;
|
||||
}
|
||||
|
@ -31,8 +26,8 @@ public class Login implements Response {
|
|||
@Route(path = "^\\/login$", type = "POST")
|
||||
@Override
|
||||
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
||||
if(user != null){
|
||||
HttpUtil.responseHeaders(writer, 403,"Access-Control-Allow-Origin: *");
|
||||
if (user != null) {
|
||||
HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *");
|
||||
return;
|
||||
}
|
||||
JSONObject informations = (JSONObject) HttpUtil.readJson(reader);
|
||||
|
@ -40,15 +35,14 @@ public class Login implements Response {
|
|||
String pseudo = (String) informations.get("pseudo");
|
||||
String password = (String) informations.get("passwd");
|
||||
int id;
|
||||
if ((id = databaseRepo.login(pseudo, password)) >= 0){
|
||||
HttpUtil.responseHeaders(writer, 200,
|
||||
"Access-Control-Allow-Origin: *",
|
||||
if ((id = databaseRepo.login(pseudo, password)) >= 0) {
|
||||
HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *",
|
||||
"Access-Control-Expose-Headers: Authorization",
|
||||
"Authorization: Bearer "+this.router.createAuthUser(id));
|
||||
return;
|
||||
"Authorization: Bearer " + this.router.createAuthUser(id));
|
||||
}
|
||||
} else {
|
||||
HttpUtil.responseHeaders(writer, 400, "Access-Control-Allow-Origin: *");
|
||||
}
|
||||
HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -22,30 +22,33 @@ public class PlayerDetails implements Response {
|
|||
this.databaseRepo = databaseRepo;
|
||||
}
|
||||
|
||||
@Route(path = "^\\/player\\/?(.+)?$")
|
||||
@Route(path = "^\\/player\\/?(.+)?$", needLogin = true)
|
||||
@Override
|
||||
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
||||
Player player;
|
||||
if (matcher.group(1) != null){
|
||||
if (matcher.group(1) != null) {
|
||||
player = databaseRepo.getPlayerDetails(matcher.group(1));
|
||||
} else {
|
||||
player = databaseRepo.getPlayerDetails(user.getId());
|
||||
}
|
||||
JSONObject playerJSON = new JSONObject();
|
||||
if (player != null) {
|
||||
HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *");
|
||||
playerJSON.put("pseudo", player.getPseudo());
|
||||
playerJSON.put("email", player.getEmail());
|
||||
playerJSON.put("firstname", player.getFirstname());
|
||||
playerJSON.put("lastname", player.getLastname());
|
||||
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("score", player.getTotalScore());
|
||||
playerJSON.put("completions", player.getTotalCompletion());
|
||||
playerJSON.put("tries", player.getTotalTries());
|
||||
if (player.getBadges() != null) playerJSON.put("badges", player.getJsonBadges());
|
||||
if(player.getAvatar() != null) playerJSON.put("avatar", Base64.getEncoder().encodeToString(player.getAvatar()));
|
||||
if (player.getBadges() != null)
|
||||
playerJSON.put("badges", player.getJsonBadges());
|
||||
if (player.getAvatar() != null)
|
||||
playerJSON.put("avatar", Base64.getEncoder().encodeToString(player.getAvatar()));
|
||||
HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *");
|
||||
writer.write(playerJSON.toJSONString().replace("\\", ""));
|
||||
} else {
|
||||
HttpUtil.responseHeaders(writer, 400, "Access-Control-Allow-Origin: *");
|
||||
|
|
|
@ -1,11 +1,7 @@
|
|||
package be.jeffcheasey88.peeratcode.routes;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.jose4j.json.internal.json_simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
import be.jeffcheasey88.peeratcode.framework.HttpReader;
|
||||
|
@ -25,20 +21,25 @@ public class PuzzleElement implements Response {
|
|||
this.databaseRepo = databaseRepo;
|
||||
}
|
||||
|
||||
@Route(path = "^\\/puzzle\\/([0-9]+)$")
|
||||
@Route(path = "^\\/puzzle\\/([0-9]+)$", needLogin = true)
|
||||
@Override
|
||||
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
||||
HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *", "Content-Type: application/json");
|
||||
Puzzle puzzle = databaseRepo.getPuzzle(extractId(matcher));
|
||||
if (puzzle != null) {
|
||||
JSONObject puzzleJSON = new JSONObject();
|
||||
puzzleJSON.put("id", puzzle.getId());
|
||||
puzzleJSON.put("name", puzzle.getName());
|
||||
puzzleJSON.put("content", puzzle.getContent());
|
||||
if (puzzle.getTags() != null) puzzleJSON.put("tags", puzzle.getJsonTags());
|
||||
if (puzzle.getDepend() > 0) puzzleJSON.put("depend", puzzle.getDepend());
|
||||
if (puzzle.getTags() != null)
|
||||
puzzleJSON.put("tags", puzzle.getJsonTags());
|
||||
if (puzzle.getDepend() > 0)
|
||||
puzzleJSON.put("depend", puzzle.getDepend());
|
||||
HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *", "Content-Type: application/json");
|
||||
writer.write(puzzleJSON.toJSONString());
|
||||
}
|
||||
else {
|
||||
HttpUtil.responseHeaders(writer, 400, "Access-Control-Allow-Origin: *");
|
||||
}
|
||||
}
|
||||
|
||||
private int extractId(Matcher matcher) {
|
||||
|
|
|
@ -22,9 +22,11 @@ import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
|||
|
||||
public class PuzzleResponse implements Response {
|
||||
private final DatabaseRepository databaseRepo;
|
||||
private final String usersFilesPath;
|
||||
|
||||
public PuzzleResponse(DatabaseRepository databaseRepo) {
|
||||
public PuzzleResponse(DatabaseRepository databaseRepo, String initUsersFilesPath) {
|
||||
this.databaseRepo = databaseRepo;
|
||||
usersFilesPath = initUsersFilesPath;
|
||||
}
|
||||
|
||||
@Route(path = "^\\/puzzleResponse\\/([0-9]+)$", type = "POST", needLogin = true)
|
||||
|
@ -45,22 +47,23 @@ public class PuzzleResponse implements Response {
|
|||
HttpUtil.responseHeaders(writer, 406, "Access-Control-Allow-Origin: *", "Content-Type: application/json");
|
||||
responseJSON.put("tries", completion.getTries());
|
||||
} else {
|
||||
HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *");
|
||||
HttpUtil.responseHeaders(writer, 400, "Access-Control-Allow-Origin: *");
|
||||
return;
|
||||
}
|
||||
writer.write(responseJSON.toJSONString());
|
||||
}
|
||||
|
||||
private void saveSourceCode(ReceivedResponse received, Player player) throws IOException {
|
||||
Path path = Paths.get(String.format("%s/puz%04d-%s", player.getPathToSourceCode(), 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());
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class ReceivedResponse {
|
||||
|
||||
|
||||
private int puzzleId;
|
||||
private byte[] response;
|
||||
private String fileName;
|
||||
|
@ -71,7 +74,7 @@ class ReceivedResponse {
|
|||
public ReceivedResponse(Matcher matcher, HttpReader reader) throws Exception {
|
||||
this.reader = reader;
|
||||
puzzleId = Integer.parseInt(matcher.group(1));
|
||||
|
||||
|
||||
List<String> multiPartData = HttpUtil.readMultiPartData(reader);
|
||||
this.response = multiPartData.get(0).getBytes();
|
||||
this.fileName = multiPartData.get(1);
|
||||
|
|
|
@ -4,7 +4,6 @@ import java.io.IOException;
|
|||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
|
@ -15,24 +14,25 @@ import be.jeffcheasey88.peeratcode.framework.Response;
|
|||
import be.jeffcheasey88.peeratcode.framework.Route;
|
||||
import be.jeffcheasey88.peeratcode.framework.Router;
|
||||
import be.jeffcheasey88.peeratcode.framework.User;
|
||||
import be.jeffcheasey88.peeratcode.model.Player;
|
||||
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
||||
|
||||
public class Register implements Response {
|
||||
|
||||
private DatabaseRepository databaseRepo;
|
||||
private Router router;
|
||||
private String usersFilesPath;
|
||||
|
||||
public Register(DatabaseRepository databaseRepo, Router router) {
|
||||
public Register(DatabaseRepository databaseRepo, Router router, String initUsersFilesPath) {
|
||||
this.databaseRepo = databaseRepo;
|
||||
this.router = router;
|
||||
usersFilesPath = initUsersFilesPath;
|
||||
}
|
||||
|
||||
@Route(path = "^\\/register$", type = "POST")
|
||||
@Override
|
||||
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
||||
if(user != null){
|
||||
HttpUtil.responseHeaders(writer, 403,"Access-Control-Allow-Origin: *");
|
||||
if (user != null) {
|
||||
HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *");
|
||||
return;
|
||||
}
|
||||
JSONObject informations = (JSONObject) HttpUtil.readJson(reader);
|
||||
|
@ -60,27 +60,28 @@ public class Register implements Response {
|
|||
int id;
|
||||
if ((id = databaseRepo.register(pseudo, email, password, firstname, lastname, description, group,
|
||||
avatar)) >= 0) {
|
||||
HttpUtil.responseHeaders(writer, 200,
|
||||
"Access-Control-Allow-Origin: *",
|
||||
HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *",
|
||||
"Access-Control-Expose-Headers: Authorization",
|
||||
"Authorization: Bearer " + this.router.createAuthUser(id));
|
||||
createFolderToSaveSourceCode(pseudo);
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *");
|
||||
HttpUtil.responseHeaders(writer, 400, "Access-Control-Allow-Origin: *");
|
||||
JSONObject error = new JSONObject();
|
||||
error.put("username_valid", pseudoAvailable);
|
||||
error.put("email_valid", emailAvailable);
|
||||
writer.write(error.toJSONString());
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
HttpUtil.responseHeaders(writer, 400, "Access-Control-Allow-Origin: *");
|
||||
}
|
||||
HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *");
|
||||
}
|
||||
|
||||
private void createFolderToSaveSourceCode(String pseudo) throws IOException {
|
||||
Files.createDirectories(Paths.get(String.format(Player.PATH_TO_CODE, pseudo)));
|
||||
|
||||
Files.createDirectories(Paths.get(String.format("%s/%s", usersFilesPath, pseudo)));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -10,25 +10,25 @@ import be.jeffcheasey88.peeratcode.framework.Route;
|
|||
import be.jeffcheasey88.peeratcode.framework.User;
|
||||
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
||||
|
||||
public class Result implements Response{
|
||||
|
||||
public class Result implements Response {
|
||||
|
||||
private DatabaseRepository repo;
|
||||
|
||||
public Result(DatabaseRepository repo){
|
||||
|
||||
public Result(DatabaseRepository repo) {
|
||||
this.repo = repo;
|
||||
}
|
||||
|
||||
|
||||
@Route(path = "^\\/result\\/(\\d+)$", needLogin = true)
|
||||
@Override
|
||||
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
||||
int puzzle = Integer.parseInt(matcher.group(1));
|
||||
|
||||
|
||||
int score = this.repo.getScore(user.getId(), puzzle);
|
||||
if(score < 0) {
|
||||
if (score < 0) {
|
||||
HttpUtil.responseHeaders(writer, 425, "Access-Control-Allow-Origin: *");
|
||||
}else{
|
||||
} else {
|
||||
HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *");
|
||||
writer.write(score+"");
|
||||
writer.write(score + "");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -13,11 +13,11 @@ import be.jeffcheasey88.peeratcode.framework.User;
|
|||
import be.jeffcheasey88.peeratcode.model.Group;
|
||||
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
||||
|
||||
public class CreateGroup implements Response{
|
||||
|
||||
public class CreateGroup implements Response {
|
||||
|
||||
private DatabaseRepository repo;
|
||||
|
||||
public CreateGroup(DatabaseRepository repo){
|
||||
|
||||
public CreateGroup(DatabaseRepository repo) {
|
||||
this.repo = repo;
|
||||
}
|
||||
|
||||
|
@ -25,10 +25,10 @@ public class CreateGroup implements Response{
|
|||
@Override
|
||||
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
||||
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: *");
|
||||
}else{
|
||||
} else {
|
||||
HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,11 +13,11 @@ import be.jeffcheasey88.peeratcode.framework.User;
|
|||
import be.jeffcheasey88.peeratcode.model.Group;
|
||||
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
||||
|
||||
public class GroupList implements Response{
|
||||
|
||||
public class GroupList implements Response {
|
||||
|
||||
private DatabaseRepository repo;
|
||||
|
||||
public GroupList(DatabaseRepository repo){
|
||||
|
||||
public GroupList(DatabaseRepository repo) {
|
||||
this.repo = repo;
|
||||
}
|
||||
|
||||
|
@ -26,7 +26,8 @@ public class GroupList implements Response{
|
|||
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
||||
HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *");
|
||||
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());
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue