Search in sources :

Example 1 with BuilderConfiguration

use of org.springframework.web.servlet.mvc.method.RequestMappingInfo.BuilderConfiguration in project snow-owl by b2ihealthcare.

the class SnowOwlApiConfig method createRequestMappingHandlerMapping.

@Override
protected RequestMappingHandlerMapping createRequestMappingHandlerMapping() {
    return new RequestMappingHandlerMapping() {

        private StringValueResolver embeddedValueResolver;

        @Override
        public void setEmbeddedValueResolver(StringValueResolver resolver) {
            super.setEmbeddedValueResolver(resolver);
            this.embeddedValueResolver = resolver;
        }

        @Override
        protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) {
            RequestMappingInfo info = createRequestMappingInfo(method);
            if (info != null) {
                RequestMappingInfo typeInfo = createRequestMappingInfo(handlerType);
                if (typeInfo != null) {
                    info = typeInfo.combine(info);
                }
                String prefix = getPrefix(handlerType);
                if (prefix != null && !prefix.equals("/")) {
                    BuilderConfiguration config = new BuilderConfiguration();
                    config.setPathMatcher(getPathMatcher());
                    config.setSuffixPatternMatch(false);
                    info = RequestMappingInfo.paths(prefix).options(config).build().combine(info);
                }
            }
            return info;
        }

        @Nullable
        private RequestMappingInfo createRequestMappingInfo(AnnotatedElement element) {
            RequestMapping requestMapping = AnnotatedElementUtils.findMergedAnnotation(element, RequestMapping.class);
            RequestCondition<?> condition = (element instanceof Class ? getCustomTypeCondition((Class<?>) element) : getCustomMethodCondition((Method) element));
            return (requestMapping != null ? createRequestMappingInfo(requestMapping, condition) : null);
        }

        private String getPrefix(Class<?> handlerType) {
            for (Map.Entry<String, Predicate<Class<?>>> entry : getPathPrefixes().entrySet()) {
                if (entry.getValue().test(handlerType)) {
                    String prefix = entry.getKey();
                    if (this.embeddedValueResolver != null) {
                        prefix = this.embeddedValueResolver.resolveStringValue(prefix);
                    }
                    return prefix;
                }
            }
            return null;
        }
    };
}
Also used : BuilderConfiguration(org.springframework.web.servlet.mvc.method.RequestMappingInfo.BuilderConfiguration) RequestMappingInfo(org.springframework.web.servlet.mvc.method.RequestMappingInfo) AnnotatedElement(java.lang.reflect.AnnotatedElement) Method(java.lang.reflect.Method) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) Predicate(java.util.function.Predicate) HandlerTypePredicate(org.springframework.web.method.HandlerTypePredicate) StringValueResolver(org.springframework.util.StringValueResolver) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) ImmutableMap(com.google.common.collect.ImmutableMap)

Aggregations

ImmutableMap (com.google.common.collect.ImmutableMap)1 AnnotatedElement (java.lang.reflect.AnnotatedElement)1 Method (java.lang.reflect.Method)1 Predicate (java.util.function.Predicate)1 StringValueResolver (org.springframework.util.StringValueResolver)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 HandlerTypePredicate (org.springframework.web.method.HandlerTypePredicate)1 RequestMappingInfo (org.springframework.web.servlet.mvc.method.RequestMappingInfo)1 BuilderConfiguration (org.springframework.web.servlet.mvc.method.RequestMappingInfo.BuilderConfiguration)1 RequestMappingHandlerMapping (org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping)1