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.StringPool;
28  import com.liferay.portal.model.Theme;
29  import com.liferay.portal.theme.ThemeDisplay;
30  import com.liferay.portal.util.WebKeys;
31  import com.liferay.portal.velocity.VelocityContextPool;
32  import com.liferay.portal.velocity.VelocityVariables;
33  
34  import java.io.StringWriter;
35  
36  import javax.servlet.RequestDispatcher;
37  import javax.servlet.ServletContext;
38  import javax.servlet.http.HttpServletRequest;
39  import javax.servlet.http.HttpServletResponse;
40  import javax.servlet.jsp.PageContext;
41  
42  import org.apache.commons.logging.Log;
43  import org.apache.commons.logging.LogFactory;
44  import org.apache.struts.taglib.tiles.ComponentConstants;
45  import org.apache.struts.tiles.ComponentContext;
46  import org.apache.velocity.VelocityContext;
47  import org.apache.velocity.app.Velocity;
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   *
55   */
56  public class ThemeUtil {
57  
58      public static void include(
59              ServletContext servletContext, HttpServletRequest request,
60              HttpServletResponse response, PageContext pageContext, String page,
61              Theme theme)
62          throws Exception {
63  
64          String extension = theme.getTemplateExtension();
65  
66          if (extension.equals("vm")) {
67              includeVM(servletContext, request, pageContext, page, theme, true);
68          }
69          else {
70              String path =
71                  theme.getTemplatesPath() + StringPool.SLASH + page;
72  
73              includeJSP(servletContext, request, response, path, theme);
74          }
75      }
76  
77      public static void includeJSP(
78              ServletContext servletContext, HttpServletRequest request,
79              HttpServletResponse response, String path, Theme theme)
80          throws Exception {
81  
82          String tilesTitle = _getTilesVariables(request, "title");
83          String tilesContent = _getTilesVariables(request, "content");
84          boolean tilesSelectable = GetterUtil.getBoolean(
85              _getTilesVariables(request, "selectable"));
86  
87          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
88              WebKeys.THEME_DISPLAY);
89  
90          themeDisplay.setTilesTitle(tilesTitle);
91          themeDisplay.setTilesContent(tilesContent);
92          themeDisplay.setTilesSelectable(tilesSelectable);
93  
94          if (theme.isWARFile()) {
95              ServletContext themeServletContext = servletContext.getContext(
96                  theme.getContextPath());
97  
98              if (themeServletContext == null) {
99                  _log.error(
100                     "Theme " + theme.getThemeId() + " cannot find its " +
101                         "servlet context at " + theme.getServletContextName());
102             }
103             else {
104                 RequestDispatcher requestDispatcher =
105                     themeServletContext.getRequestDispatcher(path);
106 
107                 if (requestDispatcher == null) {
108                     _log.error(
109                         "Theme " + theme.getThemeId() + " does not have " +
110                             path);
111                 }
112                 else {
113                     requestDispatcher.include(request, response);
114                 }
115             }
116         }
117         else {
118             RequestDispatcher requestDispatcher =
119                 servletContext.getRequestDispatcher(path);
120 
121             if (requestDispatcher == null) {
122                 _log.error(
123                     "Theme " + theme.getThemeId() + " does not have " + path);
124             }
125             else {
126                 requestDispatcher.include(request, response);
127             }
128         }
129     }
130 
131     public static String includeVM(
132             ServletContext servletContext, HttpServletRequest request,
133             PageContext pageContext, String page, Theme theme, boolean write)
134         throws Exception {
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, servletContext);
150         }
151 
152         int pos = page.lastIndexOf(StringPool.PERIOD);
153 
154         StringBuilder sb = new StringBuilder();
155 
156         sb.append(ctxName);
157         sb.append(theme.getVelocityResourceListener());
158         sb.append(theme.getTemplatesPath());
159         sb.append(StringPool.SLASH);
160         sb.append(page.substring(0, pos));
161         sb.append(".vm");
162 
163         String source = sb.toString();
164 
165         if (!Velocity.resourceExists(source)) {
166             _log.error(source + " does not exist");
167 
168             return null;
169         }
170 
171         StringWriter sw = new StringWriter();
172 
173         VelocityContext velocityContext = null;
174 
175         // LEP-6865
176 
177         if (_innerVelocityContext == null) {
178             velocityContext = new VelocityContext();
179 
180             _innerVelocityContext = velocityContext;
181         }
182         else {
183             velocityContext = new VelocityContext(_innerVelocityContext);
184         }
185 
186         // Velocity variables
187 
188         VelocityVariables.insertVariables(velocityContext, request);
189 
190         // liferay:include tag library
191 
192         StringServletResponse stringResponse = new StringServletResponse(
193             (HttpServletResponse)pageContext.getResponse());
194 
195         VelocityTaglib velocityTaglib = new VelocityTaglib(
196             servletContext, request, stringResponse, pageContext);
197 
198         request.setAttribute(WebKeys.VELOCITY_TAGLIB, velocityTaglib);
199 
200         velocityContext.put("taglibLiferay", velocityTaglib);
201         velocityContext.put("theme", velocityTaglib);
202 
203         // Merge templates
204 
205         Velocity.mergeTemplate(source, StringPool.UTF8, velocityContext, sw);
206 
207         // Print output
208 
209         String output = sw.toString();
210 
211         if (write) {
212             pageContext.getOut().print(output);
213 
214             return null;
215         }
216         else {
217             return output;
218         }
219     }
220 
221     private static String _getTilesVariables(
222         HttpServletRequest request, String attributeName) {
223 
224         ComponentContext componentContext =
225             (ComponentContext)request.getAttribute(
226                 ComponentConstants.COMPONENT_CONTEXT);
227 
228         String value = null;
229 
230         if (componentContext != null) {
231             value = (String)componentContext.getAttribute(attributeName);
232         }
233 
234         return value;
235     }
236 
237     private static Log _log = LogFactory.getLog(ThemeUtil.class);
238 
239     private static VelocityContext _innerVelocityContext;
240 
241 }