View Javadoc

    1   /***
    2    * JFoxSOAF, Service-Oriented Application Framework
    3    *
    4    * Copyright (C) www.huihoo.org
    5    *
    6    * Distributable under GNU LGPL
    7    *
    8    * For more information, please visit: http://www.huihoo.org/jfox/jfoxsoaf
    9    */
    10
    11  package org.huihoo.jfox.soaf.util.filter;
    12
    13  import java.io.IOException;
    14
    15  import javax.servlet.FilterChain;
    16  import javax.servlet.FilterConfig;
    17  import javax.servlet.ServletException;
    18  import javax.servlet.ServletRequest;
    19  import javax.servlet.ServletResponse;
    20
    21  /***
    22   * <p>
    23   * Filter that sets the character encoding to be used in parsing the incoming
    24   * request, either unconditionally or only if the client did not specify a
    25   * character encoding. Resource of this filter is based on the following
    26   * initialization parameters:
    27   * </p>
    28   *
    29   * @author <a href="mailto:founder_chen@yahoo.com.cn">Peter Cheng </a>
    30   * @version $Revision: 1.2 $ $Date: 2004/10/25 14:46:36 $
    31   * @version Revision: 1.0
    32   */
    33
    34  public class EncodingFilter {
    35
    36      /***
    37       * The default character encoding to set for requests that pass through this
    38       * filter.
    39       */
    40      private String encoding = null;
    41
    42      /***
    43       * The filter configuration object we are associated with. If this value is
    44       * null, this filter instance is not currently configured.
    45       */
    46      private FilterConfig filterConfig = null;
    47
    48      /***
    49       * Should a character encoding specified by the client be ignored?
    50       */
    51      private boolean ignore = true;
    52
    53      /***
    54       * Take this filter out of service.
    55       */
    56      public void destroy() {
    57          this.encoding = null;
    58          this.filterConfig = null;
    59      }
    60
    61      /***
    62       * Select and set (if specified) the character encoding to be used to
    63       * interpret request parameters for this request.
    64       *
    65       * @param request
    66       *            The servlet request we are processing
    67       * @param response
    68       *            The servlet response we are creating
    69       * @param chain
    70       *            The filter chain we are processing
    71       * @exception java.io.IOException
    72       *                if an input/output error occurs
    73       * @exception javax.servlet.ServletException
    74       *                if a servlet error occurs
    75       */
    76      public void doFilter(ServletRequest request, ServletResponse response,
    77              FilterChain chain) throws IOException, ServletException {
    78          //   Conditionally select and set the character encoding to be used
    79          if (ignore || (request.getCharacterEncoding() == null)) {
    80              String encoding = selectEncoding(request);
    81              if (encoding != null)
    82                  request.setCharacterEncoding(encoding);
    83          }
    84
    85          //   Pass control on to the next filter
    86          chain.doFilter(request, response);
    87      }
    88
    89      /***
    90       * Place this filter into service.
    91       *
    92       * @param filterConfig
    93       *            The filter configuration object
    94       */
    95      public void init(FilterConfig filterConfig) throws ServletException {
    96
    97          this.filterConfig = filterConfig;
    98          String encodingConfig = filterConfig.getInitParameter("encoding");
    99          if (encodingConfig != null) {
    100             this.encoding = encodingConfig;
    101         }
    102         String value = filterConfig.getInitParameter("ignore");
    103         if (value == null) {
    104             this.ignore = true;
    105         } else if (value.equalsIgnoreCase("true")) {
    106             this.ignore = true;
    107         } else if (value.equalsIgnoreCase("yes")) {
    108             this.ignore = true;
    109         } else {
    110             this.ignore = false;
    111         }
    112     }
    113
    114     /***
    115      * Select an appropriate character encoding to be used, based on the
    116      * characteristics of the current request and/or filter initialization
    117      * parameters. If no character encoding should be set, return
    118      * <code>null</code>.
    119      * <p>
    120      * The default implementation unconditionally returns the value configured
    121      * by the <strong>encoding </strong> initialization parameter for this
    122      * filter.
    123      *
    124      * @param request
    125      *            The servlet request we are processing
    126      */
    127     protected String selectEncoding(ServletRequest request) {
    128         return (this.encoding);
    129     }
    130
    131     /***
    132      * @return Returns the encoding.
    133      */
    134     public String getEncoding() {
    135         return encoding;
    136     }
    137
    138     /***
    139      * @return Returns the ignore.
    140      */
    141     public boolean isIgnore() {
    142         return ignore;
    143     }
    144 }