001/**
002 * Copyright 2005-2018 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krad.web.filter;
017
018import javax.servlet.Filter;
019import javax.servlet.FilterChain;
020import javax.servlet.FilterConfig;
021import javax.servlet.ServletException;
022import javax.servlet.ServletRequest;
023import javax.servlet.ServletResponse;
024import javax.servlet.http.HttpServletResponse;
025import java.io.IOException;
026import java.util.UUID;
027
028/**
029 * Used to set the encoding for the request/response
030 *
031 * @author Kuali Rice Team (rice.collab@kuali.org)
032 */
033public class CharsetFilter implements Filter {
034    private String encoding;
035
036    public void init(FilterConfig config) throws ServletException {
037        encoding = config.getInitParameter("requestEncoding");
038
039        if (encoding == null) {
040            encoding = "UTF-8";
041        }
042    }
043
044    public void doFilter(ServletRequest request, ServletResponse response,
045            FilterChain next) throws IOException, ServletException {
046        // Respect the client-specified character encoding
047        // (see HTTP specification section 3.4.1)
048        if (null == request.getCharacterEncoding()) {
049            request.setCharacterEncoding(encoding);
050        }
051
052        /**
053         * Set the default response content type and encoding
054         */
055        response.setContentType("text/html; charset=UTF-8");
056        response.setCharacterEncoding("UTF-8");
057
058        next.doFilter(request, response);
059    }
060
061    public void destroy() {}
062}