1   /**
2    * Copyright (c) 2000-2009 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.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  /**
50   * <a href="ThemeUtil.java.html"><b><i>View Source</i></b></a>
51   *
52   * @author Brian Wing Shun Chan
53   * @author Brian Myunghun Kim
54   * @author Raymond Augé
55   *
56   */
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         // The servlet context name will be null when the theme is deployed to
138         // the root directory in Tomcat. See
139         // com.liferay.portal.servlet.MainServlet and
140         // com.liferay.portlet.PortletContextImpl for other cases where a null
141         // servlet context name is also converted to an empty string.
142 
143         String ctxName = GetterUtil.getString(theme.getServletContextName());
144 
145         if (VelocityContextPool.get(ctxName) == null) {
146 
147             // This should only happen if the Velocity template is the first
148             // page to be accessed in the system
149 
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         // Velocity variables
178 
179         VelocityVariables.insertVariables(velocityContext, request);
180 
181         // Theme servlet context
182 
183         ServletContext themeServletContext = VelocityContextPool.get(ctxName);
184 
185         // liferay:include tag library
186 
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         // Merge templates
200 
201         VelocityEngineUtil.mergeTemplate(source, velocityContext, stringWriter);
202 
203         // Print output
204 
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 }