Coverage Report - net.admin4j.ui.filters.RequestTrackingFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
RequestTrackingFilter
69%
16/23
62%
5/8
3.333
 
 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  
 import javax.servlet.http.HttpServletRequest;
 25  
 
 26  
 import net.admin4j.config.Admin4JConfiguration;
 27  
 import net.admin4j.deps.commons.lang3.StringUtils;
 28  
 import net.admin4j.util.Admin4jRuntimeException;
 29  
 import net.admin4j.util.FixedSizeRollingList;
 30  
 import net.admin4j.util.ServletUtils;
 31  
 import net.admin4j.vo.HttpRequestVO;
 32  
 
 33  
 /**
 34  
  * Filter that will track requests in a session for possible error reporting. 
 35  
  * @author D. Ashmore
 36  
  * @since 1.0.2
 37  
  */
 38  9
 public class RequestTrackingFilter implements Filter {
 39  
     
 40  
     public static final int DEFAULT_NBR_REQUESTS_TRACKED = 5;
 41  
     public static final String REQUEST_TRACKING_SESSION_ATTRIBUTE_NAME = "admin4j.previous.requests";
 42  
     
 43  
     private int nbrRequestsTracked;
 44  
 
 45  
     public void destroy() {
 46  
         // NoOp
 47  
 
 48  0
     }
 49  
 
 50  
     @SuppressWarnings("unchecked")
 51  
     public void doFilter(ServletRequest request, ServletResponse response,
 52  
             FilterChain chain) throws IOException, ServletException {
 53  9
         HttpServletRequest httpRequest = (HttpServletRequest)request;
 54  
         
 55  9
         FixedSizeRollingList<HttpRequestVO> list = (FixedSizeRollingList<HttpRequestVO>)
 56  
                 httpRequest.getSession().getAttribute(REQUEST_TRACKING_SESSION_ATTRIBUTE_NAME);
 57  9
         if (list == null) {
 58  6
             list = new FixedSizeRollingList<HttpRequestVO>(nbrRequestsTracked);
 59  6
             httpRequest.getSession().setAttribute(REQUEST_TRACKING_SESSION_ATTRIBUTE_NAME, list);
 60  
         }
 61  
         
 62  9
         list.add(new HttpRequestVO(httpRequest.getRequestURI(), 
 63  
                 ServletUtils.listRequestAttributes(httpRequest)));
 64  9
         chain.doFilter(request, response);
 65  9
     }
 66  
 
 67  
     public void init(FilterConfig config) throws ServletException {
 68  6
         String nbrRequestsStr = config.getInitParameter("nbr.requests.tracked");
 69  
         
 70  6
         if (Admin4JConfiguration.getRequestHistoryNbrRetained() != null) {
 71  0
             if (Admin4JConfiguration.getRequestHistoryNbrRetained() > 0) {
 72  0
                 nbrRequestsTracked = Admin4JConfiguration.getRequestHistoryNbrRetained();
 73  
             }
 74  
             else {
 75  0
                 throw new Admin4jRuntimeException("request.history.nbr.retained must be greater than zero")
 76  
                 .addContextValue("request.history.nbr.retained", Admin4JConfiguration.getRequestHistoryNbrRetained());
 77  
             }
 78  
         }
 79  0
         else if (StringUtils.isEmpty(nbrRequestsStr)) {
 80  3
             nbrRequestsTracked = DEFAULT_NBR_REQUESTS_TRACKED;
 81  
         }
 82  
         else {
 83  
             try {
 84  3
                 nbrRequestsTracked = Integer.valueOf(nbrRequestsStr);
 85  0
             } catch (NumberFormatException e) {
 86  0
                 throw new Admin4jRuntimeException("request.history.nbr.retained must be numeric")
 87  
                 .addContextValue("request.history.nbr.retained", nbrRequestsStr);
 88  3
             }
 89  
         }
 90  6
         Admin4JStandardFilterChain.registerFilter(this);
 91  
 
 92  6
     }
 93  
 
 94  
 }