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.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  /**
51   * <a href="ThemeUtil.java.html"><b><i>View Source</i></b></a>
52   *
53   * @author Brian Wing Shun Chan
54   * @author Brian Myunghun Kim
55   *
56   */
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         // The servlet context name will be null when the theme is deployed to
134         // the root directory in Tomcat. See
135         // com.liferay.portal.servlet.MainServlet and
136         // com.liferay.portlet.PortletContextImpl for other cases where a null
137         // servlet context name is also converted to an empty string.
138 
139         String ctxName = GetterUtil.getString(theme.getServletContextName());
140 
141         if (VelocityContextPool.get(ctxName) == null) {
142 
143             // This should only happen if the Velocity template is the first
144             // page to be accessed in the system
145 
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         // Velocity variables
173 
174         VelocityVariables.insertVariables(vc, req);
175 
176         // liferay:include tag library
177 
178         StringServletResponse stringServletRes = new StringServletResponse(
179             (HttpServletResponse)pageContext.getResponse());
180 
181         VelocityTaglib velocityTaglib = new VelocityTaglib(
182             ctx, req, stringServletRes, pageContext);
183 
184         req.setAttribute(WebKeys.VELOCITY_TAGLIB, velocityTaglib);
185 
186         vc.put("taglibLiferay", velocityTaglib);
187         vc.put("theme", velocityTaglib);
188 
189         // Merge templates
190 
191         Velocity.mergeTemplate(source, StringPool.UTF8, vc, sw);
192 
193         // Print output
194 
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 }