Search in sources :

Example 1 with RequestHandlerMeta

use of eu.okaeri.platform.web.meta.RequestHandlerMeta in project okaeri-platform by OkaeriPoland.

the class RequestHandlerComponentResolver method make.

@Override
public Object make(@NonNull ComponentCreator creator, @NonNull BeanManifest manifest, @NonNull Injector injector) {
    long start = System.currentTimeMillis();
    BeanManifest parent = manifest.getParent();
    Class<?> parentClass = parent.getType();
    Method method = manifest.getMethod();
    RequestHandlerMeta handlerMeta = RequestHandlerMeta.of(parentClass, method);
    int[] contextIndexes = handlerMeta.getContextIndexes();
    Map<Integer, Object[]> prefilledCallCache = new CacheMap<>(256);
    Handler handler = context -> {
        Object[] call = null;
        try {
            call = this.getCall(prefilledCallCache, handlerMeta, context, injector);
            method.invoke(parent.getObject(), call);
            this.flushCall(call, contextIndexes, handlerMeta);
        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException exception) {
            this.logger.error("Handler (" + method + ") failure [" + Arrays.toString(call) + "]", exception);
        }
    };
    HandlerType handlerType = handlerMeta.getType();
    String handlerPath = handlerMeta.getPath();
    SimpleRouteRole[] handlerPermittedRoles = handlerMeta.getPermittedRoles();
    this.javalin.addHandler(handlerType, handlerPath, handler, handlerPermittedRoles);
    long took = System.currentTimeMillis() - start;
    creator.log(ComponentHelper.buildComponentMessage().type("Added handler").name(parentClass.getSimpleName() + "#" + method.getName()).took(took).meta("path", handlerPath).meta("type", handlerType).meta("permittedRoles", Arrays.stream(handlerPermittedRoles).map(SimpleRouteRole::getName).collect(Collectors.toList())).build());
    creator.increaseStatistics("handlers", 1);
    return handler;
}
Also used : Arrays(java.util.Arrays) Inject(eu.okaeri.injector.annotation.Inject) Logger(org.slf4j.Logger) NonNull(lombok.NonNull) RequestHandlerMeta(eu.okaeri.platform.web.meta.RequestHandlerMeta) Injector(eu.okaeri.injector.Injector) Javalin(io.javalin.Javalin) SimpleRouteRole(eu.okaeri.platform.web.meta.role.SimpleRouteRole) HandlerType(io.javalin.http.HandlerType) Collectors(java.util.stream.Collectors) InvocationTargetException(java.lang.reflect.InvocationTargetException) Handler(io.javalin.http.Handler) BeanManifest(eu.okaeri.platform.core.component.manifest.BeanManifest) ComponentHelper(eu.okaeri.platform.core.component.ComponentHelper) RequestHandlerHelper(eu.okaeri.platform.web.meta.RequestHandlerHelper) ComponentCreator(eu.okaeri.platform.core.component.creator.ComponentCreator) Context(io.javalin.http.Context) Parameter(java.lang.reflect.Parameter) OkaeriInjector(eu.okaeri.injector.OkaeriInjector) Map(java.util.Map) ComponentResolver(eu.okaeri.platform.core.component.creator.ComponentResolver) CacheMap(eu.okaeri.commons.cache.CacheMap) PathParamMeta(eu.okaeri.platform.web.meta.PathParamMeta) Method(java.lang.reflect.Method) HandlerType(io.javalin.http.HandlerType) Handler(io.javalin.http.Handler) Method(java.lang.reflect.Method) SimpleRouteRole(eu.okaeri.platform.web.meta.role.SimpleRouteRole) RequestHandlerMeta(eu.okaeri.platform.web.meta.RequestHandlerMeta) BeanManifest(eu.okaeri.platform.core.component.manifest.BeanManifest) CacheMap(eu.okaeri.commons.cache.CacheMap)

Aggregations

CacheMap (eu.okaeri.commons.cache.CacheMap)1 Injector (eu.okaeri.injector.Injector)1 OkaeriInjector (eu.okaeri.injector.OkaeriInjector)1 Inject (eu.okaeri.injector.annotation.Inject)1 ComponentHelper (eu.okaeri.platform.core.component.ComponentHelper)1 ComponentCreator (eu.okaeri.platform.core.component.creator.ComponentCreator)1 ComponentResolver (eu.okaeri.platform.core.component.creator.ComponentResolver)1 BeanManifest (eu.okaeri.platform.core.component.manifest.BeanManifest)1 PathParamMeta (eu.okaeri.platform.web.meta.PathParamMeta)1 RequestHandlerHelper (eu.okaeri.platform.web.meta.RequestHandlerHelper)1 RequestHandlerMeta (eu.okaeri.platform.web.meta.RequestHandlerMeta)1 SimpleRouteRole (eu.okaeri.platform.web.meta.role.SimpleRouteRole)1 Javalin (io.javalin.Javalin)1 Context (io.javalin.http.Context)1 Handler (io.javalin.http.Handler)1 HandlerType (io.javalin.http.HandlerType)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 Parameter (java.lang.reflect.Parameter)1 Arrays (java.util.Arrays)1