com.litongjava.tio.http.server.router.DefaultHttpRequestRouter Maven / Gradle / Ivy
package com.litongjava.tio.http.server.router;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import com.litongjava.tio.http.server.handler.HttpRequestHandler;
public class DefaultHttpRequestRouter implements HttpRequestRouter {
Map requestMapping = new ConcurrentHashMap<>();
public void add(String path, HttpRequestHandler handler) {
requestMapping.put(path, handler);
}
/**
* find route /* 表示匹配任何以特定路径开始的路径,/** 表示匹配该路径及其下的任何子路径
*/
public HttpRequestHandler find(String path) {
HttpRequestHandler httpRequestRouteHandler = requestMapping.get(path);
if (httpRequestRouteHandler != null) {
return httpRequestRouteHandler;
}
// Check for wildcard matches
Set> entrySet = requestMapping.entrySet();
for (Map.Entry entry : entrySet) {
String key = entry.getKey();
if (key.endsWith("/*")) {
String baseRoute = key.substring(0, key.length() - 1);
if (path.startsWith(baseRoute)) {
return entry.getValue();
}
} else if (key.endsWith("/**")) {
String baseRoute = key.substring(0, key.length() - 2);
if (path.startsWith(baseRoute)) {
return entry.getValue();
}
}
}
return null;
}
@Override
public Map all() {
return requestMapping;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy