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