001    /**
002     * Copyright (c) 2000-2010 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.fragment;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.servlet.StringServletResponse;
020    import com.liferay.portal.kernel.util.HttpUtil;
021    import com.liferay.portal.kernel.util.ParamUtil;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.servlet.filters.BasePortalFilter;
024    import com.liferay.util.servlet.ServletResponseUtil;
025    
026    import javax.servlet.FilterChain;
027    import javax.servlet.http.HttpServletRequest;
028    import javax.servlet.http.HttpServletResponse;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     */
033    public class FragmentFilter extends BasePortalFilter {
034    
035            public static final String SKIP_FILTER =
036                    FragmentFilter.class.getName() + "SKIP_FILTER";
037    
038            protected String getContent(HttpServletRequest request, String content) {
039                    String fragmentId = ParamUtil.getString(request, "p_f_id");
040    
041                    int x = content.indexOf("<!-- Begin fragment " + fragmentId + " -->");
042                    int y = content.indexOf("<!-- End fragment " + fragmentId + " -->");
043    
044                    if ((x == -1) || (y == -1)) {
045                            return content;
046                    }
047    
048                    x = content.indexOf(">", x);
049    
050                    return content.substring(x + 1, y);
051            }
052    
053            protected boolean isAlreadyFiltered(HttpServletRequest request) {
054                    if (request.getAttribute(SKIP_FILTER) != null) {
055                            return true;
056                    }
057                    else {
058                            return false;
059                    }
060            }
061    
062            protected boolean isFragment(
063                    HttpServletRequest request, HttpServletResponse response) {
064    
065                    String fragmentId = ParamUtil.getString(request, "p_f_id");
066    
067                    if (Validator.isNotNull(fragmentId)) {
068                            return true;
069                    }
070                    else {
071                            return false;
072                    }
073            }
074    
075            protected void processFilter(
076                            HttpServletRequest request, HttpServletResponse response,
077                            FilterChain filterChain)
078                    throws Exception {
079    
080                    if (isFragment(request, response) && !isAlreadyFiltered(request)) {
081                            request.setAttribute(SKIP_FILTER, Boolean.TRUE);
082    
083                            if (_log.isDebugEnabled()) {
084                                    String completeURL = HttpUtil.getCompleteURL(request);
085    
086                                    _log.debug("Fragmenting " + completeURL);
087                            }
088    
089                            StringServletResponse stringServerResponse =
090                                    new StringServletResponse(response);
091    
092                            processFilter(
093                                    FragmentFilter.class, request, stringServerResponse,
094                                    filterChain);
095    
096                            String content = getContent(
097                                    request, stringServerResponse.getString());
098    
099                            ServletResponseUtil.write(response, content);
100                    }
101                    else {
102                            if (_log.isDebugEnabled()) {
103                                    String completeURL = HttpUtil.getCompleteURL(request);
104    
105                                    _log.debug("Not fragmenting " + completeURL);
106                            }
107    
108                            processFilter(FragmentFilter.class, request, response, filterChain);
109                    }
110            }
111    
112            private static Log _log = LogFactoryUtil.getLog(FragmentFilter.class);
113    
114    }