Example usage for org.apache.shiro.web.filter.mgt NamedFilterList proxy

List of usage examples for org.apache.shiro.web.filter.mgt NamedFilterList proxy

Introduction

In this page you can find the example usage for org.apache.shiro.web.filter.mgt NamedFilterList proxy.

Prototype

FilterChain proxy(FilterChain filterChain);

Source Link

Document

Returns a new FilterChain instance that will first execute this list's Filter s (in list order) and end with the execution of the given filterChain instance.

Usage

From source file:org.tolven.shiro.web.filter.mgt.TolvenFilterChainResolver.java

License:Open Source License

@Override
public FilterChain getChain(ServletRequest servletRequest, ServletResponse servletResponse,
        FilterChain originalChain) {/*from  ww  w . j  a  va 2  s  .  co  m*/
    String requestURI = getPathWithinApplication(servletRequest);
    HttpServletRequest request = (HttpServletRequest) servletRequest;
    String urlMethod = request.getMethod();
    if (logger.isDebugEnabled()) {
        logger.debug("requestURI=" + urlMethod + " " + requestURI);
    }
    TolvenAuthorization authz = getAuthBean().getAuthorization(urlMethod, request.getContextPath(), requestURI);
    if (authz == null) {
        throw new RuntimeException(
                "authorization url cannot be found for request: " + urlMethod + " " + requestURI);
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Matched TolvenAuthorization=" + authz);
    }
    String authorizationURI = authz.getUrl();
    if (!StringUtils.hasText(authorizationURI)) {
        throw new RuntimeException(
                "authorization url cannot be null or empty for request: " + urlMethod + " " + requestURI);
    }
    String filterString = authz.getFilters();
    if (filterString == null || filterString.trim().length() == 0) {
        return originalChain;
    } else {
        DefaultFilterChainManager temporaryManager = new DefaultFilterChainManager(getFilterConfig());
        boolean init = getFilterConfig() != null; //only call filter.init if there is a FilterConfig available
        if (logger.isDebugEnabled()) {
            logger.debug("Adding filters to temporary manager");
        }
        Map<String, Filter> filters = null;
        try {
            filters = getFilters(filterString);
        } catch (Exception ex) {
            throw new RuntimeException("Failed to get chain filters for: " + request.getContextPath(), ex);
        }
        for (Entry<String, Filter> entry : filters.entrySet()) {
            temporaryManager.addFilter(entry.getKey(), entry.getValue(), init);
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Added filters to temporary manager");
        }
        try {
            temporaryManager.createChain(authorizationURI, filterString);
        } catch (Exception ex) {
            throw new RuntimeException("Could not create chain: " + authorizationURI + " for filters: "
                    + filterString + " in context: " + getFilterConfig().getServletContext().getContextPath(),
                    ex);
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Created filter chain: " + authorizationURI + " with " + filterString);
        }
        NamedFilterList chain = temporaryManager.getChain(authorizationURI);
        return chain.proxy(originalChain);
    }
}