1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.servlet.filters.validhtml;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.servlet.StringServletResponse;
20  import com.liferay.portal.kernel.util.HttpUtil;
21  import com.liferay.portal.kernel.util.StringPool;
22  import com.liferay.portal.kernel.util.StringUtil;
23  import com.liferay.portal.servlet.filters.BasePortalFilter;
24  import com.liferay.util.servlet.ServletResponseUtil;
25  
26  import javax.servlet.FilterChain;
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpServletResponse;
29  
30  /**
31   * <a href="ValidHtmlFilter.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Julio Camarero
34   */
35  public class ValidHtmlFilter extends BasePortalFilter {
36  
37      public static final String SKIP_FILTER =
38          ValidHtmlFilter.class.getName() + "SKIP_FILTER";
39  
40      protected String getContent(HttpServletRequest request, String content) {
41          content = StringUtil.replaceLast(
42              content, _CLOSE_BODY, StringPool.BLANK);
43          content = StringUtil.replaceLast(
44              content, _CLOSE_HTML, _CLOSE_BODY + _CLOSE_HTML);
45  
46          return content;
47      }
48  
49      protected boolean isAlreadyFiltered(HttpServletRequest request) {
50          if (request.getAttribute(SKIP_FILTER) != null) {
51              return true;
52          }
53          else {
54              return false;
55          }
56      }
57  
58      protected void processFilter(
59              HttpServletRequest request, HttpServletResponse response,
60              FilterChain filterChain)
61          throws Exception {
62  
63          if (isAlreadyFiltered(request)) {
64              processFilter(
65                  ValidHtmlFilter.class, request, response, filterChain);
66  
67              return;
68          }
69  
70          request.setAttribute(SKIP_FILTER, Boolean.TRUE);
71  
72          if (_log.isDebugEnabled()) {
73              String completeURL = HttpUtil.getCompleteURL(request);
74  
75              _log.debug("Ensuring valid HTML " + completeURL);
76          }
77  
78          StringServletResponse stringServerResponse = new StringServletResponse(
79              response);
80  
81          processFilter(
82              ValidHtmlFilter.class, request, stringServerResponse, filterChain);
83  
84          String content = getContent(request, stringServerResponse.getString());
85  
86          ServletResponseUtil.write(response, content);
87      }
88  
89      private static final String _CLOSE_BODY = "</body>";
90  
91      private static final String _CLOSE_HTML = "</html>";
92  
93      private static Log _log = LogFactoryUtil.getLog(ValidHtmlFilter.class);
94  
95  }