From e488ed9d5adb31f03a73eb509dbfe901982fd074 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Prante?= Date: Thu, 23 Mar 2023 11:59:30 +0100 Subject: [PATCH] working on routing with prefix --- .../http/server/BaseHttpServerContext.java | 1 - .../resource/ClassLoaderResourceHandler.java | 16 ++-- .../net/http/server/route/BaseHttpRoute.java | 30 +++++- .../server/route/BaseHttpRouteResolver.java | 26 +++--- .../net/http/server/route/BaseHttpRouter.java | 18 +++- .../xbib/net/http/server/route/HttpRoute.java | 4 + .../http/server/route/HttpRouteResolver.java | 6 +- .../route/base/BaseHttpRouteResolverTest.java | 91 +++++++++---------- 8 files changed, 103 insertions(+), 89 deletions(-) diff --git a/net-http-server/src/main/java/org/xbib/net/http/server/BaseHttpServerContext.java b/net-http-server/src/main/java/org/xbib/net/http/server/BaseHttpServerContext.java index d72df0c..bb1c3d8 100644 --- a/net-http-server/src/main/java/org/xbib/net/http/server/BaseHttpServerContext.java +++ b/net-http-server/src/main/java/org/xbib/net/http/server/BaseHttpServerContext.java @@ -104,7 +104,6 @@ public class BaseHttpServerContext implements HttpServerContext { setContextURL(request().getBaseURL()); } httpRequest = createRequest(); - logger.log(Level.FINER, "request = " + httpRequest); attributes.put("request", httpRequest); next = false; } diff --git a/net-http-server/src/main/java/org/xbib/net/http/server/resource/ClassLoaderResourceHandler.java b/net-http-server/src/main/java/org/xbib/net/http/server/resource/ClassLoaderResourceHandler.java index bc535ef..4f3899c 100644 --- a/net-http-server/src/main/java/org/xbib/net/http/server/resource/ClassLoaderResourceHandler.java +++ b/net-http-server/src/main/java/org/xbib/net/http/server/resource/ClassLoaderResourceHandler.java @@ -75,9 +75,9 @@ public class ClassLoaderResourceHandler extends AbstractResourceHandler { private URL url; ClassLoaderResource(HttpServerContext httpServerContext) throws IOException { - String effectivePath = httpServerContext.request().getRequestPath().substring(1); - this.mimeType = mimeTypeService.getContentType(effectivePath); - this.resourcePath = effectivePath.startsWith("/") ? effectivePath.substring(1) : effectivePath; + String requestPath = httpServerContext.request().getRequestPath().substring(1); + this.mimeType = mimeTypeService.getContentType(requestPath); + this.resourcePath = requestPath.startsWith("/") ? requestPath.substring(1) : requestPath; String path = prefix != null ? (prefix.endsWith("/") ? prefix : prefix + "/") : "/"; path = resourcePath.startsWith("/") ? path + resourcePath.substring(1) : path + resourcePath; String normalizedPath = PathNormalizer.normalize(resourcePath); @@ -88,19 +88,15 @@ public class ClassLoaderResourceHandler extends AbstractResourceHandler { this.name = normalizedPath; this.baseName = basename(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); if (url != null) { this.url = URL.create(url.toString()); URLConnection urlConnection = url.openConnection(); this.lastModified = Instant.ofEpochMilli(urlConnection.getLastModified()); this.length = urlConnection.getContentLength(); - if (logger.isLoggable(Level.FINER)) { - logger.log(Level.FINER, "success: path=[" + path + - "] -> url=" + url + " lastModified=" + lastModified + "length=" + length); - } + logger.log(Level.FINER, "success: path=[" + path + + "] -> url=" + url + " lastModified=" + lastModified + "length=" + length); } else { this.lastModified = Instant.now(); this.length = 0; diff --git a/net-http-server/src/main/java/org/xbib/net/http/server/route/BaseHttpRoute.java b/net-http-server/src/main/java/org/xbib/net/http/server/route/BaseHttpRoute.java index 440fc2e..9de0eb9 100644 --- a/net-http-server/src/main/java/org/xbib/net/http/server/route/BaseHttpRoute.java +++ b/net-http-server/src/main/java/org/xbib/net/http/server/route/BaseHttpRoute.java @@ -21,24 +21,31 @@ public class BaseHttpRoute implements HttpRoute { private final Collection httpMethods; + private final String prefix; + private final String path; private final List segments; private final String sortKey; - public BaseHttpRoute(HttpAddress httpAddress, HttpMethod httpMethod, String path) { - this(httpAddress, Set.of(httpMethod), path, false); + public BaseHttpRoute(HttpAddress httpAddress, HttpMethod httpMethod, String prefix, String path) { + this(httpAddress, Set.of(httpMethod), prefix, path, false); } - public BaseHttpRoute(HttpAddress httpAddress, Collection httpMethods, String path, boolean onlyStrings) { + public BaseHttpRoute(HttpAddress httpAddress, + Collection httpMethods, + String prefix, + String path, + boolean onlyStrings) { Objects.requireNonNull(httpAddress, "address"); Objects.requireNonNull(httpMethods, "methods"); Objects.requireNonNull(path, "path"); this.httpAddress = httpAddress; this.httpMethods = httpMethods; + this.prefix = prefix; this.path = path; - this.segments = onlyStrings ? createStringSegments(path): createSegments(path); + this.segments = onlyStrings ? createStringSegments(getEffectivePath()): createSegments(getEffectivePath()); this.sortKey = createSortKey(); } @@ -52,11 +59,24 @@ public class BaseHttpRoute implements HttpRoute { return httpMethods; } + @Override + public String getPrefix() { + return prefix; + } + @Override public String getPath() { return path; } + public String getEffectivePath() { + String path = getPath(); + if (getPrefix() != null && !getPrefix().isEmpty()) { + path = path.replaceFirst(getPrefix(), ""); + } + return path; + } + public List getSegments() { return segments; } @@ -78,7 +98,7 @@ public class BaseHttpRoute implements HttpRoute { if (requestedSegments.isEmpty() && segments.isEmpty()) { 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) { MatchResult matchResult = segments.get(0).match(new StringSegment(requestedRoute.getPath())); if (matchResult == MatchResult.TRUE) { diff --git a/net-http-server/src/main/java/org/xbib/net/http/server/route/BaseHttpRouteResolver.java b/net-http-server/src/main/java/org/xbib/net/http/server/route/BaseHttpRouteResolver.java index 3eb566d..fb660a8 100644 --- a/net-http-server/src/main/java/org/xbib/net/http/server/route/BaseHttpRouteResolver.java +++ b/net-http-server/src/main/java/org/xbib/net/http/server/route/BaseHttpRouteResolver.java @@ -11,10 +11,14 @@ import java.util.Arrays; import java.util.Comparator; import java.util.List; import java.util.Map; +import java.util.logging.Level; +import java.util.logging.Logger; import java.util.stream.Collectors; public class BaseHttpRouteResolver implements HttpRouteResolver { + private static final Logger logger = Logger.getLogger(BaseHttpRouteResolver.class.getName()); + private final Builder builder; private BaseHttpRouteResolver(Builder builder) { @@ -22,7 +26,7 @@ public class BaseHttpRouteResolver implements HttpRouteResolver { } /** - * 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 listener the listener where the results are going */ @@ -32,7 +36,8 @@ public class BaseHttpRouteResolver implements HttpRouteResolver { ParameterBuilder parameterBuilder = Parameter.builder().domain("PATH"); boolean match = entry.getKey().matches(parameterBuilder, httpRoute); if (match && listener != null) { - List list = Arrays.stream(httpRoute.getPath().replaceFirst(builder.prefix, "").split("/")) + String path = httpRoute.getEffectivePath(); + List list = Arrays.stream(path.split("/")) .filter(s -> !s.isEmpty()).collect(Collectors.toList()); listener.onResult(new Result<>(entry.getValue(), list, parameterBuilder.build())); } @@ -79,32 +84,23 @@ public class BaseHttpRouteResolver implements HttpRouteResolver { private final List> routes; - private String prefix; - private boolean sort; private Builder() { this.comparator = new RouteComparator<>(); - this.prefix = ""; this.routes = new ArrayList<>(); this.sort = false; } @Override - public HttpRouteResolver.Builder setPrefix(String prefix) { - this.prefix = prefix; + public HttpRouteResolver.Builder add(HttpAddress httpAddress, HttpMethod httpMethod, String prefix, String path, T value) { + add(new BaseHttpRoute(httpAddress, Set.of(httpMethod), prefix, path, false), value); return this; } @Override - public HttpRouteResolver.Builder add(HttpAddress httpAddress, HttpMethod httpMethod, String path, T value) { - add(new BaseHttpRoute(httpAddress, Set.of(httpMethod), prefix + path, false), value); - return this; - } - - @Override - public HttpRouteResolver.Builder add(HttpAddress httpAddress, Set httpMethods, String path, T value) { - add(new BaseHttpRoute(httpAddress, httpMethods, prefix + path, false), value); + public HttpRouteResolver.Builder add(HttpAddress httpAddress, Set httpMethods, String prefix, String path, T value) { + add(new BaseHttpRoute(httpAddress, httpMethods, prefix, path, false), value); return this; } diff --git a/net-http-server/src/main/java/org/xbib/net/http/server/route/BaseHttpRouter.java b/net-http-server/src/main/java/org/xbib/net/http/server/route/BaseHttpRouter.java index d114056..a99e853 100644 --- a/net-http-server/src/main/java/org/xbib/net/http/server/route/BaseHttpRouter.java +++ b/net-http-server/src/main/java/org/xbib/net/http/server/route/BaseHttpRouter.java @@ -48,9 +48,13 @@ public class BaseHttpRouter implements HttpRouter { HttpRouteResolver.Builder httpRouteResolverBuilder = newHttpRouteResolverBuilder(); for (HttpDomain domain : builder.domains) { for (HttpService httpService : domain.getServices()) { - logger.log(Level.FINE, "adding " + domain.getAddress() + " " + httpService.getMethods() + " " + httpService.getPathSpecification() + " " + httpService); - HttpRoute httpRoute = new BaseHttpRoute(domain.getAddress(), httpService.getMethods(), httpService.getPathSpecification(), false); - httpRouteResolverBuilder.setPrefix(httpService.getPrefix()); + logger.log(Level.FINER, "adding " + domain.getAddress() + " " + httpService.getMethods() + + " prefix = " + httpService.getPrefix() + + " path = " + httpService.getPathSpecification() + " " + httpService); + HttpRoute httpRoute = new BaseHttpRoute(domain.getAddress(), + httpService.getMethods(), + httpService.getPrefix(), + httpService.getPathSpecification(), false); httpRouteResolverBuilder.add(httpRoute, httpService); } } @@ -88,13 +92,17 @@ public class BaseHttpRouter implements HttpRouter { Objects.requireNonNull(requestBuilder); Objects.requireNonNull(requestBuilder.getRequestURI()); Objects.requireNonNull(requestBuilder.getBaseURL()); - requestBuilder.setRequestPath(extractPath(requestBuilder.getRequestURI())); HttpDomain httpDomain = findDomain(requestBuilder.getBaseURL()); if (httpDomain == null) { httpDomain = builder.domains.iterator().next(); } List> 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); HttpServerContext httpServerContext = application.createContext(httpDomain, requestBuilder, responseBuilder); route(httpServerContext, httpRouteResolverResults); diff --git a/net-http-server/src/main/java/org/xbib/net/http/server/route/HttpRoute.java b/net-http-server/src/main/java/org/xbib/net/http/server/route/HttpRoute.java index 6c8a942..b8612e4 100644 --- a/net-http-server/src/main/java/org/xbib/net/http/server/route/HttpRoute.java +++ b/net-http-server/src/main/java/org/xbib/net/http/server/route/HttpRoute.java @@ -11,8 +11,12 @@ public interface HttpRoute { Collection getHttpMethods(); + String getPrefix(); + String getPath(); + String getEffectivePath(); + boolean matches(ParameterBuilder parameterBuilder, HttpRoute requestedRoute); String getSortKey(); diff --git a/net-http-server/src/main/java/org/xbib/net/http/server/route/HttpRouteResolver.java b/net-http-server/src/main/java/org/xbib/net/http/server/route/HttpRouteResolver.java index f3ce388..9a345ca 100644 --- a/net-http-server/src/main/java/org/xbib/net/http/server/route/HttpRouteResolver.java +++ b/net-http-server/src/main/java/org/xbib/net/http/server/route/HttpRouteResolver.java @@ -13,13 +13,11 @@ public interface HttpRouteResolver { interface Builder { - Builder setPrefix(String prefix); - Builder add(HttpRoute route, T value); - Builder add(HttpAddress httpAddress, HttpMethod httpMethod, String path, T value); + Builder add(HttpAddress httpAddress, HttpMethod httpMethod, String prefix, String path, T value); - Builder add(HttpAddress httpAddress, Set httpMethods, String path, T value); + Builder add(HttpAddress httpAddress, Set httpMethods, String prefix, String path, T value); Builder sort(boolean sort); diff --git a/net-http-server/src/test/java/org/xbib/net/http/server/route/base/BaseHttpRouteResolverTest.java b/net-http-server/src/test/java/org/xbib/net/http/server/route/base/BaseHttpRouteResolverTest.java index 4cb5856..8634861 100644 --- a/net-http-server/src/test/java/org/xbib/net/http/server/route/base/BaseHttpRouteResolverTest.java +++ b/net-http-server/src/test/java/org/xbib/net/http/server/route/base/BaseHttpRouteResolverTest.java @@ -18,9 +18,9 @@ public class BaseHttpRouteResolverTest { public void testEmptyRouteResolver() { BaseHttpRouteResolver.Builder builder = BaseHttpRouteResolver.builder(); HttpRouteResolver resolver = builder - .add(HttpAddress.http1("host"), HttpMethod.POST, "/", 1) + .add(HttpAddress.http1("host"), HttpMethod.POST, "", "/", 1) .build(); - HttpRoute route = new BaseHttpRoute(HttpAddress.http1("host"), HttpMethod.POST, ""); + HttpRoute route = new BaseHttpRoute(HttpAddress.http1("host"), HttpMethod.POST, "", ""); AtomicInteger atomicInteger = new AtomicInteger(0); resolver.resolve(route, r -> atomicInteger.incrementAndGet()); assertEquals(1, atomicInteger.get()); @@ -31,7 +31,7 @@ public class BaseHttpRouteResolverTest { BaseHttpRouteResolver.Builder builder = BaseHttpRouteResolver.builder(); HttpRouteResolver resolver = builder .build(); - HttpRoute route = new BaseHttpRoute(HttpAddress.http1("host"), HttpMethod.POST, "/"); + HttpRoute route = new BaseHttpRoute(HttpAddress.http1("host"), HttpMethod.POST, "", "/"); AtomicInteger atomicInteger = new AtomicInteger(0); resolver.resolve(route, r -> atomicInteger.incrementAndGet()); assertEquals(0, atomicInteger.get()); @@ -41,9 +41,9 @@ public class BaseHttpRouteResolverTest { public void testEmptyRouteMatchResolver() { BaseHttpRouteResolver.Builder builder = BaseHttpRouteResolver.builder(); HttpRouteResolver resolver = builder - .add(HttpAddress.http1("host"), HttpMethod.POST, "/", 1) + .add(HttpAddress.http1("host"), HttpMethod.POST, "", "/", 1) .build(); - HttpRoute route = new BaseHttpRoute(HttpAddress.http1("host"), HttpMethod.POST, "/"); + HttpRoute route = new BaseHttpRoute(HttpAddress.http1("host"), HttpMethod.POST, "", "/"); AtomicInteger atomicInteger = new AtomicInteger(0); resolver.resolve(route, r -> { assertEquals(1, r.getValue()); @@ -56,9 +56,9 @@ public class BaseHttpRouteResolverTest { public void testSingleRouteResolver() { BaseHttpRouteResolver.Builder builder = BaseHttpRouteResolver.builder(); HttpRouteResolver resolver = builder - .add(HttpAddress.http1("host"), HttpMethod.POST, "/path", 1) + .add(HttpAddress.http1("host"), HttpMethod.POST, "", "/path", 1) .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); resolver.resolve(route, r -> { assertEquals(1, r.getValue()); @@ -71,9 +71,9 @@ public class BaseHttpRouteResolverTest { public void testSingleRouteMismatchResolver() { BaseHttpRouteResolver.Builder builder = BaseHttpRouteResolver.builder(); HttpRouteResolver resolver = builder - .add(HttpAddress.http1("host"), HttpMethod.POST, "/path1", 1) + .add(HttpAddress.http1("host"), HttpMethod.POST, "", "/path1", 1) .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); resolver.resolve(route, r -> { assertEquals(1, r.getValue()); @@ -86,9 +86,9 @@ public class BaseHttpRouteResolverTest { public void testSingleRouteMismatchTooLongResolver() { BaseHttpRouteResolver.Builder builder = BaseHttpRouteResolver.builder(); HttpRouteResolver resolver = builder - .add(HttpAddress.http1("host"), HttpMethod.POST, "/a", 1) + .add(HttpAddress.http1("host"), HttpMethod.POST, "/a", "", 1) .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); resolver.resolve(route, r -> { assertEquals(1, r.getValue()); @@ -101,9 +101,9 @@ public class BaseHttpRouteResolverTest { public void testSingleRouteCatchAllResolver() { BaseHttpRouteResolver.Builder builder = BaseHttpRouteResolver.builder(); HttpRouteResolver resolver = builder - .add(HttpAddress.http1("host"), HttpMethod.POST, "/**", 1) + .add(HttpAddress.http1("host"), HttpMethod.POST, "", "/**", 1) .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); resolver.resolve(route, r -> { assertEquals(1, r.getValue()); @@ -116,9 +116,9 @@ public class BaseHttpRouteResolverTest { public void testSingleRouteCatchAllLongPathResolver() { BaseHttpRouteResolver.Builder builder = BaseHttpRouteResolver.builder(); HttpRouteResolver resolver = builder - .add(HttpAddress.http1("host"), HttpMethod.POST, "/**", 1) + .add(HttpAddress.http1("host"), HttpMethod.POST, "", "/**", 1) .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); resolver.resolve(route, r -> { assertEquals(1, r.getValue()); @@ -131,9 +131,9 @@ public class BaseHttpRouteResolverTest { public void testSingleRouteJpegResolver() { BaseHttpRouteResolver.Builder builder = BaseHttpRouteResolver.builder(); HttpRouteResolver resolver = builder - .add(HttpAddress.http1("host"), HttpMethod.POST, "glob:*.jpg", 1) + .add(HttpAddress.http1("host"), HttpMethod.POST, "", "glob:*.jpg", 1) .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); resolver.resolve(route, r -> { assertEquals(1, r.getValue()); @@ -146,9 +146,9 @@ public class BaseHttpRouteResolverTest { public void testSingleRouteGlobJpegResolver() { BaseHttpRouteResolver.Builder builder = BaseHttpRouteResolver.builder(); HttpRouteResolver resolver = builder - .add(HttpAddress.http1("host"), HttpMethod.POST, "glob:**.jpg", 1) + .add(HttpAddress.http1("host"), HttpMethod.POST, "", "glob:**.jpg", 1) .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); resolver.resolve(route, r -> { assertEquals(1, r.getValue()); @@ -161,9 +161,9 @@ public class BaseHttpRouteResolverTest { public void testSingleRouteParameterResolver() { BaseHttpRouteResolver.Builder builder = BaseHttpRouteResolver.builder(); HttpRouteResolver resolver = builder - .add(HttpAddress.http1("host"), HttpMethod.POST, "/{token}", 1) + .add(HttpAddress.http1("host"), HttpMethod.POST, "", "/{token}", 1) .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); resolver.resolve(route, r -> { assertEquals(1, r.getValue()); @@ -177,9 +177,9 @@ public class BaseHttpRouteResolverTest { public void testTwoRouteParameterResolver() { BaseHttpRouteResolver.Builder builder = BaseHttpRouteResolver.builder(); HttpRouteResolver resolver = builder - .add(HttpAddress.http1("host"), HttpMethod.POST, "/{token}/{key}", 1) + .add(HttpAddress.http1("host"), HttpMethod.POST, "", "/{token}/{key}", 1) .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); resolver.resolve(route, r -> { assertEquals(1, r.getValue()); @@ -193,11 +193,11 @@ public class BaseHttpRouteResolverTest { public void testMultiRouteResolver() { BaseHttpRouteResolver.Builder builder = BaseHttpRouteResolver.builder(); HttpRouteResolver resolver = builder - .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/c", 3) + .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/c", 3) .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); resolver.resolve(route, r -> { assertEquals(1, (int) r.getValue()); @@ -210,18 +210,16 @@ public class BaseHttpRouteResolverTest { public void testMultiRouteLongestFirstResolver() { HttpRouteResolver.Builder builder = BaseHttpRouteResolver.builder(); HttpRouteResolver resolver = builder - .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/d/e/f/g", 3) + .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/d/e/f/g", 3) .sort(true) .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); resolver.resolve(route, r -> { - switch (atomicInteger.get()) { - case 0: - assertEquals(1, (int) r.getValue()); - break; + if (atomicInteger.get() == 0) { + assertEquals(1, (int) r.getValue()); } atomicInteger.incrementAndGet(); }); @@ -232,22 +230,18 @@ public class BaseHttpRouteResolverTest { public void testMultiRouteLongestFirstWithGlobFirstResolver() { HttpRouteResolver.Builder builder = BaseHttpRouteResolver.builder(); HttpRouteResolver resolver = builder - .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/d/e/f/g", 2) - .add(HttpAddress.http1("host"), HttpMethod.POST, "glob:**", 1) + .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/d/e/f/g", 2) + .add(HttpAddress.http1("host"), HttpMethod.POST, "", "glob:**", 1) .sort(true) .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); resolver.resolve(route, r -> { switch (atomicInteger.get()) { - case 0: - assertEquals(1, (int) r.getValue()); - break; - case 1: - assertEquals(4, (int) r.getValue()); - break; + case 0 -> assertEquals(1, (int) r.getValue()); + case 1 -> assertEquals(4, (int) r.getValue()); } atomicInteger.incrementAndGet(); }); @@ -258,10 +252,9 @@ public class BaseHttpRouteResolverTest { public void testContextRouteResolver() { BaseHttpRouteResolver.Builder builder = BaseHttpRouteResolver.builder(); HttpRouteResolver resolver = builder - .setPrefix("/app") - .add(HttpAddress.http1("host"), HttpMethod.POST, "/path", 1) + .add(HttpAddress.http1("host"), HttpMethod.POST, "/app", "/path", 1) .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); resolver.resolve(route, r -> { assertEquals(1, r.getValue());