KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > servlet > CharsetRemoverFilter


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

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 /**
22  * Makes sure that there is no charset on the content type of certain
23  * types. This is mainly because real-player does not understand
24  * that. But it could be used for other content-types as well (There
25  * are probably more lousy client programs out there).
26  *
27  * It can be configured by a file WEB-INF/config/charsetremover.properties with
28  * <contenttype>=<supposed charset> properties.
29  *
30  * @author Michiel Meeuwissen
31  * @version $Id: CharsetRemoverFilter.java,v 1.6 2005/07/11 08:22:24 michiel Exp $
32  * @since MMBase-1.7.4
33  */

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     /**
46      * Initializes the filter
47      */

48     public void init(javax.servlet.FilterConfig JavaDoc 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); // check every 10 secs if config changed
54
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 JavaDoc, ServletException {
81
82         HttpServletResponseWrapper wrapper = new HttpServletResponseWrapper((HttpServletResponse) servletResponse) {
83                 private String JavaDoc contentType;
84                 private PrintWriter writer = null;
85
86                 
87                 public void setContentType(String JavaDoc ct) {
88                     contentType = ct;
89                 }
90                 /**
91                  * This is the essence of this whole thing. The idea
92                  * is to fake the use of getOutputStream(). Then you
93                  * are in byte-writing mode. and charset's become
94                  * irrelevant,and tomcat will not add one any more.
95                  */

96                 
97                 public PrintWriter getWriter() throws IOException {
98                     if (writer == null) {
99                         String JavaDoc charSet = contentType == null ? null : (String JavaDoc) 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 JavaDoc 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     /**
140      * destroys the filter
141      */

142     public void destroy() {
143     }
144
145
146 }
147
Popular Tags