Coverage Report - net.admin4j.ui.filters.ErrorLoggingFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
ErrorLoggingFilter
78%
15/19
50%
2/4
1.75
 
 1  
 /*
 2  
  * This software is licensed under the Apache License, Version 2.0
 3  
  * (the "License") agreement; you may not use this file except in compliance with
 4  
  * the License.  You may obtain a copy of the License at
 5  
  * 
 6  
  *      http://www.apache.org/licenses/LICENSE-2.0
 7  
  * 
 8  
  * Unless required by applicable law or agreed to in writing, software
 9  
  * distributed under the License is distributed on an "AS IS" BASIS,
 10  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 11  
  * See the License for the specific language governing permissions and
 12  
  * limitations under the License.
 13  
  */
 14  
 package net.admin4j.ui.filters;
 15  
 
 16  
 import java.io.IOException;
 17  
 
 18  
 import javax.servlet.Filter;
 19  
 import javax.servlet.FilterChain;
 20  
 import javax.servlet.FilterConfig;
 21  
 import javax.servlet.ServletException;
 22  
 import javax.servlet.ServletRequest;
 23  
 import javax.servlet.ServletResponse;
 24  
 
 25  
 import net.admin4j.config.Admin4JConfiguration;
 26  
 import net.admin4j.deps.commons.lang3.StringUtils;
 27  
 import net.admin4j.util.ServletUtils;
 28  
 
 29  
 import org.slf4j.Logger;
 30  
 import org.slf4j.LoggerFactory;
 31  
 
 32  
 /**
 33  
  * This filter will log Web Transactions errors.
 34  
  * <p>Init parameters for this filter are as follows:</p>
 35  
  * <li>logger -- Optional.  Default net.admin4j.ui.filters.ErrorLoggingFilter</li>
 36  
  * @author D. Ashmore
 37  
  * @since 1.0
 38  
  */
 39  18
 public class ErrorLoggingFilter implements Filter {
 40  
     
 41  18
     private Logger logger = null;
 42  
 
 43  
     public void destroy() {
 44  
         // NoOp
 45  
 
 46  0
     }
 47  
 
 48  
     public void doFilter(ServletRequest request, ServletResponse response,
 49  
             FilterChain chain) throws IOException, ServletException {
 50  6
         try {chain.doFilter(request, response);}
 51  3
         catch (Throwable t) {
 52  3
             logger.error("Web Transaction Error", t);
 53  3
             ServletUtils.reThrowServletFilterException(t);
 54  3
         }
 55  
 
 56  3
     }
 57  
 
 58  
     public void init(FilterConfig config) throws ServletException {
 59  9
         new Admin4JConfiguration();
 60  9
         String loggerName = config.getInitParameter("logger");
 61  0
         if (StringUtils.isEmpty(loggerName)) {
 62  9
             loggerName = Admin4JConfiguration.getWebTransactionErrorLoggerName();
 63  
         }
 64  
         
 65  0
         if (StringUtils.isEmpty(loggerName)) {
 66  9
             logger = LoggerFactory.getLogger(ErrorLoggingFilter.class);
 67  
         }
 68  0
         else logger = LoggerFactory.getLogger(loggerName);
 69  
         
 70  9
         Admin4JStandardFilterChain.registerFilter(this);
 71  
 
 72  9
     }
 73  
 
 74  
     /**
 75  
      * Note:  Here for testing.
 76  
      * @return
 77  
      */
 78  
     protected Logger getLogger() {
 79  6
         return logger;
 80  
     }
 81  
 
 82  
 }