File Source: CharEncodingFilter.java

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  The ASF licenses this file to You
     4   * under the Apache License, Version 2.0 (the "License"); you may not
     5   * 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.  For additional information regarding
    15   * copyright in this work, please see the NOTICE file in the top level
    16   * directory of this distribution.
    17   */
    18  
    19  package org.apache.roller.weblogger.ui.core.filters;
    20  
    21  import java.io.IOException;
    22  import java.io.UnsupportedEncodingException;
    23  import javax.servlet.Filter;
    24  import javax.servlet.FilterChain;
    25  import javax.servlet.FilterConfig;
    26  import javax.servlet.ServletException;
    27  import javax.servlet.ServletRequest;
    28  import javax.servlet.ServletResponse;
    29  import org.apache.commons.logging.Log;
    30  import org.apache.commons.logging.LogFactory;
    31  
    32  
    33  /**
    34   * Entry point filter for all requests. This filter ensures that the request 
    35   * encoding is set to UTF-8 before any other processing forces request parsing 
    36   * using a default encoding.  It also syncs up the Struts and JSTL locales.  
    37   * This filter should normally be first and last in the chain.
    38   *
    39   * @author <a href="mailto:anil@busybuddha.org">Anil Gangolli</a>
    40   */
         /* 
    P/P   *  Method: void org.apache.roller.weblogger.ui.core.filters.CharEncodingFilter()
          * 
          *  Postconditions:
          *    this.mFilterConfig == null
          */
    41  public class CharEncodingFilter implements Filter {
    42      
    43      private FilterConfig mFilterConfig = null;
             /* 
    P/P       *  Method: org.apache.roller.weblogger.ui.core.filters.CharEncodingFilter__static_init
              * 
              *  Presumptions:
              *    org.apache.commons.logging.LogFactory:getFactory(...)@44 != null
              * 
              *  Postconditions:
              *    init'ed(mLogger)
              */
    44      private static Log mLogger =
    45              LogFactory.getFactory().getInstance(CharEncodingFilter.class);
    46      
    47      /**
    48       * init
    49       */
    50      public void init(FilterConfig filterConfig) throws ServletException {
                 /* 
    P/P           *  Method: void init(FilterConfig)
                  * 
                  *  Postconditions:
                  *    this.mFilterConfig == filterConfig
                  *    init'ed(this.mFilterConfig)
                  */
    51          mFilterConfig = filterConfig;
    52      }
    53      
    54      /**
    55       * destroy
    56       */
    57      public void destroy() {
             /* 
    P/P       *  Method: void destroy()
              */
    58      }
    59      
    60      /**
    61       * Set the character encoding and sync up Struts and JSTL locales.  This filter should normally be first (and last)
    62       * in the chain.
    63       */
    64      public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
    65      throws IOException, ServletException {
                 /* 
    P/P           *  Method: void doFilter(ServletRequest, ServletResponse, FilterChain)
                  * 
                  *  Preconditions:
                  *    chain != null
                  *    mLogger != null
                  *    req != null
                  * 
                  *  Test Vectors:
                  *    org.apache.commons.logging.Log:isDebugEnabled(...)@66: {0}, {1}
                  *    org.apache.commons.logging.Log:isDebugEnabled(...)@70: {0}, {1}
                  */
    66          if (mLogger.isDebugEnabled()) mLogger.debug("Processing CharEncodingFilter");
    67          try {
    68              
    69              req.setCharacterEncoding("UTF-8");            
    70              if (mLogger.isDebugEnabled()) mLogger.debug("Set request character encoding to UTF-8");
    71              
    72          } catch (UnsupportedEncodingException e) {
    73              // This should never happen since UTF-8 is a Java-specified required encoding.
    74              throw new ServletException("Can't set incoming encoding to UTF-8");
    75          }
    76          
    77          chain.doFilter(req, res);
    78      }
    79      
    80  }








SofCheck Inspector Build Version : 2.18479
CharEncodingFilter.java 2009-Jan-02 14:25:40
CharEncodingFilter.class 2009-Sep-04 03:12:44