From 5b28627f32641bbb0f59ca99a993459ec4dc2a9a Mon Sep 17 00:00:00 2001 From: TudbuT Date: Wed, 6 Jul 2022 15:58:12 +0200 Subject: [PATCH] rename eventlistener -> improve requesthandler --- .../tudbut/tryumph/events/RequestHandler.java | 124 ++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 src/de/tudbut/tryumph/events/RequestHandler.java diff --git a/src/de/tudbut/tryumph/events/RequestHandler.java b/src/de/tudbut/tryumph/events/RequestHandler.java new file mode 100644 index 0000000..2208aca --- /dev/null +++ b/src/de/tudbut/tryumph/events/RequestHandler.java @@ -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 res, Callback rej); + } + + private Listener catcher; + + public RequestHandler(Listener catcher) { + this.catcher = catcher; + } + + public void handle(Request request, Callback res, Callback 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; + } +}