improve event listener
This commit is contained in:
parent
839d0f030f
commit
37f08b5178
2 changed files with 101 additions and 22 deletions
|
@ -2,10 +2,12 @@ package de.tudbut.tryumph.events;
|
||||||
|
|
||||||
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
|
import java.lang.reflect.Parameter;
|
||||||
|
|
||||||
import de.tudbut.async.Callback;
|
import de.tudbut.async.Callback;
|
||||||
import de.tudbut.tryumph.server.Request;
|
import de.tudbut.tryumph.server.Request;
|
||||||
import de.tudbut.tryumph.server.Response;
|
import de.tudbut.tryumph.server.Response;
|
||||||
|
import tudbut.parsing.TCN;
|
||||||
|
|
||||||
public class EventListener {
|
public class EventListener {
|
||||||
|
|
||||||
|
@ -21,34 +23,96 @@ public class EventListener {
|
||||||
Method method = methods[i];
|
Method method = methods[i];
|
||||||
if(method.getDeclaredAnnotations().length == 0)
|
if(method.getDeclaredAnnotations().length == 0)
|
||||||
continue;
|
continue;
|
||||||
if(method.getReturnType() == void.class) {
|
boolean usable = true;
|
||||||
boolean usable = true;
|
if(method.getDeclaredAnnotation(GET.class) != null && !request.method.equals("GET")) {
|
||||||
if(method.getDeclaredAnnotation(GET.class) != null && !request.method.equals("GET")) {
|
usable = false;
|
||||||
usable = false;
|
}
|
||||||
}
|
if(method.getDeclaredAnnotation(POST.class) != null && !request.method.equals("POST")) {
|
||||||
if(method.getDeclaredAnnotation(POST.class) != null && !request.method.equals("POST")) {
|
usable = false;
|
||||||
usable = false;
|
}
|
||||||
}
|
Path pathA = method.getDeclaredAnnotation(Path.class);
|
||||||
Path pathA = method.getDeclaredAnnotation(Path.class);
|
if(pathA != null && !request.realPath.matches("^" + pathA.value() + "$")) {
|
||||||
if(pathA != null && !request.realPath.matches("^" + pathA.value() + "$")) {
|
usable = false;
|
||||||
usable = false;
|
}
|
||||||
}
|
RequestMethod methodA = method.getDeclaredAnnotation(RequestMethod.class);
|
||||||
RequestMethod methodA = method.getDeclaredAnnotation(RequestMethod.class);
|
if(methodA != null && !request.method.matches("^" + methodA.value() + "$")) {
|
||||||
if(methodA != null && !request.method.matches("^" + methodA.value() + "$")) {
|
usable = false;
|
||||||
usable = false;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if(usable) {
|
if(usable) {
|
||||||
|
if(method.getReturnType() == void.class) {
|
||||||
try {
|
try {
|
||||||
method.invoke(catcher, request, res, rej);
|
method.invoke(catcher, request, res, rej);
|
||||||
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
|
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
else if(method.getReturnType() == Response.class) {
|
||||||
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 {
|
||||||
|
res.call((Response) method.invoke(catcher, args));
|
||||||
|
} 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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package de.tudbut.tryumph.server.http;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
|
import java.net.SocketException;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
@ -25,7 +26,14 @@ public class HTTPRequestReader {
|
||||||
}
|
}
|
||||||
|
|
||||||
private int read() throws IOException {
|
private int read() throws IOException {
|
||||||
int i = stream.read();
|
int i;
|
||||||
|
try {
|
||||||
|
i = stream.read();
|
||||||
|
} catch (SocketException e) {
|
||||||
|
if(e.getMessage().equals("Connection reset"))
|
||||||
|
throw new Stop();
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
if(i == -1) {
|
if(i == -1) {
|
||||||
throw new Stop();
|
throw new Stop();
|
||||||
}
|
}
|
||||||
|
@ -33,7 +41,14 @@ public class HTTPRequestReader {
|
||||||
}
|
}
|
||||||
|
|
||||||
private int read(byte[] bytes) throws IOException {
|
private int read(byte[] bytes) throws IOException {
|
||||||
int i = stream.read(bytes);
|
int i;
|
||||||
|
try {
|
||||||
|
i = stream.read(bytes);
|
||||||
|
} catch (SocketException e) {
|
||||||
|
if(e.getMessage().equals("Connection reset"))
|
||||||
|
throw new Stop();
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
if(i != bytes.length) {
|
if(i != bytes.length) {
|
||||||
throw new Stop();
|
throw new Stop();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue