use of net.nemerosa.ontrack.boot.support.APIInfo in project ontrack by nemerosa.
the class APIController method getApiInfos.
private List<APIInfo> getApiInfos() {
List<APIInfo> apiInfos = new ArrayList<>();
Collection<Object> controllers = applicationContext.getBeansWithAnnotation(Controller.class).values();
controllers.forEach(controller -> {
APIInfo apiInfo = new APIInfo(cleanProxiedClassName(controller.getClass()), getAPIName(controller.getClass()));
// Root request mapping
RequestMapping typeAnnotation = AnnotationUtils.findAnnotation(controller.getClass(), RequestMapping.class);
// Gets all the methods
ReflectionUtils.doWithMethods(controller.getClass(), method -> {
RequestMapping methodAnnotation = AnnotationUtils.findAnnotation(method, RequestMapping.class);
if (methodAnnotation != null) {
APIMethodInfo apiMethodInfo = collectAPIMethodInfo(apiInfo, method, typeAnnotation, methodAnnotation);
apiInfo.add(apiMethodInfo);
}
}, ReflectionUtils.USER_DECLARED_METHODS);
// OK for this API
apiInfos.add(apiInfo);
});
return apiInfos;
}
use of net.nemerosa.ontrack.boot.support.APIInfo in project ontrack by nemerosa.
the class APIController method describe.
@RequestMapping(value = "/describe", method = RequestMethod.GET)
public APIDescription describe(HttpServletRequest request, @RequestParam String path) throws Exception {
HandlerExecutionChain executionChain = handlerMapping.getHandler(new HttpServletRequestWrapper(request) {
@Override
public String getRequestURI() {
return path;
}
@Override
public String getServletPath() {
return path;
}
});
// Gets the handler
Object handler = executionChain.getHandler();
if (handler instanceof HandlerMethod) {
HandlerMethod handlerMethod = (HandlerMethod) handler;
String type = handlerMethod.getBeanType().getName();
String method = handlerMethod.getMethod().getName();
// Gets all the infos
List<APIInfo> apiInfos = getApiInfos();
// Looking for any GET mapping
APIMethodInfo get = apiInfos.stream().flatMap(i -> i.getMethods().stream()).filter(mi -> StringUtils.equals(type, mi.getApiInfo().getType()) && StringUtils.equals(method, mi.getMethod())).findFirst().orElseThrow(() -> new APIMethodInfoNotFoundException(path));
// Gets all methods with the same path pattern
List<APIMethodInfo> methods = apiInfos.stream().flatMap(i -> i.getMethods().stream()).filter(mi -> StringUtils.equals(get.getPath(), mi.getPath())).collect(Collectors.toList());
// OK
return new APIDescription(path, methods);
} else {
throw new APIMethodInfoNotFoundException(path);
}
}
Aggregations