Search in sources :

Example 1 with DhisApiVersion

use of org.hisp.dhis.common.DhisApiVersion in project dhis2-core by dhis2.

the class CustomRequestMappingHandlerMapping method getMappingForMethod.

@Override
protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) {
    RequestMappingInfo info = super.getMappingForMethod(method, handlerType);
    if (info == null) {
        return null;
    }
    ApiVersion typeApiVersion = AnnotationUtils.findAnnotation(handlerType, ApiVersion.class);
    ApiVersion methodApiVersion = AnnotationUtils.findAnnotation(method, ApiVersion.class);
    if (typeApiVersion == null && methodApiVersion == null) {
        return info;
    }
    RequestMethodsRequestCondition methodsCondition = info.getMethodsCondition();
    if (methodsCondition.getMethods().isEmpty()) {
        methodsCondition = new RequestMethodsRequestCondition(RequestMethod.GET);
    }
    Set<String> rqmPatterns = info.getPatternsCondition().getPatterns();
    Set<String> patterns = new HashSet<>();
    Set<DhisApiVersion> versions = getVersions(typeApiVersion, methodApiVersion);
    for (String pattern : rqmPatterns) {
        versions.stream().filter(version -> !version.isIgnore()).forEach(version -> {
            if (!pattern.startsWith(version.getVersionString())) {
                if (pattern.startsWith("/")) {
                    patterns.add("/" + version.getVersion() + pattern);
                } else {
                    patterns.add("/" + version.getVersion() + "/" + pattern);
                }
            } else {
                patterns.add(pattern);
            }
        });
    }
    PatternsRequestCondition patternsRequestCondition = new PatternsRequestCondition(patterns.toArray(new String[] {}), null, null, true, true, null);
    return new RequestMappingInfo(null, patternsRequestCondition, methodsCondition, info.getParamsCondition(), info.getHeadersCondition(), info.getConsumesCondition(), info.getProducesCondition(), info.getCustomCondition());
}
Also used : DhisApiVersion(org.hisp.dhis.common.DhisApiVersion) HashSet(java.util.HashSet) DhisApiVersion(org.hisp.dhis.common.DhisApiVersion) RequestMethodsRequestCondition(org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondition) RequestMappingInfo(org.springframework.web.servlet.mvc.method.RequestMappingInfo) Arrays(java.util.Arrays) PatternsRequestCondition(org.springframework.web.servlet.mvc.condition.PatternsRequestCondition) AnnotationUtils(org.springframework.core.annotation.AnnotationUtils) Set(java.util.Set) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) ApiVersion(org.hisp.dhis.webapi.mvc.annotation.ApiVersion) Method(java.lang.reflect.Method) DhisApiVersion(org.hisp.dhis.common.DhisApiVersion) ApiVersion(org.hisp.dhis.webapi.mvc.annotation.ApiVersion) PatternsRequestCondition(org.springframework.web.servlet.mvc.condition.PatternsRequestCondition) RequestMappingInfo(org.springframework.web.servlet.mvc.method.RequestMappingInfo) RequestMethodsRequestCondition(org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondition) HashSet(java.util.HashSet)

Aggregations

Method (java.lang.reflect.Method)1 Arrays (java.util.Arrays)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 DhisApiVersion (org.hisp.dhis.common.DhisApiVersion)1 ApiVersion (org.hisp.dhis.webapi.mvc.annotation.ApiVersion)1 AnnotationUtils (org.springframework.core.annotation.AnnotationUtils)1 RequestMethod (org.springframework.web.bind.annotation.RequestMethod)1 PatternsRequestCondition (org.springframework.web.servlet.mvc.condition.PatternsRequestCondition)1 RequestMethodsRequestCondition (org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondition)1 RequestMappingInfo (org.springframework.web.servlet.mvc.method.RequestMappingInfo)1 RequestMappingHandlerMapping (org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping)1