/hudson-core/src/main/java/hudson/util/CharacterEncodingFilter.java

http://github.com/hudson/hudson · Java · 110 lines · 56 code · 15 blank · 39 comment · 11 complexity · 536e51b0ce8ae09eb9b1076105cc6f90 MD5 · raw file

  1. /*
  2. * The MIT License
  3. *
  4. * Copyright (c) 2010, Oracle Corporation, Seiji Sogabe
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. package hudson.util;
  25. import java.io.IOException;
  26. import java.util.logging.Level;
  27. import java.util.logging.Logger;
  28. import javax.servlet.Filter;
  29. import javax.servlet.FilterChain;
  30. import javax.servlet.FilterConfig;
  31. import javax.servlet.ServletException;
  32. import javax.servlet.ServletRequest;
  33. import javax.servlet.ServletResponse;
  34. import javax.servlet.http.HttpServletRequest;
  35. /**
  36. * Filter that sets the character encoding to be used in parsing the request
  37. * to avoid Non-ASCII characters garbled.
  38. *
  39. * @author Seiji Sogabe
  40. */
  41. public class CharacterEncodingFilter implements Filter {
  42. /**
  43. * The default character encoding.
  44. */
  45. private static final String ENCODING = "UTF-8";
  46. private static final Boolean DISABLE_FILTER
  47. = Boolean.getBoolean(CharacterEncodingFilter.class.getName() + ".disableFilter");
  48. /**
  49. * The character encoding sets forcibly?
  50. */
  51. private static final Boolean FORCE_ENCODING
  52. = Boolean.getBoolean(CharacterEncodingFilter.class.getName() + ".forceEncoding");
  53. public void init(FilterConfig filterConfig) throws ServletException {
  54. LOGGER.log(Level.INFO,
  55. "CharacterEncodingFilter initialized. DISABLE_FILTER: {0} FORCE_ENCODING: {1}",
  56. new Object[]{DISABLE_FILTER, FORCE_ENCODING});
  57. }
  58. public void destroy() {
  59. LOGGER.info("CharacterEncodingFilter destroyed.");
  60. }
  61. public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
  62. throws IOException, ServletException {
  63. if (!DISABLE_FILTER) {
  64. if (request instanceof HttpServletRequest) {
  65. HttpServletRequest req = (HttpServletRequest) request;
  66. if (shouldSetCharacterEncoding(req)) {
  67. req.setCharacterEncoding(ENCODING);
  68. }
  69. }
  70. }
  71. chain.doFilter(request, response);
  72. }
  73. private boolean shouldSetCharacterEncoding(HttpServletRequest req) {
  74. String method = req.getMethod();
  75. if (!"POST".equalsIgnoreCase(method)) {
  76. return false;
  77. }
  78. // containers often implement RFCs incorrectly in that it doesn't interpret query parameter
  79. // decoding with UTF-8. This will ensure we get it right.
  80. // but doing this for config.xml submission could potentiall overwrite valid
  81. // "text/xml;charset=xxx"
  82. String contentType = req.getContentType();
  83. if (contentType != null) {
  84. boolean isXmlSubmission = contentType.startsWith("application/xml") || contentType.startsWith("text/xml");
  85. if (isXmlSubmission) {
  86. return false;
  87. }
  88. }
  89. if (FORCE_ENCODING || req.getCharacterEncoding() == null) {
  90. return true;
  91. }
  92. return false;
  93. }
  94. private static final Logger LOGGER = Logger.getLogger(CharacterEncodingFilter.class.getName());
  95. }