Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2014-2015 Philip Helger (www.helger.com) philip[at]helger[dot]com Licensed 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 com.helger.webbasics.servlet;
 
 
 
 
Special servlet filter that applies a certain encoding to a request and a response.

Author(s):
Philip Helger
 
 public class CharacterEncodingFilter implements Filter
 {
  
Name of the init parameter for the encoding
 
   public static final String FILTER_INITPARAM_ENCODING = "encoding";
  
Name of the init parameter to force setting the encoding
 
   public static final String FILTER_INITPARAM_FORCE_ENCODING = "forceEncoding";
  
The default encoding is UTF-8
 
   public static final String DEFAULT_ENCODING = .;
  
By default the encoding is not enforced.
 
   public static final boolean DEFAULT_FORCE_ENCODING = false;
   private static final String REQUEST_ATTR = CharacterEncodingFilter.class.getName ();
   private static final Logger s_aLogger = LoggerFactory.getLogger (CharacterEncodingFilter.class);
 
   private boolean m_bForceEncoding = ;

  

Returns:
The encoding to be used by this filter. Neither null nor empty.
 
   @Nonnull
   @Nonempty
   protected String getEncoding ()
   {
     return ;
   }
 
   protected boolean isForceEncoding ()
   {
     return ;
   }
 
   public void init (@Nonnull final FilterConfig aFilterConfigthrows ServletException
   {
     // encoding
     final String sEncoding = aFilterConfig.getInitParameter ();
     if (sEncoding != null)
     {
       // Throws IllegalArgumentException in case it is unknown
       CharsetManager.getCharsetFromName (sEncoding);
        = sEncoding;
     }
 
     // force encoding?
     final String sForceEncoding = aFilterConfig.getInitParameter ();
     if (sForceEncoding != null)
        = StringParser.parseBool (sForceEncoding);
   }
 
   public void doFilter (@Nonnull final ServletRequest aRequest,
                         @Nonnull final ServletResponse aResponse,
                         @Nonnull final FilterChain aChainthrows IOExceptionServletException
   {
     // Avoid double filtering
    if (aRequest.getAttribute () == null)
    {
      final String sEncoding = getEncoding ();
      final String sOldRequestEncoding = aRequest.getCharacterEncoding ();
      // We need this for all form data etc.
      if (sOldRequestEncoding == null || isForceEncoding ())
      {
        aRequest.setCharacterEncoding (sEncoding);
        if (sOldRequestEncoding != null && !sEncoding.equals (sOldRequestEncoding))
          .info ("Changed request encoding from '" + sOldRequestEncoding + "' to '" + sEncoding + "'");
      }
      aResponse.setCharacterEncoding (sEncoding);
      aRequest.setAttribute (.);
    }
    // Next filter in the chain
    aChain.doFilter (aRequestaResponse);
  }
  public void destroy ()
  {}
New to GrepCode? Check out our FAQ X