1
22
23 package com.liferay.taglib.util;
24
25 import com.liferay.portal.kernel.log.Log;
26 import com.liferay.portal.kernel.log.LogFactoryUtil;
27 import com.liferay.portal.kernel.servlet.StringServletResponse;
28 import com.liferay.portal.kernel.util.GetterUtil;
29 import com.liferay.portal.kernel.util.StringPool;
30 import com.liferay.portal.kernel.velocity.VelocityContext;
31 import com.liferay.portal.kernel.velocity.VelocityEngineUtil;
32 import com.liferay.portal.model.Theme;
33 import com.liferay.portal.theme.ThemeDisplay;
34 import com.liferay.portal.util.WebKeys;
35 import com.liferay.portal.velocity.VelocityContextPool;
36 import com.liferay.portal.velocity.VelocityVariables;
37
38 import java.io.StringWriter;
39
40 import javax.servlet.RequestDispatcher;
41 import javax.servlet.ServletContext;
42 import javax.servlet.http.HttpServletRequest;
43 import javax.servlet.http.HttpServletResponse;
44 import javax.servlet.jsp.PageContext;
45
46 import org.apache.struts.taglib.tiles.ComponentConstants;
47 import org.apache.struts.tiles.ComponentContext;
48
49
57 public class ThemeUtil {
58
59 public static void include(
60 ServletContext servletContext, HttpServletRequest request,
61 HttpServletResponse response, PageContext pageContext, String page,
62 Theme theme)
63 throws Exception {
64
65 String extension = theme.getTemplateExtension();
66
67 if (extension.equals("vm")) {
68 includeVM(servletContext, request, pageContext, page, theme, true);
69 }
70 else {
71 String path =
72 theme.getTemplatesPath() + StringPool.SLASH + page;
73
74 includeJSP(servletContext, request, response, path, theme);
75 }
76 }
77
78 public static void includeJSP(
79 ServletContext servletContext, HttpServletRequest request,
80 HttpServletResponse response, String path, Theme theme)
81 throws Exception {
82
83 String tilesTitle = _getTilesVariables(request, "title");
84 String tilesContent = _getTilesVariables(request, "content");
85 boolean tilesSelectable = GetterUtil.getBoolean(
86 _getTilesVariables(request, "selectable"));
87
88 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
89 WebKeys.THEME_DISPLAY);
90
91 themeDisplay.setTilesTitle(tilesTitle);
92 themeDisplay.setTilesContent(tilesContent);
93 themeDisplay.setTilesSelectable(tilesSelectable);
94
95 if (theme.isWARFile()) {
96 ServletContext themeServletContext = servletContext.getContext(
97 theme.getContextPath());
98
99 if (themeServletContext == null) {
100 _log.error(
101 "Theme " + theme.getThemeId() + " cannot find its " +
102 "servlet context at " + theme.getServletContextName());
103 }
104 else {
105 RequestDispatcher requestDispatcher =
106 themeServletContext.getRequestDispatcher(path);
107
108 if (requestDispatcher == null) {
109 _log.error(
110 "Theme " + theme.getThemeId() + " does not have " +
111 path);
112 }
113 else {
114 requestDispatcher.include(request, response);
115 }
116 }
117 }
118 else {
119 RequestDispatcher requestDispatcher =
120 servletContext.getRequestDispatcher(path);
121
122 if (requestDispatcher == null) {
123 _log.error(
124 "Theme " + theme.getThemeId() + " does not have " + path);
125 }
126 else {
127 requestDispatcher.include(request, response);
128 }
129 }
130 }
131
132 public static String includeVM(
133 ServletContext servletContext, HttpServletRequest request,
134 PageContext pageContext, String page, Theme theme, boolean write)
135 throws Exception {
136
137
143 String ctxName = GetterUtil.getString(theme.getServletContextName());
144
145 if (VelocityContextPool.get(ctxName) == null) {
146
147
150 VelocityContextPool.put(ctxName, servletContext);
151 }
152
153 int pos = page.lastIndexOf(StringPool.PERIOD);
154
155 StringBuilder sb = new StringBuilder();
156
157 sb.append(ctxName);
158 sb.append(theme.getVelocityResourceListener());
159 sb.append(theme.getTemplatesPath());
160 sb.append(StringPool.SLASH);
161 sb.append(page.substring(0, pos));
162 sb.append(".vm");
163
164 String source = sb.toString();
165
166 if (!VelocityEngineUtil.resourceExists(source)) {
167 _log.error(source + " does not exist");
168
169 return null;
170 }
171
172 StringWriter stringWriter = new StringWriter();
173
174 VelocityContext velocityContext =
175 VelocityEngineUtil.getWrappedStandardToolsContext();
176
177
179 VelocityVariables.insertVariables(velocityContext, request);
180
181
183 ServletContext themeServletContext = VelocityContextPool.get(ctxName);
184
185
187 StringServletResponse stringResponse = new StringServletResponse(
188 (HttpServletResponse)pageContext.getResponse());
189
190 VelocityTaglib velocityTaglib = new VelocityTaglib(
191 servletContext, request, stringResponse, pageContext);
192
193 request.setAttribute(WebKeys.VELOCITY_TAGLIB, velocityTaglib);
194
195 velocityContext.put("themeServletContext", themeServletContext);
196 velocityContext.put("taglibLiferay", velocityTaglib);
197 velocityContext.put("theme", velocityTaglib);
198
199
201 VelocityEngineUtil.mergeTemplate(source, velocityContext, stringWriter);
202
203
205 String output = stringWriter.toString();
206
207 if (write) {
208 pageContext.getOut().print(output);
209
210 return null;
211 }
212 else {
213 return output;
214 }
215 }
216
217 private static String _getTilesVariables(
218 HttpServletRequest request, String attributeName) {
219
220 ComponentContext componentContext =
221 (ComponentContext)request.getAttribute(
222 ComponentConstants.COMPONENT_CONTEXT);
223
224 String value = null;
225
226 if (componentContext != null) {
227 value = (String)componentContext.getAttribute(attributeName);
228 }
229
230 return value;
231 }
232
233 private static Log _log = LogFactoryUtil.getLog(ThemeUtil.class);
234
235 }