1 10 package org.mmbase.servlet; 11 12 import java.io.*; 13 import java.util.*; 14 15 import javax.servlet.*; 16 import javax.servlet.http.*; 17 18 import org.mmbase.util.*; 19 import org.mmbase.util.logging.*; 20 21 34 35 public class CharsetRemoverFilter implements Filter { 36 private static final Logger log = Logging.getLoggerInstance(CharsetRemoverFilter.class); 37 38 39 Properties contentTypes = new Properties(); 40 FileWatcher watcher = new FileWatcher(true) { 41 public void onChange(File file) { 42 load(file); 43 } 44 }; 45 48 public void init(javax.servlet.FilterConfig filterConfig) throws ServletException { 49 File file = new File(filterConfig.getServletContext().getRealPath("WEB-INF/config/charsetremover.properties")); 50 log.info("Init of CharsetRemover Filter, using " + file); 51 load(file); 52 watcher.add(file); 53 watcher.setDelay(10 * 1000); watcher.start(); 55 56 } 57 58 public void load(File file) { 59 log.info("Reading " + file); 60 contentTypes.clear(); 61 if (file.canRead()) { 62 try { 63 contentTypes.load(new FileInputStream(file)); 64 } catch (IOException ioe) { 65 log.error(ioe); 66 } 67 } else { 68 log.warn("This file does not exist, using defaults"); 69 contentTypes.put("audio/x-pn-realaudio", "ISO-8859-1"); 70 contentTypes.put("text/vnd.rn-realtext","ISO-8859-1"); 71 contentTypes.put("audio/x-pn-realaudio-plugin", "ISO-8859-1"); 72 contentTypes.put("image/vnd.rn-realpix", "ISO-8859-1"); 73 contentTypes.put("application/smil", "ISO-8859-1"); 74 } 75 log.info("The following content-types will have no charset on the content-type: " + contentTypes); 76 } 77 78 79 public void doFilter(final ServletRequest servletRequest, final ServletResponse servletResponse, FilterChain filterChain) 80 throws java.io.IOException , ServletException { 81 82 HttpServletResponseWrapper wrapper = new HttpServletResponseWrapper((HttpServletResponse) servletResponse) { 83 private String contentType; 84 private PrintWriter writer = null; 85 86 87 public void setContentType(String ct) { 88 contentType = ct; 89 } 90 96 97 public PrintWriter getWriter() throws IOException { 98 if (writer == null) { 99 String charSet = contentType == null ? null : (String ) contentTypes.get(contentType); 100 if (charSet != null) { 101 if (contentType != null) { 102 super.setContentType(contentType); 103 } 104 if (log.isDebugEnabled()) { 105 log.debug("Wrapping outputstream to avoid charset " + charSet); 106 } 107 try { 108 writer = new PrintWriter(new OutputStreamWriter(getOutputStream(), charSet), false) { 109 public void write(String s, int off, int len) { 110 super.write(s, off, len); 111 flush(); 112 } 113 114 }; 115 } catch (UnsupportedEncodingException uee) { 116 log.error(uee); 117 writer = super.getWriter(); 118 } 119 } else { 120 if (contentType != null) { 121 super.setContentType(contentType); 122 } 123 124 if (log.isDebugEnabled()) { 125 log.debug(" " + contentType + " is not contained by " + contentTypes); 126 } 127 writer = super.getWriter(); 128 } 129 } 130 if (log.isDebugEnabled()) { 131 log.debug("Returning " + writer.getClass()); 132 } 133 return writer; 134 } 135 }; 136 filterChain.doFilter(servletRequest, wrapper); 137 138 } 139 142 public void destroy() { 143 } 144 145 146 } 147
| Popular Tags
|