blob: 08fcbbe15114ce81db650a04952a50a9700f4fc5 [file] [log] [blame]
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../../jacoco-resources/report.gif" type="image/gif"/><title>PathMatchingFilterChainResolver.java</title><link rel="stylesheet" href="../../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../../index.html" class="el_report">Apache Shiro :: All (aggregate jar)</a> &gt; <a href="../index.html" class="el_bundle">shiro-web</a> &gt; <a href="index.source.html" class="el_package">org.apache.shiro.web.filter.mgt</a> &gt; <span class="el_source">PathMatchingFilterChainResolver.java</span></div><h1>PathMatchingFilterChainResolver.java</h1><pre class="source lang-java linenums">/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* &quot;License&quot;); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* &quot;AS IS&quot; BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.shiro.web.filter.mgt;
import org.apache.shiro.util.AntPathMatcher;
import org.apache.shiro.util.PatternMatcher;
import org.apache.shiro.web.util.WebUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
/**
* A {@code FilterChainResolver} that resolves {@link FilterChain}s based on url path
* matching, as determined by a configurable {@link #setPathMatcher(org.apache.shiro.util.PatternMatcher) PathMatcher}.
* &lt;p/&gt;
* This implementation functions by consulting a {@link org.apache.shiro.web.filter.mgt.FilterChainManager} for all configured filter chains (keyed
* by configured path pattern). If an incoming Request path matches one of the configured path patterns (via
* the {@code PathMatcher}, the corresponding configured {@code FilterChain} is returned.
*
* @since 1.0
*/
public class PathMatchingFilterChainResolver implements FilterChainResolver {
<span class="fc" id="L44"> private static transient final Logger log = LoggerFactory.getLogger(PathMatchingFilterChainResolver.class);</span>
private FilterChainManager filterChainManager;
private PatternMatcher pathMatcher;
<span class="fc" id="L50"> public PathMatchingFilterChainResolver() {</span>
<span class="fc" id="L51"> this.pathMatcher = new AntPathMatcher();</span>
<span class="fc" id="L52"> this.filterChainManager = new DefaultFilterChainManager();</span>
<span class="fc" id="L53"> }</span>
<span class="fc" id="L55"> public PathMatchingFilterChainResolver(FilterConfig filterConfig) {</span>
<span class="fc" id="L56"> this.pathMatcher = new AntPathMatcher();</span>
<span class="fc" id="L57"> this.filterChainManager = new DefaultFilterChainManager(filterConfig);</span>
<span class="fc" id="L58"> }</span>
/**
* Returns the {@code PatternMatcher} used when determining if an incoming request's path
* matches a configured filter chain. Unless overridden, the
* default implementation is an {@link org.apache.shiro.util.AntPathMatcher AntPathMatcher}.
*
* @return the {@code PatternMatcher} used when determining if an incoming request's path
* matches a configured filter chain.
*/
public PatternMatcher getPathMatcher() {
<span class="fc" id="L69"> return pathMatcher;</span>
}
/**
* Sets the {@code PatternMatcher} used when determining if an incoming request's path
* matches a configured filter chain. Unless overridden, the
* default implementation is an {@link org.apache.shiro.util.AntPathMatcher AntPathMatcher}.
*
* @param pathMatcher the {@code PatternMatcher} used when determining if an incoming request's path
* matches a configured filter chain.
*/
public void setPathMatcher(PatternMatcher pathMatcher) {
<span class="fc" id="L81"> this.pathMatcher = pathMatcher;</span>
<span class="fc" id="L82"> }</span>
public FilterChainManager getFilterChainManager() {
<span class="fc" id="L85"> return filterChainManager;</span>
}
@SuppressWarnings({&quot;UnusedDeclaration&quot;})
public void setFilterChainManager(FilterChainManager filterChainManager) {
<span class="fc" id="L90"> this.filterChainManager = filterChainManager;</span>
<span class="fc" id="L91"> }</span>
public FilterChain getChain(ServletRequest request, ServletResponse response, FilterChain originalChain) {
<span class="fc" id="L94"> FilterChainManager filterChainManager = getFilterChainManager();</span>
<span class="fc bfc" id="L95" title="All 2 branches covered."> if (!filterChainManager.hasChains()) {</span>
<span class="fc" id="L96"> return null;</span>
}
<span class="fc" id="L99"> String requestURI = getPathWithinApplication(request);</span>
//the 'chain names' in this implementation are actually path patterns defined by the user. We just use them
//as the chain name for the FilterChainManager's requirements
<span class="fc bfc" id="L103" title="All 2 branches covered."> for (String pathPattern : filterChainManager.getChainNames()) {</span>
// If the path does match, then pass on to the subclass implementation for specific checks:
<span class="fc bfc" id="L106" title="All 2 branches covered."> if (pathMatches(pathPattern, requestURI)) {</span>
<span class="pc bpc" id="L107" title="1 of 2 branches missed."> if (log.isTraceEnabled()) {</span>
<span class="fc" id="L108"> log.trace(&quot;Matched path pattern [&quot; + pathPattern + &quot;] for requestURI [&quot; + requestURI + &quot;]. &quot; +</span>
&quot;Utilizing corresponding filter chain...&quot;);
}
<span class="fc" id="L111"> return filterChainManager.proxy(originalChain, pathPattern);</span>
}
<span class="fc" id="L113"> }</span>
<span class="fc" id="L115"> return null;</span>
}
/**
* Returns {@code true} if an incoming request path (the {@code path} argument)
* matches a configured filter chain path (the {@code pattern} argument), {@code false} otherwise.
* &lt;p/&gt;
* Simply delegates to
* &lt;b&gt;&lt;code&gt;{@link #getPathMatcher() getPathMatcher()}.{@link org.apache.shiro.util.PatternMatcher#matches(String, String) matches(pattern,path)}&lt;/code&gt;&lt;/b&gt;.
* Subclass implementors should think carefully before overriding this method, as typically a custom
* {@code PathMatcher} should be configured for custom path matching behavior instead. Favor OO composition
* rather than inheritance to limit your exposure to Shiro implementation details which may change over time.
*
* @param pattern the pattern to match against
* @param path the value to match with the specified {@code pattern}
* @return {@code true} if the request {@code path} matches the specified filter chain url {@code pattern},
* {@code false} otherwise.
*/
protected boolean pathMatches(String pattern, String path) {
<span class="fc" id="L134"> PatternMatcher pathMatcher = getPathMatcher();</span>
<span class="fc" id="L135"> return pathMatcher.matches(pattern, path);</span>
}
/**
* Merely returns
* &lt;code&gt;WebUtils.{@link org.apache.shiro.web.util.WebUtils#getPathWithinApplication(javax.servlet.http.HttpServletRequest) getPathWithinApplication(request)}&lt;/code&gt;
* and can be overridden by subclasses for custom request-to-application-path resolution behavior.
*
* @param request the incoming {@code ServletRequest}
* @return the request's path within the appliation.
*/
protected String getPathWithinApplication(ServletRequest request) {
<span class="fc" id="L147"> return WebUtils.getPathWithinApplication(WebUtils.toHttp(request));</span>
}
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.eclemma.org/jacoco">JaCoCo</a> 0.7.7.201606060606</span></div></body></html>