1 23 24 package org.infoglue.cms.util; 25 26 import java.io.IOException ; 27 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 import org.infoglue.cms.controllers.kernel.impl.simple.LanguageController; 37 import org.infoglue.cms.entities.management.LanguageVO; 38 39 40 public class SetCharacterEncodingFilter implements Filter 41 { 42 43 44 48 protected String encoding = null; 49 50 51 55 protected FilterConfig filterConfig = null; 56 57 58 61 protected boolean ignore = true; 62 63 64 65 68 public void destroy() { 69 70 this.encoding = null; 71 this.filterConfig = null; 72 73 } 74 75 76 87 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException , ServletException 88 { 89 String referer = ((HttpServletRequest )request).getHeader("referer"); 91 if(referer != null && referer.length() > 0 && referer.indexOf("ViewPage!renderDecoratedPage.action") > -1) 92 { 93 try 94 { 95 int startIndex = referer.indexOf("&languageId="); 96 if(startIndex > -1) 97 { 98 int endIndex = referer.indexOf("&", startIndex + 12); 99 String languageId = referer.substring(startIndex + 12, endIndex); 100 LanguageVO languageVO = LanguageController.getController().getLanguageVOWithId(new Integer (languageId)); 102 request.setCharacterEncoding(languageVO.getCharset()); 103 } 104 } 105 catch(Exception e) 106 { 107 e.printStackTrace(); 108 } 109 } 110 else if (ignore || (request.getCharacterEncoding() == null)) 111 { 112 String encoding = selectEncoding(request); 113 if (encoding != null) 114 request.setCharacterEncoding(encoding); 115 } 116 117 chain.doFilter(request, response); 119 120 } 121 122 123 128 public void init(FilterConfig filterConfig) throws ServletException 129 { 130 131 this.filterConfig = filterConfig; 132 this.encoding = filterConfig.getInitParameter("encoding"); 133 String value = filterConfig.getInitParameter("ignore"); 134 if (value == null) 135 this.ignore = true; 136 else if (value.equalsIgnoreCase("true")) 137 this.ignore = true; 138 else if (value.equalsIgnoreCase("yes")) 139 this.ignore = true; 140 else 141 this.ignore = false; 142 143 } 144 145 146 158 protected String selectEncoding(ServletRequest request) 159 { 160 String inputCharacterEncoding = CmsPropertyHandler.getInputCharacterEncoding(this.encoding); 161 162 if(inputCharacterEncoding != null && !inputCharacterEncoding.equals("") && !inputCharacterEncoding.equalsIgnoreCase("@inputCharacterEncoding@")) 163 return inputCharacterEncoding; 164 else 165 return (this.encoding); 166 } 167 168 169 } 170 | Popular Tags |