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();
}
@Override
public String getPrefix() {
return builder.prefix;
}
@Override
public String getPathSpecification() {
return builder.pathSpec;

View file

@ -13,10 +13,12 @@ import java.util.Objects;
public class BaseHttpServiceBuilder implements HttpServiceBuilder {
protected Collection<HttpMethod> methods;
protected String prefix;
protected String pathSpec;
protected Collection<HttpMethod> methods;
protected Collection<HttpHandler> handlers;
protected Collection<ParameterDefinition> 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);

View file

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

View file

@ -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);

View file

@ -32,8 +32,8 @@ public class DecoratingHttpService implements HttpService {
}
@Override
public Collection<HttpMethod> 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<HttpMethod> getMethods() {
return delegate.getMethods();
}
@Override
public Collection<HttpHandler> getHandlers() {
return delegate.getHandlers();

View file

@ -90,6 +90,12 @@ public class BaseHttpRouteResolver<T> implements HttpRouteResolver<T> {
this.sort = false;
}
@Override
public HttpRouteResolver.Builder<T> setPrefix(String prefix) {
this.prefix = prefix;
return this;
}
@Override
public HttpRouteResolver.Builder<T> 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<T> implements HttpRouteResolver<T> {
return this;
}
public HttpRouteResolver.Builder<T> setPrefix(String prefix) {
this.prefix = prefix;
return this;
}
@Override
public BaseHttpRouteResolver<T> build() {
if (sort) {

View file

@ -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);
}
}

View file

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