Skip to content

Commit 6f6b824

Browse files
committed
[UNDERTOW-2303] Minor improvements to documentation / comments.
1 parent 3d7eeff commit 6f6b824

File tree

3 files changed

+43
-12
lines changed

3 files changed

+43
-12
lines changed

core/src/main/java/io/undertow/server/RoutingHandler.java

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,13 @@ public void handleRequest(final HttpServerExchange exchange) throws Exception {
223223
handleFallback(exchange);
224224
}
225225

226+
/**
227+
* Gets the builder for the specified HTTP method. A new builder will be created and added for the specified HTTP method if
228+
* one wasn't already present, otherwise the existing builder will be returned.
229+
*
230+
* @param method The HTTP method.
231+
* @return The builder.
232+
*/
226233
private PathTemplateRouterFactory.Builder<RoutingMatchBuilder, RoutingMatch> getOrAddMethodRouterBuiler(
227234
final HttpString method
228235
) {
@@ -236,6 +243,15 @@ private PathTemplateRouterFactory.Builder<RoutingMatchBuilder, RoutingMatch> get
236243
return result;
237244
}
238245

246+
/**
247+
* Gets the builder for the specified HTTP method and URL path template. A new builder will be created and added for the
248+
* specified HTTP method and URL path template if one wasn't already present, otherwise the existing builder will be
249+
* returned.
250+
*
251+
* @param method The HTTP method.
252+
* @param template The URL path template. See {@link PathTemplateParser#parseTemplate(java.lang.String, java.lang.Object) }.
253+
* @return The builder.
254+
*/
239255
private RoutingMatchBuilder getOrAddMethodRoutingMatchBuilder(
240256
final HttpString method,
241257
final String template
@@ -268,12 +284,25 @@ private Map<HttpString, PathTemplateRouter<RoutingMatch>> createMethodRouters()
268284
return Collections.unmodifiableMap(result);
269285
}
270286

287+
/**
288+
* Creates a consumer around the specified builder. The consumer checks if the builder already contains the URL path
289+
* template. If the builder does not contain the template, then the template is added to the builder. If the builder already
290+
* contains the template, then the builder is left as is to avoid the builder throwing an {@link IllegalArgumentException}.
291+
*
292+
* @param <A> Target type.
293+
* @param builder The builder
294+
* @return The consumer.
295+
*/
271296
private static <A> Consumer<PathTemplateParser.PathTemplate<A>> createAddTemplateIfAbsentConsumer(
272-
final PathTemplateRouterFactory.SimpleBuilder<Object> builder,
273-
final Supplier<Object> targetFactory
297+
final PathTemplateRouterFactory.SimpleBuilder<Object> builder
274298
) {
275299
Objects.requireNonNull(builder);
276-
Objects.requireNonNull(targetFactory);
300+
301+
/* Creates a dummy factory for targets. The specified builder is only used to determine if a matching template exists
302+
for another HTTP method. If it does exist, then an HTTP 405 is returned. If not, then an HTTP 400 is returned. The
303+
target itself is therefore never used. */
304+
final Object target = new Object();
305+
final Supplier<Object> targetFactory = () -> target;
277306

278307
return (final PathTemplateParser.PathTemplate<A> item) -> {
279308
final String template = item.getPathTemplate();
@@ -289,14 +318,14 @@ private static <A> Consumer<PathTemplateParser.PathTemplate<A>> createAddTemplat
289318
}
290319

291320
private PathTemplateRouter<Object> createAllMethodsRouter() {
292-
final Object target = new Object();
293-
final Supplier<Object> targetFactory = () -> target;
294321
final PathTemplateRouterFactory.SimpleBuilder<Object> builder = PathTemplateRouterFactory.SimpleBuilder
295-
.newBuilder(target);
322+
.newBuilder(new Object());
323+
/* Adds all known patterns from all methods to a single builder in order to distinguish between HTTP 400 and 405 when
324+
a requested method / patterns combination could not be found. */
296325
methodRouterBuilders.values().stream()
297-
.flatMap(b -> b.getTemplates().keySet().stream()) //Extract all templates for all methods into a stream
298-
.map(PathTemplateParser.PathTemplatePatternEqualsAdapter::getPattern) //Extract the patterns into a stream
299-
.forEach(createAddTemplateIfAbsentConsumer(builder, targetFactory));
326+
.flatMap(b -> b.getTemplates().keySet().stream()) //Extracts all templates for all methods into a single stream
327+
.map(PathTemplateParser.PathTemplatePatternEqualsAdapter::getPattern) //Extracts the patterns into a stream
328+
.forEach(createAddTemplateIfAbsentConsumer(builder)); //Adds patterns whilst avoiding adding duplicates
300329
return builder.build();
301330
}
302331

core/src/main/java/io/undertow/server/handlers/PathTemplateHandler.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ public String toString() {
144144
sb.append(templates.get(0).getPattern().getPathTemplate()).append(" )");
145145
} else {
146146
sb.append('{').append(
147+
// Creates a ", " separated string of all patterns in this handler.
147148
templates.stream().map(s -> s.getPattern().getPathTemplate()).collect(Collectors.joining(", "))
148149
).append("} )");
149150
}

core/src/main/java/io/undertow/util/PathTemplateRouterFactory.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,7 @@ private SimpleRouter(
824824

825825
@Override
826826
public String toString() {
827-
return "SimpleRouter{" + '}';
827+
return "SimpleRouter{}";
828828
}
829829

830830
@Override
@@ -976,12 +976,12 @@ private CompositeRouter(
976976
this.routers = Objects.requireNonNull(routers);
977977
this.len = this.routers.length;
978978
this.defaultResult = Objects.requireNonNull(defaultResult);
979-
//Find the minimum number of segments in any of the underlying routers
979+
//Finds the minimum number of segments in any of the underlying routers.
980980
this.minSegmentCount = Arrays.stream(routers)
981981
.mapToInt(AbstractRouter::getMinSegmentCount)
982982
.min()
983983
.orElse(Integer.MAX_VALUE);
984-
//Find the maximum number of segments in any of the underlying routers
984+
//Finds the maximum number of segments in any of the underlying routers.
985985
this.maxSegmentCount = Arrays.stream(routers)
986986
.mapToInt(AbstractRouter::getMaxSegmentCount)
987987
.max()
@@ -1848,6 +1848,7 @@ public PathTemplateRouter<T> build() {
18481848
}
18491849

18501850
return new RouterFactory<>(
1851+
//Extracts all the URL path templates from the PatternEqualsAdapters and collects these as a set.
18511852
templates.keySet().stream()
18521853
.map(PathTemplateParser.PathTemplatePatternEqualsAdapter::getPattern)
18531854
.collect(Collectors.toSet()),

0 commit comments

Comments
 (0)