Skip to content

Commit cc02ea1

Browse files
authored
Merge pull request quarkusio#35917 from snazy/route-order-constants
2 parents 5199e18 + 938d9e3 commit cc02ea1

File tree

7 files changed

+126
-46
lines changed

7 files changed

+126
-46
lines changed

docs/src/main/asciidoc/http-reference.adoc

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,3 +590,30 @@ link:https://undertow.io/undertow-docs/undertow-docs-2.0.0/index.html#predicates
590590
=== web.xml
591591

592592
If you are using a `web.xml` file as your configuration file, you can place it in the `src/main/resources/META-INF` directory.
593+
594+
=== Built-in route order values
595+
596+
Route order values are the values that are specified via Vert.x route `io.vertx.ext.web.Route.order(int)` function.
597+
598+
Quarkus registers a couple of routes with specific order values.
599+
The constants are defined in the `io.quarkus.vertx.http.runtime.RouteConstants` class and listed in the table below.
600+
A custom route should define the order of value 20000 or higher so that it does not interfere with the functionality provided by Quarkus and extensions.
601+
602+
Route order constants defined in `io.quarkus.vertx.http.runtime.RouteConstants` and known extensions:
603+
604+
[cols="1,1,3"]
605+
|===
606+
| Route order value| Constant name| Origin
607+
| `Integer.MIN_VALUE` | `ROUTE_ORDER_ACCESS_LOG_HANDLER` | Access-log handler, if enabled in the configuration.
608+
| `Integer.MIN_VALUE` | `ROUTE_ORDER_RECORD_START_TIME` | Handler adding the start-time, if enabled in the configuration.
609+
| `Integer.MIN_VALUE` | `ROUTE_ORDER_HOT_REPLACEMENT` | -replacement body handler.
610+
| `Integer.MIN_VALUE` | `ROUTE_ORDER_BODY_HANDLER_MANAGEMENT` | Body handler for the management router.
611+
| `Integer.MIN_VALUE` | `ROUTE_ORDER_HEADERS` | Handlers that add headers specified in the configuration.
612+
| `Integer.MIN_VALUE` | `ROUTE_ORDER_CORS_MANAGEMENT` | CORS-Origin handler of the management router.
613+
| `Integer.MIN_VALUE + 1` | `ROUTE_ORDER_BODY_HANDLER` | Body handler.
614+
| `-2` | `ROUTE_ORDER_UPLOAD_LIMIT` | Route that enforces the upload body size limit.
615+
| `0` | `ROUTE_ORDER_COMPRESSION` | Compression handler.
616+
| `1000` | `ROUTE_ORDER_BEFORE_DEFAULT` | Route with priority over the default route (add an offset from this value).
617+
| `10000` | `ROUTE_ORDER_DEFAULT` | Default route order (i.e. Static Resources, Servlet).
618+
| `20000` | `ROUTE_ORDER_AFTER_DEFAULT` | Route without priority over the default route (add an offset from this value)
619+
|===

extensions/resteasy-classic/resteasy/deployment/src/main/java/io/quarkus/resteasy/deployment/ResteasyStandaloneBuildStep.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
import io.quarkus.vertx.http.deployment.RequireVirtualHttpBuildItem;
4242
import io.quarkus.vertx.http.deployment.RouteBuildItem;
4343
import io.quarkus.vertx.http.runtime.HttpBuildTimeConfig;
44-
import io.quarkus.vertx.http.runtime.VertxHttpRecorder;
44+
import io.quarkus.vertx.http.runtime.RouteConstants;
4545
import io.vertx.core.Handler;
4646
import io.vertx.ext.web.RoutingContext;
4747

@@ -136,7 +136,7 @@ public void boot(ShutdownContextBuildItem shutdown,
136136
routes.produce(
137137
RouteBuildItem.builder()
138138
.orderedRoute(standalone.deploymentRootPath,
139-
VertxHttpRecorder.AFTER_DEFAULT_ROUTE_ORDER_MARK + REST_ROUTE_ORDER_OFFSET)
139+
RouteConstants.ROUTE_ORDER_AFTER_DEFAULT + REST_ROUTE_ORDER_OFFSET)
140140
.handler(handler).build());
141141
String matchPath = standalone.deploymentRootPath;
142142
if (matchPath.endsWith("/")) {
@@ -146,7 +146,7 @@ public void boot(ShutdownContextBuildItem shutdown,
146146
}
147147
// Match paths that begin with the deployment path
148148
routes.produce(RouteBuildItem.builder()
149-
.orderedRoute(matchPath, VertxHttpRecorder.AFTER_DEFAULT_ROUTE_ORDER_MARK + REST_ROUTE_ORDER_OFFSET)
149+
.orderedRoute(matchPath, RouteConstants.ROUTE_ORDER_AFTER_DEFAULT + REST_ROUTE_ORDER_OFFSET)
150150
.handler(handler).build());
151151

152152
recorder.start(shutdown, requireVirtual.isPresent());

extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/main/java/io/quarkus/resteasy/reactive/server/deployment/ResteasyReactiveProcessor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@
203203
import io.quarkus.vertx.http.deployment.FilterBuildItem;
204204
import io.quarkus.vertx.http.deployment.RouteBuildItem;
205205
import io.quarkus.vertx.http.runtime.HttpBuildTimeConfig;
206-
import io.quarkus.vertx.http.runtime.VertxHttpRecorder;
206+
import io.quarkus.vertx.http.runtime.RouteConstants;
207207
import io.vertx.core.Handler;
208208
import io.vertx.core.http.HttpServerRequest;
209209
import io.vertx.core.http.HttpServerResponse;
@@ -1251,11 +1251,11 @@ public void setupDeployment(BeanContainerBuildItem beanContainerBuildItem,
12511251
.produce(new ResteasyReactiveDeploymentInfoBuildItem(deploymentInfo));
12521252

12531253
boolean servletPresent = false;
1254-
int order = VertxHttpRecorder.AFTER_DEFAULT_ROUTE_ORDER_MARK + REST_ROUTE_ORDER_OFFSET;
1254+
int order = RouteConstants.ROUTE_ORDER_AFTER_DEFAULT + REST_ROUTE_ORDER_OFFSET;
12551255
if (capabilities.isPresent("io.quarkus.servlet")) {
12561256
//if servlet is present we run RR before the default route
12571257
//otherwise we run after it
1258-
order = VertxHttpRecorder.BEFORE_DEFAULT_ROUTE_ORDER_MARK + REST_ROUTE_ORDER_OFFSET;
1258+
order = RouteConstants.ROUTE_ORDER_BEFORE_DEFAULT + REST_ROUTE_ORDER_OFFSET;
12591259
servletPresent = true;
12601260
}
12611261

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package io.quarkus.vertx.http.runtime;
2+
3+
/**
4+
* Route order value constants used in Quarkus, update {@code reactive-routes.adoc} when changing this class.
5+
*/
6+
@SuppressWarnings("JavadocDeclaration")
7+
public final class RouteConstants {
8+
private RouteConstants() {
9+
}
10+
11+
/**
12+
* Order value ({@value #ROUTE_ORDER_ACCESS_LOG_HANDLER}) for the access-log handler, if enabled in the configuration.
13+
*/
14+
public static final int ROUTE_ORDER_ACCESS_LOG_HANDLER = Integer.MIN_VALUE;
15+
/**
16+
* Order value ({@value #ROUTE_ORDER_RECORD_START_TIME}) for the handler adding the start-time, if enabled in the
17+
* configuration.
18+
*/
19+
public static final int ROUTE_ORDER_RECORD_START_TIME = Integer.MIN_VALUE;
20+
/**
21+
* Order value ({@value #ROUTE_ORDER_HOT_REPLACEMENT}) for the hot-replacement body handler.
22+
*/
23+
public static final int ROUTE_ORDER_HOT_REPLACEMENT = Integer.MIN_VALUE;
24+
/**
25+
* Order value ({@value #ROUTE_ORDER_BODY_HANDLER_MANAGEMENT}) for the body handler for the management router.
26+
*/
27+
public static final int ROUTE_ORDER_BODY_HANDLER_MANAGEMENT = Integer.MIN_VALUE;
28+
/**
29+
* Order value ({@value #ROUTE_ORDER_HEADERS}) for the handlers that add headers specified in the configuration.
30+
*/
31+
public static final int ROUTE_ORDER_HEADERS = Integer.MIN_VALUE;
32+
/**
33+
* Order value ({@value #ROUTE_ORDER_CORS_MANAGEMENT}) for the CORS-Origin handler of the management router.
34+
*/
35+
public static final int ROUTE_ORDER_CORS_MANAGEMENT = Integer.MIN_VALUE;
36+
/**
37+
* Order value ({@value #ROUTE_ORDER_BODY_HANDLER}) for the body handler.
38+
*/
39+
public static final int ROUTE_ORDER_BODY_HANDLER = Integer.MIN_VALUE + 1;
40+
/**
41+
* Order value ({@value #ROUTE_ORDER_UPLOAD_LIMIT}) for the route that enforces the upload body size limit.
42+
*/
43+
public static final int ROUTE_ORDER_UPLOAD_LIMIT = -2;
44+
/**
45+
* Order value ({@value #ROUTE_ORDER_COMPRESSION}) for the compression handler.
46+
*/
47+
public static final int ROUTE_ORDER_COMPRESSION = 0;
48+
/**
49+
* Order value ({@value #ROUTE_ORDER_BEFORE_DEFAULT}) for route with priority over the default route (add an offset from
50+
* this value)
51+
*/
52+
public static final int ROUTE_ORDER_BEFORE_DEFAULT = 1_000;
53+
/**
54+
* Default route order (i.e. Static Resources, Servlet): ({@value #ROUTE_ORDER_DEFAULT})
55+
*/
56+
public static final int ROUTE_ORDER_DEFAULT = 10_000;
57+
/**
58+
* Order value ({@value #ROUTE_ORDER_AFTER_DEFAULT}) for route without priority over the default route (add an offset
59+
* from this value)
60+
*/
61+
public static final int ROUTE_ORDER_AFTER_DEFAULT = 20_000;
62+
}

extensions/vertx-http/runtime/src/main/java/io/quarkus/vertx/http/runtime/VertxHttpRecorder.java

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -117,21 +117,6 @@ public class VertxHttpRecorder {
117117

118118
private static final String DISABLE_WEBSOCKETS_PROP_NAME = "vertx.disableWebsockets";
119119

120-
/**
121-
* Order mark for route with priority over the default route (add an offset from this mark)
122-
**/
123-
public static final int BEFORE_DEFAULT_ROUTE_ORDER_MARK = 1_000;
124-
125-
/**
126-
* Default route order (i.e. Static Resources, Servlet)
127-
**/
128-
public static final int DEFAULT_ROUTE_ORDER = 10_000;
129-
130-
/**
131-
* Order mark for route without priority over the default route (add an offset from this mark)
132-
**/
133-
public static final int AFTER_DEFAULT_ROUTE_ORDER_MARK = 20_000;
134-
135120
private static final Logger LOGGER = Logger.getLogger(VertxHttpRecorder.class.getName());
136121

137122
private static volatile Handler<RoutingContext> hotReplacementHandler;
@@ -273,7 +258,7 @@ public static void startServerAfterFailedStart() {
273258
}
274259
Router router = Router.router(vertx);
275260
if (hotReplacementHandler != null) {
276-
router.route().order(Integer.MIN_VALUE).blockingHandler(hotReplacementHandler);
261+
router.route().order(RouteConstants.ROUTE_ORDER_HOT_REPLACEMENT).blockingHandler(hotReplacementHandler);
277262
}
278263

279264
Handler<HttpServerRequest> root = router;
@@ -402,7 +387,7 @@ public void finalizeRouter(BeanContainer container, Consumer<Route> defaultRoute
402387
}
403388

404389
if (defaultRouteHandler != null) {
405-
defaultRouteHandler.accept(httpRouteRouter.route().order(DEFAULT_ROUTE_ORDER));
390+
defaultRouteHandler.accept(httpRouteRouter.route().order(RouteConstants.ROUTE_ORDER_DEFAULT));
406391
}
407392

408393
applyCompression(httpBuildTimeConfig.enableCompression, httpRouteRouter);
@@ -412,7 +397,7 @@ public void finalizeRouter(BeanContainer container, Consumer<Route> defaultRoute
412397
if (requireBodyHandler) {
413398
//if this is set then everything needs the body handler installed
414399
//TODO: config etc
415-
httpRouteRouter.route().order(Integer.MIN_VALUE + 1).handler(new Handler<RoutingContext>() {
400+
httpRouteRouter.route().order(RouteConstants.ROUTE_ORDER_BODY_HANDLER).handler(new Handler<RoutingContext>() {
416401
@Override
417402
public void handle(RoutingContext routingContext) {
418403
routingContext.request().resume();
@@ -433,13 +418,14 @@ public void handle(RoutingContext routingContext) {
433418
if (hotReplacementHandler != null) {
434419
//recorders are always executed in the current CL
435420
ClassLoader currentCl = Thread.currentThread().getContextClassLoader();
436-
httpRouteRouter.route().order(Integer.MIN_VALUE).handler(new Handler<RoutingContext>() {
437-
@Override
438-
public void handle(RoutingContext event) {
439-
Thread.currentThread().setContextClassLoader(currentCl);
440-
hotReplacementHandler.handle(event);
441-
}
442-
});
421+
httpRouteRouter.route().order(RouteConstants.ROUTE_ORDER_HOT_REPLACEMENT)
422+
.handler(new Handler<RoutingContext>() {
423+
@Override
424+
public void handle(RoutingContext event) {
425+
Thread.currentThread().setContextClassLoader(currentCl);
426+
hotReplacementHandler.handle(event);
427+
}
428+
});
443429
}
444430
root = httpRouteRouter;
445431
} else {
@@ -449,7 +435,7 @@ public void handle(RoutingContext event) {
449435

450436
if (hotReplacementHandler != null) {
451437
ClassLoader currentCl = Thread.currentThread().getContextClassLoader();
452-
mainRouter.route().order(Integer.MIN_VALUE).handler(new Handler<RoutingContext>() {
438+
mainRouter.route().order(RouteConstants.ROUTE_ORDER_HOT_REPLACEMENT).handler(new Handler<RoutingContext>() {
453439
@Override
454440
public void handle(RoutingContext event) {
455441
Thread.currentThread().setContextClassLoader(currentCl);
@@ -484,15 +470,16 @@ public void handle(RoutingContext event) {
484470
AccessLogHandler handler = new AccessLogHandler(receiver, accessLog.pattern, getClass().getClassLoader(),
485471
accessLog.excludePattern);
486472
if (rootPath.equals("/") || nonRootPath.equals("/")) {
487-
mainRouterRuntimeValue.orElse(httpRouterRuntimeValue).getValue().route().order(Integer.MIN_VALUE)
473+
mainRouterRuntimeValue.orElse(httpRouterRuntimeValue).getValue().route()
474+
.order(RouteConstants.ROUTE_ORDER_ACCESS_LOG_HANDLER)
488475
.handler(handler);
489476
} else if (nonRootPath.startsWith(rootPath)) {
490-
httpRouteRouter.route().order(Integer.MIN_VALUE).handler(handler);
477+
httpRouteRouter.route().order(RouteConstants.ROUTE_ORDER_ACCESS_LOG_HANDLER).handler(handler);
491478
} else if (rootPath.startsWith(nonRootPath)) {
492-
frameworkRouter.getValue().route().order(Integer.MIN_VALUE).handler(handler);
479+
frameworkRouter.getValue().route().order(RouteConstants.ROUTE_ORDER_ACCESS_LOG_HANDLER).handler(handler);
493480
} else {
494-
httpRouteRouter.route().order(Integer.MIN_VALUE).handler(handler);
495-
frameworkRouter.getValue().route().order(Integer.MIN_VALUE).handler(handler);
481+
httpRouteRouter.route().order(RouteConstants.ROUTE_ORDER_ACCESS_LOG_HANDLER).handler(handler);
482+
frameworkRouter.getValue().route().order(RouteConstants.ROUTE_ORDER_ACCESS_LOG_HANDLER).handler(handler);
496483
}
497484

498485
quarkusWrapperNeeded = true;
@@ -518,7 +505,7 @@ public void handle(HttpServerRequest event) {
518505
Handler<HttpServerRequest> delegate = root;
519506
root = HttpServerCommonHandlers.enforceDuplicatedContext(delegate);
520507
if (httpConfiguration.recordRequestStartTime) {
521-
httpRouteRouter.route().order(Integer.MIN_VALUE).handler(new Handler<RoutingContext>() {
508+
httpRouteRouter.route().order(RouteConstants.ROUTE_ORDER_RECORD_START_TIME).handler(new Handler<RoutingContext>() {
522509
@Override
523510
public void handle(RoutingContext event) {
524511
event.put(REQUEST_START_TIME, System.nanoTime());
@@ -539,9 +526,10 @@ public void handle(RoutingContext event) {
539526
mr.route().last().failureHandler(
540527
new QuarkusErrorHandler(launchMode.isDevOrTest(), httpConfiguration.unhandledErrorContentTypeDefault));
541528

542-
mr.route().order(Integer.MIN_VALUE).handler(createBodyHandlerForManagementInterface());
529+
mr.route().order(RouteConstants.ROUTE_ORDER_BODY_HANDLER_MANAGEMENT)
530+
.handler(createBodyHandlerForManagementInterface());
543531
// We can use "*" here as the management interface is not expected to be used publicly.
544-
mr.route().order(Integer.MIN_VALUE).handler(CorsHandler.create().addOrigin("*"));
532+
mr.route().order(RouteConstants.ROUTE_ORDER_CORS_MANAGEMENT).handler(CorsHandler.create().addOrigin("*"));
545533

546534
HttpServerCommonHandlers.applyFilters(managementConfiguration.getValue().filter, mr);
547535
for (Filter filter : managementInterfaceFilterList) {
@@ -562,7 +550,7 @@ public void handle(RoutingContext event) {
562550

563551
private void applyCompression(boolean enableCompression, Router httpRouteRouter) {
564552
if (enableCompression) {
565-
httpRouteRouter.route().order(0).handler(new Handler<RoutingContext>() {
553+
httpRouteRouter.route().order(RouteConstants.ROUTE_ORDER_COMPRESSION).handler(new Handler<RoutingContext>() {
566554
@Override
567555
public void handle(RoutingContext ctx) {
568556
// Add "Content-Encoding: identity" header that disables the compression

extensions/vertx-http/runtime/src/main/java/io/quarkus/vertx/http/runtime/options/HttpServerCommonHandlers.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import io.quarkus.vertx.http.runtime.HeaderConfig;
1717
import io.quarkus.vertx.http.runtime.ProxyConfig;
1818
import io.quarkus.vertx.http.runtime.ResumingRequestWrapper;
19+
import io.quarkus.vertx.http.runtime.RouteConstants;
1920
import io.quarkus.vertx.http.runtime.ServerLimitsConfig;
2021
import io.quarkus.vertx.http.runtime.TrustedProxyCheck;
2122
import io.quarkus.vertx.http.runtime.VertxHttpRecorder;
@@ -33,7 +34,7 @@ public static void enforceMaxBodySize(ServerLimitsConfig limits, Router httpRout
3334
if (limits.maxBodySize.isPresent()) {
3435
long limit = limits.maxBodySize.get().asLongValue();
3536
Long limitObj = limit;
36-
httpRouteRouter.route().order(-2).handler(new Handler<RoutingContext>() {
37+
httpRouteRouter.route().order(RouteConstants.ROUTE_ORDER_UPLOAD_LIMIT).handler(new Handler<RoutingContext>() {
3738
@Override
3839
public void handle(RoutingContext event) {
3940
String lengthString = event.request().headers().get(HttpHeaderNames.CONTENT_LENGTH);
@@ -150,7 +151,7 @@ public static void applyHeaders(Map<String, HeaderConfig> headers, Router httpRo
150151
var config = entry.getValue();
151152
if (config.methods.isEmpty()) {
152153
httpRouteRouter.route(config.path)
153-
.order(Integer.MIN_VALUE)
154+
.order(RouteConstants.ROUTE_ORDER_HEADERS)
154155
.handler(new Handler<RoutingContext>() {
155156
@Override
156157
public void handle(RoutingContext event) {
@@ -161,7 +162,7 @@ public void handle(RoutingContext event) {
161162
} else {
162163
for (String method : config.methods.get()) {
163164
httpRouteRouter.route(HttpMethod.valueOf(method.toUpperCase(Locale.ROOT)), config.path)
164-
.order(Integer.MIN_VALUE)
165+
.order(RouteConstants.ROUTE_ORDER_HEADERS)
165166
.handler(new Handler<RoutingContext>() {
166167
@Override
167168
public void handle(RoutingContext event) {

integration-tests/vertx-http/src/main/java/io/quarkus/it/vertx/UploadRoute.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import jakarta.enterprise.event.Observes;
77

88
import io.quarkus.runtime.StartupEvent;
9+
import io.quarkus.vertx.http.runtime.RouteConstants;
910
import io.quarkus.vertx.http.runtime.ServerLimitsConfig;
1011
import io.quarkus.vertx.http.runtime.options.HttpServerCommonHandlers;
1112
import io.vertx.core.buffer.Buffer;
@@ -21,11 +22,12 @@ public class UploadRoute {
2122

2223
/**
2324
* Installs two POST-routes - one that bypasses the body-length limit using {@code order(-3)}
24-
* ({@link HttpServerCommonHandlers#enforceMaxBodySize(ServerLimitsConfig, Router)} uses {@code order(-2)}) and one that
25+
* ({@link HttpServerCommonHandlers#enforceMaxBodySize(ServerLimitsConfig, Router)} uses
26+
* {@value RouteConstants#ROUTE_ORDER_UPLOAD_LIMIT} for {@link io.vertx.ext.web.Route#order(int)}) and one that
2527
* does not bypass body-size enforcement.
2628
*/
2729
void installRoute(@Observes StartupEvent startupEvent, Router router) {
28-
router.post("/unlimited-upload").order(-3).handler(UploadHandler::newRequest);
30+
router.post("/unlimited-upload").order(RouteConstants.ROUTE_ORDER_UPLOAD_LIMIT - 1).handler(UploadHandler::newRequest);
2931
router.post("/limited-upload").handler(UploadHandler::newRequest);
3032
}
3133

0 commit comments

Comments
 (0)