working on routing with prefix

This commit is contained in:
Jörg Prante 2023-03-23 11:59:30 +01:00
parent a78d88bc47
commit e488ed9d5a
8 changed files with 103 additions and 89 deletions

View file

@ -104,7 +104,6 @@ public class BaseHttpServerContext implements HttpServerContext {
setContextURL(request().getBaseURL()); setContextURL(request().getBaseURL());
} }
httpRequest = createRequest(); httpRequest = createRequest();
logger.log(Level.FINER, "request = " + httpRequest);
attributes.put("request", httpRequest); attributes.put("request", httpRequest);
next = false; next = false;
} }

View file

@ -75,9 +75,9 @@ public class ClassLoaderResourceHandler extends AbstractResourceHandler {
private URL url; private URL url;
ClassLoaderResource(HttpServerContext httpServerContext) throws IOException { ClassLoaderResource(HttpServerContext httpServerContext) throws IOException {
String effectivePath = httpServerContext.request().getRequestPath().substring(1); String requestPath = httpServerContext.request().getRequestPath().substring(1);
this.mimeType = mimeTypeService.getContentType(effectivePath); this.mimeType = mimeTypeService.getContentType(requestPath);
this.resourcePath = effectivePath.startsWith("/") ? effectivePath.substring(1) : effectivePath; this.resourcePath = requestPath.startsWith("/") ? requestPath.substring(1) : requestPath;
String path = prefix != null ? (prefix.endsWith("/") ? prefix : prefix + "/") : "/"; String path = prefix != null ? (prefix.endsWith("/") ? prefix : prefix + "/") : "/";
path = resourcePath.startsWith("/") ? path + resourcePath.substring(1) : path + resourcePath; path = resourcePath.startsWith("/") ? path + resourcePath.substring(1) : path + resourcePath;
String normalizedPath = PathNormalizer.normalize(resourcePath); String normalizedPath = PathNormalizer.normalize(resourcePath);
@ -88,19 +88,15 @@ public class ClassLoaderResourceHandler extends AbstractResourceHandler {
this.name = normalizedPath; this.name = normalizedPath;
this.baseName = basename(name); this.baseName = basename(name);
this.suffix = suffix(name); this.suffix = suffix(name);
if (logger.isLoggable(Level.FINER)) { logger.log(Level.FINER, "trying: path=" + path + " classLoader=" + classLoader);
logger.log(Level.FINER, "trying: path=" + path + " classLoader=" + classLoader);
}
java.net.URL url = classLoader.getResource(path); java.net.URL url = classLoader.getResource(path);
if (url != null) { if (url != null) {
this.url = URL.create(url.toString()); this.url = URL.create(url.toString());
URLConnection urlConnection = url.openConnection(); URLConnection urlConnection = url.openConnection();
this.lastModified = Instant.ofEpochMilli(urlConnection.getLastModified()); this.lastModified = Instant.ofEpochMilli(urlConnection.getLastModified());
this.length = urlConnection.getContentLength(); this.length = urlConnection.getContentLength();
if (logger.isLoggable(Level.FINER)) { logger.log(Level.FINER, "success: path=[" + path +
logger.log(Level.FINER, "success: path=[" + path + "] -> url=" + url + " lastModified=" + lastModified + "length=" + length);
"] -> url=" + url + " lastModified=" + lastModified + "length=" + length);
}
} else { } else {
this.lastModified = Instant.now(); this.lastModified = Instant.now();
this.length = 0; this.length = 0;

View file

@ -21,24 +21,31 @@ public class BaseHttpRoute implements HttpRoute {
private final Collection<HttpMethod> httpMethods; private final Collection<HttpMethod> httpMethods;
private final String prefix;
private final String path; private final String path;
private final List<RouteSegment> segments; private final List<RouteSegment> segments;
private final String sortKey; private final String sortKey;
public BaseHttpRoute(HttpAddress httpAddress, HttpMethod httpMethod, String path) { public BaseHttpRoute(HttpAddress httpAddress, HttpMethod httpMethod, String prefix, String path) {
this(httpAddress, Set.of(httpMethod), path, false); this(httpAddress, Set.of(httpMethod), prefix, path, false);
} }
public BaseHttpRoute(HttpAddress httpAddress, Collection<HttpMethod> httpMethods, String path, boolean onlyStrings) { public BaseHttpRoute(HttpAddress httpAddress,
Collection<HttpMethod> httpMethods,
String prefix,
String path,
boolean onlyStrings) {
Objects.requireNonNull(httpAddress, "address"); Objects.requireNonNull(httpAddress, "address");
Objects.requireNonNull(httpMethods, "methods"); Objects.requireNonNull(httpMethods, "methods");
Objects.requireNonNull(path, "path"); Objects.requireNonNull(path, "path");
this.httpAddress = httpAddress; this.httpAddress = httpAddress;
this.httpMethods = httpMethods; this.httpMethods = httpMethods;
this.prefix = prefix;
this.path = path; this.path = path;
this.segments = onlyStrings ? createStringSegments(path): createSegments(path); this.segments = onlyStrings ? createStringSegments(getEffectivePath()): createSegments(getEffectivePath());
this.sortKey = createSortKey(); this.sortKey = createSortKey();
} }
@ -52,11 +59,24 @@ public class BaseHttpRoute implements HttpRoute {
return httpMethods; return httpMethods;
} }
@Override
public String getPrefix() {
return prefix;
}
@Override @Override
public String getPath() { public String getPath() {
return path; return path;
} }
public String getEffectivePath() {
String path = getPath();
if (getPrefix() != null && !getPrefix().isEmpty()) {
path = path.replaceFirst(getPrefix(), "");
}
return path;
}
public List<RouteSegment> getSegments() { public List<RouteSegment> getSegments() {
return segments; return segments;
} }
@ -78,7 +98,7 @@ public class BaseHttpRoute implements HttpRoute {
if (requestedSegments.isEmpty() && segments.isEmpty()) { if (requestedSegments.isEmpty() && segments.isEmpty()) {
return true; return true;
} }
// special case: single segment with pattern to match, we must ignore the incoming segments // special case catch all: single segment with pattern to match, we must ignore the incoming segments
if (segments.size() == 1 && segments.get(0) instanceof PatternSegment) { if (segments.size() == 1 && segments.get(0) instanceof PatternSegment) {
MatchResult matchResult = segments.get(0).match(new StringSegment(requestedRoute.getPath())); MatchResult matchResult = segments.get(0).match(new StringSegment(requestedRoute.getPath()));
if (matchResult == MatchResult.TRUE) { if (matchResult == MatchResult.TRUE) {

View file

@ -11,10 +11,14 @@ import java.util.Arrays;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class BaseHttpRouteResolver<T> implements HttpRouteResolver<T> { public class BaseHttpRouteResolver<T> implements HttpRouteResolver<T> {
private static final Logger logger = Logger.getLogger(BaseHttpRouteResolver.class.getName());
private final Builder<T> builder; private final Builder<T> builder;
private BaseHttpRouteResolver(Builder<T> builder) { private BaseHttpRouteResolver(Builder<T> builder) {
@ -22,7 +26,7 @@ public class BaseHttpRouteResolver<T> implements HttpRouteResolver<T> {
} }
/** /**
* This naive rsolver walks through all configured routes and tries to match them. * This naive resolver walks through all configured routes and tries to match them.
* @param httpRoute the route to match against * @param httpRoute the route to match against
* @param listener the listener where the results are going * @param listener the listener where the results are going
*/ */
@ -32,7 +36,8 @@ public class BaseHttpRouteResolver<T> implements HttpRouteResolver<T> {
ParameterBuilder parameterBuilder = Parameter.builder().domain("PATH"); ParameterBuilder parameterBuilder = Parameter.builder().domain("PATH");
boolean match = entry.getKey().matches(parameterBuilder, httpRoute); boolean match = entry.getKey().matches(parameterBuilder, httpRoute);
if (match && listener != null) { if (match && listener != null) {
List<String> list = Arrays.stream(httpRoute.getPath().replaceFirst(builder.prefix, "").split("/")) String path = httpRoute.getEffectivePath();
List<String> list = Arrays.stream(path.split("/"))
.filter(s -> !s.isEmpty()).collect(Collectors.toList()); .filter(s -> !s.isEmpty()).collect(Collectors.toList());
listener.onResult(new Result<>(entry.getValue(), list, parameterBuilder.build())); listener.onResult(new Result<>(entry.getValue(), list, parameterBuilder.build()));
} }
@ -79,32 +84,23 @@ public class BaseHttpRouteResolver<T> implements HttpRouteResolver<T> {
private final List<Map.Entry<HttpRoute, T>> routes; private final List<Map.Entry<HttpRoute, T>> routes;
private String prefix;
private boolean sort; private boolean sort;
private Builder() { private Builder() {
this.comparator = new RouteComparator<>(); this.comparator = new RouteComparator<>();
this.prefix = "";
this.routes = new ArrayList<>(); this.routes = new ArrayList<>();
this.sort = false; this.sort = false;
} }
@Override @Override
public HttpRouteResolver.Builder<T> setPrefix(String prefix) { public HttpRouteResolver.Builder<T> add(HttpAddress httpAddress, HttpMethod httpMethod, String prefix, String path, T value) {
this.prefix = prefix; add(new BaseHttpRoute(httpAddress, Set.of(httpMethod), prefix, path, false), value);
return this; return this;
} }
@Override @Override
public HttpRouteResolver.Builder<T> add(HttpAddress httpAddress, HttpMethod httpMethod, String path, T value) { public HttpRouteResolver.Builder<T> add(HttpAddress httpAddress, Set<HttpMethod> httpMethods, String prefix, String path, T value) {
add(new BaseHttpRoute(httpAddress, Set.of(httpMethod), prefix + path, false), value); add(new BaseHttpRoute(httpAddress, httpMethods, prefix, path, false), value);
return this;
}
@Override
public HttpRouteResolver.Builder<T> add(HttpAddress httpAddress, Set<HttpMethod> httpMethods, String path, T value) {
add(new BaseHttpRoute(httpAddress, httpMethods, prefix + path, false), value);
return this; return this;
} }

View file

@ -48,9 +48,13 @@ public class BaseHttpRouter implements HttpRouter {
HttpRouteResolver.Builder<HttpService> httpRouteResolverBuilder = newHttpRouteResolverBuilder(); HttpRouteResolver.Builder<HttpService> httpRouteResolverBuilder = newHttpRouteResolverBuilder();
for (HttpDomain domain : builder.domains) { for (HttpDomain domain : builder.domains) {
for (HttpService httpService : domain.getServices()) { for (HttpService httpService : domain.getServices()) {
logger.log(Level.FINE, "adding " + domain.getAddress() + " " + httpService.getMethods() + " " + httpService.getPathSpecification() + " " + httpService); logger.log(Level.FINER, "adding " + domain.getAddress() + " " + httpService.getMethods() +
HttpRoute httpRoute = new BaseHttpRoute(domain.getAddress(), httpService.getMethods(), httpService.getPathSpecification(), false); " prefix = " + httpService.getPrefix() +
httpRouteResolverBuilder.setPrefix(httpService.getPrefix()); " path = " + httpService.getPathSpecification() + " " + httpService);
HttpRoute httpRoute = new BaseHttpRoute(domain.getAddress(),
httpService.getMethods(),
httpService.getPrefix(),
httpService.getPathSpecification(), false);
httpRouteResolverBuilder.add(httpRoute, httpService); httpRouteResolverBuilder.add(httpRoute, httpService);
} }
} }
@ -88,13 +92,17 @@ public class BaseHttpRouter implements HttpRouter {
Objects.requireNonNull(requestBuilder); Objects.requireNonNull(requestBuilder);
Objects.requireNonNull(requestBuilder.getRequestURI()); Objects.requireNonNull(requestBuilder.getRequestURI());
Objects.requireNonNull(requestBuilder.getBaseURL()); Objects.requireNonNull(requestBuilder.getBaseURL());
requestBuilder.setRequestPath(extractPath(requestBuilder.getRequestURI()));
HttpDomain httpDomain = findDomain(requestBuilder.getBaseURL()); HttpDomain httpDomain = findDomain(requestBuilder.getBaseURL());
if (httpDomain == null) { if (httpDomain == null) {
httpDomain = builder.domains.iterator().next(); httpDomain = builder.domains.iterator().next();
} }
List<HttpRouteResolver.Result<HttpService>> httpRouteResolverResults = new ArrayList<>(); List<HttpRouteResolver.Result<HttpService>> httpRouteResolverResults = new ArrayList<>();
HttpRoute httpRoute = new BaseHttpRoute(httpDomain.getAddress(), Set.of(requestBuilder.getMethod()), requestBuilder.getRequestPath(), true); requestBuilder.setRequestPath(extractPath(requestBuilder.getRequestURI()));
HttpRoute httpRoute = new BaseHttpRoute(httpDomain.getAddress(),
Set.of(requestBuilder.getMethod()),
"",
requestBuilder.getRequestPath(),
true);
httpRouteResolver.resolve(httpRoute, httpRouteResolverResults::add); httpRouteResolver.resolve(httpRoute, httpRouteResolverResults::add);
HttpServerContext httpServerContext = application.createContext(httpDomain, requestBuilder, responseBuilder); HttpServerContext httpServerContext = application.createContext(httpDomain, requestBuilder, responseBuilder);
route(httpServerContext, httpRouteResolverResults); route(httpServerContext, httpRouteResolverResults);

View file

@ -11,8 +11,12 @@ public interface HttpRoute {
Collection<HttpMethod> getHttpMethods(); Collection<HttpMethod> getHttpMethods();
String getPrefix();
String getPath(); String getPath();
String getEffectivePath();
boolean matches(ParameterBuilder parameterBuilder, HttpRoute requestedRoute); boolean matches(ParameterBuilder parameterBuilder, HttpRoute requestedRoute);
String getSortKey(); String getSortKey();

View file

@ -13,13 +13,11 @@ public interface HttpRouteResolver<T> {
interface Builder<T> { interface Builder<T> {
Builder<T> setPrefix(String prefix);
Builder<T> add(HttpRoute route, T value); Builder<T> add(HttpRoute route, T value);
Builder<T> add(HttpAddress httpAddress, HttpMethod httpMethod, String path, T value); Builder<T> add(HttpAddress httpAddress, HttpMethod httpMethod, String prefix, String path, T value);
Builder<T> add(HttpAddress httpAddress, Set<HttpMethod> httpMethods, String path, T value); Builder<T> add(HttpAddress httpAddress, Set<HttpMethod> httpMethods, String prefix, String path, T value);
Builder<T> sort(boolean sort); Builder<T> sort(boolean sort);

View file

@ -18,9 +18,9 @@ public class BaseHttpRouteResolverTest {
public void testEmptyRouteResolver() { public void testEmptyRouteResolver() {
BaseHttpRouteResolver.Builder<Integer> builder = BaseHttpRouteResolver.builder(); BaseHttpRouteResolver.Builder<Integer> builder = BaseHttpRouteResolver.builder();
HttpRouteResolver<Integer> resolver = builder HttpRouteResolver<Integer> resolver = builder
.add(HttpAddress.http1("host"), HttpMethod.POST, "/", 1) .add(HttpAddress.http1("host"), HttpMethod.POST, "", "/", 1)
.build(); .build();
HttpRoute route = new BaseHttpRoute(HttpAddress.http1("host"), HttpMethod.POST, ""); HttpRoute route = new BaseHttpRoute(HttpAddress.http1("host"), HttpMethod.POST, "", "");
AtomicInteger atomicInteger = new AtomicInteger(0); AtomicInteger atomicInteger = new AtomicInteger(0);
resolver.resolve(route, r -> atomicInteger.incrementAndGet()); resolver.resolve(route, r -> atomicInteger.incrementAndGet());
assertEquals(1, atomicInteger.get()); assertEquals(1, atomicInteger.get());
@ -31,7 +31,7 @@ public class BaseHttpRouteResolverTest {
BaseHttpRouteResolver.Builder<Integer> builder = BaseHttpRouteResolver.builder(); BaseHttpRouteResolver.Builder<Integer> builder = BaseHttpRouteResolver.builder();
HttpRouteResolver<Integer> resolver = builder HttpRouteResolver<Integer> resolver = builder
.build(); .build();
HttpRoute route = new BaseHttpRoute(HttpAddress.http1("host"), HttpMethod.POST, "/"); HttpRoute route = new BaseHttpRoute(HttpAddress.http1("host"), HttpMethod.POST, "", "/");
AtomicInteger atomicInteger = new AtomicInteger(0); AtomicInteger atomicInteger = new AtomicInteger(0);
resolver.resolve(route, r -> atomicInteger.incrementAndGet()); resolver.resolve(route, r -> atomicInteger.incrementAndGet());
assertEquals(0, atomicInteger.get()); assertEquals(0, atomicInteger.get());
@ -41,9 +41,9 @@ public class BaseHttpRouteResolverTest {
public void testEmptyRouteMatchResolver() { public void testEmptyRouteMatchResolver() {
BaseHttpRouteResolver.Builder<Integer> builder = BaseHttpRouteResolver.builder(); BaseHttpRouteResolver.Builder<Integer> builder = BaseHttpRouteResolver.builder();
HttpRouteResolver<Integer> resolver = builder HttpRouteResolver<Integer> resolver = builder
.add(HttpAddress.http1("host"), HttpMethod.POST, "/", 1) .add(HttpAddress.http1("host"), HttpMethod.POST, "", "/", 1)
.build(); .build();
HttpRoute route = new BaseHttpRoute(HttpAddress.http1("host"), HttpMethod.POST, "/"); HttpRoute route = new BaseHttpRoute(HttpAddress.http1("host"), HttpMethod.POST, "", "/");
AtomicInteger atomicInteger = new AtomicInteger(0); AtomicInteger atomicInteger = new AtomicInteger(0);
resolver.resolve(route, r -> { resolver.resolve(route, r -> {
assertEquals(1, r.getValue()); assertEquals(1, r.getValue());
@ -56,9 +56,9 @@ public class BaseHttpRouteResolverTest {
public void testSingleRouteResolver() { public void testSingleRouteResolver() {
BaseHttpRouteResolver.Builder<Integer> builder = BaseHttpRouteResolver.builder(); BaseHttpRouteResolver.Builder<Integer> builder = BaseHttpRouteResolver.builder();
HttpRouteResolver<Integer> resolver = builder HttpRouteResolver<Integer> resolver = builder
.add(HttpAddress.http1("host"), HttpMethod.POST, "/path", 1) .add(HttpAddress.http1("host"), HttpMethod.POST, "", "/path", 1)
.build(); .build();
HttpRoute route = new BaseHttpRoute(HttpAddress.http1("host"), HttpMethod.POST, "/path"); HttpRoute route = new BaseHttpRoute(HttpAddress.http1("host"), HttpMethod.POST, "", "/path");
AtomicInteger atomicInteger = new AtomicInteger(0); AtomicInteger atomicInteger = new AtomicInteger(0);
resolver.resolve(route, r -> { resolver.resolve(route, r -> {
assertEquals(1, r.getValue()); assertEquals(1, r.getValue());
@ -71,9 +71,9 @@ public class BaseHttpRouteResolverTest {
public void testSingleRouteMismatchResolver() { public void testSingleRouteMismatchResolver() {
BaseHttpRouteResolver.Builder<Integer> builder = BaseHttpRouteResolver.builder(); BaseHttpRouteResolver.Builder<Integer> builder = BaseHttpRouteResolver.builder();
HttpRouteResolver<Integer> resolver = builder HttpRouteResolver<Integer> resolver = builder
.add(HttpAddress.http1("host"), HttpMethod.POST, "/path1", 1) .add(HttpAddress.http1("host"), HttpMethod.POST, "", "/path1", 1)
.build(); .build();
HttpRoute route = new BaseHttpRoute(HttpAddress.http1("host"), HttpMethod.POST, "/path2"); HttpRoute route = new BaseHttpRoute(HttpAddress.http1("host"), HttpMethod.POST, "", "/path2");
AtomicInteger atomicInteger = new AtomicInteger(0); AtomicInteger atomicInteger = new AtomicInteger(0);
resolver.resolve(route, r -> { resolver.resolve(route, r -> {
assertEquals(1, r.getValue()); assertEquals(1, r.getValue());
@ -86,9 +86,9 @@ public class BaseHttpRouteResolverTest {
public void testSingleRouteMismatchTooLongResolver() { public void testSingleRouteMismatchTooLongResolver() {
BaseHttpRouteResolver.Builder<Integer> builder = BaseHttpRouteResolver.builder(); BaseHttpRouteResolver.Builder<Integer> builder = BaseHttpRouteResolver.builder();
HttpRouteResolver<Integer> resolver = builder HttpRouteResolver<Integer> resolver = builder
.add(HttpAddress.http1("host"), HttpMethod.POST, "/a", 1) .add(HttpAddress.http1("host"), HttpMethod.POST, "/a", "", 1)
.build(); .build();
HttpRoute route = new BaseHttpRoute(HttpAddress.http1("host"), HttpMethod.POST, "/a/b"); HttpRoute route = new BaseHttpRoute(HttpAddress.http1("host"), HttpMethod.POST, "", "/a/b");
AtomicInteger atomicInteger = new AtomicInteger(0); AtomicInteger atomicInteger = new AtomicInteger(0);
resolver.resolve(route, r -> { resolver.resolve(route, r -> {
assertEquals(1, r.getValue()); assertEquals(1, r.getValue());
@ -101,9 +101,9 @@ public class BaseHttpRouteResolverTest {
public void testSingleRouteCatchAllResolver() { public void testSingleRouteCatchAllResolver() {
BaseHttpRouteResolver.Builder<Integer> builder = BaseHttpRouteResolver.builder(); BaseHttpRouteResolver.Builder<Integer> builder = BaseHttpRouteResolver.builder();
HttpRouteResolver<Integer> resolver = builder HttpRouteResolver<Integer> resolver = builder
.add(HttpAddress.http1("host"), HttpMethod.POST, "/**", 1) .add(HttpAddress.http1("host"), HttpMethod.POST, "", "/**", 1)
.build(); .build();
HttpRoute route = new BaseHttpRoute(HttpAddress.http1("host"), HttpMethod.POST, "/path"); HttpRoute route = new BaseHttpRoute(HttpAddress.http1("host"), HttpMethod.POST, "", "/path");
AtomicInteger atomicInteger = new AtomicInteger(0); AtomicInteger atomicInteger = new AtomicInteger(0);
resolver.resolve(route, r -> { resolver.resolve(route, r -> {
assertEquals(1, r.getValue()); assertEquals(1, r.getValue());
@ -116,9 +116,9 @@ public class BaseHttpRouteResolverTest {
public void testSingleRouteCatchAllLongPathResolver() { public void testSingleRouteCatchAllLongPathResolver() {
BaseHttpRouteResolver.Builder<Integer> builder = BaseHttpRouteResolver.builder(); BaseHttpRouteResolver.Builder<Integer> builder = BaseHttpRouteResolver.builder();
HttpRouteResolver<Integer> resolver = builder HttpRouteResolver<Integer> resolver = builder
.add(HttpAddress.http1("host"), HttpMethod.POST, "/**", 1) .add(HttpAddress.http1("host"), HttpMethod.POST, "", "/**", 1)
.build(); .build();
HttpRoute route = new BaseHttpRoute(HttpAddress.http1("host"), HttpMethod.POST, "/a/very/long/path"); HttpRoute route = new BaseHttpRoute(HttpAddress.http1("host"), HttpMethod.POST, "", "/a/very/long/path");
AtomicInteger atomicInteger = new AtomicInteger(0); AtomicInteger atomicInteger = new AtomicInteger(0);
resolver.resolve(route, r -> { resolver.resolve(route, r -> {
assertEquals(1, r.getValue()); assertEquals(1, r.getValue());
@ -131,9 +131,9 @@ public class BaseHttpRouteResolverTest {
public void testSingleRouteJpegResolver() { public void testSingleRouteJpegResolver() {
BaseHttpRouteResolver.Builder<Integer> builder = BaseHttpRouteResolver.builder(); BaseHttpRouteResolver.Builder<Integer> builder = BaseHttpRouteResolver.builder();
HttpRouteResolver<Integer> resolver = builder HttpRouteResolver<Integer> resolver = builder
.add(HttpAddress.http1("host"), HttpMethod.POST, "glob:*.jpg", 1) .add(HttpAddress.http1("host"), HttpMethod.POST, "", "glob:*.jpg", 1)
.build(); .build();
HttpRoute route = new BaseHttpRoute(HttpAddress.http1("host"), HttpMethod.POST, "abc.jpg"); HttpRoute route = new BaseHttpRoute(HttpAddress.http1("host"), HttpMethod.POST, "", "abc.jpg");
AtomicInteger atomicInteger = new AtomicInteger(0); AtomicInteger atomicInteger = new AtomicInteger(0);
resolver.resolve(route, r -> { resolver.resolve(route, r -> {
assertEquals(1, r.getValue()); assertEquals(1, r.getValue());
@ -146,9 +146,9 @@ public class BaseHttpRouteResolverTest {
public void testSingleRouteGlobJpegResolver() { public void testSingleRouteGlobJpegResolver() {
BaseHttpRouteResolver.Builder<Integer> builder = BaseHttpRouteResolver.builder(); BaseHttpRouteResolver.Builder<Integer> builder = BaseHttpRouteResolver.builder();
HttpRouteResolver<Integer> resolver = builder HttpRouteResolver<Integer> resolver = builder
.add(HttpAddress.http1("host"), HttpMethod.POST, "glob:**.jpg", 1) .add(HttpAddress.http1("host"), HttpMethod.POST, "", "glob:**.jpg", 1)
.build(); .build();
HttpRoute route = new BaseHttpRoute(HttpAddress.http1("host"), HttpMethod.POST, "/a/picture/abc.jpg"); HttpRoute route = new BaseHttpRoute(HttpAddress.http1("host"), HttpMethod.POST, "", "/a/picture/abc.jpg");
AtomicInteger atomicInteger = new AtomicInteger(0); AtomicInteger atomicInteger = new AtomicInteger(0);
resolver.resolve(route, r -> { resolver.resolve(route, r -> {
assertEquals(1, r.getValue()); assertEquals(1, r.getValue());
@ -161,9 +161,9 @@ public class BaseHttpRouteResolverTest {
public void testSingleRouteParameterResolver() { public void testSingleRouteParameterResolver() {
BaseHttpRouteResolver.Builder<Integer> builder = BaseHttpRouteResolver.builder(); BaseHttpRouteResolver.Builder<Integer> builder = BaseHttpRouteResolver.builder();
HttpRouteResolver<Integer> resolver = builder HttpRouteResolver<Integer> resolver = builder
.add(HttpAddress.http1("host"), HttpMethod.POST, "/{token}", 1) .add(HttpAddress.http1("host"), HttpMethod.POST, "", "/{token}", 1)
.build(); .build();
HttpRoute route = new BaseHttpRoute(HttpAddress.http1("host"), HttpMethod.POST, "/abcdef"); HttpRoute route = new BaseHttpRoute(HttpAddress.http1("host"), HttpMethod.POST, "", "/abcdef");
AtomicInteger atomicInteger = new AtomicInteger(0); AtomicInteger atomicInteger = new AtomicInteger(0);
resolver.resolve(route, r -> { resolver.resolve(route, r -> {
assertEquals(1, r.getValue()); assertEquals(1, r.getValue());
@ -177,9 +177,9 @@ public class BaseHttpRouteResolverTest {
public void testTwoRouteParameterResolver() { public void testTwoRouteParameterResolver() {
BaseHttpRouteResolver.Builder<Integer> builder = BaseHttpRouteResolver.builder(); BaseHttpRouteResolver.Builder<Integer> builder = BaseHttpRouteResolver.builder();
HttpRouteResolver<Integer> resolver = builder HttpRouteResolver<Integer> resolver = builder
.add(HttpAddress.http1("host"), HttpMethod.POST, "/{token}/{key}", 1) .add(HttpAddress.http1("host"), HttpMethod.POST, "", "/{token}/{key}", 1)
.build(); .build();
HttpRoute route = new BaseHttpRoute(HttpAddress.http1("host"), HttpMethod.POST, "/abcdef/123456"); HttpRoute route = new BaseHttpRoute(HttpAddress.http1("host"), HttpMethod.POST, "", "/abcdef/123456");
AtomicInteger atomicInteger = new AtomicInteger(0); AtomicInteger atomicInteger = new AtomicInteger(0);
resolver.resolve(route, r -> { resolver.resolve(route, r -> {
assertEquals(1, r.getValue()); assertEquals(1, r.getValue());
@ -193,11 +193,11 @@ public class BaseHttpRouteResolverTest {
public void testMultiRouteResolver() { public void testMultiRouteResolver() {
BaseHttpRouteResolver.Builder<Integer> builder = BaseHttpRouteResolver.builder(); BaseHttpRouteResolver.Builder<Integer> builder = BaseHttpRouteResolver.builder();
HttpRouteResolver<Integer> resolver = builder HttpRouteResolver<Integer> resolver = builder
.add(HttpAddress.http1("host"), HttpMethod.POST, "/a/**", 1) .add(HttpAddress.http1("host"), HttpMethod.POST, "", "/a/**", 1)
.add(HttpAddress.http1("host"), HttpMethod.POST, "/a/b/**", 2) .add(HttpAddress.http1("host"), HttpMethod.POST, "", "/a/b/**", 2)
.add(HttpAddress.http1("host"), HttpMethod.POST, "/a/b/c", 3) .add(HttpAddress.http1("host"), HttpMethod.POST, "", "/a/b/c", 3)
.build(); .build();
HttpRoute route = new BaseHttpRoute(HttpAddress.http1("host"), HttpMethod.POST, "/a"); HttpRoute route = new BaseHttpRoute(HttpAddress.http1("host"), HttpMethod.POST, "", "/a");
AtomicInteger atomicInteger = new AtomicInteger(0); AtomicInteger atomicInteger = new AtomicInteger(0);
resolver.resolve(route, r -> { resolver.resolve(route, r -> {
assertEquals(1, (int) r.getValue()); assertEquals(1, (int) r.getValue());
@ -210,18 +210,16 @@ public class BaseHttpRouteResolverTest {
public void testMultiRouteLongestFirstResolver() { public void testMultiRouteLongestFirstResolver() {
HttpRouteResolver.Builder<Integer> builder = BaseHttpRouteResolver.builder(); HttpRouteResolver.Builder<Integer> builder = BaseHttpRouteResolver.builder();
HttpRouteResolver<Integer> resolver = builder HttpRouteResolver<Integer> resolver = builder
.add(HttpAddress.http1("host"), HttpMethod.POST, "/a/**", 1) .add(HttpAddress.http1("host"), HttpMethod.POST, "", "/a/**", 1)
.add(HttpAddress.http1("host"), HttpMethod.POST, "/a/b/c/**", 2) .add(HttpAddress.http1("host"), HttpMethod.POST, "", "/a/b/c/**", 2)
.add(HttpAddress.http1("host"), HttpMethod.POST, "/a/b/c/d/e/f/g", 3) .add(HttpAddress.http1("host"), HttpMethod.POST, "", "/a/b/c/d/e/f/g", 3)
.sort(true) .sort(true)
.build(); .build();
HttpRoute route = new BaseHttpRoute(HttpAddress.http1("host"), HttpMethod.POST, "/a"); HttpRoute route = new BaseHttpRoute(HttpAddress.http1("host"), HttpMethod.POST, "", "/a");
AtomicInteger atomicInteger = new AtomicInteger(0); AtomicInteger atomicInteger = new AtomicInteger(0);
resolver.resolve(route, r -> { resolver.resolve(route, r -> {
switch (atomicInteger.get()) { if (atomicInteger.get() == 0) {
case 0: assertEquals(1, (int) r.getValue());
assertEquals(1, (int) r.getValue());
break;
} }
atomicInteger.incrementAndGet(); atomicInteger.incrementAndGet();
}); });
@ -232,22 +230,18 @@ public class BaseHttpRouteResolverTest {
public void testMultiRouteLongestFirstWithGlobFirstResolver() { public void testMultiRouteLongestFirstWithGlobFirstResolver() {
HttpRouteResolver.Builder<Integer> builder = BaseHttpRouteResolver.builder(); HttpRouteResolver.Builder<Integer> builder = BaseHttpRouteResolver.builder();
HttpRouteResolver<Integer> resolver = builder HttpRouteResolver<Integer> resolver = builder
.add(HttpAddress.http1("host"), HttpMethod.POST, "/a/**", 4) .add(HttpAddress.http1("host"), HttpMethod.POST, "", "/a/**", 4)
.add(HttpAddress.http1("host"), HttpMethod.POST, "/a/b/c/**", 3) .add(HttpAddress.http1("host"), HttpMethod.POST, "", "/a/b/c/**", 3)
.add(HttpAddress.http1("host"), HttpMethod.POST, "/a/b/c/d/e/f/g", 2) .add(HttpAddress.http1("host"), HttpMethod.POST, "", "/a/b/c/d/e/f/g", 2)
.add(HttpAddress.http1("host"), HttpMethod.POST, "glob:**", 1) .add(HttpAddress.http1("host"), HttpMethod.POST, "", "glob:**", 1)
.sort(true) .sort(true)
.build(); .build();
HttpRoute route = new BaseHttpRoute(HttpAddress.http1("host"), HttpMethod.POST, "/a"); HttpRoute route = new BaseHttpRoute(HttpAddress.http1("host"), HttpMethod.POST, "", "/a");
AtomicInteger atomicInteger = new AtomicInteger(0); AtomicInteger atomicInteger = new AtomicInteger(0);
resolver.resolve(route, r -> { resolver.resolve(route, r -> {
switch (atomicInteger.get()) { switch (atomicInteger.get()) {
case 0: case 0 -> assertEquals(1, (int) r.getValue());
assertEquals(1, (int) r.getValue()); case 1 -> assertEquals(4, (int) r.getValue());
break;
case 1:
assertEquals(4, (int) r.getValue());
break;
} }
atomicInteger.incrementAndGet(); atomicInteger.incrementAndGet();
}); });
@ -258,10 +252,9 @@ public class BaseHttpRouteResolverTest {
public void testContextRouteResolver() { public void testContextRouteResolver() {
BaseHttpRouteResolver.Builder<Integer> builder = BaseHttpRouteResolver.builder(); BaseHttpRouteResolver.Builder<Integer> builder = BaseHttpRouteResolver.builder();
HttpRouteResolver<Integer> resolver = builder HttpRouteResolver<Integer> resolver = builder
.setPrefix("/app") .add(HttpAddress.http1("host"), HttpMethod.POST, "/app", "/path", 1)
.add(HttpAddress.http1("host"), HttpMethod.POST, "/path", 1)
.build(); .build();
HttpRoute route = new BaseHttpRoute(HttpAddress.http1("host"), HttpMethod.POST, "/app/path"); HttpRoute route = new BaseHttpRoute(HttpAddress.http1("host"), HttpMethod.POST, "/app", "/app/path");
AtomicInteger atomicInteger = new AtomicInteger(0); AtomicInteger atomicInteger = new AtomicInteger(0);
resolver.resolve(route, r -> { resolver.resolve(route, r -> {
assertEquals(1, r.getValue()); assertEquals(1, r.getValue());