From 982611299e66933b9f4dd1c93002b87cef0707f4 Mon Sep 17 00:00:00 2001
From: ulrich
Date: Mon, 11 Nov 2024 13:08:34 +0000
Subject: [PATCH] Pruefung auf HTTP-Methode in Handler aufgenommen
---
src/de/uhilger/neon/Handler.java | 34 +++++++++++++++++++++++++++-------
1 files changed, 27 insertions(+), 7 deletions(-)
diff --git a/src/de/uhilger/neon/Handler.java b/src/de/uhilger/neon/Handler.java
index 6caa331..14d34d3 100644
--- a/src/de/uhilger/neon/Handler.java
+++ b/src/de/uhilger/neon/Handler.java
@@ -128,8 +128,14 @@
*/
@Override
public void handle(HttpExchange exchange) throws IOException {
- HttpHelper hh = new HttpHelper();
- String route = hh.getRouteString(exchange);
+ String route = exchange
+ .getRequestURI()
+ .getPath()
+ .substring(exchange
+ .getHttpContext()
+ .getPath()
+ .length());
+
Type requestMethod = Type.valueOf(exchange.getRequestMethod());
/*
Es wird erst geprueft, ob zu einer bestimmten Route
@@ -148,25 +154,25 @@
o = ((Map) md).get(routeRest);
if (o instanceof ActionDescriptor) {
found = true;
- handleRequest(exchange, o, routeRest, route.substring(routeRest.length()));
+ handleRequest(exchange, o, routeRest, route.substring(routeRest.length()), requestMethod);
}
pos = routeRest.lastIndexOf("/");
}
} else {
found = true;
- handleRequest(exchange, o, route, route);
+ handleRequest(exchange, o, route, route, requestMethod);
}
if (!found) {
o = dispatcher.get(requestMethod).get("/");
if (o instanceof ActionDescriptor) {
- handleRequest(exchange, o, route, route);
+ handleRequest(exchange, o, route, route, requestMethod);
}
}
}
}
- private void handleRequest(HttpExchange exchange, Object o, String route, String subroute) throws IOException {
+ private void handleRequest(HttpExchange exchange, Object o, String route, String subroute, Type requestMethod) throws IOException {
ActionDescriptor ad = (ActionDescriptor) o;
String actorClassName = ad.className;
try {
@@ -175,7 +181,7 @@
for (Method method : methods) {
Action action = method.getAnnotation(Action.class);
if (action != null) {
- if (action.route().equals("/") || action.route().startsWith(route)) {
+ if ((action.route().equals("/") || action.route().startsWith(route)) && action.type().equals(requestMethod)) {
Object[] actionArgs = getActionArgs(exchange, method, ad, subroute);
Object actorObj = actorClass.getDeclaredConstructor().newInstance();
addDataProvider(exchange, actorObj);
@@ -199,11 +205,25 @@
Parameter[] methodParams = method.getParameters();
Object[] actionArgs = new Object[count];
String[] routeParams = subroute.split("/");
+
+
+ /*
+ Fall 1: Es sind mehr als ein Parameter zu uebergeben und die Route enthaelt
+ weniger Parameter als die Methode erfordert.
+ Fall 2: Die Methode erwartet Parameter und der erste Parameter ist nicht
+ vom Typ HttpExchange.
+
+ Wenn einer dieser beiden Faelle eintritt, wird alles als Parameter an die Methode
+ uebergeben, was eventuell als Teil einer Query im URL oder im Body enthalten ist.
+ Fuer Mthoden, die nicht vom Typ HTTP GET sind, kann ein Actor kann dann den Body
+ nicht mehr lesen, weil das bereits an dieser Stelle gemacht wurde.
+ */
Map queryParams = new HashMap();
if ((count > 1 && count > routeParams.length)
|| (methodParams.length > 0 && !methodParams[0].getType().equals(HttpExchange.class))) {
queryParams = new HttpHelper().getQueryMap(exchange);
}
+
int k = 0;
for (Parameter methodParam : methodParams) {
if (methodParam.getType().equals(HttpExchange.class)) {
--
Gitblit v1.9.3