Example usage for org.springframework.web.servlet.handler SimpleUrlHandlerMapping getPathMatcher

List of usage examples for org.springframework.web.servlet.handler SimpleUrlHandlerMapping getPathMatcher

Introduction

In this page you can find the example usage for org.springframework.web.servlet.handler SimpleUrlHandlerMapping getPathMatcher.

Prototype

public PathMatcher getPathMatcher() 

Source Link

Document

Return the PathMatcher implementation to use for matching URL paths against registered URL patterns.

Usage

From source file:org.jahia.services.seo.urlrewrite.UrlRewriteService.java

public boolean prepareInbound(final HttpServletRequest request, HttpServletResponse response) {
    resetState(request);//from   w  w  w  .j av  a  2s  . c o  m
    request.setAttribute(ServerNameToSiteMapper.ATTR_NAME_CONTEXT_PATH, request.getContextPath());
    String input = request.getRequestURI();
    if (request.getContextPath().length() > 0) {
        input = StringUtils.substringAfter(input, request.getContextPath());
    }
    if (input.contains(";")) {
        input = StringUtils.substringBefore(input, ";");
    }

    String prefix = StringUtils.EMPTY;
    if (isSeoRemoveCmsPrefix() && input.length() > 1 && input.indexOf('/') == 0) {
        int end = input.indexOf('/', 1);
        prefix = end != -1 ? input.substring(1, end) : input.substring(1);
    }
    if (prefix.length() > 1) {
        boolean contains = reservedUrlPrefixSet.contains(prefix);
        request.setAttribute(ServerNameToSiteMapper.ATTR_NAME_ADD_CMS_PREFIX, !contains);
        if (contains && !"cms".equals(prefix) && !"files".equals(prefix)) {
            return false;
        }
    } else {
        request.setAttribute(ServerNameToSiteMapper.ATTR_NAME_ADD_CMS_PREFIX, false);
    }

    String path = request.getPathInfo() != null ? request.getPathInfo() : input;
    try {
        List<HandlerMapping> mappings = getRenderMapping();
        if (mappings != null) {
            for (HandlerMapping mapping : mappings) {
                if (mapping instanceof SimpleUrlHandlerMapping) {
                    SimpleUrlHandlerMapping simpleUrlHandlerMapping = (SimpleUrlHandlerMapping) mapping;
                    for (String registeredPattern : simpleUrlHandlerMapping.getUrlMap().keySet()) {
                        if (simpleUrlHandlerMapping.getPathMatcher().match(registeredPattern, path)) {
                            request.setAttribute(ServerNameToSiteMapper.ATTR_NAME_SKIP_INBOUND_SEO_RULES,
                                    Boolean.TRUE);
                            return false;
                        }
                    }
                } else {
                    HandlerExecutionChain handlerExecutionChain = mapping.getHandler(request);
                    if (handlerExecutionChain != null) {
                        // we found an execution chain for this handler, we deactivate SEO rules for this request.
                        request.setAttribute(ServerNameToSiteMapper.ATTR_NAME_SKIP_INBOUND_SEO_RULES,
                                Boolean.TRUE);
                        return false;
                    }
                }
            }
        }
    } catch (Exception ex) {
        logger.warn("Unable to load the handler mappings", ex);
    }
    String targetSiteKey = ServerNameToSiteMapper.getSiteKeyByServerName(request);
    request.setAttribute(ServerNameToSiteMapper.ATTR_NAME_VANITY_LANG, StringUtils.EMPTY);
    request.setAttribute(ServerNameToSiteMapper.ATTR_NAME_VANITY_PATH, StringUtils.EMPTY);
    if (StringUtils.isNotEmpty(targetSiteKey) && !path.startsWith("/sites")) {
        try {
            List<VanityUrl> vanityUrls = vanityUrlService.findExistingVanityUrls(path, targetSiteKey, "live");
            if (!vanityUrls.isEmpty()) {
                vanityUrls.get(0).getLanguage();
                request.setAttribute(ServerNameToSiteMapper.ATTR_NAME_VANITY_LANG,
                        vanityUrls.get(0).getLanguage());
                path = StringUtils.substringBefore(vanityUrls.get(0).getPath(),
                        "/" + VanityUrlManager.VANITYURLMAPPINGS_NODE + "/") + ".html";
                request.setAttribute(ServerNameToSiteMapper.ATTR_NAME_VANITY_PATH, path);
            }
        } catch (RepositoryException e) {
            logger.error("Cannot get vanity Url", e);
        }
    } else if (path.startsWith("/sites/")) {
        targetSiteKey = StringUtils.substringAfter(path, "/sites/");
        if (targetSiteKey.contains("/")) {
            targetSiteKey = StringUtils.substringBefore(targetSiteKey, "/");
        } else if (targetSiteKey.contains(".")) {
            // remove templateType from the url
            targetSiteKey = StringUtils.substringBeforeLast(targetSiteKey, ".");
        }
    }

    try {
        String language = siteService.getSiteDefaultLanguage(targetSiteKey);
        if (language == null) {
            // remove template from the url
            language = siteService.getSiteDefaultLanguage(StringUtils.substringBeforeLast(targetSiteKey, "."));
        }
        request.setAttribute(ServerNameToSiteMapper.ATTR_NAME_DEFAULT_LANG, language);
    } catch (JahiaException e) {
        logger.error("Cannot get site for key " + targetSiteKey, e);
    }

    return true;
}