View Javadoc

1   package com.atlassian.core.filters.encoding;
2   
3   import com.atlassian.core.filters.AbstractHttpFilter;
4   
5   import javax.servlet.http.HttpServletRequest;
6   import javax.servlet.http.HttpServletResponse;
7   import javax.servlet.FilterChain;
8   import javax.servlet.ServletException;
9   import java.io.IOException;
10  
11  /**
12   * Sets the encoding of request and response to a value defined by the application, and prevents later
13   * changes to it by wrapping the response in a {@link FixedHtmlEncodingResponseWrapper}.
14   * <p/>
15   * For unrelated functionality that used to be performed by the old filter, see the related classes below.
16   *
17   * @see com.atlassian.core.filters.cache.AbstractCachingFilter
18   * @see com.atlassian.core.filters.legacy.NoContentLocationHeaderResponseWrapper
19   * @see com.atlassian.core.filters.legacy.WordCurlyQuotesRequestWrapper
20   * @since 4.0
21   */
22  public abstract class AbstractEncodingFilter extends AbstractHttpFilter
23  {
24      /**
25       * Sets the encoding of the request and the content-type of the response (which includes the
26       * charset parameter) based on the values returned from the template methods. Wraps the request
27       * in a {@link FixedHtmlEncodingResponseWrapper} to ensure the content-type is not changed later.
28       * <p/>
29       * After setting the encoding and wrapping the request, the remainder of the filter chain is
30       * processed normally.
31       * <p/>
32       * If your application wants to be sure that the encoding set by this filter is used for all
33       * HTML responses, this filter should be the first filter in your filter chain.
34       */
35      protected final void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
36          throws IOException, ServletException
37      {
38          request.setCharacterEncoding(getEncoding());
39          response.setContentType(getContentType());
40  
41          filterChain.doFilter(request, new FixedHtmlEncodingResponseWrapper(response));
42      }
43  
44      /**
45       * Return the content type to be used for the response, via {@link HttpServletResponse#setContentType(String)}.
46       * The header should include a charset parameter. For example: "text/html; charset=UTF-8".
47       */
48      abstract protected String getContentType();
49  
50      /**
51       * Return the encoding to be used on the request, via {@link HttpServletRequest#setCharacterEncoding(String)}.
52       * For example: "UTF-8".
53       */
54      abstract protected String getEncoding();
55  }