PageRenderTime 26ms CodeModel.GetById 19ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/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 */
 24package hudson.util;
 25
 26import java.io.IOException;
 27import java.util.logging.Level;
 28import java.util.logging.Logger;
 29import javax.servlet.Filter;
 30import javax.servlet.FilterChain;
 31import javax.servlet.FilterConfig;
 32import javax.servlet.ServletException;
 33import javax.servlet.ServletRequest;
 34import javax.servlet.ServletResponse;
 35import javax.servlet.http.HttpServletRequest;
 36
 37/**
 38 * Filter that sets the character encoding to be used in parsing the request
 39 * to avoid Non-ASCII characters garbled.
 40 *
 41 * @author Seiji Sogabe
 42 */
 43public class CharacterEncodingFilter implements Filter {
 44
 45    /**
 46     * The default character encoding.
 47     */
 48    private static final String ENCODING = "UTF-8";
 49
 50    private static final Boolean DISABLE_FILTER
 51            = Boolean.getBoolean(CharacterEncodingFilter.class.getName() + ".disableFilter");
 52
 53    /**
 54     * The character encoding sets forcibly?
 55     */
 56    private static final Boolean FORCE_ENCODING
 57            = Boolean.getBoolean(CharacterEncodingFilter.class.getName() + ".forceEncoding");
 58
 59    public void init(FilterConfig filterConfig) throws ServletException {
 60        LOGGER.log(Level.INFO,
 61                "CharacterEncodingFilter initialized. DISABLE_FILTER: {0} FORCE_ENCODING: {1}",
 62                new Object[]{DISABLE_FILTER, FORCE_ENCODING});
 63    }
 64
 65    public void destroy() {
 66        LOGGER.info("CharacterEncodingFilter destroyed.");
 67    }
 68
 69    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
 70            throws IOException, ServletException {
 71
 72        if (!DISABLE_FILTER) {
 73            if (request instanceof HttpServletRequest) {
 74                HttpServletRequest req = (HttpServletRequest) request;
 75                if (shouldSetCharacterEncoding(req)) {
 76                    req.setCharacterEncoding(ENCODING);
 77                }
 78            }
 79        }
 80
 81        chain.doFilter(request, response);
 82    }
 83
 84    private boolean shouldSetCharacterEncoding(HttpServletRequest req) {
 85        String method = req.getMethod();
 86        if (!"POST".equalsIgnoreCase(method)) {
 87            return false;
 88        }
 89
 90        // containers often implement RFCs incorrectly in that it doesn't interpret query parameter
 91        // decoding with UTF-8. This will ensure we get it right.
 92        // but doing this for config.xml submission could potentiall overwrite valid
 93        // "text/xml;charset=xxx"
 94        String contentType = req.getContentType();
 95        if (contentType != null) {
 96            boolean isXmlSubmission = contentType.startsWith("application/xml") || contentType.startsWith("text/xml");
 97            if (isXmlSubmission) {
 98                return false;
 99            }
100        }
101
102        if (FORCE_ENCODING || req.getCharacterEncoding() == null) {
103            return true;
104        }
105        
106        return false;
107    }
108
109    private static final Logger LOGGER = Logger.getLogger(CharacterEncodingFilter.class.getName());
110}