All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zodiac.webui.servlet.WebUiControllerHandlerMapping Maven / Gradle / Ivy

There is a newer version: 1.6.8
Show newest version
package org.zodiac.webui.servlet;

import java.lang.reflect.Method;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.condition.PatternsRequestCondition;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import org.zodiac.commons.util.lang.Strings;
import org.zodiac.commons.util.web.Webs;
import org.zodiac.webui.WebUiController;

public class WebUiControllerHandlerMapping extends RequestMappingHandlerMapping {

    private final String webUiContextPath;

    public WebUiControllerHandlerMapping(String webUiContextPath) {
        this.webUiContextPath = webUiContextPath;
    }

    @Override
    protected Set getMappingPathPatterns(RequestMappingInfo info) {
        return super.getMappingPathPatterns(info);
    }

    @Override
    protected HandlerMethod getHandlerInternal(HttpServletRequest request) throws Exception {
        return super.getHandlerInternal(request);
    }

    @Override
    protected boolean isHandler(Class beanType) {
        return AnnotatedElementUtils.hasAnnotation(beanType, WebUiController.class);
    }

    @Override
    protected void registerHandlerMethod(Object handler, Method method, RequestMappingInfo mapping) {
        super.registerHandlerMethod(handler, method, withPrefix(mapping));
    }

    private RequestMappingInfo withPrefix(RequestMappingInfo mapping) {
        if (!Strings.hasText(webUiContextPath)) {
            return mapping;
        }
        PatternsRequestCondition patternsCondition =
            new PatternsRequestCondition(withNewPatterns(mapping.getPatternsCondition().getPatterns()));
        return new RequestMappingInfo(patternsCondition, mapping.getMethodsCondition(), mapping.getParamsCondition(),
            mapping.getHeadersCondition(), mapping.getConsumesCondition(), mapping.getProducesCondition(),
            mapping.getCustomCondition());
    }

    private String[] withNewPatterns(Set patterns) {
        // return patterns.stream().map((pattern) -> Webs.normalizePath(webUiContextPath +
        // pattern)).toArray(String[]::new);
        return patterns.stream().map((pattern) -> Webs.normalizePath(webUiContextPath, pattern)).toArray(String[]::new);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy