co.easimart.vertx.http.MultipleHandlersRouter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of vertx-util Show documentation
Show all versions of vertx-util Show documentation
Library provides utility classes for Vertx project development.
The newest version!
package co.easimart.vertx.http;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import io.vertx.core.Handler;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.ext.web.Route;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.impl.BlockingHandlerDecorator;
/**
* Util class which wraps vertx Router and delays the Route creation until the first handler is set
* to supports multiple handlers for the same route spec and preset handlers.
*/
public class MultipleHandlersRouter {
private final Router router;
private final List> presetHandlers;
public MultipleHandlersRouter(Router router) {
this.router = router;
this.presetHandlers = new LinkedList<>();
}
public void accept(HttpServerRequest request) {
router.accept(request);
}
public Route route() {
return new RouteImpl(router, presetHandlers);
}
public Route route(HttpMethod method, String path) {
return new RouteImpl(router, presetHandlers, method, path);
}
public Route route(String path) {
return new RouteImpl(router, presetHandlers, path);
}
public Route routeWithRegex(HttpMethod method, String regex) {
return new RouteImpl(router, presetHandlers, method, regex, true);
}
public Route routeWithRegex(String regex) {
return new RouteImpl(router, presetHandlers, regex, true);
}
public Route get() {
return this.route().method(HttpMethod.GET);
}
public Route get(String path) {
return this.route(HttpMethod.GET, path);
}
public Route getWithRegex(String path) {
return this.route().method(HttpMethod.GET).pathRegex(path);
}
public Route head() {
return this.route().method(HttpMethod.HEAD);
}
public Route head(String path) {
return this.route(HttpMethod.HEAD, path);
}
public Route headWithRegex(String path) {
return this.route().method(HttpMethod.HEAD).pathRegex(path);
}
public Route options() {
return this.route().method(HttpMethod.OPTIONS);
}
public Route options(String path) {
return this.route(HttpMethod.OPTIONS, path);
}
public Route optionsWithRegex(String path) {
return this.route().method(HttpMethod.OPTIONS).pathRegex(path);
}
public Route put() {
return this.route().method(HttpMethod.PUT);
}
public Route put(String path) {
return this.route(HttpMethod.PUT, path);
}
public Route putWithRegex(String path) {
return this.route().method(HttpMethod.PUT).pathRegex(path);
}
public Route post() {
return this.route().method(HttpMethod.POST);
}
public Route post(String path) {
return this.route(HttpMethod.POST, path);
}
public Route postWithRegex(String path) {
return this.route().method(HttpMethod.POST).pathRegex(path);
}
public Route delete() {
return this.route().method(HttpMethod.DELETE);
}
public Route delete(String path) {
return this.route(HttpMethod.DELETE, path);
}
public Route deleteWithRegex(String path) {
return this.route().method(HttpMethod.DELETE).pathRegex(path);
}
public Route trace() {
return this.route().method(HttpMethod.TRACE);
}
public Route trace(String path) {
return this.route(HttpMethod.TRACE, path);
}
public Route traceWithRegex(String path) {
return this.route().method(HttpMethod.TRACE).pathRegex(path);
}
public Route connect() {
return this.route().method(HttpMethod.CONNECT);
}
public Route connect(String path) {
return this.route(HttpMethod.CONNECT, path);
}
public Route connectWithRegex(String path) {
return this.route().method(HttpMethod.CONNECT).pathRegex(path);
}
public Route patch() {
return this.route().method(HttpMethod.PATCH);
}
public Route patch(String path) {
return this.route(HttpMethod.PATCH, path);
}
public Route patchWithRegex(String path) {
return this.route().method(HttpMethod.PATCH).pathRegex(path);
}
public List getRoutes() {
return router.getRoutes();
}
public void handleContext(RoutingContext ctx) {
router.handleContext(ctx);
}
public void handleFailure(RoutingContext ctx) {
router.handleFailure(ctx);
}
public synchronized Router exceptionHandler(Handler exceptionHandler) {
return router.exceptionHandler(exceptionHandler);
}
public MultipleHandlersRouter presetHandler(Handler handler) {
this.presetHandlers.add(handler);
return this;
}
public List> getPresetHandlers() {
return this.presetHandlers;
}
private static class RouteImpl implements Route {
private final Router router;
private final List> presetHandler;
private final Set methods;
private final Set consumes;
private final Set produces;
private String path;
private Boolean enabled;
private String regex;
private Integer order;
private Boolean useNormalisedPath;
private Route latestRoute;
RouteImpl(Router router, List> presetHandler) {
this.presetHandler = presetHandler;
this.methods = new HashSet<>();
this.consumes = new HashSet<>();
this.produces = new HashSet<>();
this.path = null;
this.enabled = null;
this.regex = null;
this.order = null;
this.useNormalisedPath = null;
this.router = router;
}
RouteImpl(Router router, List> presetHandler, HttpMethod method, String path) {
this(router, presetHandler);
this.methods.add(method);
this.path = path;
}
public RouteImpl(Router router, List> presetHandler, String path) {
this(router, presetHandler);
this.path = path;
}
public RouteImpl(Router router, List> presetHandler, HttpMethod method, String regex, boolean bregex) {
this(router, presetHandler);
this.methods.add(method);
this.regex = regex;
}
public RouteImpl(Router router, List> presetHandler, String regex, boolean bregex) {
this(router, presetHandler);
this.regex = regex;
}
private Route createNewRoute() {
Route r = this.router.route();
methods.forEach(r::method);
consumes.forEach(r::consumes);
produces.forEach(r::produces);
if (path != null) r.path(path);
if (enabled != null) {
if (enabled) r.enable();
else r.disable();
}
if (regex != null) r.pathRegex(regex);
if (order != null) r.order(order);
if (useNormalisedPath != null) r.useNormalisedPath(useNormalisedPath);
return r;
}
public synchronized Route method(HttpMethod method) {
this.methods.add(method);
return this;
}
public synchronized Route path(String path) {
this.path = path;
return this;
}
public synchronized Route pathRegex(String regex) {
this.regex = regex;
return this;
}
public synchronized Route produces(String contentType) {
this.produces.add(contentType);
return this;
}
public synchronized Route consumes(String contentType) {
this.consumes.add(contentType);
return this;
}
public synchronized Route order(int order) {
this.order = order;
return this;
}
@Override
public synchronized Route last() {
return this.order(2147483647);
}
public synchronized Route handler(Handler contextHandler) {
for (Handler h : presetHandler) {
createNewRoute().handler(h);
}
this.latestRoute = createNewRoute().handler(contextHandler);
return this;
}
public Route blockingHandler(Handler contextHandler) {
return this.blockingHandler(contextHandler, true);
}
public synchronized Route blockingHandler(Handler contextHandler, boolean ordered) {
return this.handler(new BlockingHandlerDecorator(contextHandler, ordered));
}
public synchronized Route failureHandler(Handler exceptionHandler) {
this.latestRoute = createNewRoute().handler(exceptionHandler);
return this;
}
public synchronized Route remove() {
throw new IllegalStateException("MultipleHandlersRoute does not supports remove()");
}
public synchronized Route disable() {
this.enabled = false;
return this;
}
public synchronized Route enable() {
this.enabled = true;
return this;
}
public Route useNormalisedPath(boolean useNormalisedPath) {
this.useNormalisedPath = useNormalisedPath;
return this;
}
public String getPath() {
return this.path;
}
@Override
public Route setRegexGroupsNames(List groups) {
throw new RuntimeException("not implemented");
}
public Route getLatestRoute() {
return this.latestRoute;
}
}
}