Coverage Report - at.ipsquare.commons.servlet.RequestEncodingFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
RequestEncodingFilter
100%
13/13
100%
8/8
2.333
 
 1  
 /**
 2  
  * Copyright (C) 2013 Matthias Langer
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *         http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package at.ipsquare.commons.servlet;
 17  
 
 18  
 import java.io.IOException;
 19  
 
 20  
 import javax.servlet.Filter;
 21  
 import javax.servlet.FilterChain;
 22  
 import javax.servlet.FilterConfig;
 23  
 import javax.servlet.ServletException;
 24  
 import javax.servlet.ServletRequest;
 25  
 import javax.servlet.ServletResponse;
 26  
 
 27  
 /**
 28  
  * A filter that consistently sets the request encoding.
 29  
  * 
 30  
  * @since 2.1.0
 31  
  * @author Matthias Langer
 32  
  */
 33  20
 public class RequestEncodingFilter implements Filter
 34  
 {
 35  
     private String encoding;
 36  
     private boolean force;
 37  
     private RequestMatcher matcher;
 38  
     
 39  
     /**
 40  
      * The init parameter name for the encoding (defaults to UTF-8).
 41  
      */
 42  
     public static final String INIT_PARAM_NAME_ENCODING = "encoding";
 43  
     
 44  
     /**
 45  
      * The init parameter name for the force encoding property (defaults to false).
 46  
      */
 47  
     public static final String INIT_PARAM_NAME_FORCE = "forceEncoding";
 48  
 
 49  
     @Override
 50  
     public void init(FilterConfig cfg) throws ServletException
 51  
     {
 52  20
         encoding = cfg.getInitParameter(INIT_PARAM_NAME_ENCODING);
 53  20
         force = Boolean.parseBoolean(cfg.getInitParameter(INIT_PARAM_NAME_FORCE));
 54  20
         matcher = PathPatternRequestMatcher.fromFilterConfig(cfg);
 55  
         
 56  20
         if(encoding == null)
 57  8
             encoding = "UTF-8";
 58  20
     }
 59  
     
 60  
     @Override
 61  
     public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException
 62  
     {
 63  20
         if(matcher.matches(req))
 64  
         {
 65  16
             if(force || req.getCharacterEncoding() == null)
 66  12
                 req.setCharacterEncoding(encoding);
 67  
         }
 68  
         
 69  20
         chain.doFilter(req, res);
 70  20
     }
 71  
     
 72  
     @Override
 73  
     public void destroy()
 74  
     {
 75  
         
 76  20
     }
 77  
 }