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