blob: 67df9b9e3ed2cb1a5281d3419aaded80d6c400af [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>AdviceFilter.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 :: Test Coverage</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.servlet</a> &gt; <span class="el_source">AdviceFilter.java</span></div><h1>AdviceFilter.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.servlet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;
/**
* A Servlet Filter that enables AOP-style &amp;quot;around&amp;quot; advice for a ServletRequest via
* {@link #preHandle(javax.servlet.ServletRequest, javax.servlet.ServletResponse) preHandle},
* {@link #postHandle(javax.servlet.ServletRequest, javax.servlet.ServletResponse) postHandle},
* and {@link #afterCompletion(javax.servlet.ServletRequest, javax.servlet.ServletResponse, Exception) afterCompletion}
* hooks.
*
* @since 0.9
*/
<span class="fc" id="L39">public abstract class AdviceFilter extends OncePerRequestFilter {</span>
/**
* The static logger available to this class only
*/
<span class="fc" id="L44"> private static final Logger log = LoggerFactory.getLogger(AdviceFilter.class);</span>
/**
* Returns {@code true} if the filter chain should be allowed to continue, {@code false} otherwise.
* It is called before the chain is actually consulted/executed.
* &lt;p/&gt;
* The default implementation returns {@code true} always and exists as a template method for subclasses.
*
* @param request the incoming ServletRequest
* @param response the outgoing ServletResponse
* @return {@code true} if the filter chain should be allowed to continue, {@code false} otherwise.
* @throws Exception if there is any error.
*/
protected boolean preHandle(ServletRequest request, ServletResponse response) throws Exception {
<span class="nc" id="L58"> return true;</span>
}
/**
* Allows 'post' advice logic to be called, but only if no exception occurs during filter chain execution. That
* is, if {@link #executeChain executeChain} throws an exception, this method will never be called. Be aware of
* this when implementing logic. Most resource 'cleanup' behavior is often done in the
* {@link #afterCompletion(javax.servlet.ServletRequest, javax.servlet.ServletResponse, Exception) afterCompletion(request,response,exception)}
* implementation, which is guaranteed to be called for every request, even when the chain processing throws
* an Exception.
* &lt;p/&gt;
* The default implementation does nothing (no-op) and exists as a template method for subclasses.
*
* @param request the incoming ServletRequest
* @param response the outgoing ServletResponse
* @throws Exception if an error occurs.
*/
@SuppressWarnings({&quot;UnusedDeclaration&quot;})
protected void postHandle(ServletRequest request, ServletResponse response) throws Exception {
<span class="fc" id="L77"> }</span>
/**
* Called in all cases in a {@code finally} block even if {@link #preHandle preHandle} returns
* {@code false} or if an exception is thrown during filter chain processing. Can be used for resource
* cleanup if so desired.
* &lt;p/&gt;
* The default implementation does nothing (no-op) and exists as a template method for subclasses.
*
* @param request the incoming ServletRequest
* @param response the outgoing ServletResponse
* @param exception any exception thrown during {@link #preHandle preHandle}, {@link #executeChain executeChain},
* or {@link #postHandle postHandle} execution, or {@code null} if no exception was thrown
* (i.e. the chain processed successfully).
* @throws Exception if an error occurs.
*/
@SuppressWarnings({&quot;UnusedDeclaration&quot;})
public void afterCompletion(ServletRequest request, ServletResponse response, Exception exception) throws Exception {
<span class="fc" id="L95"> }</span>
/**
* Actually executes the specified filter chain by calling &lt;code&gt;chain.doFilter(request,response);&lt;/code&gt;.
* &lt;p/&gt;
* Can be overridden by subclasses for custom logic.
*
* @param request the incoming ServletRequest
* @param response the outgoing ServletResponse
* @param chain the filter chain to execute
* @throws Exception if there is any error executing the chain.
*/
protected void executeChain(ServletRequest request, ServletResponse response, FilterChain chain) throws Exception {
<span class="fc" id="L108"> chain.doFilter(request, response);</span>
<span class="fc" id="L109"> }</span>
/**
* Actually implements the chain execution logic, utilizing
* {@link #preHandle(javax.servlet.ServletRequest, javax.servlet.ServletResponse) pre},
* {@link #postHandle(javax.servlet.ServletRequest, javax.servlet.ServletResponse) post}, and
* {@link #afterCompletion(javax.servlet.ServletRequest, javax.servlet.ServletResponse, Exception) after}
* advice hooks.
*
* @param request the incoming ServletRequest
* @param response the outgoing ServletResponse
* @param chain the filter chain to execute
* @throws ServletException if a servlet-related error occurs
* @throws IOException if an IO error occurs
*/
public void doFilterInternal(ServletRequest request, ServletResponse response, FilterChain chain)
throws ServletException, IOException {
<span class="fc" id="L127"> Exception exception = null;</span>
try {
<span class="fc" id="L131"> boolean continueChain = preHandle(request, response);</span>
<span class="fc bfc" id="L132" title="All 2 branches covered."> if (log.isTraceEnabled()) {</span>
<span class="fc" id="L133"> log.trace(&quot;Invoked preHandle method. Continuing chain?: [&quot; + continueChain + &quot;]&quot;);</span>
}
<span class="fc bfc" id="L136" title="All 2 branches covered."> if (continueChain) {</span>
<span class="fc" id="L137"> executeChain(request, response, chain);</span>
}
<span class="fc" id="L140"> postHandle(request, response);</span>
<span class="fc bfc" id="L141" title="All 2 branches covered."> if (log.isTraceEnabled()) {</span>
<span class="fc" id="L142"> log.trace(&quot;Successfully invoked postHandle method&quot;);</span>
}
<span class="nc" id="L145"> } catch (Exception e) {</span>
<span class="nc" id="L146"> exception = e;</span>
} finally {
<span class="fc" id="L148"> cleanup(request, response, exception);</span>
}
<span class="fc" id="L150"> }</span>
/**
* Executes cleanup logic in the {@code finally} code block in the
* {@link #doFilterInternal(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain) doFilterInternal}
* implementation.
* &lt;p/&gt;
* This implementation specifically calls
* {@link #afterCompletion(javax.servlet.ServletRequest, javax.servlet.ServletResponse, Exception) afterCompletion}
* as well as handles any exceptions properly.
*
* @param request the incoming {@code ServletRequest}
* @param response the outgoing {@code ServletResponse}
* @param existing any exception that might have occurred while executing the {@code FilterChain} or
* pre or post advice, or {@code null} if the pre/chain/post execution did not throw an {@code Exception}.
* @throws ServletException if any exception other than an {@code IOException} is thrown.
* @throws IOException if the pre/chain/post execution throw an {@code IOException}
*/
protected void cleanup(ServletRequest request, ServletResponse response, Exception existing)
throws ServletException, IOException {
<span class="fc" id="L170"> Exception exception = existing;</span>
try {
<span class="fc" id="L172"> afterCompletion(request, response, exception);</span>
<span class="fc bfc" id="L173" title="All 2 branches covered."> if (log.isTraceEnabled()) {</span>
<span class="fc" id="L174"> log.trace(&quot;Successfully invoked afterCompletion method.&quot;);</span>
}
<span class="nc" id="L176"> } catch (Exception e) {</span>
<span class="nc bnc" id="L177" title="All 2 branches missed."> if (exception == null) {</span>
<span class="nc" id="L178"> exception = e;</span>
} else {
<span class="nc" id="L180"> log.debug(&quot;afterCompletion implementation threw an exception. This will be ignored to &quot; +</span>
&quot;allow the original source exception to be propagated.&quot;, e);
}
<span class="fc" id="L183"> }</span>
<span class="pc bpc" id="L184" title="1 of 2 branches missed."> if (exception != null) {</span>
<span class="nc bnc" id="L185" title="All 2 branches missed."> if (exception instanceof ServletException) {</span>
<span class="nc" id="L186"> throw (ServletException) exception;</span>
<span class="nc bnc" id="L187" title="All 2 branches missed."> } else if (exception instanceof IOException) {</span>
<span class="nc" id="L188"> throw (IOException) exception;</span>
} else {
<span class="nc bnc" id="L190" title="All 2 branches missed."> if (log.isDebugEnabled()) {</span>
<span class="nc" id="L191"> String msg = &quot;Filter execution resulted in an unexpected Exception &quot; +</span>
&quot;(not IOException or ServletException as the Filter API recommends). &quot; +
&quot;Wrapping in ServletException and propagating.&quot;;
<span class="nc" id="L194"> log.debug(msg);</span>
}
<span class="nc" id="L196"> throw new ServletException(exception);</span>
}
}
<span class="fc" id="L199"> }</span>
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.3.201901230119</span></div></body></html>