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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -4,9 +4,6 @@ import java.io.IOException;
import java.util.SortedSet; import java.util.SortedSet;
import java.util.regex.Matcher; 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.bonus.extract.RouteDoc;
import dev.peerat.backend.model.Chapter; import dev.peerat.backend.model.Chapter;
import dev.peerat.backend.model.Group; import dev.peerat.backend.model.Group;
@ -17,6 +14,8 @@ import dev.peerat.framework.HttpReader;
import dev.peerat.framework.HttpWriter; import dev.peerat.framework.HttpWriter;
import dev.peerat.framework.Response; import dev.peerat.framework.Response;
import dev.peerat.framework.Route; import dev.peerat.framework.Route;
import dev.peerat.framework.utils.json.JsonArray;
import dev.peerat.framework.utils.json.JsonMap;
public class Leaderboard implements Response { public class Leaderboard implements Response {
@ -42,12 +41,12 @@ public class Leaderboard implements Response {
Chapter chInfo = databaseRepo.getChapter(chapterId); Chapter chInfo = databaseRepo.getChapter(chapterId);
SortedSet<Group> allGroupsForChapter = databaseRepo.getAllGroupForChapterLeaderboard(chapterId); SortedSet<Group> allGroupsForChapter = databaseRepo.getAllGroupForChapterLeaderboard(chapterId);
JSONObject leaderboardJSON = new JSONObject(); JsonMap leaderboardJSON = new JsonMap();
if (chInfo.getStartDate() != null) if (chInfo.getStartDate() != null)
leaderboardJSON.put("start_date", chInfo.getStartDate().toString()); leaderboardJSON.set("start_date", chInfo.getStartDate().toString());
if (chInfo.getEndDate() != null) if (chInfo.getEndDate() != null)
leaderboardJSON.put("end_date", chInfo.getEndDate().toString()); leaderboardJSON.set("end_date", chInfo.getEndDate().toString());
JSONArray groupsJSON = new JSONArray(); JsonArray groupsJSON = new JsonArray();
if (allGroupsForChapter != null) { if (allGroupsForChapter != null) {
int rank = 1; int rank = 1;
int sameRankCount = 1; int sameRankCount = 1;
@ -65,28 +64,28 @@ public class Leaderboard implements Response {
previousGroup = g; previousGroup = g;
} }
} }
leaderboardJSON.put("groups", groupsJSON); leaderboardJSON.set("groups", groupsJSON);
writer.write(leaderboardJSON.toJSONString().replace("\\", "")); writer.write(leaderboardJSON.toString().replace("\\", ""));
} }
public final void playersLeaderboard(HttpWriter writer) throws IOException { public final void playersLeaderboard(HttpWriter writer) throws IOException {
SortedSet<Player> allPlayers = databaseRepo.getAllPlayerForLeaderboard(); SortedSet<Player> allPlayers = databaseRepo.getAllPlayerForLeaderboard();
JSONArray playersJSON = new JSONArray(); JsonArray playersJSON = new JsonArray();
if (allPlayers != null) { if (allPlayers != null) {
for (Player player : allPlayers) { for (Player player : allPlayers) {
JSONObject playerJSON = new JSONObject(); JsonMap playerJSON = new JsonMap();
playerJSON.put("pseudo", player.getPseudo()); playerJSON.set("pseudo", player.getPseudo());
if (player.getGroups() != null) if (player.getGroups() != null)
playerJSON.put("groups", player.getJsonGroups()); playerJSON.set("groups", player.getJsonGroups());
// if (player.getAvatar() != null) // if (player.getAvatar() != null)
// playerJSON.put("avatar", Base64.getEncoder().encodeToString(player.getAvatar())); // playerJSON.put("avatar", Base64.getEncoder().encodeToString(player.getAvatar()));
playerJSON.put("rank", player.getRank()); playerJSON.set("rank", player.getRank());
playerJSON.put("score", player.getTotalScore()); playerJSON.set("score", player.getTotalScore());
playerJSON.put("completions", player.getTotalCompletion()); playerJSON.set("completions", player.getTotalCompletion());
playerJSON.put("tries", player.getTotalTries()); playerJSON.set("tries", player.getTotalTries());
playersJSON.add(playerJSON); 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.Base64;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import org.json.simple.JSONObject;
import dev.peerat.backend.bonus.extract.RouteDoc; import dev.peerat.backend.bonus.extract.RouteDoc;
import dev.peerat.backend.model.PeerAtUser; import dev.peerat.backend.model.PeerAtUser;
import dev.peerat.backend.model.Player; import dev.peerat.backend.model.Player;
@ -14,6 +12,7 @@ import dev.peerat.framework.HttpReader;
import dev.peerat.framework.HttpWriter; import dev.peerat.framework.HttpWriter;
import dev.peerat.framework.Response; import dev.peerat.framework.Response;
import dev.peerat.framework.Route; import dev.peerat.framework.Route;
import dev.peerat.framework.utils.json.JsonMap;
public class PlayerDetails implements Response { public class PlayerDetails implements Response {
@ -35,26 +34,26 @@ public class PlayerDetails implements Response {
PeerAtUser user = context.getUser(); PeerAtUser user = context.getUser();
player = databaseRepo.getPlayerDetails(user.getId()); player = databaseRepo.getPlayerDetails(user.getId());
} }
JSONObject playerJSON = new JSONObject(); JsonMap playerJSON = new JsonMap();
if (player != null) { if (player != null) {
playerJSON.put("pseudo", player.getPseudo()); playerJSON.set("pseudo", player.getPseudo());
playerJSON.put("email", player.getEmail()); playerJSON.set("email", player.getEmail());
playerJSON.put("firstname", player.getFirstname()); playerJSON.set("firstname", player.getFirstname());
playerJSON.put("lastname", player.getLastname()); playerJSON.set("lastname", player.getLastname());
playerJSON.put("description", player.getDescription()); playerJSON.set("description", player.getDescription());
if (player.getGroups() != null) if (player.getGroups() != null)
playerJSON.put("groups", player.getJsonGroups()); playerJSON.set("groups", player.getJsonGroups());
playerJSON.put("rank", player.getRank()); playerJSON.set("rank", player.getRank());
playerJSON.put("score", player.getTotalScore()); playerJSON.set("score", player.getTotalScore());
playerJSON.put("completions", player.getTotalCompletion()); playerJSON.set("completions", player.getTotalCompletion());
playerJSON.put("completionsList", player.getJsonCompletions()); playerJSON.set("completionsList", player.getJsonCompletions());
playerJSON.put("tries", player.getTotalTries()); playerJSON.set("tries", player.getTotalTries());
if (player.getBadges() != null) if (player.getBadges() != null)
playerJSON.put("badges", player.getJsonBadges()); playerJSON.set("badges", player.getJsonBadges());
if (player.getAvatar() != null) if (player.getAvatar() != null)
playerJSON.put("avatar", Base64.getEncoder().encodeToString(player.getAvatar())); playerJSON.set("avatar", Base64.getEncoder().encodeToString(player.getAvatar()));
context.response(200); context.response(200);
writer.write(playerJSON.toJSONString().replace("\\", "")); writer.write(playerJSON.toString().replace("\\", ""));
} else { } else {
context.response(400); context.response(400);
} }

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -9,10 +9,9 @@ import java.util.List;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.json.simple.JSONObject;
import dev.peerat.framework.HttpReader; import dev.peerat.framework.HttpReader;
import dev.peerat.framework.HttpWriter; import dev.peerat.framework.HttpWriter;
import dev.peerat.framework.utils.json.JsonMap;
public class WebClient { public class WebClient {
@ -49,10 +48,10 @@ public class WebClient {
} }
public void auth(String user, String password) throws Exception{ public void auth(String user, String password) throws Exception{
JSONObject login = new JSONObject(); JsonMap login = new JsonMap();
login.put("pseudo", user); login.set("pseudo", user);
login.put("passwd", password); login.set("passwd", password);
route("/login", "POST", login.toJSONString()); route("/login", "POST", login.toString());
System.out.println("["+host+"] /login "+login); System.out.println("["+host+"] /login "+login);
for(String line : this.headers){ 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{ public void register(String user, String password, String email, String firstname, String lastname, String description) throws Exception{
JSONObject register = new JSONObject(); JsonMap register = new JsonMap();
register.put("pseudo", user); register.set("pseudo", user);
register.put("passwd", password); register.set("passwd", password);
register.put("email", email); register.set("email", email);
register.put("firstname", firstname); register.set("firstname", firstname);
register.put("lastname", lastname); register.set("lastname", lastname);
register.put("description", description); register.set("description", description);
register.put("sgroup", ""); register.set("sgroup", "");
register.put("avatar", ""); register.set("avatar", "");
route("/register", "POST", register.toJSONString()); route("/register", "POST", register.toString());
System.out.println("["+host+"] /register "+register); System.out.println("["+host+"] /register "+register);
for(String line : this.headers){ for(String line : this.headers){
Matcher matcher = AUTORIZATION.matcher(line); 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 static org.junit.jupiter.api.Assertions.fail;
import org.json.simple.JSONObject;
import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test; 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.Main;
import dev.peerat.backend.WebClient; import dev.peerat.backend.WebClient;
import dev.peerat.framework.utils.json.JsonMap;
@TestInstance(Lifecycle.PER_CLASS) @TestInstance(Lifecycle.PER_CLASS)
public class ScoreTests { public class ScoreTests {
@ -42,14 +42,14 @@ public class ScoreTests {
@Test @Test
void testOnDeployed(){ void testOnDeployed(){
try{ try{
JSONObject group = new JSONObject(); JsonMap group = new JsonMap();
group.put("name", "GroupTest"); group.set("name", "GroupTest");
group.put("chapter", 2); group.set("chapter", 2);
client.register("Test1", "Test2", "Test3@Test7.be", "Test4", "Test5", "Test6"); client.register("Test1", "Test2", "Test3@Test7.be", "Test4", "Test5", "Test6");
client.assertResponseCode(200); client.assertResponseCode(200);
client.route("/groupCreate", "POST", group.toJSONString()); client.route("/groupCreate", "POST", group.toString());
client.assertResponseCode(200); client.assertResponseCode(200);
client.sendHeaders.add("content-type: ah;boundary=----WebKitFormBoundaryNUjiLBAMuX2dhxU7"); client.sendHeaders.add("content-type: ah;boundary=----WebKitFormBoundaryNUjiLBAMuX2dhxU7");
@ -60,7 +60,7 @@ public class ScoreTests {
client.disconnect(); client.disconnect();
client.auth("JeffCheasey88", "TheoPueDesPieds"); client.auth("JeffCheasey88", "TheoPueDesPieds");
client.route("/groupJoin", "POST", group.toJSONString()); client.route("/groupJoin", "POST", group.toString());
client.assertResponseCode(200); client.assertResponseCode(200);
client.sendHeaders.add("content-type: ah;boundary=----WebKitFormBoundaryNUjiLBAMuX2dhxU7"); 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 static org.junit.jupiter.api.Assertions.fail;
import org.json.simple.JSONObject;
import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test; 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.Main;
import dev.peerat.backend.WebClient; import dev.peerat.backend.WebClient;
import dev.peerat.framework.utils.json.JsonMap;
@TestInstance(Lifecycle.PER_CLASS) @TestInstance(Lifecycle.PER_CLASS)
public class TmpRoutesTests { public class TmpRoutesTests {
@ -42,12 +42,12 @@ public class TmpRoutesTests {
@Test @Test
void testOnDeployed(){ void testOnDeployed(){
try { try {
JSONObject group = new JSONObject(); JsonMap group = new JsonMap();
group.put("name", "MyTest"); group.set("name", "MyTest");
group.put("chapter", 1); group.set("chapter", 1);
client.auth("JeffCheasey88", "TheoPueDesPieds"); client.auth("JeffCheasey88", "TheoPueDesPieds");
client.route("/groupCreate", "POST", group.toJSONString()); client.route("/groupCreate", "POST", group.toString());
client.assertResponseCode(200); client.assertResponseCode(200);
}catch(Exception e){ }catch(Exception e){

View file

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