New Json lib

This commit is contained in:
jeffcheasey88 2023-09-13 23:48:14 +02:00
parent c3621da722
commit 0248b7b07c
26 changed files with 221 additions and 236 deletions

Binary file not shown.

View file

@ -8,15 +8,14 @@ import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import dev.peerat.backend.model.PeerAtUser;
import dev.peerat.framework.RequestType;
import dev.peerat.framework.Response;
import dev.peerat.framework.Route;
import dev.peerat.framework.RouteMapper;
import dev.peerat.framework.Router;
import dev.peerat.framework.utils.json.JsonArray;
import dev.peerat.framework.utils.json.JsonMap;
public class RouteExtracter {
@ -59,15 +58,15 @@ public class RouteExtracter {
}
}
public JSONObject swagger(String host) throws Exception{
JSONObject result = new JSONObject();
result.put("swagger","2.0");
JSONObject info = new JSONObject();
info.put("title", "Peer-at-code backend api routes");
info.put("description", "Using Peer-at Code Framework");
result.put("info", info);
result.put("host", host);
result.put("basePath","/");
public JsonMap swagger(String host) throws Exception{
JsonMap result = new JsonMap();
result.set("swagger","2.0");
JsonMap info = new JsonMap();
info.set("title", "Peer-at-code backend api routes");
info.set("description", "Using Peer-at Code Framework");
result.set("info", info);
result.set("host", host);
result.set("basePath","/");
List<Response> routes = new ArrayList<>();
RouteMapper[] mappers = getField(Router.class, router, "mappers");
@ -85,18 +84,18 @@ public class RouteExtracter {
name = name.substring(name.lastIndexOf('.')+1, name.length());
packages.add(name);
}
JSONArray tags = new JSONArray();
JsonArray tags = new JsonArray();
for(String tag : packages){
JSONObject current = new JSONObject();
current.put("name", tag);
JsonMap current = new JsonMap();
current.set("name", tag);
tags.add(current);
}
result.put("tags", tags);
JSONArray schemes = new JSONArray();
result.set("tags", tags);
JsonArray schemes = new JsonArray();
schemes.add("https");
result.put("schemes", schemes);
result.set("schemes", schemes);
JSONObject paths = new JSONObject();
JsonMap paths = new JsonMap();
for(Response response : routes){
Method method = response.getClass().getDeclaredMethod("exec",
@ -105,21 +104,21 @@ public class RouteExtracter {
RouteDoc[] docs = method.getDeclaredAnnotationsByType(RouteDoc.class);
if(docs.length < 1) continue;
RouteDoc base = docs[0];
JSONObject current = new JSONObject();
JSONObject data = new JSONObject();
JSONArray tag = new JSONArray();
JsonMap current = new JsonMap();
JsonMap data = new JsonMap();
JsonArray tag = new JsonArray();
String pack = response.getClass().getPackage().getName();
pack = pack.substring(pack.lastIndexOf('.')+1, pack.length());
tag.add(pack);
data.put("tags", tag);
data.set("tags", tag);
current.put(route.type().toString().toLowerCase(), data);
current.set(route.type().toString().toLowerCase(), data);
paths.put(base.path(), current);
paths.set(base.path(), current);
}
result.put("paths", paths);
result.set("paths", paths);
return result;
}

View file

@ -4,11 +4,11 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.jose4j.json.internal.json_simple.JSONArray;
import org.jose4j.json.internal.json_simple.JSONObject;
import be.jeffcheasey88.peeratcode.mapping.SeaBottle;
import be.jeffcheasey88.peeratcode.mapping.Treasure;
import dev.peerat.framework.utils.json.JsonMap;
public class Group implements Comparable<Group> {
private String name;
@ -21,11 +21,11 @@ public class Group implements Comparable<Group> {
return "Group[name="+name+", chapter="+linkToChapter+"]";
}
public Group(JSONObject json){
public Group(JsonMap json){
this.name = (String) json.get("name");
if (json.containsKey("chapter"))
if (json.has("chapter"))
this.linkToChapter = ((Number) json.get("chapter")).intValue();
// if (json.containsKey("puzzle"))
// if (json.has("puzzle"))
// this.linkToPuzzle = ((Number) json.get("puzzle")).intValue();
}

View file

@ -5,8 +5,8 @@ import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import dev.peerat.framework.utils.json.JsonArray;
import dev.peerat.framework.utils.json.JsonMap;
public class Player implements Comparable<Player> {
private String pseudo;
@ -70,9 +70,9 @@ public class Player implements Comparable<Player> {
return groups;
}
public JSONArray getJsonGroups() {
public JsonArray getJsonGroups() {
if (groups != null) {
JSONArray groupsJSON = new JSONArray();
JsonArray groupsJSON = new JsonArray();
for (Group group : groups) {
groupsJSON.add(group.toJson());
}
@ -121,13 +121,13 @@ public class Player implements Comparable<Player> {
return completions.size();
}
public JSONArray getJsonCompletions() {
JSONArray completionsJSON = new JSONArray();
public JsonArray getJsonCompletions() {
JsonArray completionsJSON = new JsonArray();
for (Completion completion : completions) {
JSONObject completionJSON = new JSONObject();
completionJSON.put("puzzleName", completion.getPuzzleName());
completionJSON.put("tries", completion.getTries());
completionJSON.put("score", completion.getScore());
JsonMap completionJSON = new JsonMap();
completionJSON.set("puzzleName", completion.getPuzzleName());
completionJSON.set("tries", completion.getTries());
completionJSON.set("score", completion.getScore());
completionsJSON.add(completionJSON);
}
return completionsJSON;
@ -145,17 +145,17 @@ public class Player implements Comparable<Player> {
return badges;
}
public JSONArray getJsonBadges() {
public JsonArray getJsonBadges() {
if (badges == null)
return null;
JSONArray badgesJSON = new JSONArray();
JsonArray badgesJSON = new JsonArray();
for (Badge badge : badges) {
JSONObject badgeJSON = new JSONObject();
badgeJSON.put("name", badge.getName());
JsonMap badgeJSON = new JsonMap();
badgeJSON.set("name", badge.getName());
byte[] logo = badge.getLogo();
if (logo != null)
badgeJSON.put("logo", Base64.getEncoder().encodeToString(logo));
badgeJSON.put("level", badge.getLevel());
badgeJSON.set("logo", Base64.getEncoder().encodeToString(logo));
badgeJSON.set("level", badge.getLevel());
badgesJSON.add(badgeJSON);
}
return badgesJSON;

View file

@ -6,8 +6,8 @@ import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import dev.peerat.framework.utils.json.JsonArray;
import dev.peerat.framework.utils.json.JsonMap;
public class Puzzle {
@ -63,13 +63,13 @@ public class Puzzle {
*
* @return DEATH
*/
public JSONArray getJsonTags() {
public JsonArray getJsonTags() {
if (tags == null)
return null;
JSONArray tagsJSON = new JSONArray();
JsonArray tagsJSON = new JsonArray();
for (String tag : tags) {
JSONObject tagJSON = new JSONObject();
tagJSON.put("name", tag);
JsonMap tagJSON = new JsonMap();
tagJSON.set("name", tag);
tagsJSON.add(tagJSON);
}
return tagsJSON;

View file

@ -3,8 +3,6 @@ package dev.peerat.backend.routes;
import java.util.Base64;
import java.util.regex.Matcher;
import org.json.simple.JSONObject;
import dev.peerat.backend.bonus.extract.RouteDoc;
import dev.peerat.backend.model.Badge;
import dev.peerat.backend.repository.DatabaseRepository;
@ -13,6 +11,7 @@ import dev.peerat.framework.HttpReader;
import dev.peerat.framework.HttpWriter;
import dev.peerat.framework.Response;
import dev.peerat.framework.Route;
import dev.peerat.framework.utils.json.JsonMap;
public class BadgeDetails implements Response {
@ -30,15 +29,14 @@ public class BadgeDetails implements Response {
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()));
badgeJSON.put("level", badge.getLevel());
JsonMap badgeJSON = new JsonMap();
if(badge != null){
badgeJSON.set("name", badge.getName());
if(badge.getLogo() != null) badgeJSON.set("logo", Base64.getEncoder().encodeToString(badge.getLogo()));
badgeJSON.set("level", badge.getLevel());
}
context.response(200);
writer.write(badgeJSON.toJSONString().replace("\\", ""));
writer.write(badgeJSON.toString().replace("\\", ""));
} else {
context.response(400);
}

View file

@ -2,9 +2,6 @@ package dev.peerat.backend.routes;
import java.util.regex.Matcher;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import dev.peerat.backend.bonus.extract.RouteDoc;
import dev.peerat.backend.model.Chapter;
import dev.peerat.backend.model.PeerAtUser;
@ -15,6 +12,8 @@ import dev.peerat.framework.HttpReader;
import dev.peerat.framework.HttpWriter;
import dev.peerat.framework.Response;
import dev.peerat.framework.Route;
import dev.peerat.framework.utils.json.JsonArray;
import dev.peerat.framework.utils.json.JsonMap;
public class ChapterElement implements Response {
@ -31,29 +30,29 @@ public class ChapterElement implements Response {
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
Chapter chapter = databaseRepo.getChapter(Integer.parseInt(matcher.group(1)));
if (chapter != null){
JSONObject chapterJSON = new JSONObject();
chapterJSON.put("id", chapter.getId());
chapterJSON.put("name", chapter.getName());
JsonMap chapterJSON = new JsonMap();
chapterJSON.set("id", chapter.getId());
chapterJSON.set("name", chapter.getName());
boolean show = chapter.isInCurrentTime();
chapterJSON.put("show", show);
chapterJSON.set("show", show);
PeerAtUser user = context.getUser();
if(show){
JSONArray puzzles = new JSONArray();
JsonArray puzzles = new JsonArray();
for (Puzzle puzzle : chapter.getPuzzles()){
JSONObject puzzleJSON = new JSONObject();
puzzleJSON.put("id", puzzle.getId());
puzzleJSON.put("name", puzzle.getName());
puzzleJSON.put("scoreMax", puzzle.getScoreMax());
if (puzzle.getTags() != null) puzzleJSON.put("tags", puzzle.getJsonTags());
JsonMap puzzleJSON = new JsonMap();
puzzleJSON.set("id", puzzle.getId());
puzzleJSON.set("name", puzzle.getName());
puzzleJSON.set("scoreMax", puzzle.getScoreMax());
if (puzzle.getTags() != null) puzzleJSON.set("tags", puzzle.getJsonTags());
int score = this.databaseRepo.getScore(user.getId(), puzzle.getId());
if(score >= 0) puzzleJSON.put("score", score);
puzzleJSON.put("show", puzzle.isInCurrentTime());
if(score >= 0) puzzleJSON.set("score", score);
puzzleJSON.set("show", puzzle.isInCurrentTime());
puzzles.add(puzzleJSON);
}
chapterJSON.put("puzzles", puzzles);
chapterJSON.set("puzzles", puzzles);
}
context.response(200);
writer.write(chapterJSON.toJSONString());
writer.write(chapterJSON.toString());
} else {
context.response(400);
}

View file

@ -3,9 +3,6 @@ package dev.peerat.backend.routes;
import java.util.List;
import java.util.regex.Matcher;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import dev.peerat.backend.bonus.extract.RouteDoc;
import dev.peerat.backend.model.Chapter;
import dev.peerat.backend.repository.DatabaseRepository;
@ -14,6 +11,8 @@ import dev.peerat.framework.HttpReader;
import dev.peerat.framework.HttpWriter;
import dev.peerat.framework.Response;
import dev.peerat.framework.Route;
import dev.peerat.framework.utils.json.JsonArray;
import dev.peerat.framework.utils.json.JsonMap;
public class ChapterList implements Response {
@ -30,16 +29,16 @@ public class ChapterList implements Response {
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception {
List<Chapter> allChapters = databaseRepo.getAllChapters();
if (allChapters != null) {
JSONArray chaptersJSON = new JSONArray();
JsonArray chaptersJSON = new JsonArray();
for (Chapter chapter : allChapters) {
JSONObject chapterJSON = new JSONObject();
chapterJSON.put("id", chapter.getId());
chapterJSON.put("name", chapter.getName());
chapterJSON.put("show", chapter.isInCurrentTime());
JsonMap chapterJSON = new JsonMap();
chapterJSON.set("id", chapter.getId());
chapterJSON.set("name", chapter.getName());
chapterJSON.set("show", chapter.isInCurrentTime());
chaptersJSON.add(chapterJSON);
}
context.response(200);
writer.write(chaptersJSON.toJSONString());
writer.write(chaptersJSON.toString());
} else {
context.response(400);
}

View file

@ -4,9 +4,6 @@ import java.io.IOException;
import java.util.SortedSet;
import java.util.regex.Matcher;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import dev.peerat.backend.bonus.extract.RouteDoc;
import dev.peerat.backend.model.Chapter;
import dev.peerat.backend.model.Group;
@ -17,6 +14,8 @@ import dev.peerat.framework.HttpReader;
import dev.peerat.framework.HttpWriter;
import dev.peerat.framework.Response;
import dev.peerat.framework.Route;
import dev.peerat.framework.utils.json.JsonArray;
import dev.peerat.framework.utils.json.JsonMap;
public class Leaderboard implements Response {
@ -42,12 +41,12 @@ public class Leaderboard implements Response {
Chapter chInfo = databaseRepo.getChapter(chapterId);
SortedSet<Group> allGroupsForChapter = databaseRepo.getAllGroupForChapterLeaderboard(chapterId);
JSONObject leaderboardJSON = new JSONObject();
JsonMap leaderboardJSON = new JsonMap();
if (chInfo.getStartDate() != null)
leaderboardJSON.put("start_date", chInfo.getStartDate().toString());
leaderboardJSON.set("start_date", chInfo.getStartDate().toString());
if (chInfo.getEndDate() != null)
leaderboardJSON.put("end_date", chInfo.getEndDate().toString());
JSONArray groupsJSON = new JSONArray();
leaderboardJSON.set("end_date", chInfo.getEndDate().toString());
JsonArray groupsJSON = new JsonArray();
if (allGroupsForChapter != null) {
int rank = 1;
int sameRankCount = 1;
@ -65,28 +64,28 @@ public class Leaderboard implements Response {
previousGroup = g;
}
}
leaderboardJSON.put("groups", groupsJSON);
writer.write(leaderboardJSON.toJSONString().replace("\\", ""));
leaderboardJSON.set("groups", groupsJSON);
writer.write(leaderboardJSON.toString().replace("\\", ""));
}
public final void playersLeaderboard(HttpWriter writer) throws IOException {
SortedSet<Player> allPlayers = databaseRepo.getAllPlayerForLeaderboard();
JSONArray playersJSON = new JSONArray();
JsonArray playersJSON = new JsonArray();
if (allPlayers != null) {
for (Player player : allPlayers) {
JSONObject playerJSON = new JSONObject();
playerJSON.put("pseudo", player.getPseudo());
JsonMap playerJSON = new JsonMap();
playerJSON.set("pseudo", player.getPseudo());
if (player.getGroups() != null)
playerJSON.put("groups", player.getJsonGroups());
playerJSON.set("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());
playerJSON.put("tries", player.getTotalTries());
playerJSON.set("rank", player.getRank());
playerJSON.set("score", player.getTotalScore());
playerJSON.set("completions", player.getTotalCompletion());
playerJSON.set("tries", player.getTotalTries());
playersJSON.add(playerJSON);
}
}
writer.write(playersJSON.toJSONString().replace("\\", ""));
writer.write(playersJSON.toString().replace("\\", ""));
}
}

View file

@ -3,8 +3,6 @@ package dev.peerat.backend.routes;
import java.util.Base64;
import java.util.regex.Matcher;
import org.json.simple.JSONObject;
import dev.peerat.backend.bonus.extract.RouteDoc;
import dev.peerat.backend.model.PeerAtUser;
import dev.peerat.backend.model.Player;
@ -14,6 +12,7 @@ import dev.peerat.framework.HttpReader;
import dev.peerat.framework.HttpWriter;
import dev.peerat.framework.Response;
import dev.peerat.framework.Route;
import dev.peerat.framework.utils.json.JsonMap;
public class PlayerDetails implements Response {
@ -35,26 +34,26 @@ public class PlayerDetails implements Response {
PeerAtUser user = context.getUser();
player = databaseRepo.getPlayerDetails(user.getId());
}
JSONObject playerJSON = new JSONObject();
JsonMap playerJSON = new JsonMap();
if (player != null) {
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());
playerJSON.set("pseudo", player.getPseudo());
playerJSON.set("email", player.getEmail());
playerJSON.set("firstname", player.getFirstname());
playerJSON.set("lastname", player.getLastname());
playerJSON.set("description", player.getDescription());
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("completionsList", player.getJsonCompletions());
playerJSON.put("tries", player.getTotalTries());
playerJSON.set("groups", player.getJsonGroups());
playerJSON.set("rank", player.getRank());
playerJSON.set("score", player.getTotalScore());
playerJSON.set("completions", player.getTotalCompletion());
playerJSON.set("completionsList", player.getJsonCompletions());
playerJSON.set("tries", player.getTotalTries());
if (player.getBadges() != null)
playerJSON.put("badges", player.getJsonBadges());
playerJSON.set("badges", player.getJsonBadges());
if (player.getAvatar() != null)
playerJSON.put("avatar", Base64.getEncoder().encodeToString(player.getAvatar()));
playerJSON.set("avatar", Base64.getEncoder().encodeToString(player.getAvatar()));
context.response(200);
writer.write(playerJSON.toJSONString().replace("\\", ""));
writer.write(playerJSON.toString().replace("\\", ""));
} else {
context.response(400);
}

View file

@ -3,8 +3,6 @@ package dev.peerat.backend.routes;
import java.time.LocalDateTime;
import java.util.regex.Matcher;
import org.json.simple.JSONObject;
import dev.peerat.backend.bonus.extract.RouteDoc;
import dev.peerat.backend.model.Chapter;
import dev.peerat.backend.model.Completion;
@ -16,6 +14,7 @@ import dev.peerat.framework.HttpReader;
import dev.peerat.framework.HttpWriter;
import dev.peerat.framework.Response;
import dev.peerat.framework.Route;
import dev.peerat.framework.utils.json.JsonMap;
public class PuzzleElement implements Response {
@ -49,20 +48,20 @@ public class PuzzleElement implements Response {
PeerAtUser user = context.getUser();
JSONObject puzzleJSON = new JSONObject();
puzzleJSON.put("id", puzzle.getId());
puzzleJSON.put("name", puzzle.getName());
puzzleJSON.put("content", puzzle.getContent());
puzzleJSON.put("scoreMax", puzzle.getScoreMax());
if(puzzle.getTags() != null) puzzleJSON.put("tags", puzzle.getJsonTags());
JsonMap puzzleJSON = new JsonMap();
puzzleJSON.set("id", puzzle.getId());
puzzleJSON.set("name", puzzle.getName());
puzzleJSON.set("content", puzzle.getContent());
puzzleJSON.set("scoreMax", puzzle.getScoreMax());
if(puzzle.getTags() != null) puzzleJSON.set("tags", puzzle.getJsonTags());
Completion completion = this.databaseRepo.getCompletionGroup(user.getId(), puzzle.getId());
if(completion != null && completion.getScore() >= 0){
puzzleJSON.put("score", completion.getScore());
puzzleJSON.put("tries", completion.getTries());
puzzleJSON.set("score", completion.getScore());
puzzleJSON.set("tries", completion.getTries());
}
if(puzzle.getDepend() > 0) puzzleJSON.put("depend", puzzle.getDepend());
if(puzzle.getDepend() > 0) puzzleJSON.set("depend", puzzle.getDepend());
context.response(200, "Content-Type: application/json");
writer.write(puzzleJSON.toJSONString());
writer.write(puzzleJSON.toString());
}
else {
context.response(400);

View file

@ -11,8 +11,6 @@ import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import org.json.simple.JSONObject;
import dev.peerat.backend.bonus.extract.RouteDoc;
import dev.peerat.backend.model.Chapter;
import dev.peerat.backend.model.Completion;
@ -27,6 +25,7 @@ import dev.peerat.framework.HttpWriter;
import dev.peerat.framework.Locker;
import dev.peerat.framework.Response;
import dev.peerat.framework.Route;
import dev.peerat.framework.utils.json.JsonMap;
public class PuzzleResponse implements Response {
private final DatabaseRepository databaseRepo;
@ -56,7 +55,7 @@ public class PuzzleResponse implements Response {
PeerAtUser user = context.getUser();
//saveSourceCode(received, databaseRepo.getPlayer(user.getId()));
JSONObject responseJSON = new JSONObject();
JsonMap responseJSON = new JsonMap();
if(this.databaseRepo.getScore(user.getId(), received.getPuzzleId()) > 0){
context.response(403);
return;
@ -74,9 +73,9 @@ public class PuzzleResponse implements Response {
if(LocalDateTime.now().isAfter(chapter.getEndDate().toLocalDateTime())){
if(Arrays.equals(currentPuzzle.getSoluce(), received.getResponse())){
context.response(200, "Content-Type: application/json");
JSONObject theoSaisPasJusteRecevoir200 = new JSONObject();
theoSaisPasJusteRecevoir200.put("success", true);
writer.write(theoSaisPasJusteRecevoir200.toJSONString());
JsonMap theoSaisPasJusteRecevoir200 = new JsonMap();
theoSaisPasJusteRecevoir200.set("success", true);
writer.write(theoSaisPasJusteRecevoir200.toString());
return;
}
context.response(423);
@ -98,14 +97,14 @@ public class PuzzleResponse implements Response {
}
if(completion.getScore() > 0){
context.response(200, "Content-Type: application/json");
responseJSON.put("score", completion.getScore());
responseJSON.put("tries", completion.getTries());
responseJSON.set("score", completion.getScore());
responseJSON.set("tries", completion.getTries());
}else{
context.response(406, "Content-Type: application/json");
responseJSON.put("tries", completion.getTries());
responseJSON.set("tries", completion.getTries());
}
writer.write(responseJSON.toJSONString());
writer.write(responseJSON.toString());
writer.flush();
leaderboard.setValue(completion);

View file

@ -15,7 +15,7 @@ public class Swagger implements Response{
public Swagger(RouteExtracter extracter, String host){
try{
this.json = extracter.swagger(host).toJSONString();
this.json = extracter.swagger(host).toString();
}catch(Exception e){
e.printStackTrace();
json = "{}";

View file

@ -2,7 +2,7 @@ package dev.peerat.backend.routes.admins;
import java.util.regex.Matcher;
import org.json.simple.JSONObject;
import org.jose4j.json.internal.json_simple.JSONObject;
import dev.peerat.backend.bonus.extract.RouteDoc;
import dev.peerat.backend.model.PeerAtUser;
@ -14,6 +14,7 @@ import dev.peerat.framework.Locker;
import dev.peerat.framework.Locker.Key;
import dev.peerat.framework.Response;
import dev.peerat.framework.Route;
import dev.peerat.framework.utils.json.JsonMap;
public class DynamicLogs implements Response{
@ -39,14 +40,14 @@ public class DynamicLogs implements Response{
while(!reader.isClosed()){
locker.lock(key);
Context instance = locker.getValue(key);
JSONObject json = new JSONObject();
json.put("logged", instance.isLogged());
if(instance.isLogged()) json.put("pseudo", repo.getPlayer(instance.<PeerAtUser>getUser().getId()).getPseudo());
json.put("path", instance.getPath());
json.put("type", instance.getType());
json.put("code", instance.getResponseCode());
JsonMap json = new JsonMap();
json.set("logged", instance.isLogged());
if(instance.isLogged()) json.set("pseudo", repo.getPlayer(instance.<PeerAtUser>getUser().getId()).getPseudo());
json.set("path", instance.getPath());
json.set("type", instance.getType());
json.set("code", instance.getResponseCode());
writer.write(json.toJSONString());
writer.write(json.toString());
writer.flush();
}
}catch(Exception e){

View file

@ -2,8 +2,6 @@ package dev.peerat.backend.routes.groups;
import java.util.regex.Matcher;
import org.json.simple.JSONArray;
import dev.peerat.backend.bonus.extract.RouteDoc;
import dev.peerat.backend.model.Group;
import dev.peerat.backend.repository.DatabaseRepository;
@ -12,6 +10,7 @@ import dev.peerat.framework.HttpReader;
import dev.peerat.framework.HttpWriter;
import dev.peerat.framework.Response;
import dev.peerat.framework.Route;
import dev.peerat.framework.utils.json.JsonArray;
public class GroupList implements Response {
@ -26,9 +25,9 @@ public class GroupList implements Response {
@Route(path = "^\\/groups$", needLogin = true)
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception {
context.response(200);
JSONArray result = new JSONArray();
JsonArray result = new JsonArray();
for(Group group : this.repo.getAllGroups()) result.add(group.toJson());
writer.write(result.toJSONString());
writer.write(result.toString());
}
}

View file

@ -2,8 +2,6 @@ package dev.peerat.backend.routes.users;
import java.util.regex.Matcher;
import org.jose4j.json.internal.json_simple.JSONObject;
import dev.peerat.backend.bonus.extract.RouteDoc;
import dev.peerat.backend.model.PeerAtUser;
import dev.peerat.backend.repository.DatabaseRepository;
@ -13,6 +11,7 @@ import dev.peerat.framework.HttpWriter;
import dev.peerat.framework.RequestType;
import dev.peerat.framework.Response;
import dev.peerat.framework.Route;
import dev.peerat.framework.utils.json.JsonMap;
public class ChangePassword implements Response{
@ -27,7 +26,7 @@ public class ChangePassword implements Response{
@Route(path = "^/user/cpw$", type = RequestType.POST, needLogin = true)
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception {
String password = (String) reader.<JSONObject>readJson().get("password");
String password = reader.<JsonMap>readJson().get("password");
repo.updatePassword(context.<PeerAtUser>getUser().getId(), password);
context.response(200);

View file

@ -5,8 +5,6 @@ import java.util.Map;
import java.util.Random;
import java.util.regex.Matcher;
import org.json.simple.JSONObject;
import dev.peerat.backend.model.PeerAtUser;
import dev.peerat.backend.repository.DatabaseRepository;
import dev.peerat.backend.utils.FormResponse;
@ -16,6 +14,7 @@ import dev.peerat.framework.HttpReader;
import dev.peerat.framework.HttpWriter;
import dev.peerat.framework.Route;
import dev.peerat.framework.Router;
import dev.peerat.framework.utils.json.JsonMap;
public class ForgotPassword extends FormResponse{
@ -38,13 +37,13 @@ public class ForgotPassword extends FormResponse{
return;
}
JSONObject json = json(reader);
JsonMap json = json(reader);
if(!areValids("email")){
context.response(400);
return;
}
String email = (String) json.get("email");
String email = json.get("email");
int player = repo.getPlayerId(email);
if(player < 0){
@ -59,8 +58,8 @@ public class ForgotPassword extends FormResponse{
return;
}
int code = ((Long)json.get("code")).intValue();
String password = (String)json.get("password");
int code = json.<Number>get("code").intValue();
String password = json.get("password");
if(code == checkCode.intValue()){
codes.remove(email);

View file

@ -4,8 +4,6 @@ import static dev.peerat.framework.RequestType.POST;
import java.util.regex.Matcher;
import org.json.simple.JSONObject;
import dev.peerat.backend.bonus.extract.RouteDoc;
import dev.peerat.backend.model.PeerAtUser;
import dev.peerat.backend.repository.DatabaseRepository;
@ -15,6 +13,7 @@ import dev.peerat.framework.HttpReader;
import dev.peerat.framework.HttpWriter;
import dev.peerat.framework.Route;
import dev.peerat.framework.Router;
import dev.peerat.framework.utils.json.JsonMap;
public class Login extends FormResponse{
@ -36,13 +35,13 @@ public class Login extends FormResponse{
context.response(403);
return;
}
JSONObject json = json(reader);
JsonMap json = json(reader);
if(!areValids("pseudo", "passwd")){
context.response(400);
return;
}
int id;
if((id = databaseRepo.login((String)json.get("pseudo"), (String)json.get("passwd"))) >= 0){
if((id = databaseRepo.login(json.get("pseudo"), json.get("passwd"))) >= 0){
context.response(200,
"Access-Control-Expose-Headers: Authorization",
"Authorization: Bearer " + this.router.createAuthUser(new PeerAtUser(id)));

View file

@ -14,14 +14,14 @@ import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAPublicKey;
import java.util.Base64;
import java.util.Map;
import java.util.Base64.Encoder;
import java.util.Map;
import java.util.regex.Matcher;
import javax.net.ssl.HttpsURLConnection;
import org.json.simple.JSONAware;
import org.json.simple.JSONObject;
import org.jose4j.json.internal.json_simple.JSONAware;
import org.jose4j.json.internal.json_simple.JSONObject;
import dev.peerat.backend.bonus.extract.RouteDoc;
import dev.peerat.backend.model.PeerAtUser;
@ -32,6 +32,7 @@ import dev.peerat.framework.HttpReader;
import dev.peerat.framework.HttpWriter;
import dev.peerat.framework.Route;
import dev.peerat.framework.Router;
import dev.peerat.framework.utils.json.JsonMap;
public class MailConfirmation extends FormResponse {
@ -73,17 +74,17 @@ public class MailConfirmation extends FormResponse {
context.response(403);
return;
}
JSONObject json = json(reader);
JsonMap json = json(reader);
if((!areValids("email","pseudo","firstname","lastname","passwd")) || (!hasFields("code"))){
context.response(400);
return;
}
String email = (String) json.get("email");
int code = ((Long)json.get("code")).intValue();
String pseudo = (String) json.get("pseudo");
String firstname = (String) json.get("firstname");
String lastname = (String) json.get("lastname");
String password = (String) json.get("passwd");
String email = json.get("email");
int code = json.<Number>get("code").intValue();
String pseudo = json.get("pseudo");
String firstname = json.get("firstname");
String lastname = json.get("lastname");
String password = json.get("passwd");
Integer checkCode = playersWaiting.get(email);
if(checkCode == null){
@ -101,10 +102,10 @@ public class MailConfirmation extends FormResponse {
"Authorization: Bearer " + this.router.createAuthUser(new PeerAtUser(id)));
}else{
context.response(400);
JSONObject error = new JSONObject();
error.put("username_valid", pseudo);
error.put("email_valid", email);
writer.write(error.toJSONString());
JsonMap error = new JsonMap();
error.set("username_valid", pseudo);
error.set("email_valid", email);
writer.write(error.toString());
}
}else{
context.response(400);

View file

@ -2,8 +2,6 @@ package dev.peerat.backend.routes.users;
import java.util.regex.Matcher;
import org.json.simple.JSONObject;
import dev.peerat.backend.bonus.extract.RouteDoc;
import dev.peerat.backend.model.PeerAtUser;
import dev.peerat.backend.model.Player;
@ -14,6 +12,7 @@ import dev.peerat.framework.HttpWriter;
import dev.peerat.framework.RequestType;
import dev.peerat.framework.Response;
import dev.peerat.framework.Route;
import dev.peerat.framework.utils.json.JsonMap;
public class ProfileSettings implements Response{
@ -28,12 +27,12 @@ public class ProfileSettings implements Response{
@Route(path = "^/user/settings$", type = RequestType.POST, needLogin = true)
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception {
JSONObject json = reader.readJson();
JsonMap json = reader.readJson();
String pseudo = (String) json.get("pseudo");
String email = (String) json.get("email");
String firstname = (String) json.get("firstname");
String lastname = (String) json.get("lastname");
String pseudo = json.get("pseudo");
String email = json.get("email");
String firstname = json.get("firstname");
String lastname = json.get("lastname");
Player player = repo.getPlayer(context.<PeerAtUser>getUser().getId());
if(!player.getPseudo().equals(pseudo)){

View file

@ -6,8 +6,6 @@ import java.util.Map;
import java.util.Random;
import java.util.regex.Matcher;
import org.json.simple.JSONObject;
import dev.peerat.backend.bonus.extract.RouteDoc;
import dev.peerat.backend.repository.DatabaseRepository;
import dev.peerat.backend.utils.FormResponse;
@ -16,6 +14,7 @@ import dev.peerat.framework.Context;
import dev.peerat.framework.HttpReader;
import dev.peerat.framework.HttpWriter;
import dev.peerat.framework.Route;
import dev.peerat.framework.utils.json.JsonMap;
public class Register extends FormResponse {
@ -39,13 +38,13 @@ public class Register extends FormResponse {
context.response(403);
return;
}
JSONObject json = json(reader);
JsonMap json = json(reader);
if(!areValids("email")){
context.response(400);
return;
}
String email = (String) json.get("email");
String email = json.get("email");
boolean emailAvailable = databaseRepo.checkEmailAvailability(email);
if(emailAvailable){
@ -55,9 +54,9 @@ public class Register extends FormResponse {
context.response(200);
}else{
context.response(400);
JSONObject error = new JSONObject();
error.put("email_valid", emailAvailable);
writer.write(error.toJSONString());
JsonMap error = new JsonMap();
error.set("email_valid", emailAvailable);
writer.write(error.toString());
}
}

View file

@ -4,14 +4,14 @@ import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;
import org.json.simple.JSONAware;
import dev.peerat.framework.HttpReader;
import dev.peerat.framework.Response;
import dev.peerat.framework.utils.json.Json;
import dev.peerat.framework.utils.json.JsonMap;
public abstract class FormResponse implements Response{
private JSONAware json;
private Json json;
private Map<String, Pattern> checker;
public FormResponse(){
@ -22,20 +22,20 @@ public abstract class FormResponse implements Response{
this.checker.put(key, regex);
}
public <T extends JSONAware> T json(HttpReader reader) throws Exception{
public <T extends Json> T json(HttpReader reader) throws Exception{
return (T) (this.json = reader.readJson());
}
public boolean hasFields(String... fields){
Map<?,?> map = (Map<?,?>)json;
JsonMap map = (JsonMap)json;
for(String field : fields){
if(!map.containsKey(field)) return false;
if(!map.has(field)) return false;
}
return true;
}
public boolean areValids(String... fields){
Map<?,?> map = (Map<?,?>)json;
JsonMap map = (JsonMap)json;
for(String field : fields){
String value = (String) map.get(field);
if(value == null) return false;

View file

@ -9,10 +9,9 @@ import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.json.simple.JSONObject;
import dev.peerat.framework.HttpReader;
import dev.peerat.framework.HttpWriter;
import dev.peerat.framework.utils.json.JsonMap;
public class WebClient {
@ -49,10 +48,10 @@ public class WebClient {
}
public void auth(String user, String password) throws Exception{
JSONObject login = new JSONObject();
login.put("pseudo", user);
login.put("passwd", password);
route("/login", "POST", login.toJSONString());
JsonMap login = new JsonMap();
login.set("pseudo", user);
login.set("passwd", password);
route("/login", "POST", login.toString());
System.out.println("["+host+"] /login "+login);
for(String line : this.headers){
@ -67,17 +66,17 @@ public class WebClient {
}
public void register(String user, String password, String email, String firstname, String lastname, String description) throws Exception{
JSONObject register = new JSONObject();
register.put("pseudo", user);
register.put("passwd", password);
register.put("email", email);
register.put("firstname", firstname);
register.put("lastname", lastname);
register.put("description", description);
register.put("sgroup", "");
register.put("avatar", "");
JsonMap register = new JsonMap();
register.set("pseudo", user);
register.set("passwd", password);
register.set("email", email);
register.set("firstname", firstname);
register.set("lastname", lastname);
register.set("description", description);
register.set("sgroup", "");
register.set("avatar", "");
route("/register", "POST", register.toJSONString());
route("/register", "POST", register.toString());
System.out.println("["+host+"] /register "+register);
for(String line : this.headers){
Matcher matcher = AUTORIZATION.matcher(line);

View file

@ -2,7 +2,6 @@ package dev.peerat.backend.routes;
import static org.junit.jupiter.api.Assertions.fail;
import org.json.simple.JSONObject;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
@ -11,6 +10,7 @@ import org.junit.jupiter.api.TestInstance.Lifecycle;
import dev.peerat.backend.Main;
import dev.peerat.backend.WebClient;
import dev.peerat.framework.utils.json.JsonMap;
@TestInstance(Lifecycle.PER_CLASS)
public class ScoreTests {
@ -42,14 +42,14 @@ public class ScoreTests {
@Test
void testOnDeployed(){
try{
JSONObject group = new JSONObject();
group.put("name", "GroupTest");
group.put("chapter", 2);
JsonMap group = new JsonMap();
group.set("name", "GroupTest");
group.set("chapter", 2);
client.register("Test1", "Test2", "Test3@Test7.be", "Test4", "Test5", "Test6");
client.assertResponseCode(200);
client.route("/groupCreate", "POST", group.toJSONString());
client.route("/groupCreate", "POST", group.toString());
client.assertResponseCode(200);
client.sendHeaders.add("content-type: ah;boundary=----WebKitFormBoundaryNUjiLBAMuX2dhxU7");
@ -60,7 +60,7 @@ public class ScoreTests {
client.disconnect();
client.auth("JeffCheasey88", "TheoPueDesPieds");
client.route("/groupJoin", "POST", group.toJSONString());
client.route("/groupJoin", "POST", group.toString());
client.assertResponseCode(200);
client.sendHeaders.add("content-type: ah;boundary=----WebKitFormBoundaryNUjiLBAMuX2dhxU7");

View file

@ -2,7 +2,6 @@ package dev.peerat.backend.routes;
import static org.junit.jupiter.api.Assertions.fail;
import org.json.simple.JSONObject;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
@ -11,6 +10,7 @@ import org.junit.jupiter.api.TestInstance.Lifecycle;
import dev.peerat.backend.Main;
import dev.peerat.backend.WebClient;
import dev.peerat.framework.utils.json.JsonMap;
@TestInstance(Lifecycle.PER_CLASS)
public class TmpRoutesTests {
@ -42,12 +42,12 @@ public class TmpRoutesTests {
@Test
void testOnDeployed(){
try {
JSONObject group = new JSONObject();
group.put("name", "MyTest");
group.put("chapter", 1);
JsonMap group = new JsonMap();
group.set("name", "MyTest");
group.set("chapter", 1);
client.auth("JeffCheasey88", "TheoPueDesPieds");
client.route("/groupCreate", "POST", group.toJSONString());
client.route("/groupCreate", "POST", group.toString());
client.assertResponseCode(200);
}catch(Exception e){

View file

@ -1,6 +1,5 @@
package dev.peerat.backend.userstories;
import org.json.simple.JSONObject;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@ -76,7 +75,7 @@ public class RegisterTests extends BaseUserStoriesTest{
@Test
public void lostField() throws Exception{
client.route("/register", "POST", new JSONObject().toJSONString());
client.route("/register", "POST", "{}");
client.assertResponseCode(400);
}