first commit

This commit is contained in:
jeffcheasey88 2023-10-01 11:45:56 +02:00
commit ddddcadb24
13 changed files with 418 additions and 0 deletions

7
.classpath Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="lib" path="PeerAtCodeFramework.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>

BIN
PeerAtCodeFramework.jar Normal file

Binary file not shown.

View File

@ -0,0 +1,51 @@
package be.jeffcheasey88.todo;
import static dev.peerat.framework.RequestType.OPTIONS;
import java.util.regex.Matcher;
import be.jeffcheasey88.todo.routes.users.Login;
import dev.peerat.framework.Context;
import dev.peerat.framework.HttpReader;
import dev.peerat.framework.HttpWriter;
import dev.peerat.framework.RequestType;
import dev.peerat.framework.Response;
import dev.peerat.framework.Route;
import dev.peerat.framework.Router;
public class Main {
public static void main(String[] args) throws Exception {
Router<User> router = new Router<User>().configureJwt(
(builder) -> builder.setExpectedIssuer("http://localhost"),
(claims) -> {
claims.setIssuer("http://localhost"); // who creates the token and signs it
claims.setExpirationTimeMinutesInTheFuture(100);
},
(claims) -> new User(claims))
.addDefaultHeaders(RequestType.GET, "Access-Control-Allow-Origin: *")
.addDefaultHeaders(RequestType.POST, "Access-Control-Allow-Origin: *");
router.setDefault((matcher, context, reader, writer) -> {
context.response(404);
writer.write("404 not Found.\n");
});
router.register(new Response(){
@Route(path = "^(.*)$", type = OPTIONS)
@Override
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception {
context.response(200,
"Access-Control-Allow-Origin: *",
"Access-Control-Allow-Methods: *",
"Access-Control-Allow-Headers: *"
);
}
});
router.register(new Login(router));
router.listen(80, false);
}
}

View File

@ -0,0 +1,26 @@
package be.jeffcheasey88.todo;
import org.jose4j.jwt.JwtClaims;
public class User extends dev.peerat.framework.User{
private String name;
public User(String name){
this.name = name;
}
public User(JwtClaims claims){
this.name = claims.getClaimValueAsString("name");
}
public String getName(){
return this.name;
}
@Override
public void write(JwtClaims claims){
claims.setClaim("name", this.name);
}
}

View File

@ -0,0 +1,5 @@
package be.jeffcheasey88.todo.model;
public class JsonRepository implements Repository{
}

View File

@ -0,0 +1,9 @@
package be.jeffcheasey88.todo.model;
import dev.peerat.framework.utils.json.JsonMap;
public interface JsonSerializable {
void serialize(JsonMap json);
}

View File

@ -0,0 +1,83 @@
package be.jeffcheasey88.todo.model;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import dev.peerat.framework.utils.json.JsonArray;
import dev.peerat.framework.utils.json.JsonMap;
public class Project implements JsonSerializable{
private String name;
private List<String> users;
private List<Task> tasks;
private Map<String, String> states;
public Project(){
this.users = new ArrayList<>();
this.tasks = new ArrayList<>();
this.states = new LinkedHashMap<>();
}
public Project(JsonMap json){
this();
this.name = json.get("name");
for(Object user : json.<JsonArray>get("users").toList()){
this.users.add((String) user);
}
for(Object task : json.<JsonArray>get("tasks").toList()){
this.tasks.add(new Task((JsonMap) task));
}
for(Entry<String, Object> entries : json.<JsonMap>get("states").entries()){
states.put(entries.getKey(), (String) entries.getValue());
}
}
@Override
public void serialize(JsonMap json){
json.set("name", this.name);
JsonArray jUsers = new JsonArray();
for(String user : users) jUsers.add(user);
json.set("users", jUsers);
JsonArray jTasks = new JsonArray();
for(Task task : tasks){
JsonMap jTask = new JsonMap();
task.serialize(jTask);
jTasks.add(jTask);
}
json.set("tasks", jTasks);
JsonMap jstates = new JsonMap();
for(Entry<String, String> entries : states.entrySet()){
jstates.set(entries.getKey(), entries.getValue());
}
json.set("states", jstates);
}
public String getName(){
return this.name;
}
public void setName(String name){
this.name = name;
}
public List<String> getUsers(){
return this.users;
}
public List<Task> getTasks(){
return this.tasks;
}
public Map<String, String> getStates(){
return this.states;
}
}

View File

@ -0,0 +1,25 @@
package be.jeffcheasey88.todo.model;
import java.util.List;
import be.jeffcheasey88.todo.User;
public interface Repository {
Result<String> login(String username, String password);
Result<List<Project>> getProjects(User user);
Result<Project> createProject(User user, String name);
Result<Project> removeProject(User user, String name);
Result<Project> getProject(User user, String name);
Result<Boolean> renameProject(User user, String lastname, String updatedname);
Result<Boolean> manageUsers(User user, String project, List<String> users);
Result<List<String>> getUsers();
}

View File

@ -0,0 +1,36 @@
package be.jeffcheasey88.todo.model;
import dev.peerat.framework.utils.json.JsonMap;
public class Result<E>{
private E element;
private int errorCode;
private JsonMap error;
public Result(E element){
this.element = element;
}
public Result(int errorCode, JsonMap error){
this.errorCode = errorCode;
this.error = error;
}
public boolean success(){
return error == null;
}
public E getElement(){
return this.element;
}
public int getErrorCode(){
return this.errorCode;
}
public JsonMap getError(){
return this.error;
}
}

View File

@ -0,0 +1,55 @@
package be.jeffcheasey88.todo.model;
import java.util.ArrayList;
import java.util.List;
import dev.peerat.framework.utils.json.JsonArray;
import dev.peerat.framework.utils.json.JsonMap;
public class Task implements JsonSerializable{
private List<String> users;
private String name;
private TaskModifier modifier;
public Task(){
this.users = new ArrayList<>();
this.modifier = new TaskModifier();
}
public Task(JsonMap json){
this();
for(Object user : json.<JsonArray>get("users").toList()){
this.users.add((String) user);
}
this.name = json.get("name");
this.modifier = new TaskModifier(json.get("modifier"));
}
@Override
public void serialize(JsonMap json){
JsonArray jusers = new JsonArray();
for(String user : users) jusers.add(user);
json.set("users", jusers);
json.set("name", this.name);
JsonMap jmodifier = new JsonMap();
this.modifier.serialize(jmodifier);
json.set("modifier", jmodifier);
}
public List<String> getUsers(){
return this.users;
}
public String getName(){
return this.name;
}
public void setName(String name){
this.name = name;
}
public TaskModifier getModifier(){
return this.modifier;
}
}

View File

@ -0,0 +1,38 @@
package be.jeffcheasey88.todo.model;
import java.util.ArrayList;
import java.util.List;
import dev.peerat.framework.utils.json.JsonArray;
import dev.peerat.framework.utils.json.JsonMap;
public class TaskContainer extends Task implements JsonSerializable{
private List<Task> tasks;
public TaskContainer(){
this.tasks = new ArrayList<>();
}
public TaskContainer(JsonMap json){
this();
for(Object task : json.<JsonArray>get("tasks").toList()){
tasks.add((Task) task);
}
}
@Override
public void serialize(JsonMap json){
JsonArray jTasks = new JsonArray();
for(Task task : tasks){
JsonMap jtask = new JsonMap();
task.serialize(jtask);
jTasks.add(jtask);
}
json.set("tasks", jTasks);
}
public List<Task> getTasks(){
return this.tasks;
}
}

View File

@ -0,0 +1,49 @@
package be.jeffcheasey88.todo.model;
import dev.peerat.framework.utils.json.JsonMap;
public class TaskModifier implements JsonSerializable{
private String state;
private String title;
private String description;
public TaskModifier(){}
public TaskModifier(JsonMap json){
this();
this.state = json.get("state");
this.title = json.get("title");
this.description = json.get("description");
}
@Override
public void serialize(JsonMap json){
json.set("state", state);
json.set("title", title);
json.set("description", description);
}
public void setState(String state){
this.state = state;
}
public String getTitle(){
return this.title;
}
public void setTitle(String title){
this.title = title;
}
public String getDescription(){
return this.description;
}
public void setDescription(String description){
this.description = description;
}
}

View File

@ -0,0 +1,34 @@
package be.jeffcheasey88.todo.routes.users;
import java.util.regex.Matcher;
import be.jeffcheasey88.todo.User;
import dev.peerat.framework.Context;
import dev.peerat.framework.HttpReader;
import dev.peerat.framework.HttpWriter;
import dev.peerat.framework.RequestType;
import dev.peerat.framework.Response;
import dev.peerat.framework.Route;
import dev.peerat.framework.Router;
public class Login implements Response{
private Router<User> router;
public Login(Router<User> router){
this.router = router;
}
@Route(path = "^/login$", type = RequestType.POST)
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception {
if (context.isLogged()){
context.response(403);
return;
}
context.response(200,
"Access-Control-Allow-Origin: *",
"Access-Control-Expose-Headers: Authorization",
"Authorization: Bearer " + this.router.createAuthUser(new User("")));
}
}