1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.servlet.filters.velocity;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.servlet.BrowserSnifferUtil;
28  import com.liferay.portal.kernel.util.HttpUtil;
29  import com.liferay.portal.kernel.util.LocaleUtil;
30  import com.liferay.portal.kernel.util.ParamUtil;
31  import com.liferay.portal.kernel.util.StringPool;
32  import com.liferay.portal.model.ColorScheme;
33  import com.liferay.portal.model.Company;
34  import com.liferay.portal.model.Theme;
35  import com.liferay.portal.service.CompanyLocalServiceUtil;
36  import com.liferay.portal.service.ThemeLocalServiceUtil;
37  import com.liferay.portal.servlet.filters.BasePortalFilter;
38  import com.liferay.portal.theme.ThemeDisplay;
39  import com.liferay.portal.theme.ThemeDisplayFactory;
40  import com.liferay.portal.util.PortalUtil;
41  import com.liferay.portal.util.WebKeys;
42  import com.liferay.portal.velocity.VelocityVariables;
43  import com.liferay.util.servlet.filters.CacheResponse;
44  import com.liferay.util.servlet.filters.CacheResponseData;
45  import com.liferay.util.servlet.filters.CacheResponseUtil;
46  
47  import java.io.IOException;
48  import java.io.StringReader;
49  import java.io.StringWriter;
50  
51  import java.util.Locale;
52  import java.util.regex.Matcher;
53  import java.util.regex.Pattern;
54  
55  import javax.servlet.FilterChain;
56  import javax.servlet.FilterConfig;
57  import javax.servlet.ServletException;
58  import javax.servlet.http.HttpServletRequest;
59  import javax.servlet.http.HttpServletResponse;
60  
61  import org.apache.velocity.VelocityContext;
62  import org.apache.velocity.app.Velocity;
63  
64  /**
65   * <a href="VelocityFilter.java.html"><b><i>View Source</i></b></a>
66   *
67   * @author Brian Wing Shun Chan
68   * @author Raymond Augé
69   *
70   */
71  public class VelocityFilter extends BasePortalFilter {
72  
73      public void init(FilterConfig filterConfig) {
74          super.init(filterConfig);
75  
76          String pattern = filterConfig.getInitParameter("pattern");
77  
78          _pattern = Pattern.compile(pattern);
79      }
80  
81      protected boolean isMatchingURL(String completeURL) {
82          Matcher matcher = _pattern.matcher(completeURL);
83  
84          return matcher.matches();
85      }
86  
87      protected void processFilter(
88              HttpServletRequest request, HttpServletResponse response,
89              FilterChain filterChain)
90          throws IOException, ServletException {
91  
92          String completeURL = HttpUtil.getCompleteURL(request);
93  
94          if (isMatchingURL(completeURL)) {
95              if (_log.isDebugEnabled()) {
96                  _log.debug("Processing " + completeURL);
97              }
98  
99              CacheResponse cacheResponse = new CacheResponse(
100                 response, StringPool.UTF8);
101 
102             processFilter(
103                 VelocityFilter.class, request, cacheResponse, filterChain);
104 
105             VelocityContext context = new VelocityContext();
106 
107             StringReader reader = new StringReader(
108                 new String(cacheResponse.getData()));
109             StringWriter writer = new StringWriter();
110 
111             ThemeDisplay themeDisplay = null;
112 
113             try {
114 
115                 // Company
116 
117                 long companyId = ParamUtil.getLong(request, "companyId");
118 
119                 Company company = CompanyLocalServiceUtil.getCompanyById(
120                     companyId);
121 
122                 // Paths
123 
124                 String contextPath = PortalUtil.getPathContext();
125 
126                 // Locale
127 
128                 String languageId = ParamUtil.getString(request, "languageId");
129 
130                 Locale locale = LocaleUtil.fromLanguageId(languageId);
131 
132                 // Theme and color scheme
133 
134                 String themeId = ParamUtil.getString(request, "themeId");
135                 String colorSchemeId = ParamUtil.getString(
136                     request, "colorSchemeId");
137 
138                 boolean wapTheme = BrowserSnifferUtil.is_wap(request);
139 
140                 Theme theme = ThemeLocalServiceUtil.getTheme(
141                     companyId, themeId, wapTheme);
142                 ColorScheme colorScheme = ThemeLocalServiceUtil.getColorScheme(
143                     companyId, theme.getThemeId(), colorSchemeId, wapTheme);
144 
145                 // Theme display
146 
147                 themeDisplay = ThemeDisplayFactory.create();
148 
149                 themeDisplay.setCompany(company);
150                 themeDisplay.setLocale(locale);
151                 themeDisplay.setLookAndFeel(contextPath, theme, colorScheme);
152                 themeDisplay.setPathContext(contextPath);
153 
154                 request.setAttribute(WebKeys.THEME_DISPLAY, themeDisplay);
155 
156                 // Velocity variables
157 
158                 VelocityVariables.insertVariables(context, request);
159 
160                 // Evaluate template
161 
162                 Velocity.evaluate(
163                     context, writer, VelocityFilter.class.getName(), reader);
164             }
165             catch (Exception e) {
166                 _log.error(e, e);
167             }
168             finally {
169                 try {
170                     if (themeDisplay != null) {
171                         ThemeDisplayFactory.recycle(themeDisplay);
172                     }
173                 }
174                 catch (Exception e) {
175                 }
176             }
177 
178             CacheResponseData data = new CacheResponseData(
179                 writer.toString().getBytes(StringPool.UTF8),
180                 cacheResponse.getContentType(), cacheResponse.getHeaders());
181 
182             CacheResponseUtil.write(response, data);
183         }
184         else {
185             if (_log.isDebugEnabled()) {
186                 _log.debug("Not processing " + completeURL);
187             }
188 
189             processFilter(VelocityFilter.class, request, response, filterChain);
190         }
191     }
192 
193     private static Log _log = LogFactoryUtil.getLog(VelocityFilter.class);
194 
195     private Pattern _pattern;
196 
197 }