-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unlike the current one, this SPI allows declaring routes without having to depend on Quarkus Vert.x HTTP. Thus, if an extension declares a route with this SPI, and Quarkus Vert.x HTTP is not present, the route is ignored. However, if Quarkus Vert.x HTTP is present, the route is registered.
- Loading branch information
1 parent
3b727fa
commit b8edb44
Showing
7 changed files
with
329 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
230 changes: 230 additions & 0 deletions
230
...ttp/deployment-spi/src/main/java/io/quarkus/vertx/http/deployment/spi/RouteBuildItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,230 @@ | ||
package io.quarkus.vertx.http.deployment.spi; | ||
|
||
import java.util.OptionalInt; | ||
import java.util.function.Consumer; | ||
|
||
import org.eclipse.microprofile.config.Config; | ||
import org.eclipse.microprofile.config.ConfigProvider; | ||
|
||
import io.quarkus.builder.item.MultiBuildItem; | ||
import io.vertx.core.Handler; | ||
import io.vertx.ext.web.Route; | ||
import io.vertx.ext.web.RoutingContext; | ||
|
||
/** | ||
* A build item that represents a route that should be added to the router. | ||
* <p> | ||
* Producing this build item does not mean the HTTP server is available. | ||
* It will be consumed if the Quarkus Vert.x HTTP extension is present. | ||
*/ | ||
public final class RouteBuildItem extends MultiBuildItem { | ||
|
||
/** | ||
* The type of route handler | ||
*/ | ||
public enum HandlerType { | ||
|
||
/** | ||
* A regular route handler invoked on the event loop. | ||
* | ||
* @see io.vertx.ext.web.Route#handler(Handler) | ||
*/ | ||
NORMAL, | ||
/** | ||
* A blocking route handler, invoked on a worker thread. | ||
* | ||
* @see io.vertx.ext.web.Route#blockingHandler(Handler) | ||
*/ | ||
BLOCKING, | ||
/** | ||
* A failure handler, invoked when an exception is thrown from a route handler. | ||
* This is invoked on the event loop. | ||
* | ||
* @see io.vertx.ext.web.Route#failureHandler(Handler) | ||
*/ | ||
FAILURE | ||
|
||
} | ||
|
||
/** | ||
* Type of routes. | ||
*/ | ||
public enum RouteType { | ||
|
||
/** | ||
* Framework routes are provided by the Quarkus framework (or extensions). | ||
* They are not related to the application business logic, but provide a non-functional feature (health, metrics...). | ||
* <p> | ||
* Framework route can be mounted on the application router (under the non application route path) or on the management | ||
* router when enabled. | ||
*/ | ||
FRAMEWORK_ROUTE, | ||
/** | ||
* Application routes are part of the application business logic. | ||
* They are mounted on the application router (so the application prefix is applied). | ||
*/ | ||
APPLICATION_ROUTE, | ||
/** | ||
* Absolute routes are part of the application business logic, and are mounted on the root router (exposed on /). | ||
*/ | ||
ABSOLUTE_ROUTE | ||
} | ||
|
||
private RouteType typeOfRoute = RouteType.APPLICATION_ROUTE; | ||
@SuppressWarnings("OptionalUsedAsFieldOrParameterType") | ||
private OptionalInt order = OptionalInt.empty(); | ||
|
||
private String path; | ||
private Consumer<Route> customizer; | ||
|
||
private boolean isManagement; | ||
|
||
private Handler<RoutingContext> handler; | ||
|
||
private HandlerType typeOfHandler = HandlerType.NORMAL; | ||
|
||
private boolean displayOnNotFoundPage; | ||
private String notFoundPageTitle; | ||
|
||
private String routeConfigKey; | ||
|
||
public RouteType getTypeOfRoute() { | ||
return typeOfRoute; | ||
} | ||
|
||
public boolean hasOrder() { | ||
return order.isPresent(); | ||
} | ||
|
||
public int getOrder() { | ||
if (order.isPresent()) { | ||
return order.getAsInt(); | ||
} else { | ||
throw new IllegalStateException("No order set"); | ||
} | ||
} | ||
|
||
public boolean hasRouteConfigKey() { | ||
return routeConfigKey != null; | ||
} | ||
|
||
public String getRouteConfigKey() { | ||
return routeConfigKey; | ||
} | ||
|
||
public Handler<RoutingContext> getHandler() { | ||
return handler; | ||
} | ||
|
||
public HandlerType getHandlerType() { | ||
return typeOfHandler; | ||
} | ||
|
||
public String getPath() { | ||
return path; | ||
} | ||
|
||
public Consumer<Route> getCustomizer() { | ||
return customizer; | ||
} | ||
|
||
public String getNotFoundPageTitle() { | ||
return notFoundPageTitle; | ||
} | ||
|
||
public boolean isDisplayOnNotFoundPage() { | ||
return displayOnNotFoundPage; | ||
} | ||
|
||
public static Builder newApplicationRoute(String path) { | ||
return new Builder(RouteType.APPLICATION_ROUTE, path, false); | ||
} | ||
|
||
public static Builder newAbsoluteRoute(String path) { | ||
return new Builder(RouteType.ABSOLUTE_ROUTE, path, false); | ||
} | ||
|
||
public static Builder newFrameworkRoute(String path) { | ||
return new Builder(RouteType.FRAMEWORK_ROUTE, path, false); | ||
} | ||
|
||
public static Builder newManagementRoute(String path) { | ||
return new Builder(RouteType.FRAMEWORK_ROUTE, path, true); | ||
} | ||
|
||
public static Builder newManagementRoute(String path, String managementConfigKey) { | ||
return new Builder(RouteType.FRAMEWORK_ROUTE, path, | ||
(managementConfigKey == null || shouldInclude(managementConfigKey))); | ||
} | ||
|
||
private static boolean shouldInclude(String managementConfigKey) { | ||
Config config = ConfigProvider.getConfig(); | ||
return config.getValue(managementConfigKey, boolean.class); | ||
} | ||
|
||
public boolean isManagement() { | ||
return isManagement; | ||
} | ||
|
||
public static class Builder { | ||
|
||
private final RouteBuildItem item; | ||
|
||
private Builder(RouteType type, String path, boolean isManagement) { | ||
item = new RouteBuildItem(); | ||
item.typeOfRoute = type; | ||
item.path = path; | ||
item.isManagement = isManagement; | ||
} | ||
|
||
public Builder withRouteCustomizer(Consumer<Route> customizer) { | ||
item.customizer = customizer; | ||
return this; | ||
} | ||
|
||
public Builder withOrder(int order) { | ||
item.order = OptionalInt.of(order); | ||
return this; | ||
} | ||
|
||
public Builder withRequestHandler(Handler<RoutingContext> handler) { | ||
item.handler = handler; | ||
return this; | ||
} | ||
|
||
public Builder asBlockingRoute() { | ||
item.typeOfHandler = HandlerType.BLOCKING; | ||
return this; | ||
} | ||
|
||
public Builder failureRoute() { | ||
item.typeOfHandler = HandlerType.FAILURE; | ||
return this; | ||
} | ||
|
||
public Builder displayOnNotFoundPage() { | ||
item.displayOnNotFoundPage = true; | ||
return this; | ||
} | ||
|
||
public Builder displayOnNotFoundPage(String notFoundPageTitle) { | ||
item.displayOnNotFoundPage = true; | ||
item.notFoundPageTitle = notFoundPageTitle; | ||
return this; | ||
} | ||
|
||
public Builder withRoutePathConfigKey(String attributeName) { | ||
item.routeConfigKey = attributeName; | ||
return this; | ||
} | ||
|
||
public RouteBuildItem build() { | ||
if (item.handler == null) { | ||
throw new IllegalArgumentException("The route handler must be set"); | ||
} | ||
|
||
return item; | ||
} | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
.../vertx-http/deployment/src/main/java/io/quarkus/vertx/http/deployment/RouteConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package io.quarkus.vertx.http.deployment; | ||
|
||
import io.quarkus.vertx.http.runtime.HandlerType; | ||
|
||
/** | ||
* Convert the route build item from the SPI to the internal representation | ||
*/ | ||
public class RouteConverter { | ||
|
||
public static RouteBuildItem convert(io.quarkus.vertx.http.deployment.spi.RouteBuildItem item, | ||
HttpRootPathBuildItem httpRootPathBuildItem, | ||
NonApplicationRootPathBuildItem nonApplicationRootPathBuildItem) { | ||
// The builder depends on the type of route | ||
RouteBuildItem.Builder builder; | ||
if (item.getTypeOfRoute() == io.quarkus.vertx.http.deployment.spi.RouteBuildItem.RouteType.FRAMEWORK_ROUTE) { | ||
builder = nonApplicationRootPathBuildItem.routeBuilder(); | ||
} else { | ||
builder = httpRootPathBuildItem.routeBuilder(); | ||
} | ||
|
||
if (item.isManagement()) { | ||
builder = builder.management(); | ||
} | ||
if (item.hasRouteConfigKey()) { | ||
builder = builder.routeConfigKey(item.getRouteConfigKey()); | ||
} | ||
|
||
builder = builder.handler(item.getHandler()).handlerType(HandlerType.valueOf(item.getHandlerType().name())); | ||
if (item.isDisplayOnNotFoundPage()) { | ||
builder = builder | ||
.displayOnNotFoundPage(item.getNotFoundPageTitle()); | ||
} | ||
|
||
if (item.hasOrder()) { | ||
builder = builder.orderedRoute(item.getPath(), item.getOrder(), item.getCustomizer()); | ||
} else { | ||
builder = builder.routeFunction(item.getPath(), item.getCustomizer()); | ||
} | ||
|
||
return builder.build(); | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.