use of net.nemerosa.ontrack.boot.support.APIDescription 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