Search in sources :

Example 1 with HandlerType

use of io.javalin.http.HandlerType 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)

Example 2 with HandlerType

use of io.javalin.http.HandlerType in project BlackOnion-Bot by Black0nion.

the class Paths method handle.

@Override
public Object handle(Context ctx, JSONObject body, Map<String, String> headers, @Nullable BlackSession session, DiscordUser user) throws Exception {
    PathListener instance = PathListener.getInstance();
    Map<HandlerType, List<String>> collected = instance.handlerMetaInfoList.stream().collect(Collectors.groupingBy(HandlerMetaInfo::getHttpMethod, mapping(HandlerMetaInfo::getPath, toList())));
    return new JSONObject(collected).put("WS", new JSONArray(instance.wsHandlerMetaInfoList.stream().map(WsHandlerMetaInfo::getPath).toList()));
}
Also used : JSONObject(org.json.JSONObject) WsHandlerMetaInfo(io.javalin.core.event.WsHandlerMetaInfo) HandlerMetaInfo(io.javalin.core.event.HandlerMetaInfo) HandlerType(io.javalin.http.HandlerType) JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList)

Example 3 with HandlerType

use of io.javalin.http.HandlerType in project okaeri-platform by OkaeriPoland.

the class RequestHandlerMeta method of.

public static RequestHandlerMeta of(Class<?> parentClass, Method method) {
    Annotation[] handlers = RequestHandlerHelper.findHandlers(method);
    if (handlers.length != 1) {
        throw new IllegalArgumentException("Cannot process method with " + handlers.length + " handler annotations: " + method);
    }
    Annotation handlerAnnotation = handlers[0];
    String path = RequestHandlerHelper.readHandlerPath(handlerAnnotation);
    HandlerType type = RequestHandlerHelper.readHandlerType(handlerAnnotation);
    String[] handlerPermittedRoles = RequestHandlerHelper.readPermittedRoles(handlerAnnotation);
    Parameter[] parameters = method.getParameters();
    int[] contextIndexes = RequestHandlerHelper.readContextIndexes(parameters);
    Map<Integer, PathParamMeta> pathParams = PathParamMeta.of(parameters);
    Controller controller = parentClass.getAnnotation(Controller.class);
    String pathPrefix = controller == null ? "" : controller.path();
    String[] defaultPermittedRoles = controller == null ? new String[] { "ANYONE" } : controller.defaultPermittedRoles();
    String[] permittedRoleNames = handlerPermittedRoles.length > 0 ? handlerPermittedRoles : defaultPermittedRoles;
    SimpleRouteRole[] permittedRoles = Arrays.stream(permittedRoleNames).map(SimpleRouteRole::new).toArray(SimpleRouteRole[]::new);
    return new RequestHandlerMeta(method, pathPrefix + path, type, permittedRoles, contextIndexes, pathParams);
}
Also used : HandlerType(io.javalin.http.HandlerType) Controller(eu.okaeri.platform.web.annotation.Controller) SimpleRouteRole(eu.okaeri.platform.web.meta.role.SimpleRouteRole) Annotation(java.lang.annotation.Annotation) Parameter(java.lang.reflect.Parameter)

Aggregations

HandlerType (io.javalin.http.HandlerType)3 SimpleRouteRole (eu.okaeri.platform.web.meta.role.SimpleRouteRole)2 Parameter (java.lang.reflect.Parameter)2 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 Controller (eu.okaeri.platform.web.annotation.Controller)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 Javalin (io.javalin.Javalin)1 HandlerMetaInfo (io.javalin.core.event.HandlerMetaInfo)1 WsHandlerMetaInfo (io.javalin.core.event.WsHandlerMetaInfo)1 Context (io.javalin.http.Context)1 Handler (io.javalin.http.Handler)1