All Downloads are FREE. Search and download functionalities are using the official Maven repository.

tech.greenfield.vertx.irked.RequestWrapper Maven / Gradle / Ivy

There is a newer version: 4.5.10
Show newest version
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 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 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 + "->?";
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy