ResponseMapper (base)

This commit is contained in:
jeffcheasey88 2025-05-02 17:02:36 +02:00
parent bbb842887b
commit dfeb611532
3 changed files with 24 additions and 2 deletions

View file

@ -0,0 +1,7 @@
package dev.peerat.framework;
public interface ResponseMapper{
void map(Context context, HttpReader reader, HttpWriter writer, Object result) throws Exception;
}

View file

@ -97,7 +97,7 @@ public class RouteMapper<U extends User>{
Object[] inv = target.getBinder().bindMethod(target.getMethod(), matcher, context, reader, writer, params); Object[] inv = target.getBinder().bindMethod(target.getMethod(), matcher, context, reader, writer, params);
Object result = target.getMethod().invoke(target.getInstance(), inv); Object result = target.getMethod().invoke(target.getInstance(), inv);
if(result == null) return true; if(result == null) return true;
//TODO MAP OBJECT TO RESPONSE router.getMapper().map(context, reader, writer, result);
return true; return true;
} }
return false; return false;

View file

@ -24,6 +24,7 @@ public class Router<U extends User>{
private Response noFileFound; private Response noFileFound;
private Authenticator<U> auth; private Authenticator<U> auth;
private String[][] headers; private String[][] headers;
private ResponseMapper responseMapper;
private ServerSocket serverSocket; private ServerSocket serverSocket;
public Router(){ public Router(){
@ -50,6 +51,11 @@ public class Router<U extends User>{
return this; return this;
} }
public Router<U> setMapper(ResponseMapper mapper){
this.responseMapper = mapper;
return this;
}
public Router<U> register(Response response){ public Router<U> register(Response response){
try{ try{
Method method = response.getClass().getDeclaredMethod("exec", Method method = response.getClass().getDeclaredMethod("exec",
@ -158,6 +164,10 @@ public class Router<U extends User>{
return this.headers[type.ordinal()]; return this.headers[type.ordinal()];
} }
public ResponseMapper getMapper(){
return this.responseMapper;
}
public Authenticator<U> getAuthenticator(){ public Authenticator<U> getAuthenticator(){
return this.auth; return this.auth;
} }
@ -193,8 +203,13 @@ public class Router<U extends User>{
return this.exceptions; return this.exceptions;
} }
public void listen(int port, boolean ssl) throws Exception{ private void checkStart(){
bindAll(); bindAll();
if(this.responseMapper == null) this.responseMapper = (c,r,w,o) -> {};
}
public void listen(int port, boolean ssl) throws Exception{
checkStart();
if (ssl) { // Not needed with the use of a proxy if (ssl) { // Not needed with the use of a proxy
try { try {
SSLServerSocketFactory ssf = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault(); SSLServerSocketFactory ssf = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();