1
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
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
117 long companyId = ParamUtil.getLong(request, "companyId");
118
119 Company company = CompanyLocalServiceUtil.getCompanyById(
120 companyId);
121
122
124 String contextPath = PortalUtil.getPathContext();
125
126
128 String languageId = ParamUtil.getString(request, "languageId");
129
130 Locale locale = LocaleUtil.fromLanguageId(languageId);
131
132
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
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
158 VelocityVariables.insertVariables(context, request);
159
160
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 }