| <?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 :: Web</a> > <a href="index.source.html" class="el_package">org.apache.shiro.web.servlet</a> > <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 |
| * "License"); 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 |
| * "AS IS" 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 &quot;around&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. |
| * <p/> |
| * 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. |
| * <p/> |
| * 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({"UnusedDeclaration"}) |
| protected void postHandle(ServletRequest request, ServletResponse response) throws Exception { |
| <span class="nc" 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. |
| * <p/> |
| * 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({"UnusedDeclaration"}) |
| public void afterCompletion(ServletRequest request, ServletResponse response, Exception exception) throws Exception { |
| <span class="nc" id="L95"> }</span> |
| |
| /** |
| * Actually executes the specified filter chain by calling <code>chain.doFilter(request,response);</code>. |
| * <p/> |
| * 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="nc" id="L108"> chain.doFilter(request, response);</span> |
| <span class="nc" 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="nc" id="L127"> Exception exception = null;</span> |
| |
| try { |
| |
| <span class="nc" id="L131"> boolean continueChain = preHandle(request, response);</span> |
| <span class="nc bnc" id="L132" title="All 2 branches missed."> if (log.isTraceEnabled()) {</span> |
| <span class="nc" id="L133"> log.trace("Invoked preHandle method. Continuing chain?: [" + continueChain + "]");</span> |
| } |
| |
| <span class="nc bnc" id="L136" title="All 2 branches missed."> if (continueChain) {</span> |
| <span class="nc" id="L137"> executeChain(request, response, chain);</span> |
| } |
| |
| <span class="nc" id="L140"> postHandle(request, response);</span> |
| <span class="nc bnc" id="L141" title="All 2 branches missed."> if (log.isTraceEnabled()) {</span> |
| <span class="nc" id="L142"> log.trace("Successfully invoked postHandle method");</span> |
| } |
| |
| <span class="nc" id="L145"> } catch (Exception e) {</span> |
| <span class="nc" id="L146"> exception = e;</span> |
| } finally { |
| <span class="nc" id="L148"> cleanup(request, response, exception);</span> |
| <span class="nc" id="L149"> }</span> |
| <span class="nc" 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. |
| * <p/> |
| * 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="nc" id="L170"> Exception exception = existing;</span> |
| try { |
| <span class="nc" id="L172"> afterCompletion(request, response, exception);</span> |
| <span class="nc bnc" id="L173" title="All 2 branches missed."> if (log.isTraceEnabled()) {</span> |
| <span class="nc" id="L174"> log.trace("Successfully invoked afterCompletion method.");</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("afterCompletion implementation threw an exception. This will be ignored to " +</span> |
| "allow the original source exception to be propagated.", e); |
| } |
| <span class="nc" id="L183"> }</span> |
| <span class="nc bnc" id="L184" title="All 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 = "Filter execution resulted in an unexpected Exception " +</span> |
| "(not IOException or ServletException as the Filter API recommends). " + |
| "Wrapping in ServletException and propagating."; |
| <span class="nc" id="L194"> log.debug(msg);</span> |
| } |
| <span class="nc" id="L196"> throw new ServletException(exception);</span> |
| } |
| } |
| <span class="nc" id="L199"> }</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> |