1
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.BrowserSnifferUtil;
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.DateFormats_IW;
32 import com.liferay.portal.kernel.util.DateUtil_IW;
33 import com.liferay.portal.kernel.util.GetterUtil;
34 import com.liferay.portal.kernel.util.GetterUtil_IW;
35 import com.liferay.portal.kernel.util.HtmlUtil;
36 import com.liferay.portal.kernel.util.HttpUtil;
37 import com.liferay.portal.kernel.util.JavaConstants;
38 import com.liferay.portal.kernel.util.LocaleUtil;
39 import com.liferay.portal.kernel.util.ParamUtil_IW;
40 import com.liferay.portal.kernel.util.Randomizer_IW;
41 import com.liferay.portal.kernel.util.StaticFieldGetter;
42 import com.liferay.portal.kernel.util.StringUtil_IW;
43 import com.liferay.portal.kernel.util.TimeZoneUtil_IW;
44 import com.liferay.portal.kernel.util.UnicodeFormatter_IW;
45 import com.liferay.portal.kernel.util.Validator;
46 import com.liferay.portal.kernel.util.Validator_IW;
47 import com.liferay.portal.kernel.velocity.VelocityContext;
48 import com.liferay.portal.kernel.xml.SAXReaderUtil;
49 import com.liferay.portal.model.Layout;
50 import com.liferay.portal.model.Theme;
51 import com.liferay.portal.service.permission.AccountPermissionUtil;
52 import com.liferay.portal.service.permission.CommonPermissionUtil;
53 import com.liferay.portal.service.permission.GroupPermissionUtil;
54 import com.liferay.portal.service.permission.LayoutPermissionUtil;
55 import com.liferay.portal.service.permission.OrganizationPermissionUtil;
56 import com.liferay.portal.service.permission.PasswordPolicyPermissionUtil;
57 import com.liferay.portal.service.permission.PortalPermissionUtil;
58 import com.liferay.portal.service.permission.PortletPermissionUtil;
59 import com.liferay.portal.service.permission.RolePermissionUtil;
60 import com.liferay.portal.service.permission.UserGroupPermissionUtil;
61 import com.liferay.portal.service.permission.UserPermissionUtil;
62 import com.liferay.portal.theme.NavItem;
63 import com.liferay.portal.theme.RequestVars;
64 import com.liferay.portal.theme.ThemeDisplay;
65 import com.liferay.portal.util.PortalUtil;
66 import com.liferay.portal.util.PrefsPropsUtil_IW;
67 import com.liferay.portal.util.PropsUtil_IW;
68 import com.liferay.portal.util.SessionClicks_IW;
69 import com.liferay.portal.util.WebKeys;
70 import com.liferay.portlet.PortletConfigImpl;
71 import com.liferay.portlet.PortletURLFactoryUtil;
72 import com.liferay.portlet.expando.service.ExpandoColumnLocalService;
73 import com.liferay.portlet.expando.service.ExpandoRowLocalService;
74 import com.liferay.portlet.expando.service.ExpandoTableLocalService;
75 import com.liferay.portlet.expando.service.ExpandoValueLocalService;
76 import com.liferay.portlet.journalcontent.util.JournalContentUtil_IW;
77 import com.liferay.util.portlet.PortletRequestUtil;
78
79 import java.util.List;
80 import java.util.Map;
81
82 import javax.portlet.PortletRequest;
83 import javax.portlet.PortletResponse;
84 import javax.portlet.RenderRequest;
85 import javax.portlet.RenderResponse;
86
87 import javax.servlet.http.HttpServletRequest;
88
89 import org.apache.struts.taglib.tiles.ComponentConstants;
90 import org.apache.struts.tiles.ComponentContext;
91 import org.apache.velocity.tools.generic.DateTool;
92 import org.apache.velocity.tools.generic.EscapeTool;
93 import org.apache.velocity.tools.generic.IteratorTool;
94 import org.apache.velocity.tools.generic.ListTool;
95 import org.apache.velocity.tools.generic.MathTool;
96 import org.apache.velocity.tools.generic.NumberTool;
97 import org.apache.velocity.tools.generic.SortTool;
98
99
106 public class VelocityVariables {
107
108 public static void insertHelperUtilities(
109 VelocityContext velocityContext, String[] restrictedVariables) {
110
111
113 velocityContext.put("arrayUtil", ArrayUtil_IW.getInstance());
114
115
117 velocityContext.put(
118 "browserSniffer", BrowserSnifferUtil.getBrowserSniffer());
119
120
122 velocityContext.put("dateFormats", DateFormats_IW.getInstance());
123
124
126 velocityContext.put("dateTool", new DateTool());
127
128
130 velocityContext.put("dateUtil", DateUtil_IW.getInstance());
131
132
134 velocityContext.put("escapeTool", new EscapeTool());
135
136
138 ServiceLocator serviceLocator = ServiceLocator.getInstance();
139
140 velocityContext.put(
141 "expandoColumnLocalService",
142 serviceLocator.findService(
143 ExpandoColumnLocalService.class.getName()));
144
145
147 velocityContext.put(
148 "expandoRowLocalService",
149 serviceLocator.findService(ExpandoRowLocalService.class.getName()));
150
151
153 velocityContext.put(
154 "expandoTableLocalService",
155 serviceLocator.findService(
156 ExpandoTableLocalService.class.getName()));
157
158
160 velocityContext.put(
161 "expandoValueLocalService",
162 serviceLocator.findService(
163 ExpandoValueLocalService.class.getName()));
164
165
167 velocityContext.put("getterUtil", GetterUtil_IW.getInstance());
168
169
171 velocityContext.put("htmlUtil", HtmlUtil.getHtml());
172
173
175 velocityContext.put("httpUtil", HttpUtil.getHttp());
176
177
179 velocityContext.put(
180 "imageToken", ImageServletTokenUtil.getImageServletToken());
181
182
184 velocityContext.put("iteratorTool", new IteratorTool());
185
186
188 velocityContext.put(
189 "journalContentUtil", JournalContentUtil_IW.getInstance());
190
191
193 velocityContext.put("languageUtil", LanguageUtil.getLanguage());
194 velocityContext.put(
195 "unicodeLanguageUtil", UnicodeLanguageUtil.getUnicodeLanguage());
196
197
199 velocityContext.put("listTool", new ListTool());
200
201
203 velocityContext.put("localeUtil", LocaleUtil.getInstance());
204
205
207 velocityContext.put("mathTool", new MathTool());
208
209
211 velocityContext.put("numberTool", new NumberTool());
212
213
215 velocityContext.put("paramUtil", ParamUtil_IW.getInstance());
216
217
219 _insertHelperUtility(
220 velocityContext, restrictedVariables, "portalUtil",
221 PortalUtil.getPortal());
222 _insertHelperUtility(
223 velocityContext, restrictedVariables, "portal",
224 PortalUtil.getPortal());
225
226
228 _insertHelperUtility(
229 velocityContext, restrictedVariables, "prefsPropsUtil",
230 PrefsPropsUtil_IW.getInstance());
231
232
234 _insertHelperUtility(
235 velocityContext, restrictedVariables, "propsUtil",
236 PropsUtil_IW.getInstance());
237
238
240 velocityContext.put(
241 "portletURLFactory", PortletURLFactoryUtil.getPortletURLFactory());
242
243
245 _insertHelperUtility(
246 velocityContext, restrictedVariables, "velocityPortletPreferences",
247 new VelocityPortletPreferences());
248
249
251 velocityContext.put(
252 "randomizer", Randomizer_IW.getInstance().getWrappedInstance());
253
254
256 UtilLocator utilLocator = UtilLocator.getInstance();
257
258 velocityContext.put(
259 "saxReaderUtil",
260 utilLocator.findUtil(SAXReaderUtil.class.getName()));
261
262
264 _insertHelperUtility(
265 velocityContext, restrictedVariables, "serviceLocator",
266 serviceLocator);
267
268
270 _insertHelperUtility(
271 velocityContext, restrictedVariables, "sessionClicks",
272 SessionClicks_IW.getInstance());
273
274
276 velocityContext.put("sortTool", new SortTool());
277
278
280 velocityContext.put(
281 "staticFieldGetter", StaticFieldGetter.getInstance());
282
283
285 velocityContext.put("stringUtil", StringUtil_IW.getInstance());
286
287
289 velocityContext.put("timeZoneUtil", TimeZoneUtil_IW.getInstance());
290
291
293 _insertHelperUtility(
294 velocityContext, restrictedVariables, "utilLocator", utilLocator);
295
296
298 velocityContext.put(
299 "unicodeFormatter", UnicodeFormatter_IW.getInstance());
300
301
303 velocityContext.put("validator", Validator_IW.getInstance());
304
305
307 velocityContext.put(
308 "accountPermission", AccountPermissionUtil.getAccountPermission());
309 velocityContext.put(
310 "commonPermission", CommonPermissionUtil.getCommonPermission());
311 velocityContext.put(
312 "groupPermission", GroupPermissionUtil.getGroupPermission());
313 velocityContext.put(
314 "layoutPermission", LayoutPermissionUtil.getLayoutPermission());
315 velocityContext.put(
316 "organizationPermission",
317 OrganizationPermissionUtil.getOrganizationPermission());
318 velocityContext.put(
319 "passwordPolicyPermission",
320 PasswordPolicyPermissionUtil.getPasswordPolicyPermission());
321 velocityContext.put(
322 "portalPermission", PortalPermissionUtil.getPortalPermission());
323 velocityContext.put(
324 "portletPermission", PortletPermissionUtil.getPortletPermission());
325 velocityContext.put(
326 "rolePermission", RolePermissionUtil.getRolePermission());
327 velocityContext.put(
328 "userGroupPermission",
329 UserGroupPermissionUtil.getUserGroupPermission());
330 velocityContext.put(
331 "userPermission", UserPermissionUtil.getUserPermission());
332
333
335 velocityContext.put(
336 "locationPermission",
337 OrganizationPermissionUtil.getOrganizationPermission());
338 }
339
340 public static void insertVariables(
341 VelocityContext velocityContext, HttpServletRequest request) {
342
343
345 velocityContext.put("request", request);
346
347
349 PortletConfigImpl portletConfigImpl =
350 (PortletConfigImpl)request.getAttribute(
351 JavaConstants.JAVAX_PORTLET_CONFIG);
352
353 if (portletConfigImpl != null) {
354 velocityContext.put("portletConfig", portletConfigImpl);
355 }
356
357
359 PortletRequest portletRequest = (PortletRequest)request.getAttribute(
360 JavaConstants.JAVAX_PORTLET_REQUEST);
361
362 if (portletRequest != null) {
363 if (portletRequest instanceof RenderRequest) {
364 velocityContext.put("renderRequest", portletRequest);
365 }
366 }
367
368
370 PortletResponse portletResponse = (PortletResponse)request.getAttribute(
371 JavaConstants.JAVAX_PORTLET_RESPONSE);
372
373 if (portletResponse != null) {
374 if (portletResponse instanceof RenderResponse) {
375 velocityContext.put("renderResponse", portletResponse);
376 }
377 }
378
379
381 if ((portletRequest != null) && (portletResponse != null)) {
382 String xmlRequest = PortletRequestUtil.toXML(
383 portletRequest, portletResponse);
384
385 velocityContext.put("xmlRequest", xmlRequest);
386 }
387
388
390 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
391 WebKeys.THEME_DISPLAY);
392
393 if (themeDisplay != null) {
394 Theme theme = themeDisplay.getTheme();
395
396 Layout layout = themeDisplay.getLayout();
397 List<Layout> layouts = themeDisplay.getLayouts();
398
399 velocityContext.put("themeDisplay", themeDisplay);
400 velocityContext.put("company", themeDisplay.getCompany());
401 velocityContext.put("user", themeDisplay.getUser());
402 velocityContext.put("realUser", themeDisplay.getRealUser());
403 velocityContext.put("layout", layout);
404 velocityContext.put("layouts", layouts);
405 velocityContext.put("plid", String.valueOf(themeDisplay.getPlid()));
406 velocityContext.put(
407 "layoutTypePortlet", themeDisplay.getLayoutTypePortlet());
408 velocityContext.put(
409 "scopeGroupId", new Long(themeDisplay.getScopeGroupId()));
410 velocityContext.put(
411 "permissionChecker", themeDisplay.getPermissionChecker());
412 velocityContext.put("locale", themeDisplay.getLocale());
413 velocityContext.put("timeZone", themeDisplay.getTimeZone());
414 velocityContext.put("theme", theme);
415 velocityContext.put("colorScheme", themeDisplay.getColorScheme());
416 velocityContext.put(
417 "portletDisplay", themeDisplay.getPortletDisplay());
418
419
421 if (layout != null) {
422 RequestVars requestVars = new RequestVars(
423 request, themeDisplay, layout.getAncestorPlid(),
424 layout.getAncestorLayoutId());
425
426 List<NavItem> navItems = NavItem.fromLayouts(
427 requestVars, layouts);
428
429 velocityContext.put("navItems", navItems);
430 }
431
432
434 String servletContextName = GetterUtil.getString(
435 theme.getServletContextName());
436
437 velocityContext.put(
438 "fullCssPath",
439 servletContextName + theme.getVelocityResourceListener() +
440 theme.getCssPath());
441
442 velocityContext.put(
443 "fullTemplatesPath",
444 servletContextName + theme.getVelocityResourceListener() +
445 theme.getTemplatesPath());
446
447
449 velocityContext.put(
450 "init",
451 themeDisplay.getPathContext() +
452 VelocityResourceListener.SERVLET_SEPARATOR +
453 "/html/themes/_unstyled/templates/init.vm");
454
455
457 velocityContext.put(
458 "portletGroupId", new Long(themeDisplay.getScopeGroupId()));
459 }
460
461
463 String tilesTitle = _insertTilesVariables(
464 velocityContext, request, "tilesTitle", "title");
465 String tilesContent = _insertTilesVariables(
466 velocityContext, request, "tilesContent", "content");
467 boolean tilesSelectable = GetterUtil.getBoolean(_insertTilesVariables(
468 velocityContext, request, "tilesSelectable", "selectable"));
469
470 if (themeDisplay != null) {
471 themeDisplay.setTilesTitle(tilesTitle);
472 themeDisplay.setTilesContent(tilesContent);
473 themeDisplay.setTilesSelectable(tilesSelectable);
474 }
475
476
478 velocityContext.put(
479 "pageTitle", request.getAttribute(WebKeys.PAGE_TITLE));
480 velocityContext.put(
481 "pageSubtitle", request.getAttribute(WebKeys.PAGE_SUBTITLE));
482
483
485 Map<String, Object> vmVariables =
486 (Map<String, Object>)request.getAttribute(WebKeys.VM_VARIABLES);
487
488 if (vmVariables != null) {
489 for (Map.Entry<String, Object> entry : vmVariables.entrySet()) {
490 String key = entry.getKey();
491 Object value = entry.getValue();
492
493 if (Validator.isNotNull(key)) {
494 velocityContext.put(key, value);
495 }
496 }
497 }
498 }
499
500 private static void _insertHelperUtility(
501 VelocityContext velocityContext, String[] restrictedVariables,
502 String key, Object value) {
503
504 if (!ArrayUtil.contains(restrictedVariables, key)) {
505 velocityContext.put(key, value);
506 }
507 }
508
509 private static String _insertTilesVariables(
510 VelocityContext velocityContext, HttpServletRequest request,
511 String attributeId, String attributeName) {
512
513 ComponentContext componentContext =
514 (ComponentContext)request.getAttribute(
515 ComponentConstants.COMPONENT_CONTEXT);
516
517 String value = null;
518
519 if (componentContext != null) {
520 value = (String)componentContext.getAttribute(attributeName);
521
522 if (value != null) {
523 velocityContext.put(attributeId, value);
524 }
525 }
526
527 return value;
528 }
529
530 }