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