It works on all application servers.
Cheers, Scott
remigijus wrote:
Hi.
How can I change encoding for all incoming pages to utf-8?
--
ATLASSIAN - http://www.atlassian.com Expert J2EE Software, Services and Support ------------------------------------------------------- Need a simple, powerful way to track and manage issues? Try JIRA - http://www.atlassian.com/software/jira
/* * Atlassian Source Code Template. * User: sfarquhar * Date: 1/08/2002 * Time: 09:27:01 * CVS Revision: $Revision: 1.8 $ * Last CVS Commit: $Date: 2003/07/18 03:37:35 $ * Author of last CVS Commit: $Author: dloeng $ */ package com.atlassian.jira.web.filters;
import com.atlassian.jira.ManagerFactory; import com.atlassian.jira.util.StringUtils; import com.opensymphony.util.TextUtils; import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponseWrapper; import java.io.IOException; import java.util.HashMap; import java.util.Iterator; import java.util.Map; /** * This filter sets the request and response encoding, as well as preventing page caching. */ public class EncodingFilter extends AbstractFilter { public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { servletRequest.setCharacterEncoding(getEncoding()); servletResponse.setContentType(getContentType()); // prevent caching of JSPs if (servletResponse instanceof HttpServletResponse) { HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse; HttpServletRequest req = (HttpServletRequest) servletRequest; String uri = TextUtils.noNull(req.getRequestURI()); if (uri.indexOf(".jsp") > 0 || uri.indexOf(".jspa") > 0) { httpServletResponse.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // http 1.1 httpServletResponse.setHeader("Pragma", "no-cache"); // http 1.0 httpServletResponse.setDateHeader("Expires", 0); // prevent proxy caching } } filterChain.doFilter(new WordCurlyQuotesRequestWrapper(servletRequest), new HttpServletResponseWrapper((HttpServletResponse) servletResponse) { public void setContentType(String s) { //JRun passes a null content type sometimes if (s != null && s.length() > "text/html".length() && s.charAt(0) == 't' && s.startsWith("text/html")) { //do nothing. This call could be trying to set the charset to another charset. //This is the case with Tomcat & Jetty, whose JSP compiler sets the charset, whether it //is specified in the JSP page or not. //So - if someone else is trying to set the content type to HTML, then they may also want //to override the charset. So - we just don't allow them to override the content-type //if they are just setting to text/html. If they want to overried with 'gif' or 'pdf' //then go straight ahead. Who are we to question the app server. I mean...really.... //NB - this can also be accomplished by setting the charset manually in the JSP page & the decorator, //but this approach allows for run-time flexibility of choosing the charsets. } else { super.setContentType(s); } } public void setHeader(String string, String string1) { //Opera 7.1+ reads the 'content-location' header and uses it as the href base for //relative URLs. //However, in Orion < 2.0.2 this header did not contain the correct context path //However, even when it is set, it points to the JSP view not the original request URL //So - we just prevent it being set. if (!isContentLocationHeader(string)) super.setHeader(string, string1); } public void addHeader(String string, String string1) { //See setHeader above if (!isContentLocationHeader(string)) super.addHeader(string, string1); } private boolean isContentLocationHeader(String headerName) { return headerName != null && "content-location".equalsIgnoreCase(headerName); } }); } public void init(FilterConfig filterConfig) { } private static String getEncoding() { try { return ManagerFactory.getApplicationProperties().getEncoding(); } catch (Exception e) { return "UTF-8"; } } private static String getContentType() { try { return ManagerFactory.getApplicationProperties().getContentType(); } catch (Exception e) { return "text/html; charset=UTF-8"; } } public static class WordCurlyQuotesRequestWrapper extends HttpServletRequestWrapper { Map parameterValueCache = new HashMap(); Map parameterMap = null; public WordCurlyQuotesRequestWrapper(ServletRequest servletRequest) { super((HttpServletRequest) servletRequest); } public String getParameter(String string) { return escapeString(super.getParameter(string)); } protected String escapeString(String string) { return StringUtils.escapeCP1252(string, getEncoding()); } public Map getParameterMap() { return super.getParameterMap(); } public String[] getParameterValues(String string) { String[] returnValue = (String[]) parameterValueCache.get(string); //if we haven't yet cached this - look it up. if (returnValue == null) { String[] parameterValues = super.getParameterValues(string); //values could be null - don't bother converting them if (parameterValues == null) return null; for (int i = 0; i < parameterValues.length; i++) { String parameterValue = escapeString(parameterValues[i]); parameterValues[i] = parameterValue; } parameterValueCache.put(string, parameterValues); returnValue = parameterValues; } return returnValue; } } }