Skip to content

Commit

Permalink
[UNDERTOW-2303] Further structural improvements and improved document…
Browse files Browse the repository at this point in the history
…ation.
  • Loading branch information
dirkroets committed Jun 4, 2024
1 parent 9194944 commit 24b1643
Show file tree
Hide file tree
Showing 11 changed files with 1,233 additions and 971 deletions.
32 changes: 16 additions & 16 deletions core/src/main/java/io/undertow/server/RoutingHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
import io.undertow.util.HttpString;
import io.undertow.util.Methods;
import io.undertow.util.PathTemplateMatch;
import io.undertow.util.PathTemplatePatternEqualsAdapter;
import io.undertow.util.PathTemplateParser;
import io.undertow.util.PathTemplateRouter;
import io.undertow.util.PathTemplaterRouteResult;
import io.undertow.util.PathTemplateRouteResult;
import io.undertow.util.PathTemplateRouterFactory;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -175,7 +175,7 @@ private void handleNoMatch(
final Routers routers,
final HttpServerExchange exchange
) throws Exception {
final PathTemplaterRouteResult<Object> routeResult = routers.allMethodsRouter
final PathTemplateRouteResult<Object> routeResult = routers.allMethodsRouter
.route(exchange.getRelativePath());
if (routeResult.getPathTemplate().isPresent()) {
handlInvalidMethod(exchange);
Expand All @@ -195,7 +195,7 @@ public void handleRequest(final HttpServerExchange exchange) throws Exception {
return;
}

final PathTemplaterRouteResult<RoutingMatch> routeResult = methodRouter.route(exchange.getRelativePath());
final PathTemplateRouteResult<RoutingMatch> routeResult = methodRouter.route(exchange.getRelativePath());
if (routeResult.getPathTemplate().isEmpty()) {
handleNoMatch(localRouters, exchange);
return;
Expand Down Expand Up @@ -244,7 +244,7 @@ private RoutingMatchBuilder getOrAddMethodRoutingMatchBuilder(

final PathTemplateRouterFactory.Builder<RoutingMatchBuilder, RoutingMatch> routeBuilder
= getOrAddMethodRouterBuiler(method);
final PathTemplateRouterFactory.Template<RoutingMatchBuilder> parsedTemplate = PathTemplateRouterFactory.parseTemplate(
final PathTemplateParser.PathTemplate<RoutingMatchBuilder> parsedTemplate = PathTemplateParser.parseTemplate(
template, new RoutingMatchBuilder()
);

Expand All @@ -268,20 +268,20 @@ private Map<HttpString, PathTemplateRouter<RoutingMatch>> createMethodRouters()
return Collections.unmodifiableMap(result);
}

private static <A> Consumer<PathTemplateRouterFactory.Template<A>> createAddTemplateIfAbsentConsumer(
private static <A> Consumer<PathTemplateParser.PathTemplate<A>> createAddTemplateIfAbsentConsumer(
final PathTemplateRouterFactory.SimpleBuilder<Object> builder,
final Supplier<Object> targetFactory
) {
Objects.requireNonNull(builder);
Objects.requireNonNull(targetFactory);

return (final PathTemplateRouterFactory.Template<A> item) -> {
return (final PathTemplateParser.PathTemplate<A> item) -> {
final String template = item.getPathTemplate();
final PathTemplateRouterFactory.Template<Supplier<Object>> parsedTemplate = PathTemplateRouterFactory.parseTemplate(
final PathTemplateParser.PathTemplate<Supplier<Object>> parsedTemplate = PathTemplateParser.parseTemplate(
template, targetFactory
);
final PathTemplatePatternEqualsAdapter<PathTemplateRouterFactory.Template<Supplier<Object>>> parsedTemplatePattern
= new PathTemplatePatternEqualsAdapter<>(parsedTemplate);
final PathTemplateParser.PathTemplatePatternEqualsAdapter<PathTemplateParser.PathTemplate<Supplier<Object>>> parsedTemplatePattern
= new PathTemplateParser.PathTemplatePatternEqualsAdapter<>(parsedTemplate);
if (!builder.getTemplates().containsKey(parsedTemplatePattern)) {
builder.getTemplates().put(parsedTemplatePattern, parsedTemplate.getTarget());
}
Expand All @@ -294,8 +294,8 @@ private PathTemplateRouter<Object> createAllMethodsRouter() {
final PathTemplateRouterFactory.SimpleBuilder<Object> builder = PathTemplateRouterFactory.SimpleBuilder
.newBuilder(target);
methodRouterBuilders.values().stream()
.flatMap(b -> b.getTemplates().keySet().stream())
.map(PathTemplatePatternEqualsAdapter::getPattern)
.flatMap(b -> b.getTemplates().keySet().stream()) //Extract all templates for all methods into a stream
.map(PathTemplateParser.PathTemplatePatternEqualsAdapter::getPattern) //Extract the patterns into a stream
.forEach(createAddTemplateIfAbsentConsumer(builder, targetFactory));
return builder.build();
}
Expand Down Expand Up @@ -402,7 +402,7 @@ its configuration (templates etc) are mutated. Mutating via the original Routin
: routingHandler.methodRouterBuilders.entrySet()) {
final PathTemplateRouterFactory.Builder<RoutingMatchBuilder, RoutingMatch> builder
= getOrAddMethodRouterBuiler(outer.getKey());
for (final Entry<PathTemplatePatternEqualsAdapter<PathTemplateRouterFactory.Template<RoutingMatchBuilder>>, RoutingMatchBuilder> inner
for (final Entry<PathTemplateParser.PathTemplatePatternEqualsAdapter<PathTemplateParser.PathTemplate<RoutingMatchBuilder>>, RoutingMatchBuilder> inner
: outer.getValue().getTemplates().entrySet()) {
builder.addTemplate(
inner.getKey().getPattern().getPathTemplate(),
Expand All @@ -423,11 +423,11 @@ private boolean removeIfPresent(final HttpString method, final String path) {
return false;
}

final PathTemplateRouterFactory.Template<RoutingMatchBuilder> parsedTemplate = PathTemplateRouterFactory.parseTemplate(
final PathTemplateParser.PathTemplate<RoutingMatchBuilder> parsedTemplate = PathTemplateParser.parseTemplate(
path, noRoutingMatchBuilder
);
final PathTemplatePatternEqualsAdapter<PathTemplateRouterFactory.Template<RoutingMatchBuilder>> parsedTemplatePattern
= new PathTemplatePatternEqualsAdapter<>(parsedTemplate);
final PathTemplateParser.PathTemplatePatternEqualsAdapter<PathTemplateParser.PathTemplate<RoutingMatchBuilder>> parsedTemplatePattern
= new PathTemplateParser.PathTemplatePatternEqualsAdapter<>(parsedTemplate);

if (!builder.getTemplates().containsKey(parsedTemplatePattern)) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
import io.undertow.server.HttpHandler;
import io.undertow.server.HttpServerExchange;
import io.undertow.util.AttachmentKey;
import io.undertow.util.PathTemplatePatternEqualsAdapter;
import io.undertow.util.PathTemplateParser;
import io.undertow.util.PathTemplateRouter;
import io.undertow.util.PathTemplaterRouteResult;
import io.undertow.util.PathTemplateRouteResult;
import io.undertow.util.PathTemplateRouterFactory;
import java.util.function.Supplier;
import java.util.ArrayList;
Expand Down Expand Up @@ -135,7 +135,7 @@ public PathTemplateHandler remove(final String uriTemplate) {

@Override
public String toString() {
final List<PathTemplatePatternEqualsAdapter<PathTemplateRouterFactory.Template<Supplier<HttpHandler>>>> templates
final List<PathTemplateParser.PathTemplatePatternEqualsAdapter<PathTemplateParser.PathTemplate<Supplier<HttpHandler>>>> templates
= new ArrayList<>(builder.getBuilder().getTemplates().keySet());

final StringBuilder sb = new StringBuilder();
Expand All @@ -152,7 +152,7 @@ public String toString() {

@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
final PathTemplaterRouteResult<HttpHandler> routeResult = router.route(exchange.getRelativePath());
final PathTemplateRouteResult<HttpHandler> routeResult = router.route(exchange.getRelativePath());
if (routeResult.getPathTemplate().isEmpty()) {
// This is the default handler, therefore it doesn't contain path parameters.
routeResult.getTarget().handleRequest(exchange);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import io.undertow.server.HttpHandler;
import io.undertow.server.HttpServerExchange;
import io.undertow.util.PathTemplateRouter;
import io.undertow.util.PathTemplaterRouteResult;
import io.undertow.util.PathTemplateRouteResult;
import io.undertow.util.PathTemplateRouterFactory;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -53,7 +53,7 @@ public PathTemplateRouterHandler(

@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
final PathTemplaterRouteResult<HttpHandler> routeResult = router.route(exchange.getRelativePath());
final PathTemplateRouteResult<HttpHandler> routeResult = router.route(exchange.getRelativePath());
if (routeResult.getPathTemplate().isEmpty()) {
// This is the default handler, therefore it doesn't contain path parameters.
routeResult.getTarget().handleRequest(exchange);
Expand Down
Loading

0 comments on commit 24b1643

Please sign in to comment.