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.portal.velocity;
24  
25  import com.liferay.portal.kernel.language.LanguageUtil;
26  import com.liferay.portal.kernel.language.UnicodeLanguageUtil;
27  import com.liferay.portal.kernel.servlet.BrowserSniffer_IW;
28  import com.liferay.portal.kernel.servlet.ImageServletTokenUtil;
29  import com.liferay.portal.kernel.util.ArrayUtil;
30  import com.liferay.portal.kernel.util.ArrayUtil_IW;
31  import com.liferay.portal.kernel.util.DateUtil_IW;
32  import com.liferay.portal.kernel.util.GetterUtil;
33  import com.liferay.portal.kernel.util.GetterUtil_IW;
34  import com.liferay.portal.kernel.util.JavaConstants;
35  import com.liferay.portal.kernel.util.ParamUtil_IW;
36  import com.liferay.portal.kernel.util.Randomizer_IW;
37  import com.liferay.portal.kernel.util.StaticFieldGetter;
38  import com.liferay.portal.kernel.util.StringUtil_IW;
39  import com.liferay.portal.kernel.util.UnicodeFormatter_IW;
40  import com.liferay.portal.kernel.util.Validator;
41  import com.liferay.portal.model.Layout;
42  import com.liferay.portal.model.Theme;
43  import com.liferay.portal.service.permission.AccountPermissionUtil;
44  import com.liferay.portal.service.permission.CommonPermissionUtil;
45  import com.liferay.portal.service.permission.GroupPermissionUtil;
46  import com.liferay.portal.service.permission.LayoutPermissionUtil;
47  import com.liferay.portal.service.permission.OrganizationPermissionUtil;
48  import com.liferay.portal.service.permission.PasswordPolicyPermissionUtil;
49  import com.liferay.portal.service.permission.PortalPermissionUtil;
50  import com.liferay.portal.service.permission.PortletPermissionUtil;
51  import com.liferay.portal.service.permission.RolePermissionUtil;
52  import com.liferay.portal.service.permission.UserGroupPermissionUtil;
53  import com.liferay.portal.service.permission.UserPermissionUtil;
54  import com.liferay.portal.theme.NavItem;
55  import com.liferay.portal.theme.RequestVars;
56  import com.liferay.portal.theme.ThemeDisplay;
57  import com.liferay.portal.util.DateFormats_IW;
58  import com.liferay.portal.util.PortalUtil_IW;
59  import com.liferay.portal.util.PrefsPropsUtil_IW;
60  import com.liferay.portal.util.PropsUtil_IW;
61  import com.liferay.portal.util.ServiceLocator;
62  import com.liferay.portal.util.SessionClicks_IW;
63  import com.liferay.portal.util.WebKeys;
64  import com.liferay.portlet.PortletConfigImpl;
65  import com.liferay.portlet.PortletURLFactory;
66  import com.liferay.util.Html_IW;
67  import com.liferay.util.HttpUtil_IW;
68  
69  import java.util.Iterator;
70  import java.util.List;
71  import java.util.Map;
72  
73  import javax.portlet.PortletRequest;
74  import javax.portlet.PortletResponse;
75  import javax.portlet.RenderRequest;
76  import javax.portlet.RenderResponse;
77  
78  import javax.servlet.http.HttpServletRequest;
79  
80  import org.apache.struts.taglib.tiles.ComponentConstants;
81  import org.apache.struts.tiles.ComponentContext;
82  import org.apache.velocity.VelocityContext;
83  import org.apache.velocity.tools.generic.DateTool;
84  import org.apache.velocity.tools.generic.EscapeTool;
85  import org.apache.velocity.tools.generic.IteratorTool;
86  import org.apache.velocity.tools.generic.ListTool;
87  import org.apache.velocity.tools.generic.MathTool;
88  import org.apache.velocity.tools.generic.NumberTool;
89  import org.apache.velocity.tools.generic.SortTool;
90  
91  /**
92   * <a href="VelocityVariables.java.html"><b><i>View Source</i></b></a>
93   *
94   * @author Brian Wing Shun Chan
95   *
96   */
97  public class VelocityVariables {
98  
99      public static void insertHelperUtilities(
100         VelocityContext vc, String[] restrictedVariables) {
101 
102         // Array util
103 
104         vc.put("arrayUtil", ArrayUtil_IW.getInstance());
105 
106         // Browser sniffer
107 
108         vc.put("browserSniffer", BrowserSniffer_IW.getInstance());
109 
110         // Date formats
111 
112         vc.put("dateFormats", DateFormats_IW.getInstance());
113 
114         // Date tool
115 
116         vc.put("dateTool", new DateTool());
117 
118         // Date util
119 
120         vc.put("dateUtil", DateUtil_IW.getInstance());
121 
122         // Escape tool
123 
124         vc.put("escapeTool", new EscapeTool());
125 
126         // Getter util
127 
128         vc.put("getterUtil", GetterUtil_IW.getInstance());
129 
130         // Html util
131 
132         vc.put("htmlUtil", Html_IW.getInstance());
133 
134         // Http util
135 
136         vc.put("httpUtil", HttpUtil_IW.getInstance());
137 
138         // ImageServletToken
139 
140         vc.put("imageToken", ImageServletTokenUtil.getImageServletToken());
141 
142         // Iterator tool
143 
144         vc.put("iteratorTool", new IteratorTool());
145 
146         // Language util
147 
148         vc.put("languageUtil", LanguageUtil.getLanguage());
149         vc.put("unicodeLanguageUtil", UnicodeLanguageUtil.getUnicodeLanguage());
150 
151         // List tool
152 
153         vc.put("listTool", new ListTool());
154 
155         // Math tool
156 
157         vc.put("mathTool", new MathTool());
158 
159         // Number tool
160 
161         vc.put("numberTool", new NumberTool());
162 
163         // Param util
164 
165         vc.put("paramUtil", ParamUtil_IW.getInstance());
166 
167         // Portal util
168 
169         _insertHelperUtility(
170             vc, restrictedVariables, "portalUtil", PortalUtil_IW.getInstance());
171         _insertHelperUtility(
172             vc, restrictedVariables, "portal", PortalUtil_IW.getInstance());
173 
174         // Prefs props util
175 
176         _insertHelperUtility(
177             vc, restrictedVariables, "prefsPropsUtil",
178             PrefsPropsUtil_IW.getInstance());
179 
180         // Props util
181 
182         _insertHelperUtility(
183             vc, restrictedVariables, "propsUtil", PropsUtil_IW.getInstance());
184 
185         // Portlet URL factory
186 
187         vc.put("portletURLFactory", PortletURLFactory.getInstance());
188 
189         // Portlet preferences
190 
191         _insertHelperUtility(
192             vc, restrictedVariables, "velocityPortletPreferences",
193             new VelocityPortletPreferences());
194 
195         // Randomizer
196 
197         vc.put("randomizer", Randomizer_IW.getInstance().getWrappedInstance());
198 
199         // Service locator
200 
201         _insertHelperUtility(
202             vc, restrictedVariables, "serviceLocator",
203             ServiceLocator.getInstance());
204 
205         // Session clicks
206 
207         _insertHelperUtility(
208             vc, restrictedVariables, "sessionClicks",
209             SessionClicks_IW.getInstance());
210 
211         // Sort tool
212 
213         vc.put("sortTool", new SortTool());
214 
215         // Static field getter
216 
217         vc.put("staticFieldGetter", StaticFieldGetter.getInstance());
218 
219         // String util
220 
221         vc.put("stringUtil", StringUtil_IW.getInstance());
222 
223         // Unicode formatter
224 
225         vc.put("unicodeFormatter", UnicodeFormatter_IW.getInstance());
226 
227         // Permissions
228 
229         vc.put(
230             "accountPermission", AccountPermissionUtil.getAccountPermission());
231         vc.put("commonPermission", CommonPermissionUtil.getCommonPermission());
232         vc.put("groupPermission", GroupPermissionUtil.getGroupPermission());
233         vc.put("layoutPermission", LayoutPermissionUtil.getLayoutPermission());
234         vc.put(
235             "organizationPermission",
236             OrganizationPermissionUtil.getOrganizationPermission());
237         vc.put(
238             "passwordPolicyPermission",
239             PasswordPolicyPermissionUtil.getPasswordPolicyPermission());
240         vc.put("portalPermission", PortalPermissionUtil.getPortalPermission());
241         vc.put(
242             "portletPermission", PortletPermissionUtil.getPortletPermission());
243         vc.put("rolePermission", RolePermissionUtil.getRolePermission());
244         vc.put(
245             "userGroupPermission",
246             UserGroupPermissionUtil.getUserGroupPermission());
247         vc.put("userPermission", UserPermissionUtil.getUserPermission());
248 
249         // Deprecated permissions
250 
251         vc.put(
252             "locationPermission",
253             OrganizationPermissionUtil.getOrganizationPermission());
254     }
255 
256     public static void insertVariables(
257         VelocityContext vc, HttpServletRequest req) {
258 
259         // Request
260 
261         vc.put("request", req);
262 
263         // Portlet config
264 
265         PortletConfigImpl portletConfig = (PortletConfigImpl)req.getAttribute(
266             JavaConstants.JAVAX_PORTLET_CONFIG);
267 
268         if (portletConfig != null) {
269             vc.put("portletConfig", portletConfig);
270         }
271 
272         // Render request
273 
274         PortletRequest portletRequest = (PortletRequest)req.getAttribute(
275             JavaConstants.JAVAX_PORTLET_REQUEST);
276 
277         if (portletRequest != null) {
278             if (portletRequest instanceof RenderRequest) {
279                 vc.put("renderRequest", portletRequest);
280             }
281         }
282 
283         // Render response
284 
285         PortletResponse portletResponse = (PortletResponse)req.getAttribute(
286             JavaConstants.JAVAX_PORTLET_RESPONSE);
287 
288         if (portletResponse != null) {
289             if (portletResponse instanceof RenderResponse) {
290                 vc.put("renderResponse", portletResponse);
291             }
292         }
293 
294         // Theme display
295 
296         ThemeDisplay themeDisplay =
297             (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
298 
299         if (themeDisplay != null) {
300             Theme theme = themeDisplay.getTheme();
301 
302             Layout layout = themeDisplay.getLayout();
303             List layouts = themeDisplay.getLayouts();
304 
305             vc.put("themeDisplay", themeDisplay);
306             vc.put("company", themeDisplay.getCompany());
307             vc.put("user", themeDisplay.getUser());
308             vc.put("realUser", themeDisplay.getRealUser());
309             vc.put("layout", layout);
310             vc.put("layouts", layouts);
311             vc.put("plid", String.valueOf(themeDisplay.getPlid()));
312             vc.put("layoutTypePortlet", themeDisplay.getLayoutTypePortlet());
313             vc.put(
314                 "portletGroupId", new Long(themeDisplay.getPortletGroupId()));
315             vc.put("permissionChecker", themeDisplay.getPermissionChecker());
316             vc.put("locale", themeDisplay.getLocale());
317             vc.put("timeZone", themeDisplay.getTimeZone());
318             vc.put("theme", theme);
319             vc.put("colorScheme", themeDisplay.getColorScheme());
320             vc.put("portletDisplay", themeDisplay.getPortletDisplay());
321 
322             // Navigation items
323 
324             if (layout != null) {
325                 RequestVars requestVars = new RequestVars(
326                     req, themeDisplay, layout.getAncestorPlid(),
327                     layout.getAncestorLayoutId());
328 
329                 List navItems = NavItem.fromLayouts(requestVars, layouts);
330 
331                 vc.put("navItems", navItems);
332             }
333 
334             // Full css and templates path
335 
336             String ctxName = GetterUtil.getString(
337                 theme.getServletContextName());
338 
339             vc.put(
340                 "fullCssPath",
341                 ctxName + theme.getVelocityResourceListener() +
342                     theme.getCssPath());
343 
344             vc.put(
345                 "fullTemplatesPath",
346                 ctxName + theme.getVelocityResourceListener() +
347                     theme.getTemplatesPath());
348 
349             // Init
350 
351             vc.put(
352                 "init",
353                 themeDisplay.getPathContext() +
354                     VelocityResourceListener.SERVLET_SEPARATOR +
355                         "/html/themes/_unstyled/templates/init.vm");
356         }
357 
358         // Tiles attributes
359 
360         String tilesTitle = _insertTilesVariables(
361             vc, req, "tilesTitle", "title");
362         String tilesContent = _insertTilesVariables(
363             vc, req, "tilesContent", "content");
364         boolean tilesSelectable = GetterUtil.getBoolean(_insertTilesVariables(
365             vc, req, "tilesSelectable", "selectable"));
366 
367         if (themeDisplay != null) {
368             themeDisplay.setTilesTitle(tilesTitle);
369             themeDisplay.setTilesContent(tilesContent);
370             themeDisplay.setTilesSelectable(tilesSelectable);
371         }
372 
373         // Page title and subtitle
374 
375         vc.put("pageTitle", req.getAttribute(WebKeys.PAGE_TITLE));
376         vc.put("pageSubtitle", req.getAttribute(WebKeys.PAGE_SUBTITLE));
377 
378         // Helper utilities
379 
380         insertHelperUtilities(vc, null);
381 
382         // Insert custom vm variables
383 
384         Map vmVariables = (Map)req.getAttribute(WebKeys.VM_VARIABLES);
385 
386         if (vmVariables != null) {
387             Iterator itr = vmVariables.entrySet().iterator();
388 
389             while (itr.hasNext()) {
390                 Map.Entry entry = (Map.Entry)itr.next();
391 
392                 String key = (String)entry.getKey();
393                 Object value = entry.getValue();
394 
395                 if (Validator.isNotNull(key)) {
396                     vc.put(key, value);
397                 }
398             }
399         }
400     }
401 
402     private static void _insertHelperUtility(
403         VelocityContext vc, String[] restrictedVariables, String key,
404         Object value) {
405 
406         if (!ArrayUtil.contains(restrictedVariables, key)) {
407             vc.put(key, value);
408         }
409     }
410 
411     private static String _insertTilesVariables(
412         VelocityContext vc, HttpServletRequest req, String attributeId,
413         String attributeName) {
414 
415         ComponentContext componentContext = (ComponentContext)req.getAttribute(
416             ComponentConstants.COMPONENT_CONTEXT);
417 
418         String value = null;
419 
420         if (componentContext != null) {
421             value = (String)componentContext.getAttribute(attributeName);
422 
423             if (value != null) {
424                 vc.put(attributeId, value);
425             }
426         }
427 
428         return value;
429     }
430 
431 }