rename eventlistener -> improve requesthandler

This commit is contained in:
Daniella / Tove 2022-07-06 15:58:12 +02:00
parent f482f8ae9f
commit 5b28627f32

View file

@ -0,0 +1,124 @@
package de.tudbut.tryumph.events;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import de.tudbut.async.Callback;
import de.tudbut.tryumph.server.Request;
import de.tudbut.tryumph.server.Response;
import tudbut.parsing.TCN;
public class RequestHandler {
public interface Listener {
void handleError(Throwable error, Callback<Response> res, Callback<Throwable> rej);
}
private Listener catcher;
public RequestHandler(Listener catcher) {
this.catcher = catcher;
}
public void handle(Request request, Callback<Response> res, Callback<Throwable> rej) {
Method[] methods = catcher.getClass().getDeclaredMethods();
for(int i = 0; i < methods.length; i++) {
Method method = methods[i];
if(method.getDeclaredAnnotations().length == 0)
continue;
boolean usable = true;
if(method.getDeclaredAnnotation(GET.class) != null && !request.method.equals("GET")) {
usable = false;
}
if(method.getDeclaredAnnotation(POST.class) != null && !request.method.equals("POST")) {
usable = false;
}
Path pathA = method.getDeclaredAnnotation(Path.class);
if(pathA != null && !request.realPath.matches("^" + pathA.value() + "$")) {
usable = false;
}
RequestMethod methodA = method.getDeclaredAnnotation(RequestMethod.class);
if(methodA != null && !request.method.matches("^" + methodA.value() + "$")) {
usable = false;
}
if(usable) {
if(method.getReturnType() == void.class) {
try {
method.invoke(catcher, request, res, rej);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
e.printStackTrace();
}
}
else if(method.getReturnType() == Response.class) {
Object[] args = new Object[method.getParameterCount()];
Parameter[] params = method.getParameters();
TCN q = request.query();
TCN b = null;
for(int j = 0; j < params.length; j++) {
Parameter param = params[j];
try {
if(param.getType() == Request.class) {
args[j] = request;
}
PPathFragment pathFragment = param.getDeclaredAnnotation(PPathFragment.class);
if(pathFragment != null) {
args[j] = request.splitPath[pathFragment.value()];
}
PQuery query = param.getDeclaredAnnotation(PQuery.class);
if(query != null) {
args[j] = fromTCN(q, query.value(), param);
}
PBody body = param.getDeclaredAnnotation(PBody.class);
if(body != null) {
try {
if(b == null)
b = request.bodyURLEncoded();
args[j] = fromTCN(b, body.value(), param);
} catch (Exception e) {
args[j] = null;
}
}
PCookie cookie = param.getDeclaredAnnotation(PCookie.class);
if(cookie != null) {
args[j] = request.cookies.get(cookie.value());
}
PData data = param.getDeclaredAnnotation(PData.class);
if(data != null) {
args[j] = fromTCN(request.context.data, data.value(), param);
}
PHeader header = param.getDeclaredAnnotation(PHeader.class);
if(header != null) {
args[j] = request.headers.get(header.value());
}
} catch(Exception e) {
e.printStackTrace();
}
}
try {
Response resp = (Response) method.invoke(catcher, args);
if(resp != null)
res.call(resp);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
catcher.handleError(e, res, rej);
}
}
}
}
}
private Object fromTCN(TCN tcn, String s, Parameter param) {
Method[] tcnMethods = TCN.class.getDeclaredMethods();
for(int m = 0; m < tcnMethods.length; m++) {
Method tcnMethod = tcnMethods[m];
if(tcnMethod.getParameterCount() == 1 && tcnMethod.getReturnType() == param.getType())
try {
return tcnMethod.invoke(tcn, s);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
e.printStackTrace();
}
}
return null;
}
}