All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.litongjava.tio.http.server.router.DefaultHttpRequestFunctionRouter Maven / Gradle / Ivy

There is a newer version: 3.7.3.v20241201-RELEASE
Show newest version
package com.litongjava.tio.http.server.router;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import com.litongjava.model.type.TioTypeReference;
import com.litongjava.tio.http.server.handler.IHttpRequestFunction;
import com.litongjava.tio.http.server.handler.RouteEntry;

public class DefaultHttpRequestFunctionRouter implements HttpRequestFunctionRouter {

  // RouteEntry 用于保存函数及其类型引用
  private final Map> requestMapping = new ConcurrentHashMap<>();

  @Override
  public  void add(String path, IHttpRequestFunction function, TioTypeReference typeReference) {
    requestMapping.put(path, new RouteEntry<>(function, typeReference));
  }

  @SuppressWarnings("unchecked")
  @Override
  public  RouteEntry find(String path) {
    RouteEntry entry = requestMapping.get(path);
    if (entry != null) {
      try {
        return (RouteEntry) entry;
      } catch (ClassCastException e) {
        throw new IllegalArgumentException("Function type mismatch for path: " + path, e);
      }
    }

    // Check for wildcard matches
    Set>> entrySet = requestMapping.entrySet();

    for (Map.Entry> mapEntry : entrySet) {
      String key = mapEntry.getKey();

      if (key.endsWith("/*")) {
        String baseRoute = key.substring(0, key.length() - 1);
        if (path.startsWith(baseRoute)) {
          return (RouteEntry) mapEntry.getValue(); // 强制类型转换
        }
      } else if (key.endsWith("/**")) {
        String baseRoute = key.substring(0, key.length() - 2);
        if (path.startsWith(baseRoute)) {
          return (RouteEntry) mapEntry.getValue(); // 强制类型转换
        }
      }
    }

    return null;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy