View Javadoc

1   /*
2    *  Java HTML Tidy - JTidy
3    *  HTML parser and pretty printer
4    *
5    *  Copyright (c) 1998-2000 World Wide Web Consortium (Massachusetts
6    *  Institute of Technology, Institut National de Recherche en
7    *  Informatique et en Automatique, Keio University). All Rights
8    *  Reserved.
9    *
10   *  Contributing Author(s):
11   *
12   *     Dave Raggett <dsr@w3.org>
13   *     Andy Quick <ac.quick@sympatico.ca> (translation to Java)
14   *     Gary L Peskin <garyp@firstech.com> (Java development)
15   *     Sami Lempinen <sami@lempinen.net> (release management)
16   *     Fabrizio Giustina <fgiust at users.sourceforge.net>
17   *     Vlad Skarzhevskyy <vlads at users.sourceforge.net> (JTidy servlet  development)
18   *
19   *  The contributing author(s) would like to thank all those who
20   *  helped with testing, bug fixes, and patience.  This wouldn't
21   *  have been possible without all of you.
22   *
23   *  COPYRIGHT NOTICE:
24   *
25   *  This software and documentation is provided "as is," and
26   *  the copyright holders and contributing author(s) make no
27   *  representations or warranties, express or implied, including
28   *  but not limited to, warranties of merchantability or fitness
29   *  for any particular purpose or that the use of the software or
30   *  documentation will not infringe any third party patents,
31   *  copyrights, trademarks or other rights.
32   *
33   *  The copyright holders and contributing author(s) will not be
34   *  liable for any direct, indirect, special or consequential damages
35   *  arising out of any use of the software or documentation, even if
36   *  advised of the possibility of such damage.
37   *
38   *  Permission is hereby granted to use, copy, modify, and distribute
39   *  this source code, or portions hereof, documentation and executables,
40   *  for any purpose, without fee, subject to the following restrictions:
41   *
42   *  1. The origin of this source code must not be misrepresented.
43   *  2. Altered versions must be plainly marked as such and must
44   *     not be misrepresented as being the original source.
45   *  3. This Copyright notice may not be removed or altered from any
46   *     source or altered source distribution.
47   *
48   *  The copyright holders and contributing author(s) specifically
49   *  permit, without fee, and encourage the use of this source code
50   *  as a component for supporting the Hypertext Markup Language in
51   *  commercial products. If you use this source code in a product,
52   *  acknowledgment is not required but would be appreciated.
53   *
54   */
55  package org.w3c.tidy.servlet.filter;
56  /*
57   * Created on 02.10.2004 by vlads
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 &lt;jtidy:tidy&gt; 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      * &lt;!--jtidy:requestID--&gt;
130      * &lt;!--jtidy:validationImage--&gt;
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         // nothing to destroy
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             // don't filter!
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 }