Search in sources :

Example 6 with URLHandler

use of org.broadleafcommerce.cms.url.domain.URLHandler in project BroadleafCommerce by BroadleafCommerce.

the class URLHandlerServiceImpl method findURLHandlerByURI.

/**
 * Checks the passed in URL to determine if there is a matching URLHandler.
 * Returns null if no handler was found.
 *
 * @param uri
 * @return
 */
@Override
public URLHandler findURLHandlerByURI(String uri) {
    // This allows clients or implementors to manipulate the URI, for example making it all lower case.
    // The default implementation simply does not manipulate the URI in any way, but simply returns
    // what is passed in.
    uri = manipulateUri(uri);
    URLHandler handler = null;
    Site site = null;
    if (BroadleafRequestContext.getBroadleafRequestContext() != null) {
        site = BroadleafRequestContext.getBroadleafRequestContext().getNonPersistentSite();
    }
    String key = buildURLHandlerCacheKey(site, uri);
    // See if this is in cache first, but only if we are in production
    if (BroadleafRequestContext.getBroadleafRequestContext().isProductionSandBox()) {
        handler = getUrlHandlerFromCache(key);
    }
    if (handler == null) {
        // Check for an exact match in the DB...
        handler = urlHandlerDao.findURLHandlerByURI(uri);
        if (handler == null) {
            // Check for a regex match
            handler = checkForMatches(uri);
        }
        if (handler == null) {
            // Use the NullURLHandler instance. This will be cached to indicate that
            // This URL does not have a match.
            handler = NULL_URL_HANDLER;
        } else if (!(URLHandlerDTO.class.isAssignableFrom(handler.getClass()))) {
            // Create a non-entity instance of the DTO to cache.
            handler = new URLHandlerDTO(handler.getNewURL(), handler.getUrlRedirectType());
        }
        if (BroadleafRequestContext.getBroadleafRequestContext().isProductionSandBox()) {
            getUrlHandlerCache().put(new Element(key, handler));
        }
    }
    if (handler instanceof NullURLHandler) {
        return null;
    }
    return handler;
}
Also used : NullURLHandler(org.broadleafcommerce.cms.url.domain.NullURLHandler) URLHandler(org.broadleafcommerce.cms.url.domain.URLHandler) Site(org.broadleafcommerce.common.site.domain.Site) URLHandlerDTO(org.broadleafcommerce.cms.url.domain.URLHandlerDTO) Element(net.sf.ehcache.Element) NullURLHandler(org.broadleafcommerce.cms.url.domain.NullURLHandler)

Example 7 with URLHandler

use of org.broadleafcommerce.cms.url.domain.URLHandler in project BroadleafCommerce by BroadleafCommerce.

the class URLHandlerServiceImpl method checkForMatches.

protected URLHandler checkForMatches(String requestURI) {
    URLHandler currentHandler = null;
    try {
        List<URLHandler> urlHandlers = findAllURLHandlers();
        for (URLHandler urlHandler : urlHandlers) {
            currentHandler = urlHandler;
            String incomingUrl = wrapStringsWithAnchors(currentHandler.getIncomingURL());
            Pattern p = urlPatternMap.get(incomingUrl);
            if (p == null) {
                p = Pattern.compile(incomingUrl);
                urlPatternMap.put(incomingUrl, p);
            }
            Matcher m = p.matcher(requestURI);
            if (m.find()) {
                String newUrl = m.replaceFirst(urlHandler.getNewURL());
                if (newUrl.equals(urlHandler.getNewURL())) {
                    return urlHandler;
                } else {
                    return new URLHandlerDTO(newUrl, urlHandler.getUrlRedirectType());
                }
            }
        }
    } catch (RuntimeException re) {
        if (currentHandler != null) {
            // We don't want an invalid regex to cause tons of logging
            if (LOG.isWarnEnabled()) {
                LOG.warn("Error parsing URL Handler (incoming =" + currentHandler.getIncomingURL() + "), outgoing = ( " + currentHandler.getNewURL() + "), " + requestURI);
            }
        }
    }
    return null;
}
Also used : NullURLHandler(org.broadleafcommerce.cms.url.domain.NullURLHandler) URLHandler(org.broadleafcommerce.cms.url.domain.URLHandler) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) URLHandlerDTO(org.broadleafcommerce.cms.url.domain.URLHandlerDTO)

Example 8 with URLHandler

use of org.broadleafcommerce.cms.url.domain.URLHandler in project BroadleafCommerce by BroadleafCommerce.

the class URLHandlerFilter method doFilterInternalUnlessIgnored.

@Override
protected void doFilterInternalUnlessIgnored(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
    String contextPath = request.getContextPath();
    String requestURIWithoutContext;
    if (request.getContextPath() != null) {
        requestURIWithoutContext = request.getRequestURI().substring(request.getContextPath().length());
    } else {
        requestURIWithoutContext = request.getRequestURI();
    }
    requestURIWithoutContext = URLDecoder.decode(requestURIWithoutContext, charEncoding);
    URLHandler handler = urlHandlerService.findURLHandlerByURI(requestURIWithoutContext);
    if (handler != null) {
        String url = UrlUtil.fixRedirectUrl(contextPath, handler.getNewURL());
        url = fixQueryString(request, url);
        extensionManager.getProxy().processPreRedirect(request, response, url);
        if (URLRedirectType.FORWARD == handler.getUrlRedirectType()) {
            request.getRequestDispatcher(handler.getNewURL()).forward(request, response);
        } else if (URLRedirectType.REDIRECT_PERM == handler.getUrlRedirectType()) {
            response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
            response.setHeader("Location", url);
            response.setHeader("Connection", "close");
        } else if (URLRedirectType.REDIRECT_TEMP == handler.getUrlRedirectType()) {
            response.sendRedirect(url);
        }
    } else {
        filterChain.doFilter(request, response);
    }
}
Also used : URLHandler(org.broadleafcommerce.cms.url.domain.URLHandler)

Example 9 with URLHandler

use of org.broadleafcommerce.cms.url.domain.URLHandler in project BroadleafCommerce by BroadleafCommerce.

the class URLHandlerServiceTest method testFoundRegExUrl.

@Test
public void testFoundRegExUrl() {
    URLHandler h = handlerService.checkForMatches("/simple_regex");
    assertTrue(h.getNewURL().equals("/NewSimpleRegex"));
}
Also used : URLHandler(org.broadleafcommerce.cms.url.domain.URLHandler) Test(org.junit.Test)

Example 10 with URLHandler

use of org.broadleafcommerce.cms.url.domain.URLHandler in project BroadleafCommerce by BroadleafCommerce.

the class URLHandlerServiceTest method testFoundBadMatchComplexUrl.

@Test
public void testFoundBadMatchComplexUrl() {
    URLHandler h = handlerService.checkForMatches("/simple_regex/test");
    assertTrue(h == null);
}
Also used : URLHandler(org.broadleafcommerce.cms.url.domain.URLHandler) Test(org.junit.Test)

Aggregations

URLHandler (org.broadleafcommerce.cms.url.domain.URLHandler)11 Test (org.junit.Test)6 NullURLHandler (org.broadleafcommerce.cms.url.domain.NullURLHandler)2 URLHandlerDTO (org.broadleafcommerce.cms.url.domain.URLHandlerDTO)2 URLHandlerImpl (org.broadleafcommerce.cms.url.domain.URLHandlerImpl)2 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 NoResultException (javax.persistence.NoResultException)1 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)1 Element (net.sf.ehcache.Element)1 Site (org.broadleafcommerce.common.site.domain.Site)1