001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.ServletResponseUtil;
020    import com.liferay.portal.kernel.servlet.StringServletResponse;
021    import com.liferay.portal.kernel.util.HttpUtil;
022    import com.liferay.portal.kernel.util.ParamUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.servlet.filters.BasePortalFilter;
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            @Override
039            public boolean isFilterEnabled(
040                    HttpServletRequest request, HttpServletResponse response) {
041    
042                    if (isFragment(request, response) && !isAlreadyFiltered(request)) {
043                            return true;
044                    }
045                    else {
046                            return false;
047                    }
048            }
049    
050            protected String getContent(HttpServletRequest request, String content) {
051                    String fragmentId = ParamUtil.getString(request, "p_f_id");
052    
053                    int x = content.indexOf("<!-- Begin fragment " + fragmentId + " -->");
054                    int y = content.indexOf("<!-- End fragment " + fragmentId + " -->");
055    
056                    if ((x == -1) || (y == -1)) {
057                            return content;
058                    }
059    
060                    x = content.indexOf(">", x);
061    
062                    return content.substring(x + 1, y);
063            }
064    
065            protected boolean isAlreadyFiltered(HttpServletRequest request) {
066                    if (request.getAttribute(SKIP_FILTER) != null) {
067                            return true;
068                    }
069                    else {
070                            return false;
071                    }
072            }
073    
074            protected boolean isFragment(
075                    HttpServletRequest request, HttpServletResponse response) {
076    
077                    String fragmentId = ParamUtil.getString(request, "p_f_id");
078    
079                    if (Validator.isNotNull(fragmentId)) {
080                            return true;
081                    }
082                    else {
083                            return false;
084                    }
085            }
086    
087            @Override
088            protected void processFilter(
089                            HttpServletRequest request, HttpServletResponse response,
090                            FilterChain filterChain)
091                    throws Exception {
092    
093                    request.setAttribute(SKIP_FILTER, Boolean.TRUE);
094    
095                    if (_log.isDebugEnabled()) {
096                            String completeURL = HttpUtil.getCompleteURL(request);
097    
098                            _log.debug("Fragmenting " + completeURL);
099                    }
100    
101                    StringServletResponse stringServerResponse = new StringServletResponse(
102                            response);
103    
104                    processFilter(
105                            FragmentFilter.class, request, stringServerResponse, filterChain);
106    
107                    String content = getContent(request, stringServerResponse.getString());
108    
109                    ServletResponseUtil.write(response, content);
110            }
111    
112            private static Log _log = LogFactoryUtil.getLog(FragmentFilter.class);
113    
114    }