001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.servlet.filters.validhtml;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.servlet.ServletResponseUtil;
020    import com.liferay.portal.kernel.servlet.StringServletResponse;
021    import com.liferay.portal.kernel.util.ContentTypes;
022    import com.liferay.portal.kernel.util.HttpUtil;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.kernel.util.StringUtil;
025    import com.liferay.portal.servlet.filters.BasePortalFilter;
026    
027    import javax.servlet.FilterChain;
028    import javax.servlet.http.HttpServletRequest;
029    import javax.servlet.http.HttpServletResponse;
030    
031    /**
032     * @author Julio Camarero
033     * @author Brian Wing Shun Chan
034     * @author Shuyang Zhou
035     */
036    public class ValidHtmlFilter extends BasePortalFilter {
037    
038            public static final String SKIP_FILTER =
039                    ValidHtmlFilter.class.getName() + "SKIP_FILTER";
040    
041            @Override
042            public boolean isFilterEnabled(
043                    HttpServletRequest request, HttpServletResponse response) {
044    
045                    if (isEnsureValidHtml(request, response) &&
046                            !isAlreadyFiltered(request)) {
047    
048                            return true;
049                    }
050                    else {
051                            return false;
052                    }
053            }
054    
055            protected String getContent(HttpServletRequest request, String content) {
056                    content = StringUtil.replaceLast(
057                            content, _CLOSE_BODY, StringPool.BLANK);
058                    content = StringUtil.replaceLast(
059                            content, _CLOSE_HTML, _CLOSE_BODY + _CLOSE_HTML);
060    
061                    return content;
062            }
063    
064            protected boolean isAlreadyFiltered(HttpServletRequest request) {
065                    if (request.getAttribute(SKIP_FILTER) != null) {
066                            return true;
067                    }
068                    else {
069                            return false;
070                    }
071            }
072    
073            protected boolean isEnsureValidHtml(
074                    HttpServletRequest request, HttpServletResponse response) {
075    
076                    String contentType = response.getContentType();
077    
078                    if ((contentType != null) &&
079                            contentType.startsWith(ContentTypes.TEXT_HTML)) {
080    
081                            return true;
082                    }
083                    else {
084                            return false;
085                    }
086            }
087    
088            @Override
089            protected void processFilter(
090                            HttpServletRequest request, HttpServletResponse response,
091                            FilterChain filterChain)
092                    throws Exception {
093    
094                    request.setAttribute(SKIP_FILTER, Boolean.TRUE);
095    
096                    if (_log.isDebugEnabled()) {
097                            String completeURL = HttpUtil.getCompleteURL(request);
098    
099                            _log.debug("Ensuring valid HTML " + completeURL);
100                    }
101    
102                    StringServletResponse stringServerResponse = new StringServletResponse(
103                            response);
104    
105                    processFilter(
106                            ValidHtmlFilter.class, request, stringServerResponse, filterChain);
107    
108                    String content = getContent(request, stringServerResponse.getString());
109    
110                    ServletResponseUtil.write(response, content);
111            }
112    
113            private static final String _CLOSE_BODY = "</body>";
114    
115            private static final String _CLOSE_HTML = "</html>";
116    
117            private static Log _log = LogFactoryUtil.getLog(ValidHtmlFilter.class);
118    
119    }