001    /**
002     * Copyright (c) 2000-2012 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 (isAlreadyFiltered(request)) {
046                            return false;
047                    }
048                    else {
049                            return true;
050                    }
051            }
052    
053            protected String getContent(HttpServletRequest request, String content) {
054                    content = StringUtil.replaceLast(
055                            content, _CLOSE_BODY, StringPool.BLANK);
056                    content = StringUtil.replaceLast(
057                            content, _CLOSE_HTML, _CLOSE_BODY + _CLOSE_HTML);
058    
059                    return content;
060            }
061    
062            protected boolean isAlreadyFiltered(HttpServletRequest request) {
063                    if (request.getAttribute(SKIP_FILTER) != null) {
064                            return true;
065                    }
066                    else {
067                            return false;
068                    }
069            }
070    
071            @Override
072            protected void processFilter(
073                            HttpServletRequest request, HttpServletResponse response,
074                            FilterChain filterChain)
075                    throws Exception {
076    
077                    request.setAttribute(SKIP_FILTER, Boolean.TRUE);
078    
079                    if (_log.isDebugEnabled()) {
080                            String completeURL = HttpUtil.getCompleteURL(request);
081    
082                            _log.debug("Ensuring valid HTML " + completeURL);
083                    }
084    
085                    StringServletResponse stringServerResponse = new StringServletResponse(
086                            response);
087    
088                    processFilter(
089                            ValidHtmlFilter.class, request, stringServerResponse, filterChain);
090    
091                    String contentType = response.getContentType();
092    
093                    if ((contentType != null) &&
094                            contentType.startsWith(ContentTypes.TEXT_HTML)) {
095    
096                            String content = getContent(
097                                    request, stringServerResponse.getString());
098    
099                            ServletResponseUtil.write(response, content);
100                    }
101                    else {
102                            ServletResponseUtil.write(response, stringServerResponse);
103                    }
104            }
105    
106            private static final String _CLOSE_BODY = "</body>";
107    
108            private static final String _CLOSE_HTML = "</html>";
109    
110            private static Log _log = LogFactoryUtil.getLog(ValidHtmlFilter.class);
111    
112    }