1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.servlet.filters.themepreview;
16  
17  import com.liferay.portal.kernel.servlet.StringServletResponse;
18  import com.liferay.portal.kernel.util.ParamUtil;
19  import com.liferay.portal.servlet.filters.BasePortalFilter;
20  import com.liferay.portal.servlet.filters.strip.StripFilter;
21  import com.liferay.portal.theme.ThemeDisplay;
22  import com.liferay.portal.util.WebKeys;
23  import com.liferay.util.servlet.ServletResponseUtil;
24  
25  import java.util.regex.Matcher;
26  import java.util.regex.Pattern;
27  
28  import javax.servlet.FilterChain;
29  import javax.servlet.http.HttpServletRequest;
30  import javax.servlet.http.HttpServletResponse;
31  
32  /**
33   * <a href="ThemePreviewFilter.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Ganesh Ram
36   */
37  public class ThemePreviewFilter extends BasePortalFilter {
38  
39      protected String getContent(HttpServletRequest request, String content) {
40          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
41              WebKeys.THEME_DISPLAY);
42  
43          Pattern cssPattern = Pattern.compile(
44              themeDisplay.getPathThemeCss());
45  
46          Matcher cssMatcher = cssPattern.matcher(content);
47  
48          content = cssMatcher.replaceAll("css");
49  
50          Pattern imagePattern = Pattern.compile(
51              themeDisplay.getPathThemeImages());
52  
53          Matcher imageMatcher = imagePattern.matcher(content);
54  
55          content = imageMatcher.replaceAll("images");
56  
57          return content;
58      }
59  
60      protected boolean isThemePreview(HttpServletRequest request) {
61          if (ParamUtil.getBoolean(request, _THEME_PREVIEW)) {
62              return true;
63          }
64          else {
65              return false;
66          }
67      }
68  
69      protected void processFilter(
70              HttpServletRequest request, HttpServletResponse response,
71              FilterChain filterChain)
72          throws Exception {
73  
74          if (isThemePreview(request)) {
75              request.setAttribute(StripFilter.SKIP_FILTER, Boolean.TRUE);
76  
77              StringServletResponse stringServerResponse =
78                  new StringServletResponse(response);
79  
80              processFilter(
81                  ThemePreviewFilter.class, request, stringServerResponse,
82                  filterChain);
83  
84              String content = getContent(
85                  request, stringServerResponse.getString());
86  
87              ServletResponseUtil.write(response, content);
88          }
89          else {
90              processFilter(
91                  ThemePreviewFilter.class, request, response, filterChain);
92          }
93      }
94  
95      private static final String _THEME_PREVIEW = "themePreview";
96  
97  }