1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   * 
13   */
14  
15  package com.liferay.taglib.util;
16  
17  import com.liferay.portal.kernel.io.unsync.UnsyncStringWriter;
18  import com.liferay.portal.kernel.log.Log;
19  import com.liferay.portal.kernel.log.LogFactoryUtil;
20  import com.liferay.portal.kernel.servlet.StringServletResponse;
21  import com.liferay.portal.kernel.util.GetterUtil;
22  import com.liferay.portal.kernel.util.StringBundler;
23  import com.liferay.portal.kernel.util.StringPool;
24  import com.liferay.portal.kernel.util.WebKeys;
25  import com.liferay.portal.kernel.velocity.VelocityContext;
26  import com.liferay.portal.kernel.velocity.VelocityEngineUtil;
27  import com.liferay.portal.model.Theme;
28  import com.liferay.portal.theme.ThemeDisplay;
29  import com.liferay.portal.velocity.VelocityContextPool;
30  import com.liferay.portal.velocity.VelocityVariables;
31  
32  import javax.servlet.RequestDispatcher;
33  import javax.servlet.ServletContext;
34  import javax.servlet.http.HttpServletRequest;
35  import javax.servlet.http.HttpServletResponse;
36  import javax.servlet.jsp.PageContext;
37  
38  import org.apache.struts.taglib.tiles.ComponentConstants;
39  import org.apache.struts.tiles.ComponentContext;
40  
41  /**
42   * <a href="ThemeUtil.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   * @author Brian Myunghun Kim
46   * @author Raymond Augé
47   */
48  public class ThemeUtil {
49  
50      public static void include(
51              ServletContext servletContext, HttpServletRequest request,
52              HttpServletResponse response, PageContext pageContext, String page,
53              Theme theme)
54          throws Exception {
55  
56          String extension = theme.getTemplateExtension();
57  
58          if (extension.equals(_TEMPLATE_EXTENSION_VM)) {
59              includeVM(servletContext, request, pageContext, page, theme, true);
60          }
61          else {
62              String path =
63                  theme.getTemplatesPath() + StringPool.SLASH + page;
64  
65              includeJSP(servletContext, request, response, path, theme);
66          }
67      }
68  
69      public static void includeJSP(
70              ServletContext servletContext, HttpServletRequest request,
71              HttpServletResponse response, String path, Theme theme)
72          throws Exception {
73  
74          String tilesTitle = _getTilesVariables(request, "title");
75          String tilesContent = _getTilesVariables(request, "content");
76          boolean tilesSelectable = GetterUtil.getBoolean(
77              _getTilesVariables(request, "selectable"));
78  
79          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
80              WebKeys.THEME_DISPLAY);
81  
82          themeDisplay.setTilesTitle(tilesTitle);
83          themeDisplay.setTilesContent(tilesContent);
84          themeDisplay.setTilesSelectable(tilesSelectable);
85  
86          if (theme.isWARFile()) {
87              ServletContext themeServletContext = servletContext.getContext(
88                  theme.getContextPath());
89  
90              if (themeServletContext == null) {
91                  _log.error(
92                      "Theme " + theme.getThemeId() + " cannot find its " +
93                          "servlet context at " + theme.getServletContextName());
94              }
95              else {
96                  RequestDispatcher requestDispatcher =
97                      themeServletContext.getRequestDispatcher(path);
98  
99                  if (requestDispatcher == null) {
100                     _log.error(
101                         "Theme " + theme.getThemeId() + " does not have " +
102                             path);
103                 }
104                 else {
105                     requestDispatcher.include(request, response);
106                 }
107             }
108         }
109         else {
110             RequestDispatcher requestDispatcher =
111                 servletContext.getRequestDispatcher(path);
112 
113             if (requestDispatcher == null) {
114                 _log.error(
115                     "Theme " + theme.getThemeId() + " does not have " + path);
116             }
117             else {
118                 requestDispatcher.include(request, response);
119             }
120         }
121     }
122 
123     public static String includeVM(
124             ServletContext servletContext, HttpServletRequest request,
125             PageContext pageContext, String page, Theme theme, boolean write)
126         throws Exception {
127 
128         // The servlet context name will be null when the theme is deployed to
129         // the root directory in Tomcat. See
130         // com.liferay.portal.servlet.MainServlet and
131         // com.liferay.portlet.PortletContextImpl for other cases where a null
132         // servlet context name is also converted to an empty string.
133 
134         String ctxName = GetterUtil.getString(theme.getServletContextName());
135 
136         if (VelocityContextPool.get(ctxName) == null) {
137 
138             // This should only happen if the Velocity template is the first
139             // page to be accessed in the system
140 
141             VelocityContextPool.put(ctxName, servletContext);
142         }
143 
144         int pos = page.lastIndexOf(StringPool.PERIOD);
145 
146         StringBuilder sb = new StringBuilder();
147 
148         sb.append(ctxName);
149         sb.append(theme.getVelocityResourceListener());
150         sb.append(theme.getTemplatesPath());
151         sb.append(StringPool.SLASH);
152         sb.append(page.substring(0, pos));
153         sb.append(StringPool.PERIOD);
154         sb.append(_TEMPLATE_EXTENSION_VM);
155 
156         String source = sb.toString();
157 
158         if (!VelocityEngineUtil.resourceExists(source)) {
159             _log.error(source + " does not exist");
160 
161             return null;
162         }
163 
164         UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter();
165 
166         VelocityContext velocityContext =
167             VelocityEngineUtil.getWrappedStandardToolsContext();
168 
169         // Velocity variables
170 
171         VelocityVariables.insertVariables(velocityContext, request);
172 
173         // Theme servlet context
174 
175         ServletContext themeServletContext = VelocityContextPool.get(ctxName);
176 
177         velocityContext.put("themeServletContext", themeServletContext);
178 
179         // Tag libraries
180 
181         StringServletResponse stringResponse = new StringServletResponse(
182             (HttpServletResponse)pageContext.getResponse());
183 
184         VelocityTaglib velocityTaglib = new VelocityTaglib(
185             servletContext, request, stringResponse, pageContext);
186 
187         request.setAttribute(WebKeys.VELOCITY_TAGLIB, velocityTaglib);
188 
189         velocityContext.put("taglibLiferay", velocityTaglib);
190         velocityContext.put("theme", velocityTaglib);
191 
192         // Merge templates
193 
194         VelocityEngineUtil.mergeTemplate(
195             source, velocityContext, unsyncStringWriter);
196 
197         if (write) {
198             StringBundler unsyncStringWriterSB =
199                 unsyncStringWriter.getStringBundler();
200 
201             unsyncStringWriterSB.writeTo(pageContext.getOut());
202 
203             return null;
204         }
205         else {
206             return unsyncStringWriter.toString();
207         }
208     }
209 
210     private static String _getTilesVariables(
211         HttpServletRequest request, String attributeName) {
212 
213         ComponentContext componentContext =
214             (ComponentContext)request.getAttribute(
215                 ComponentConstants.COMPONENT_CONTEXT);
216 
217         String value = null;
218 
219         if (componentContext != null) {
220             value = (String)componentContext.getAttribute(attributeName);
221         }
222 
223         return value;
224     }
225 
226     private static final String _TEMPLATE_EXTENSION_VM = "vm";
227 
228     private static Log _log = LogFactoryUtil.getLog(ThemeUtil.class);
229 
230 }