add prefix to HttpService

This commit is contained in:
Jörg Prante 2023-03-23 09:26:15 +01:00
parent 65279dbed3
commit a78d88bc47
8 changed files with 39 additions and 11 deletions

View file

@ -25,6 +25,11 @@ public class BaseHttpService implements HttpService {
return new BaseHttpServiceBuilder(); return new BaseHttpServiceBuilder();
} }
@Override
public String getPrefix() {
return builder.prefix;
}
@Override @Override
public String getPathSpecification() { public String getPathSpecification() {
return builder.pathSpec; return builder.pathSpec;

View file

@ -13,10 +13,12 @@ import java.util.Objects;
public class BaseHttpServiceBuilder implements HttpServiceBuilder { public class BaseHttpServiceBuilder implements HttpServiceBuilder {
protected Collection<HttpMethod> methods; protected String prefix;
protected String pathSpec; protected String pathSpec;
protected Collection<HttpMethod> methods;
protected Collection<HttpHandler> handlers; protected Collection<HttpHandler> handlers;
protected Collection<ParameterDefinition> parameterDefinitions; protected Collection<ParameterDefinition> parameterDefinitions;
@ -24,19 +26,21 @@ public class BaseHttpServiceBuilder implements HttpServiceBuilder {
protected HttpSecurityDomain securityDomain; protected HttpSecurityDomain securityDomain;
protected BaseHttpServiceBuilder() { protected BaseHttpServiceBuilder() {
this.prefix = "";
this.pathSpec = "/**";
this.methods = new HashSet<>(); this.methods = new HashSet<>();
methods.add(HttpMethod.GET); methods.add(HttpMethod.GET);
this.pathSpec = "/**";
this.handlers = null; this.handlers = null;
this.securityDomain = null; this.securityDomain = null;
} }
@Override @Override
public BaseHttpServiceBuilder setMethod(HttpMethod... method) { public BaseHttpServiceBuilder setPrefix(String prefix) {
this.methods = new LinkedHashSet<>(Arrays.asList(method)); this.prefix = prefix;
return this; return this;
} }
@Override
public BaseHttpServiceBuilder setPath(String path) { public BaseHttpServiceBuilder setPath(String path) {
if (path != null) { if (path != null) {
this.pathSpec = PathNormalizer.normalize(path); this.pathSpec = PathNormalizer.normalize(path);
@ -44,6 +48,12 @@ public class BaseHttpServiceBuilder implements HttpServiceBuilder {
return this; return this;
} }
@Override
public BaseHttpServiceBuilder setMethod(HttpMethod... method) {
this.methods = new LinkedHashSet<>(Arrays.asList(method));
return this;
}
@Override @Override
public BaseHttpServiceBuilder setHandler(HttpHandler... handler) { public BaseHttpServiceBuilder setHandler(HttpHandler... handler) {
this.handlers = Arrays.asList(handler); this.handlers = Arrays.asList(handler);

View file

@ -7,6 +7,8 @@ import org.xbib.net.http.HttpMethod;
public interface HttpService extends HttpHandler { public interface HttpService extends HttpHandler {
String getPrefix();
String getPathSpecification(); String getPathSpecification();
Collection<HttpMethod> getMethods(); Collection<HttpMethod> getMethods();

View file

@ -5,6 +5,8 @@ import org.xbib.net.http.HttpMethod;
public interface HttpServiceBuilder { public interface HttpServiceBuilder {
HttpServiceBuilder setPrefix(String prefix);
HttpServiceBuilder setPath(String path); HttpServiceBuilder setPath(String path);
HttpServiceBuilder setMethod(HttpMethod... method); HttpServiceBuilder setMethod(HttpMethod... method);

View file

@ -32,8 +32,8 @@ public class DecoratingHttpService implements HttpService {
} }
@Override @Override
public Collection<HttpMethod> getMethods() { public String getPrefix() {
return delegate.getMethods(); return delegate.getPrefix();
} }
@Override @Override
@ -41,6 +41,11 @@ public class DecoratingHttpService implements HttpService {
return delegate.getPathSpecification(); return delegate.getPathSpecification();
} }
@Override
public Collection<HttpMethod> getMethods() {
return delegate.getMethods();
}
@Override @Override
public Collection<HttpHandler> getHandlers() { public Collection<HttpHandler> getHandlers() {
return delegate.getHandlers(); return delegate.getHandlers();

View file

@ -90,6 +90,12 @@ public class BaseHttpRouteResolver<T> implements HttpRouteResolver<T> {
this.sort = false; this.sort = false;
} }
@Override
public HttpRouteResolver.Builder<T> setPrefix(String prefix) {
this.prefix = prefix;
return this;
}
@Override @Override
public HttpRouteResolver.Builder<T> add(HttpAddress httpAddress, HttpMethod httpMethod, String path, T value) { public HttpRouteResolver.Builder<T> add(HttpAddress httpAddress, HttpMethod httpMethod, String path, T value) {
add(new BaseHttpRoute(httpAddress, Set.of(httpMethod), prefix + path, false), value); add(new BaseHttpRoute(httpAddress, Set.of(httpMethod), prefix + path, false), value);
@ -114,11 +120,6 @@ public class BaseHttpRouteResolver<T> implements HttpRouteResolver<T> {
return this; return this;
} }
public HttpRouteResolver.Builder<T> setPrefix(String prefix) {
this.prefix = prefix;
return this;
}
@Override @Override
public BaseHttpRouteResolver<T> build() { public BaseHttpRouteResolver<T> build() {
if (sort) { if (sort) {

View file

@ -50,6 +50,7 @@ public class BaseHttpRouter implements HttpRouter {
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.FINE, "adding " + domain.getAddress() + " " + httpService.getMethods() + " " + httpService.getPathSpecification() + " " + httpService);
HttpRoute httpRoute = new BaseHttpRoute(domain.getAddress(), httpService.getMethods(), httpService.getPathSpecification(), false); HttpRoute httpRoute = new BaseHttpRoute(domain.getAddress(), httpService.getMethods(), httpService.getPathSpecification(), false);
httpRouteResolverBuilder.setPrefix(httpService.getPrefix());
httpRouteResolverBuilder.add(httpRoute, httpService); httpRouteResolverBuilder.add(httpRoute, httpService);
} }
} }

View file

@ -13,6 +13,8 @@ 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 path, T value);