Ultrakompakter HTTP Server
ulrich
6 days ago 31ff37baa3f5562ef959ec91aa3ea60b7c89f2fe
src/de/uhilger/neon/Handler.java
@@ -73,6 +73,8 @@
   * ausgefuehrt werden soll
   */
  public void setActor(Type methodType, String route, String className) {
    Logger.getLogger(Handler.class.getName()).log(
            Level.FINER, "type {0} route {1} className {2}", new Object[]{methodType, route, className});
    ActionDescriptor ad = new ActionDescriptor();
    ad.className = className;
    ad.routeParams = new HashMap<>();
@@ -85,6 +87,8 @@
              .split("/");
      for (int i = 0; i < params.length; i++) {
        ad.routeParams.put(params[i], i);
        Logger.getLogger(Handler.class.getName()).log(
                Level.FINER, "ActionDescriptor param {0} no. {1}", new Object[]{params[i], i});
      }
      ad.route = route.substring(0, pos - 1);
    } else {
@@ -96,11 +100,13 @@
    //        .log(Level.INFO, "{0} {1} {2}", new Object[]{methodType, route, className});
    //dispatcher.get(methodType).put(ad.route, ad);
    Object adMapObj = dispatcher.get(methodType);
    if(adMapObj instanceof HashMap hashMap) {
    if(adMapObj instanceof HashMap) {
      @SuppressWarnings("unchecked")
      HashMap<String, ActionDescriptor> map = hashMap;
      HashMap<String, ActionDescriptor> map = (HashMap) adMapObj;
      map.put(ad.route, ad);
      Logger.getLogger(Handler.class.getName()).log(Level.FINER, "ActionDescriptor route {0} className {1}", new Object[]{route, className});
      Logger.getLogger(Handler.class.getName()).log(
              Level.FINER, "ActionDescriptor route {0} className {1}", new Object[]{route, className});
      //dispatcher.put(methodType, map);
    } else {
      Logger.getLogger(Handler.class.getName()).finer("ActionDescriptorMap nicht gefunden");        
    }
@@ -149,7 +155,8 @@
                    .getPath()
                    .length());            
    String requestMethodStr = exchange.getRequestMethod();        
    Logger.getLogger(Handler.class.getName()).log(Level.FINER, "method {0} route {1}", new Object[]{requestMethodStr, route});
    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 
@@ -166,7 +173,8 @@
      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});
          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;
@@ -190,22 +198,33 @@
      }
    } else {
      // keine Actions fuer HTTP Methode
      Logger.getLogger(Handler.class.getName()).log(Level.FINER, "Kein Actions fuer HTTP-Methode {0}", requestMethodStr);
      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, Type requestMethod) throws IOException {
    Logger.getLogger(Handler.class.getName()).log(Level.FINER, "Handle Request route {0} subroute {1}", new Object[]{route, subroute});
  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;
    Logger.getLogger(Handler.class.getName()).log(
            Level.FINER, "Handle Request actor class name {0}", new Object[]{actorClassName});
    try {
      Class actorClass = Class.forName(actorClassName);
      //Class actorClass = Class.forName(actorClassName);
      Scanner s = new Scanner(o.getClass(), Actor.class);
      Class actorClass = s.getCl().loadClass(actorClassName);
    Logger.getLogger(Handler.class.getName()).log(
            Level.FINER, "Handle Request actor class found: {0}", new Object[]{actorClass.getName()});
      Method[] methods = actorClass.getMethods();
      for (Method method : methods) {
        Action action = method.getAnnotation(Action.class);
        if (action != null) {
          if ((action.route().equals("/") || action.route().startsWith(route)) && action.type().equals(requestMethod)) {
          if ((action.route().equals("/") ||
                  action.route().startsWith(route)) && action.type().equals(requestMethod)) {
            Object[] actionArgs = getActionArgs(exchange, method, ad, subroute);
            @SuppressWarnings("unchecked")
            Object conObj = actorClass.getDeclaredConstructor();
@@ -263,17 +282,38 @@
      if (methodParam.getType().equals(HttpExchange.class)) {
        actionArgs[k] = exchange;
      } else {
        Logger.getLogger(Handler.class.getName()).finer("method name " + methodParam.getName());
        Integer i = ad.routeParams.getOrDefault(methodParam.getName(), -1);
        Logger.getLogger(Handler.class.getName()).finer("param no " + i);
        if (i < 0) {
          actionArgs[k] = queryParams.get(methodParam.getName());
          i = getArgNoFromArgStr(methodParam.getName());
          Logger.getLogger(Handler.class.getName()).finer("param no " + i);
          if(i < 0) {
            actionArgs[k] = queryParams.get(methodParam.getName());
          } else {
            actionArgs[k] = routeParams[i];
            Logger.getLogger(Handler.class.getName()).finer("routeParam " + i + ": " + routeParams[i]);
          }
        } else {
          actionArgs[k] = routeParams[i + 1];
        }
          Logger.getLogger(Handler.class.getName()).finer("routeParam " + i+1 + ": " + routeParams[i + 1]);
        }
        Logger.getLogger(Handler.class.getName()).finer("ActionArgs " + k + ": " + actionArgs[k]);
      }
      ++k;
    }
    return actionArgs;
  }
  private int getArgNoFromArgStr(String argStr) {
    int argNo = -1;
    if(argStr.contains("arg")) {
    String numStr = argStr.substring("arg".length());
    Logger.getLogger(Handler.class.getName()).finer("numStr " + numStr);
    argNo = Integer.parseInt(numStr);
    }
    return argNo;
  }
  private void addDataProvider(HttpExchange exchange, Object actorObj) {
    if (actorObj instanceof DataConsumer) {