rename eventlistener -> improve requesthandler

This commit is contained in:
Daniella / Tove 2022-07-06 15:57:55 +02:00
parent 137c20f270
commit f482f8ae9f
2 changed files with 8 additions and 123 deletions

View file

@ -1,120 +0,0 @@
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 EventListener {
private Object catcher;
public EventListener(Object 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) {
e.printStackTrace();
}
}
}
}
}
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;
}
}

View file

@ -10,18 +10,18 @@ import de.tudbut.async.Callback;
import de.tudbut.async.ComposeCallback;
import de.tudbut.async.TaskCallable;
import de.tudbut.tryumph.config.IRequestCatcher;
import de.tudbut.tryumph.events.EventListener;
import de.tudbut.tryumph.events.GET;
import de.tudbut.tryumph.events.POST;
import de.tudbut.tryumph.events.Path;
import de.tudbut.tryumph.events.RequestHandler;
import de.tudbut.tryumph.server.HTMLParsing;
import de.tudbut.tryumph.server.Request;
import de.tudbut.tryumph.server.Response;
import tudbut.parsing.TCN;
public class Main implements IRequestCatcher {
public class Main implements IRequestCatcher, RequestHandler.Listener {
EventListener listener = new EventListener(this);
RequestHandler listener = new RequestHandler(this);
@Override
public TaskCallable<ComposeCallback<Request, Response>> onConnect(Socket socket) {
@ -79,4 +79,9 @@ public class Main implements IRequestCatcher {
res.call(r);
}
@Override
public void handleError(Throwable error, Callback<Response> res, Callback<Throwable> rej) {
rej.call(error);
}
}