tech.greenfield.vertx.irked.RequestWrapper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of irked-vertx Show documentation
Show all versions of irked-vertx Show documentation
Opinionated framework for vertx-web route configuration and dispatch
package tech.greenfield.vertx.irked;
import java.util.Objects;
import java.util.function.Function;
import io.vertx.core.Handler;
import io.vertx.ext.web.RoutingContext;
public class RequestWrapper implements Function, Handler {
private enum Type { Root, Controller, Handler, Custom }
private Type type;
private Controller ctr;
protected Function wrapper;
private Handler super RoutingContext> handler;
public RequestWrapper(Controller ctr) {
this(ctr, Request::new);
type = Type.Root;
}
public RequestWrapper(Controller ctr, Function requestWrapper) {
this.ctr = Objects.requireNonNull(ctr, "Controller instance is not set!");
this.wrapper = requestWrapper;
type = Type.Controller;
}
public RequestWrapper(Handler super RoutingContext> handler, Function requestWrapper) {
this.handler = Objects.requireNonNull(handler, "Handler instance is not set!");
this.wrapper = requestWrapper;
type = Type.Handler;
}
/**
* Helper c'tor for extensions that want to provide their own handling
* @param parent Wrapping wrapper
*/
protected RequestWrapper(Function parent) {
wrapper = parent;
type = Type.Custom;
}
@Override
public Request apply(RoutingContext r) {
return ctr.getRequestContext(wrapper.apply(r));
}
@Override
public void handle(RoutingContext r) {
this.handler.handle(wrapper.apply(r));
}
public String toString() {
switch (type) {
case Root: return "HTTP=>" + ctr;
case Controller: return wrapper + "->" + ctr;
case Handler: return wrapper + "." + handler;
default:
case Custom: return wrapper + "->?";
}
}
}