001    /**
002     * Copyright (c) 2000-2010 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.themepreview;
016    
017    import com.liferay.portal.kernel.servlet.StringServletResponse;
018    import com.liferay.portal.kernel.util.ParamUtil;
019    import com.liferay.portal.servlet.filters.BasePortalFilter;
020    import com.liferay.portal.servlet.filters.strip.StripFilter;
021    import com.liferay.portal.theme.ThemeDisplay;
022    import com.liferay.portal.util.WebKeys;
023    import com.liferay.util.servlet.ServletResponseUtil;
024    
025    import java.util.regex.Matcher;
026    import java.util.regex.Pattern;
027    
028    import javax.servlet.FilterChain;
029    import javax.servlet.http.HttpServletRequest;
030    import javax.servlet.http.HttpServletResponse;
031    
032    /**
033     * @author Ganesh Ram
034     */
035    public class ThemePreviewFilter extends BasePortalFilter {
036    
037            protected String getContent(HttpServletRequest request, String content) {
038                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
039                            WebKeys.THEME_DISPLAY);
040    
041                    Pattern cssPattern = Pattern.compile(
042                            themeDisplay.getPathThemeCss());
043    
044                    Matcher cssMatcher = cssPattern.matcher(content);
045    
046                    content = cssMatcher.replaceAll("css");
047    
048                    Pattern imagePattern = Pattern.compile(
049                            themeDisplay.getPathThemeImages());
050    
051                    Matcher imageMatcher = imagePattern.matcher(content);
052    
053                    content = imageMatcher.replaceAll("images");
054    
055                    return content;
056            }
057    
058            protected boolean isThemePreview(HttpServletRequest request) {
059                    if (ParamUtil.getBoolean(request, _THEME_PREVIEW)) {
060                            return true;
061                    }
062                    else {
063                            return false;
064                    }
065            }
066    
067            protected void processFilter(
068                            HttpServletRequest request, HttpServletResponse response,
069                            FilterChain filterChain)
070                    throws Exception {
071    
072                    if (isThemePreview(request)) {
073                            request.setAttribute(StripFilter.SKIP_FILTER, Boolean.TRUE);
074    
075                            StringServletResponse stringServerResponse =
076                                    new StringServletResponse(response);
077    
078                            processFilter(
079                                    ThemePreviewFilter.class, request, stringServerResponse,
080                                    filterChain);
081    
082                            String content = getContent(
083                                    request, stringServerResponse.getString());
084    
085                            ServletResponseUtil.write(response, content);
086                    }
087                    else {
088                            processFilter(
089                                    ThemePreviewFilter.class, request, response, filterChain);
090                    }
091            }
092    
093            private static final String _THEME_PREVIEW = "themePreview";
094    
095    }