| | |
| | | import de.uhilger.neon.Action.Type; |
| | | import de.uhilger.neon.entity.ActionDescriptor; |
| | | import java.io.IOException; |
| | | import java.lang.reflect.Constructor; |
| | | import java.lang.reflect.InvocationTargetException; |
| | | import java.lang.reflect.Method; |
| | | import java.lang.reflect.Parameter; |
| | |
| | | import java.util.Iterator; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.logging.Level; |
| | | import java.util.logging.Logger; |
| | | |
| | | /** |
| | | * Objekte der Klasse Handler nehmen Objekte entgegen die die Annotationen NeonActor enthalten. |
| | |
| | | |
| | | //Logger.getLogger(Handler.class.getName()) |
| | | // .log(Level.INFO, "{0} {1} {2}", new Object[]{methodType, route, className}); |
| | | dispatcher.get(methodType).put(ad.route, ad); |
| | | //dispatcher.get(methodType).put(ad.route, ad); |
| | | Object adMapObj = dispatcher.get(methodType); |
| | | if(adMapObj instanceof HashMap hashMap) { |
| | | @SuppressWarnings("unchecked") |
| | | HashMap<String, ActionDescriptor> map = hashMap; |
| | | map.put(ad.route, ad); |
| | | Logger.getLogger(Handler.class.getName()).log(Level.FINER, "ActionDescriptor route {0} className {1}", new Object[]{route, className}); |
| | | } else { |
| | | Logger.getLogger(Handler.class.getName()).finer("ActionDescriptorMap nicht gefunden"); |
| | | } |
| | | |
| | | } |
| | | |
| | | /** |
| | |
| | | .getHttpContext() |
| | | .getPath() |
| | | .length()); |
| | | |
| | | Type requestMethod = Type.valueOf(exchange.getRequestMethod()); |
| | | String requestMethodStr = exchange.getRequestMethod(); |
| | | Logger.getLogger(Handler.class.getName()).log(Level.FINER, "method {0} route {1}", new Object[]{requestMethodStr, route}); |
| | | Type requestMethod = Type.valueOf(requestMethodStr); |
| | | /* |
| | | Es wird erst geprueft, ob zu einer bestimmten Route |
| | | ein Actor registriert wurde. Wenn kein Actor mit dieser |
| | |
| | | Object md = dispatcher.get(requestMethod); |
| | | if (md instanceof Map) { |
| | | int pos = route.lastIndexOf("/"); |
| | | Logger.getLogger(Handler.class.getName()).log(Level.FINER, "pos {0}", pos); |
| | | Object o = ((Map) md).get(route); |
| | | if (!(o instanceof ActionDescriptor)) { |
| | | while (!found && (pos > -1)) { |
| | | String routeRest = route.substring(0, pos); |
| | | Logger.getLogger(Handler.class.getName()).log(Level.FINER, "pos {0} routeRest {1}", new Object[]{pos, routeRest}); |
| | | 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) { |
| | | Logger.getLogger(Handler.class.getName()).log(Level.FINER, "{0} not found ", route); |
| | | o = dispatcher.get(requestMethod).get("/"); |
| | | if (o instanceof ActionDescriptor) { |
| | | handleRequest(exchange, o, route, route); |
| | | handleRequest(exchange, o, route, route, requestMethod); |
| | | } else { |
| | | // kein ActionDescriptor für '/' |
| | | Logger.getLogger(Handler.class.getName()).log(Level.FINER, "Kein Actiondescriptor fuer '/'"); |
| | | } |
| | | } |
| | | } else { |
| | | // keine Actions fuer HTTP Methode |
| | | Logger.getLogger(Handler.class.getName()).log(Level.FINER, "Kein Actions fuer HTTP-Methode {0}", requestMethodStr); |
| | | } |
| | | |
| | | } |
| | | |
| | | 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 { |
| | | Logger.getLogger(Handler.class.getName()).log(Level.FINER, "Handle Request route {0} subroute {1}", new Object[]{route, subroute}); |
| | | ActionDescriptor ad = (ActionDescriptor) o; |
| | | String actorClassName = ad.className; |
| | | try { |
| | |
| | | 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); |
| | | Object antwort = method.invoke(actorObj, actionArgs); |
| | | if (!action.handlesResponse()) { |
| | | respond(exchange, antwort); |
| | | @SuppressWarnings("unchecked") |
| | | Object conObj = actorClass.getDeclaredConstructor(); |
| | | if(conObj instanceof Constructor) { |
| | | Constructor con = (Constructor) conObj; |
| | | Object actorObj; |
| | | actorObj = con.newInstance(); |
| | | addDataProvider(exchange, actorObj); |
| | | Object antwort = method.invoke(actorObj, actionArgs); |
| | | if (!action.handlesResponse()) { |
| | | respond(exchange, antwort); |
| | | } |
| | | } else { |
| | | // kein Konstruktor |
| | | Logger.getLogger(Handler.class.getName()).info("Kein Konstruktor gefunden"); |
| | | } |
| | | } |
| | | } |
| | |
| | | InstantiationException | IllegalAccessException | IllegalArgumentException | |
| | | InvocationTargetException ex) { |
| | | // Klasse nicht gefunden. Muss das geloggt oder sonstwie behandel werden? |
| | | Logger.getLogger(Handler.class.getName()).finer("Kein passende Actor-Klasse gefunden"); |
| | | } |
| | | //} |
| | | } |