refractor package for maven repository & add Context & generic getting user & take response code & add default headers

This commit is contained in:
jeffcheasey88 2023-08-29 16:11:37 +02:00
parent 28e9da1536
commit f735a7d6bf
13 changed files with 78 additions and 29 deletions

View file

@ -1,9 +0,0 @@
package be.jeffcheasey88.peeratcode.framework;
import java.util.regex.Matcher;
public interface Response{
void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception;
}

View file

@ -1,4 +1,4 @@
package be.jeffcheasey88.peeratcode.framework;
package dev.peerat.framework;
import java.net.Socket;
import java.util.Arrays;

View file

@ -0,0 +1,35 @@
package dev.peerat.framework;
public class Context{
private User user;
private int responseCode;
private HttpWriter writer;
private String[] headers;
public Context(User user, HttpWriter writer, String... headers){
this.user = user;
this.writer = writer;
this.headers = headers;
}
public <U extends User> U getUser(){
return (U) user;
}
public void response(int code, String... headers) throws Exception{
if(headers != null && headers.length > 0 && this.headers.length > 0){
String[] copy = new String[this.headers.length+headers.length];
System.arraycopy(this.headers, 0, copy, 0, this.headers.length);
System.arraycopy(headers, 0, copy, this.headers.length, headers.length);
this.headers = copy;
}
this.responseCode = code;
this.writer.response(code, this.headers);
}
public int getResponseCode(){
return this.responseCode;
}
}

View file

@ -1,4 +1,4 @@
package be.jeffcheasey88.peeratcode.framework;
package dev.peerat.framework;
import java.io.BufferedReader;
import java.io.IOException;

View file

@ -1,4 +1,4 @@
package be.jeffcheasey88.peeratcode.framework;
package dev.peerat.framework;
import java.io.BufferedWriter;
import java.io.IOException;
@ -39,13 +39,11 @@ public class HttpWriter{
this.writer.close();
}
public void response(int code, String... headers) throws Exception{
void response(int code, String... headers) throws Exception{
write("HTTP/1.1 "+code+codeMessage(code)+"\n");
for(String header : headers) write(header+"\n");
write("\n");
flush();
StackTraceElement[] e = Thread.currentThread().getStackTrace();
System.out.println(e[2]+" -> response "+code);
}
private static String[] HEIGHTBITS = new String[9];

View file

@ -1,4 +1,4 @@
package be.jeffcheasey88.peeratcode.framework;
package dev.peerat.framework;
import java.util.HashMap;
import java.util.Map;

View file

@ -1,4 +1,4 @@
package be.jeffcheasey88.peeratcode.framework;
package dev.peerat.framework;
public enum RequestType{

View file

@ -0,0 +1,9 @@
package dev.peerat.framework;
import java.util.regex.Matcher;
public interface Response{
void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception;
}

View file

@ -1,4 +1,4 @@
package be.jeffcheasey88.peeratcode.framework;
package dev.peerat.framework;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;

View file

@ -1,4 +1,4 @@
package be.jeffcheasey88.peeratcode.framework;
package dev.peerat.framework;
import java.lang.reflect.Method;
import java.net.ServerSocket;
@ -27,19 +27,26 @@ import org.jose4j.lang.JoseException;
public class Router<U extends User>{
private Map<RequestType, Map<Response, Route>> responses;
public static void main(String[] args) {
}
private Map<Response, Route>[] responses;
private Map<Response, Pattern> patterns;
private Response noFileFound;
private RsaJsonWebKey rsaJsonWebKey;
private JwtConsumer jwtConsumer;
private Consumer<JwtClaims> claims;
private Function<JwtClaims, U> userCreator;
private String[][] headers;
public Router() throws Exception{
this.responses = new HashMap<>();
for(RequestType type : RequestType.values()) this.responses.put(type, new HashMap<>());
int types = RequestType.values().length;
this.responses = new HashMap[types];
for(RequestType type : RequestType.values()) this.responses[type.ordinal()] = new HashMap<>();
this.patterns = new HashMap<>();
this.rsaJsonWebKey = RsaJwkGenerator.generateJwk(2048);
this.headers = new String[types][0];
}
public Router<U> configureJwt(Consumer<JwtConsumerBuilder> consumer, Consumer<JwtClaims> claims, Function<JwtClaims, U> userCreator){
@ -57,6 +64,14 @@ public class Router<U extends User>{
return this;
}
public void addDefaultHeaders(RequestType type, String... headers){
String[] origin = this.headers[type.ordinal()];
String[] copy = new String[origin.length+headers.length];
System.arraycopy(origin, 0, copy, 0, origin.length);
System.arraycopy(headers, 0, copy, origin.length, headers.length);
this.headers[type.ordinal()] = copy;
}
public void listen(int port, boolean ssl) throws Exception{
if (ssl) { // Not needed with the use of a proxy
SSLServerSocket server = null;
@ -95,7 +110,7 @@ public class Router<U extends User>{
Response.class.getDeclaredMethods()[0].getParameterTypes());
Route route = method.getAnnotation(Route.class);
this.responses.get(route.type()).put(response, route);
this.responses[route.type().ordinal()].put(response, route);
this.patterns.put(response, Pattern.compile(route.path()));
}catch(Exception e){
throw new IllegalArgumentException(e);
@ -110,7 +125,8 @@ public class Router<U extends User>{
void exec(RequestType type, String path, User user, HttpReader reader, HttpWriter writer) throws Exception{
if(type == null) return;
for(Entry<Response, Route> routes : this.responses.get(type).entrySet()){
Context context = new Context(user, writer, this.headers[type.ordinal()]);
for(Entry<Response, Route> routes : this.responses[type.ordinal()].entrySet()){
Matcher matcher = this.patterns.get(routes.getKey()).matcher(path);
if(matcher.matches()){
if(user == null && routes.getValue().needLogin()){
@ -122,11 +138,11 @@ public class Router<U extends User>{
reader = new WebSocketReader(reader);
writer = new WebSocketWriter(writer);
}
routes.getKey().exec(matcher, user, reader, writer);
routes.getKey().exec(matcher, context, reader, writer);
return;
}
}
if(noFileFound != null) noFileFound.exec(null, user, reader, writer);
if(noFileFound != null) noFileFound.exec(null, context, reader, writer);
}
public Router<U> configureSSL(String keyStore, String keyStorePassword){

View file

@ -1,4 +1,4 @@
package be.jeffcheasey88.peeratcode.framework;
package dev.peerat.framework;
import org.jose4j.jwt.JwtClaims;

View file

@ -1,4 +1,4 @@
package be.jeffcheasey88.peeratcode.framework;
package dev.peerat.framework;
import java.io.IOException;

View file

@ -1,4 +1,4 @@
package be.jeffcheasey88.peeratcode.framework;
package dev.peerat.framework;
import java.io.IOException;