Search in sources :

Example 6 with RequestMappingInfo

use of org.springframework.web.servlet.mvc.method.RequestMappingInfo in project spring-framework by spring-projects.

the class RequestMappingHandlerMappingTests method getMappingOverridesConsumesFromTypeLevelAnnotation.

// SPR-14988
@Test
public void getMappingOverridesConsumesFromTypeLevelAnnotation() throws Exception {
    RequestMappingInfo requestMappingInfo = assertComposedAnnotationMapping(RequestMethod.GET);
    assertArrayEquals(new MediaType[] { MediaType.ALL }, new ArrayList<>(requestMappingInfo.getConsumesCondition().getConsumableMediaTypes()).toArray());
}
Also used : RequestMappingInfo(org.springframework.web.servlet.mvc.method.RequestMappingInfo) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 7 with RequestMappingInfo

use of org.springframework.web.servlet.mvc.method.RequestMappingInfo in project spring-framework by spring-projects.

the class RequestMappingHandlerMappingTests method resolveRequestMappingViaComposedAnnotation.

@Test
public void resolveRequestMappingViaComposedAnnotation() throws Exception {
    RequestMappingInfo info = assertComposedAnnotationMapping("postJson", "/postJson", RequestMethod.POST);
    assertEquals(MediaType.APPLICATION_JSON_VALUE, info.getConsumesCondition().getConsumableMediaTypes().iterator().next().toString());
    assertEquals(MediaType.APPLICATION_JSON_VALUE, info.getProducesCondition().getProducibleMediaTypes().iterator().next().toString());
}
Also used : RequestMappingInfo(org.springframework.web.servlet.mvc.method.RequestMappingInfo) Test(org.junit.Test)

Example 8 with RequestMappingInfo

use of org.springframework.web.servlet.mvc.method.RequestMappingInfo in project nikita-noark5-core by HiOA-ABI.

the class AfterApplicationStartup method afterApplicationStarts.

/**
     * afterApplicationStarts, go through list of endpoints and make a list of endpoints and
     * the HTTP methods they support.
     *
     */
public void afterApplicationStarts() {
    for (Map.Entry<RequestMappingInfo, HandlerMethod> entry : handlerMapping.getHandlerMethods().entrySet()) {
        RequestMappingInfo requestMappingInfo = entry.getKey();
        // Assuming there is always a non-null value
        String servletPaths = requestMappingInfo.getPatternsCondition().toString();
        // servletPath starts with "[" and ends with "]". Removing them if they are there
        if (true == servletPaths.startsWith("[")) {
            servletPaths = servletPaths.substring(1);
        }
        if (true == servletPaths.endsWith("]")) {
            servletPaths = servletPaths.substring(0, servletPaths.length() - 1);
        }
        String[] servletPathList = servletPaths.split("\\s+");
        for (String servletPath : servletPathList) {
            if (servletPath != null && false == servletPath.contains("|")) {
                // This is done to be consist on a lookup
                if (false == servletPath.endsWith("/")) {
                    servletPath += SLASH;
                }
                Set<RequestMethod> httpMethodRequests = requestMappingInfo.getMethodsCondition().getMethods();
                if (null != httpMethodRequests && null != servletPath) {
                    // RequestMethod and HTTPMethod are different types, have to convert them here
                    Set<HttpMethod> httpMethods = new TreeSet<>();
                    for (RequestMethod requestMethod : httpMethodRequests) {
                        if (requestMethod.equals(requestMethod.GET)) {
                            httpMethods.add(HttpMethod.GET);
                        } else if (requestMethod.equals(requestMethod.DELETE)) {
                            httpMethods.add(HttpMethod.DELETE);
                        } else if (requestMethod.equals(requestMethod.OPTIONS)) {
                            httpMethods.add(HttpMethod.OPTIONS);
                        } else if (requestMethod.equals(requestMethod.HEAD)) {
                            httpMethods.add(HttpMethod.HEAD);
                        } else if (requestMethod.equals(requestMethod.PATCH)) {
                            httpMethods.add(HttpMethod.PATCH);
                        } else if (requestMethod.equals(requestMethod.POST)) {
                            httpMethods.add(HttpMethod.POST);
                        } else if (requestMethod.equals(requestMethod.PUT)) {
                            httpMethods.add(HttpMethod.PUT);
                        } else if (requestMethod.equals(requestMethod.TRACE)) {
                            httpMethods.add(HttpMethod.TRACE);
                        }
                    }
                    out.println("Adding " + servletPath + " methods " + httpMethods);
                    CommonUtils.WebUtils.addRequestToMethodMap(servletPath, httpMethods);
                } else {
                    logger.warn("Missing HTTP Methods for the following servletPath [" + servletPath + "]");
                }
            }
        }
    }
}
Also used : RequestMappingInfo(org.springframework.web.servlet.mvc.method.RequestMappingInfo) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) TreeSet(java.util.TreeSet) Map(java.util.Map) HandlerMethod(org.springframework.web.method.HandlerMethod) HttpMethod(org.springframework.http.HttpMethod)

Example 9 with RequestMappingInfo

use of org.springframework.web.servlet.mvc.method.RequestMappingInfo 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) DhisApiVersion(org.hisp.dhis.common.DhisApiVersion) RequestMethodsRequestCondition(org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondition) RequestMappingInfo(org.springframework.web.servlet.mvc.method.RequestMappingInfo) HashSet(java.util.HashSet) Arrays(java.util.Arrays) PatternsRequestCondition(org.springframework.web.servlet.mvc.condition.PatternsRequestCondition) AnnotationUtils(org.springframework.core.annotation.AnnotationUtils) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) Set(java.util.Set) 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

RequestMappingInfo (org.springframework.web.servlet.mvc.method.RequestMappingInfo)9 RequestMethod (org.springframework.web.bind.annotation.RequestMethod)3 PatternsRequestCondition (org.springframework.web.servlet.mvc.condition.PatternsRequestCondition)3 Method (java.lang.reflect.Method)2 Map (java.util.Map)2 Test (org.junit.Test)2 HandlerMethod (org.springframework.web.method.HandlerMethod)2 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 TreeMap (java.util.TreeMap)1 TreeSet (java.util.TreeSet)1 PostConstruct (javax.annotation.PostConstruct)1 DhisApiVersion (org.hisp.dhis.common.DhisApiVersion)1 ApiVersion (org.hisp.dhis.webapi.mvc.annotation.ApiVersion)1 AnnotationUtils (org.springframework.core.annotation.AnnotationUtils)1 HttpMethod (org.springframework.http.HttpMethod)1 RequestMatchResult (org.springframework.web.servlet.handler.RequestMatchResult)1 NameValueExpression (org.springframework.web.servlet.mvc.condition.NameValueExpression)1