1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 package org.w3c.tidy.servlet.filter;
56
57
58
59 import java.io.IOException;
60
61 import javax.servlet.Filter;
62 import javax.servlet.FilterChain;
63 import javax.servlet.FilterConfig;
64 import javax.servlet.ServletException;
65 import javax.servlet.ServletRequest;
66 import javax.servlet.ServletResponse;
67 import javax.servlet.http.HttpServletRequest;
68 import javax.servlet.http.HttpServletResponse;
69
70 import org.apache.commons.logging.Log;
71 import org.apache.commons.logging.LogFactory;
72
73 import org.w3c.tidy.servlet.Consts;
74 import org.w3c.tidy.servlet.TidyProcessor;
75 import org.w3c.tidy.servlet.properties.JTidyServletProperties;
76
77
78 /***
79 * Wrapp the Response and creates TidyProcessor who does all the work.
80 *
81 * Use this filter instead of TidyTag if you don't
82 * want to modify your JSP pages or HTML is denerated by non JSP servlets.
83 *
84 * @author Vlad Skarzhevskyy <a href="mailto:skarzhevskyy@gmail.com">skarzhevskyy@gmail.com</a>
85 * @version $Revision: 1.5 $ ($Author: vlads $)
86 */
87 public class JTidyFilter implements Filter
88 {
89 /***
90 * name of the parameter <code>properties.filename</code> containing the properties file path.
91 */
92 public static final String CONFIG_PROPERTIES_FILE_NAME = Consts.CONFIG_PROPERTIES_FILE_NAME;
93
94 /***
95 * name of the parameter <code>config</code> containing JTidy Parser configutation string.
96 *
97 * JTidy Parser configutation string
98 * Examples of config string: indent: auto; indent-spaces: 2
99 */
100 public static final String CONFIG_CONFIG = "config";
101
102 /***
103 * name of the parameter <code>tee</code>.
104 * Do not buffer the output, Preform validation only
105 * This may send the responce back to browser while we are still parsing the HTML
106 * May solve problem for some applications that are flushing the OutputStream
107 */
108 public static final String CONFIG_TEE = "tee";
109
110 /***
111 * name of the parameter <code>validateOnly</code>.
112 *
113 * validateOnly only do not change output.
114 */
115 public static final String CONFIG_VALIDATE_ONLY = "validateOnly";
116
117 /***
118 * name of the parameter <code>doubleValidation</code>.
119 *
120 * Performs validation of html processed by <jtidy:tidy> jsp tag
121 * By default this is not done. Only Usefull for testing JTidy
122 * This will create second requestID to store the data
123 */
124 public static final String CONFIG_DOUBLE_VALIDATION = "doubleValidation";
125
126 /***
127 * name of the parameter <code>commentsSubst</code>.
128 * Special html comments:
129 * <!--jtidy:requestID-->
130 * <!--jtidy:validationImage-->
131 *
132 */
133 public static final String CONFIG_COMMENTS_SUBST = "commentsSubst";
134 /***
135 * Logger.
136 */
137 private Log log;
138
139 /***
140 * @see #CONFIG_CONFIG.
141 */
142 private String config;
143
144 /***
145 * @see #CONFIG_VALIDATE_ONLY
146 */
147 private boolean validateOnly;
148
149 /***
150 * @see #CONFIG_TEE.
151 */
152 private boolean tee;
153
154 /***
155 * @see #CONFIG_DOUBLE_VALIDATION.
156 */
157 private boolean doubleValidation;
158
159 /***
160 * @see #CONFIG_COMMENTS_SUBST.
161 */
162 private boolean commentsSubst;
163
164 /***
165 * Convert String to beelean.
166 * @param value "true"
167 * @param defaultValue if value anything else but true.
168 * @return Returns boolean value
169 */
170 private boolean getBoolean(String value, boolean defaultValue)
171 {
172 if (value != null)
173 {
174 return value.equalsIgnoreCase("true");
175 }
176 else
177 {
178 return defaultValue;
179 }
180 }
181
182 /***
183 * {@inheritDoc}
184 */
185 public void init(FilterConfig filterConfig)
186 {
187 log = LogFactory.getLog(JTidyFilter.class);
188
189 JTidyServletProperties.getInstance().loadFile(filterConfig.getInitParameter(CONFIG_PROPERTIES_FILE_NAME));
190
191 tee = getBoolean(filterConfig.getInitParameter(CONFIG_TEE), false);
192 doubleValidation = getBoolean(filterConfig.getInitParameter(CONFIG_DOUBLE_VALIDATION), false);
193 validateOnly = getBoolean(filterConfig.getInitParameter(CONFIG_VALIDATE_ONLY), false);
194 config = filterConfig.getInitParameter(CONFIG_CONFIG);
195 commentsSubst = getBoolean(filterConfig.getInitParameter(CONFIG_COMMENTS_SUBST), false);
196 }
197
198 /***
199 * {@inheritDoc}
200 */
201 public void destroy()
202 {
203
204 }
205
206 /***
207 * Buffer the Response.
208 * {@inheritDoc}
209 */
210 public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
211 throws IOException, ServletException
212 {
213 if (!(servletRequest instanceof HttpServletRequest))
214 {
215 log.debug("Filter has been called, Request is not HTML");
216
217 filterChain.doFilter(servletRequest, servletResponse);
218 return;
219 }
220
221 TidyProcessor tidyProcessor = new TidyProcessor(
222 ((HttpServletRequest) servletRequest).getSession(),
223 (HttpServletRequest) servletRequest,
224 (HttpServletResponse) servletResponse);
225 tidyProcessor.setValidateOnly(validateOnly);
226 tidyProcessor.setDoubleValidation(doubleValidation);
227 tidyProcessor.setConfig(config);
228 tidyProcessor.setCommentsSubst(commentsSubst);
229
230 log.debug(((HttpServletRequest) servletRequest).getRequestURI());
231
232 BufferedServletResponse wrappedResponse = new BufferedServletResponse(
233 (HttpServletResponse) servletResponse,
234 tidyProcessor);
235 wrappedResponse.setTee(tee);
236
237 try
238 {
239 filterChain.doFilter(servletRequest, wrappedResponse);
240 log.debug("Filter finished");
241 }
242 finally
243 {
244 wrappedResponse.finishResponse();
245 }
246 }
247
248 }