1   /**
2    * Copyright (c) 2000-2007 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.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.Template;
48  import org.apache.velocity.VelocityContext;
49  import org.apache.velocity.app.Velocity;
50  
51  /**
52   * <a href="ThemeUtil.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Brian Wing Shun Chan
55   * @author Brian Myunghun Kim
56   *
57   */
58  public class ThemeUtil {
59  
60      public static void include(
61              ServletContext ctx, HttpServletRequest req, HttpServletResponse res,
62              PageContext pageContext, String page, Theme theme)
63          throws Exception {
64  
65          String extension = theme.getTemplateExtension();
66  
67          if (extension.equals("vm")) {
68              includeVM(ctx, req, pageContext, page, theme, true);
69          }
70          else {
71              String path =
72                  theme.getTemplatesPath() + StringPool.SLASH + page;
73  
74              includeJSP(ctx, req, res, path, theme);
75          }
76      }
77  
78      public static void includeJSP(
79              ServletContext ctx, HttpServletRequest req, HttpServletResponse res,
80              String path, Theme theme)
81          throws Exception {
82  
83          String tilesTitle = _getTilesVariables(req, "title");
84          String tilesContent = _getTilesVariables(req, "content");
85          boolean tilesSelectable = GetterUtil.getBoolean(
86              _getTilesVariables(req, "selectable"));
87  
88          ThemeDisplay themeDisplay =
89              (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
90  
91          themeDisplay.setTilesTitle(tilesTitle);
92          themeDisplay.setTilesContent(tilesContent);
93          themeDisplay.setTilesSelectable(tilesSelectable);
94  
95          if (theme.isWARFile()) {
96              ServletContext themeCtx = ctx.getContext(theme.getContextPath());
97  
98              if (themeCtx == null) {
99                  _log.error(
100                     "Theme " + theme.getThemeId() + " cannot find its " +
101                         "servlet context at " + theme.getServletContextName());
102             }
103             else {
104                 RequestDispatcher rd = themeCtx.getRequestDispatcher(path);
105 
106                 if (rd == null) {
107                     _log.error(
108                         "Theme " + theme.getThemeId() + " does not have " +
109                             path);
110                 }
111                 else {
112                     rd.include(req, res);
113                 }
114             }
115         }
116         else {
117             RequestDispatcher rd = ctx.getRequestDispatcher(path);
118 
119             if (rd == null) {
120                 _log.error(
121                     "Theme " + theme.getThemeId() + " does not have " + path);
122             }
123             else {
124                 rd.include(req, res);
125             }
126         }
127     }
128 
129     public static String includeVM(
130             ServletContext ctx, HttpServletRequest req, PageContext pageContext,
131             String page, Theme theme, boolean write)
132         throws Exception {
133 
134         // Get template
135 
136         // The servlet context name will be null when the theme is deployed to
137         // the root directory in Tomcat. See
138         // com.liferay.portal.servlet.MainServlet and
139         // com.liferay.portlet.PortletContextImpl for other cases where a null
140         // servlet context name is also converted to an empty string.
141 
142         String ctxName = GetterUtil.getString(theme.getServletContextName());
143 
144         if (VelocityContextPool.get(ctxName) == null) {
145 
146             // This should only happen if the Velocity template is the first
147             // page to be accessed in the system
148 
149             VelocityContextPool.put(ctxName, ctx);
150         }
151 
152         int pos = page.lastIndexOf(StringPool.PERIOD);
153 
154         StringMaker sm = new StringMaker();
155 
156         sm.append(ctxName);
157         sm.append(theme.getVelocityResourceListener());
158         sm.append(theme.getTemplatesPath());
159         sm.append(StringPool.SLASH);
160         sm.append(page.substring(0, pos));
161         sm.append(".vm");
162 
163         String source = sm.toString();
164 
165         if (!Velocity.resourceExists(source)) {
166             _log.error(source + " does not exist");
167 
168             return null;
169         }
170 
171         Template template = Velocity.getTemplate(source);
172 
173         StringWriter sw = new StringWriter();
174 
175         VelocityContext vc = new VelocityContext();
176 
177         // Velocity variables
178 
179         VelocityVariables.insertVariables(vc, req);
180 
181         // liferay:include tag library
182 
183         StringServletResponse stringServletResponse = new StringServletResponse(
184             (HttpServletResponse)pageContext.getResponse());
185 
186         VelocityTaglib velocityTaglib = new VelocityTaglib(
187             ctx, req, stringServletResponse, pageContext);
188 
189         req.setAttribute(WebKeys.VELOCITY_TAGLIB, velocityTaglib);
190 
191         vc.put("taglibLiferay", velocityTaglib);
192         vc.put("theme", velocityTaglib);
193 
194         // Merge templates
195 
196         template.merge(vc, sw);
197 
198         // Print output
199 
200         String output = sw.toString();
201 
202         if (write) {
203             pageContext.getOut().print(output);
204 
205             return null;
206         }
207         else {
208             return output;
209         }
210     }
211 
212     private static String _getTilesVariables(
213         HttpServletRequest req, String attributeName) {
214 
215         ComponentContext componentContext = (ComponentContext)req.getAttribute(
216             ComponentConstants.COMPONENT_CONTEXT);
217 
218         String value = null;
219 
220         if (componentContext != null) {
221             value = (String)componentContext.getAttribute(attributeName);
222         }
223 
224         return value;
225     }
226 
227     private static Log _log = LogFactory.getLog(ThemeUtil.class);
228 
229 }