From a78d88bc479d2db0a2a821b417f4d23d4bc7adc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Prante?= Date: Thu, 23 Mar 2023 09:26:15 +0100 Subject: [PATCH] add prefix to HttpService --- .../xbib/net/http/server/BaseHttpService.java | 5 +++++ .../http/server/BaseHttpServiceBuilder.java | 18 ++++++++++++++---- .../org/xbib/net/http/server/HttpService.java | 2 ++ .../net/http/server/HttpServiceBuilder.java | 2 ++ .../server/decorate/DecoratingHttpService.java | 9 +++++++-- .../server/route/BaseHttpRouteResolver.java | 11 ++++++----- .../net/http/server/route/BaseHttpRouter.java | 1 + .../http/server/route/HttpRouteResolver.java | 2 ++ 8 files changed, 39 insertions(+), 11 deletions(-) diff --git a/net-http-server/src/main/java/org/xbib/net/http/server/BaseHttpService.java b/net-http-server/src/main/java/org/xbib/net/http/server/BaseHttpService.java index 2f88d62..d9d606a 100644 --- a/net-http-server/src/main/java/org/xbib/net/http/server/BaseHttpService.java +++ b/net-http-server/src/main/java/org/xbib/net/http/server/BaseHttpService.java @@ -25,6 +25,11 @@ public class BaseHttpService implements HttpService { return new BaseHttpServiceBuilder(); } + @Override + public String getPrefix() { + return builder.prefix; + } + @Override public String getPathSpecification() { return builder.pathSpec; diff --git a/net-http-server/src/main/java/org/xbib/net/http/server/BaseHttpServiceBuilder.java b/net-http-server/src/main/java/org/xbib/net/http/server/BaseHttpServiceBuilder.java index 5fa92ad..b79542b 100644 --- a/net-http-server/src/main/java/org/xbib/net/http/server/BaseHttpServiceBuilder.java +++ b/net-http-server/src/main/java/org/xbib/net/http/server/BaseHttpServiceBuilder.java @@ -13,10 +13,12 @@ import java.util.Objects; public class BaseHttpServiceBuilder implements HttpServiceBuilder { - protected Collection methods; + protected String prefix; protected String pathSpec; + protected Collection methods; + protected Collection handlers; protected Collection parameterDefinitions; @@ -24,19 +26,21 @@ public class BaseHttpServiceBuilder implements HttpServiceBuilder { protected HttpSecurityDomain securityDomain; protected BaseHttpServiceBuilder() { + this.prefix = ""; + this.pathSpec = "/**"; this.methods = new HashSet<>(); methods.add(HttpMethod.GET); - this.pathSpec = "/**"; this.handlers = null; this.securityDomain = null; } @Override - public BaseHttpServiceBuilder setMethod(HttpMethod... method) { - this.methods = new LinkedHashSet<>(Arrays.asList(method)); + public BaseHttpServiceBuilder setPrefix(String prefix) { + this.prefix = prefix; return this; } + @Override public BaseHttpServiceBuilder setPath(String path) { if (path != null) { this.pathSpec = PathNormalizer.normalize(path); @@ -44,6 +48,12 @@ public class BaseHttpServiceBuilder implements HttpServiceBuilder { return this; } + @Override + public BaseHttpServiceBuilder setMethod(HttpMethod... method) { + this.methods = new LinkedHashSet<>(Arrays.asList(method)); + return this; + } + @Override public BaseHttpServiceBuilder setHandler(HttpHandler... handler) { this.handlers = Arrays.asList(handler); diff --git a/net-http-server/src/main/java/org/xbib/net/http/server/HttpService.java b/net-http-server/src/main/java/org/xbib/net/http/server/HttpService.java index 4149195..c074f09 100644 --- a/net-http-server/src/main/java/org/xbib/net/http/server/HttpService.java +++ b/net-http-server/src/main/java/org/xbib/net/http/server/HttpService.java @@ -7,6 +7,8 @@ import org.xbib.net.http.HttpMethod; public interface HttpService extends HttpHandler { + String getPrefix(); + String getPathSpecification(); Collection getMethods(); diff --git a/net-http-server/src/main/java/org/xbib/net/http/server/HttpServiceBuilder.java b/net-http-server/src/main/java/org/xbib/net/http/server/HttpServiceBuilder.java index 9f26226..398d49e 100644 --- a/net-http-server/src/main/java/org/xbib/net/http/server/HttpServiceBuilder.java +++ b/net-http-server/src/main/java/org/xbib/net/http/server/HttpServiceBuilder.java @@ -5,6 +5,8 @@ import org.xbib.net.http.HttpMethod; public interface HttpServiceBuilder { + HttpServiceBuilder setPrefix(String prefix); + HttpServiceBuilder setPath(String path); HttpServiceBuilder setMethod(HttpMethod... method); diff --git a/net-http-server/src/main/java/org/xbib/net/http/server/decorate/DecoratingHttpService.java b/net-http-server/src/main/java/org/xbib/net/http/server/decorate/DecoratingHttpService.java index 7d130af..aaf4105 100644 --- a/net-http-server/src/main/java/org/xbib/net/http/server/decorate/DecoratingHttpService.java +++ b/net-http-server/src/main/java/org/xbib/net/http/server/decorate/DecoratingHttpService.java @@ -32,8 +32,8 @@ public class DecoratingHttpService implements HttpService { } @Override - public Collection getMethods() { - return delegate.getMethods(); + public String getPrefix() { + return delegate.getPrefix(); } @Override @@ -41,6 +41,11 @@ public class DecoratingHttpService implements HttpService { return delegate.getPathSpecification(); } + @Override + public Collection getMethods() { + return delegate.getMethods(); + } + @Override public Collection getHandlers() { return delegate.getHandlers(); 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 b17e443..3eb566d 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 @@ -90,6 +90,12 @@ public class BaseHttpRouteResolver implements HttpRouteResolver { this.sort = false; } + @Override + public HttpRouteResolver.Builder setPrefix(String prefix) { + this.prefix = prefix; + 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); @@ -114,11 +120,6 @@ public class BaseHttpRouteResolver implements HttpRouteResolver { return this; } - public HttpRouteResolver.Builder setPrefix(String prefix) { - this.prefix = prefix; - return this; - } - @Override public BaseHttpRouteResolver build() { if (sort) { 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 bc92997..d114056 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 @@ -50,6 +50,7 @@ public class BaseHttpRouter implements HttpRouter { 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()); httpRouteResolverBuilder.add(httpRoute, httpService); } } 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 950032f..f3ce388 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,6 +13,8 @@ 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);