Search in sources :

Example 1 with APIDescription

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);
    }
}
Also used : API(net.nemerosa.ontrack.ui.support.API) RequestParam(org.springframework.web.bind.annotation.RequestParam) java.util(java.util) RunProfile(net.nemerosa.ontrack.common.RunProfile) HttpServletRequestWrapper(javax.servlet.http.HttpServletRequestWrapper) Autowired(org.springframework.beans.factory.annotation.Autowired) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) Controller(org.springframework.stereotype.Controller) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) StringUtils(org.apache.commons.lang3.StringUtils) HandlerMethod(org.springframework.web.method.HandlerMethod) HttpServletRequest(javax.servlet.http.HttpServletRequest) Resources(net.nemerosa.ontrack.ui.resource.Resources) NameDescription.nd(net.nemerosa.ontrack.model.structure.NameDescription.nd) APIMethodInfo(net.nemerosa.ontrack.boot.support.APIMethodInfo) Qualifier(org.springframework.beans.factory.annotation.Qualifier) Method(java.lang.reflect.Method) APIDescription(net.nemerosa.ontrack.boot.support.APIDescription) AbstractResourceController(net.nemerosa.ontrack.ui.controller.AbstractResourceController) StringUtils.capitalize(org.apache.commons.lang3.StringUtils.capitalize) MvcUriComponentsBuilder.on(org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder.on) AnnotationUtils(org.springframework.core.annotation.AnnotationUtils) NameDescription(net.nemerosa.ontrack.model.structure.NameDescription) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) APIMethodInfoNotFoundException(net.nemerosa.ontrack.model.exceptions.APIMethodInfoNotFoundException) ApplicationContext(org.springframework.context.ApplicationContext) Profile(org.springframework.context.annotation.Profile) RestController(org.springframework.web.bind.annotation.RestController) Collectors(java.util.stream.Collectors) ReflectionUtils(org.springframework.util.ReflectionUtils) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) APIInfo(net.nemerosa.ontrack.boot.support.APIInfo) APIMethod(net.nemerosa.ontrack.ui.support.APIMethod) APIInfo(net.nemerosa.ontrack.boot.support.APIInfo) APIDescription(net.nemerosa.ontrack.boot.support.APIDescription) HandlerMethod(org.springframework.web.method.HandlerMethod) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) HttpServletRequestWrapper(javax.servlet.http.HttpServletRequestWrapper) APIMethodInfoNotFoundException(net.nemerosa.ontrack.model.exceptions.APIMethodInfoNotFoundException) APIMethodInfo(net.nemerosa.ontrack.boot.support.APIMethodInfo) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

Method (java.lang.reflect.Method)1 java.util (java.util)1 Collectors (java.util.stream.Collectors)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletRequestWrapper (javax.servlet.http.HttpServletRequestWrapper)1 APIDescription (net.nemerosa.ontrack.boot.support.APIDescription)1 APIInfo (net.nemerosa.ontrack.boot.support.APIInfo)1 APIMethodInfo (net.nemerosa.ontrack.boot.support.APIMethodInfo)1 RunProfile (net.nemerosa.ontrack.common.RunProfile)1 APIMethodInfoNotFoundException (net.nemerosa.ontrack.model.exceptions.APIMethodInfoNotFoundException)1 NameDescription (net.nemerosa.ontrack.model.structure.NameDescription)1 NameDescription.nd (net.nemerosa.ontrack.model.structure.NameDescription.nd)1 AbstractResourceController (net.nemerosa.ontrack.ui.controller.AbstractResourceController)1 Resources (net.nemerosa.ontrack.ui.resource.Resources)1 API (net.nemerosa.ontrack.ui.support.API)1 APIMethod (net.nemerosa.ontrack.ui.support.APIMethod)1 StringUtils (org.apache.commons.lang3.StringUtils)1 StringUtils.capitalize (org.apache.commons.lang3.StringUtils.capitalize)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 Qualifier (org.springframework.beans.factory.annotation.Qualifier)1