use of eu.okaeri.platform.web.meta.role.SimpleRouteRole 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;
}
use of eu.okaeri.platform.web.meta.role.SimpleRouteRole 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);
}
Aggregations