001
014
015 package com.liferay.portal.template;
016
017 import com.liferay.portal.kernel.audit.AuditMessageFactoryUtil;
018 import com.liferay.portal.kernel.audit.AuditRouterUtil;
019 import com.liferay.portal.kernel.json.JSONFactoryUtil;
020 import com.liferay.portal.kernel.language.LanguageUtil;
021 import com.liferay.portal.kernel.language.UnicodeLanguageUtil;
022 import com.liferay.portal.kernel.log.Log;
023 import com.liferay.portal.kernel.log.LogFactoryUtil;
024 import com.liferay.portal.kernel.portlet.PortletModeFactory_IW;
025 import com.liferay.portal.kernel.portlet.WindowStateFactory_IW;
026 import com.liferay.portal.kernel.servlet.BrowserSnifferUtil;
027 import com.liferay.portal.kernel.template.Template;
028 import com.liferay.portal.kernel.template.TemplateHandler;
029 import com.liferay.portal.kernel.template.TemplateHandlerRegistryUtil;
030 import com.liferay.portal.kernel.template.TemplateVariableGroup;
031 import com.liferay.portal.kernel.util.ArrayUtil_IW;
032 import com.liferay.portal.kernel.util.CalendarFactoryUtil;
033 import com.liferay.portal.kernel.util.DateUtil_IW;
034 import com.liferay.portal.kernel.util.FastDateFormatFactoryUtil;
035 import com.liferay.portal.kernel.util.GetterUtil;
036 import com.liferay.portal.kernel.util.GetterUtil_IW;
037 import com.liferay.portal.kernel.util.HtmlUtil;
038 import com.liferay.portal.kernel.util.HttpUtil;
039 import com.liferay.portal.kernel.util.JavaConstants;
040 import com.liferay.portal.kernel.util.ListMergeable;
041 import com.liferay.portal.kernel.util.LocaleUtil;
042 import com.liferay.portal.kernel.util.ParamUtil_IW;
043 import com.liferay.portal.kernel.util.PrefsPropsUtil;
044 import com.liferay.portal.kernel.util.PropsUtil;
045 import com.liferay.portal.kernel.util.Randomizer_IW;
046 import com.liferay.portal.kernel.util.StaticFieldGetter;
047 import com.liferay.portal.kernel.util.StringPool;
048 import com.liferay.portal.kernel.util.StringUtil_IW;
049 import com.liferay.portal.kernel.util.TimeZoneUtil_IW;
050 import com.liferay.portal.kernel.util.UnicodeFormatter_IW;
051 import com.liferay.portal.kernel.util.Validator_IW;
052 import com.liferay.portal.kernel.xml.SAXReader;
053 import com.liferay.portal.model.Layout;
054 import com.liferay.portal.model.Theme;
055 import com.liferay.portal.service.GroupLocalService;
056 import com.liferay.portal.service.GroupService;
057 import com.liferay.portal.service.LayoutLocalService;
058 import com.liferay.portal.service.LayoutService;
059 import com.liferay.portal.service.OrganizationLocalService;
060 import com.liferay.portal.service.OrganizationService;
061 import com.liferay.portal.service.UserLocalService;
062 import com.liferay.portal.service.UserService;
063 import com.liferay.portal.service.permission.AccountPermissionUtil;
064 import com.liferay.portal.service.permission.CommonPermissionUtil;
065 import com.liferay.portal.service.permission.GroupPermissionUtil;
066 import com.liferay.portal.service.permission.LayoutPermissionUtil;
067 import com.liferay.portal.service.permission.OrganizationPermissionUtil;
068 import com.liferay.portal.service.permission.PasswordPolicyPermissionUtil;
069 import com.liferay.portal.service.permission.PortalPermissionUtil;
070 import com.liferay.portal.service.permission.PortletPermissionUtil;
071 import com.liferay.portal.service.permission.RolePermissionUtil;
072 import com.liferay.portal.service.permission.UserGroupPermissionUtil;
073 import com.liferay.portal.service.permission.UserPermissionUtil;
074 import com.liferay.portal.theme.NavItem;
075 import com.liferay.portal.theme.ThemeDisplay;
076 import com.liferay.portal.util.ClassLoaderUtil;
077 import com.liferay.portal.util.PortalUtil;
078 import com.liferay.portal.util.SessionClicks_IW;
079 import com.liferay.portal.util.WebKeys;
080 import com.liferay.portal.webserver.WebServerServletTokenUtil;
081 import com.liferay.portlet.PortletURLFactoryUtil;
082 import com.liferay.portlet.documentlibrary.util.DLUtil;
083 import com.liferay.portlet.dynamicdatamapping.util.DDMUtil;
084 import com.liferay.portlet.expando.service.ExpandoColumnLocalService;
085 import com.liferay.portlet.expando.service.ExpandoRowLocalService;
086 import com.liferay.portlet.expando.service.ExpandoTableLocalService;
087 import com.liferay.portlet.expando.service.ExpandoValueLocalService;
088 import com.liferay.portlet.journalcontent.util.JournalContentUtil;
089 import com.liferay.taglib.util.VelocityTaglibImpl;
090 import com.liferay.util.portlet.PortletRequestUtil;
091
092 import java.lang.reflect.Method;
093
094 import java.util.Collections;
095 import java.util.HashMap;
096 import java.util.List;
097 import java.util.Locale;
098 import java.util.Map;
099 import java.util.Set;
100 import java.util.concurrent.ConcurrentHashMap;
101
102 import javax.portlet.PortletConfig;
103 import javax.portlet.PortletRequest;
104 import javax.portlet.PortletResponse;
105 import javax.portlet.RenderRequest;
106 import javax.portlet.RenderResponse;
107
108 import javax.servlet.http.HttpServletRequest;
109
110 import org.apache.struts.taglib.tiles.ComponentConstants;
111 import org.apache.struts.tiles.ComponentContext;
112
113
117 public class TemplateContextHelper {
118
119 public static Map<String, TemplateVariableGroup> getTemplateVariableGroups(
120 long classNameId, long classPK, String language, Locale locale)
121 throws Exception {
122
123 TemplateHandler templateHandler =
124 TemplateHandlerRegistryUtil.getTemplateHandler(classNameId);
125
126 if (templateHandler == null) {
127 return Collections.emptyMap();
128 }
129
130 Map<String, TemplateVariableGroup> templateVariableGroups =
131 templateHandler.getTemplateVariableGroups(
132 classPK, language, locale);
133
134 TemplateVariableGroup portalServicesTemplateVariableGroup =
135 new TemplateVariableGroup("portal-services");
136
137 portalServicesTemplateVariableGroup.setAutocompleteEnabled(false);
138
139 portalServicesTemplateVariableGroup.addServiceLocatorVariables(
140 GroupLocalService.class, GroupService.class,
141 LayoutLocalService.class, LayoutService.class,
142 OrganizationLocalService.class, OrganizationService.class,
143 UserLocalService.class, UserService.class);
144
145 templateVariableGroups.put(
146 portalServicesTemplateVariableGroup.getLabel(),
147 portalServicesTemplateVariableGroup);
148
149 return templateVariableGroups;
150 }
151
152 public Map<String, Object> getHelperUtilities(
153 ClassLoader classLoader, boolean restricted) {
154
155 Map<String, Object>[] helperUtilitiesArray = _helperUtilitiesMaps.get(
156 classLoader);
157
158 if (helperUtilitiesArray == null) {
159 helperUtilitiesArray = (Map<String, Object>[])new Map<?, ?>[2];
160
161 _helperUtilitiesMaps.put(classLoader, helperUtilitiesArray);
162 }
163 else {
164 Map<String, Object> helperUtilities = null;
165
166 if (restricted) {
167 helperUtilities = helperUtilitiesArray[1];
168 }
169 else {
170 helperUtilities = helperUtilitiesArray[0];
171 }
172
173 if (helperUtilities != null) {
174 return helperUtilities;
175 }
176 }
177
178 Map<String, Object> helperUtilities = new HashMap<String, Object>();
179
180 populateCommonHelperUtilities(helperUtilities);
181 populateExtraHelperUtilities(helperUtilities);
182
183 if (restricted) {
184 Set<String> restrictedVariables = getRestrictedVariables();
185
186 for (String restrictedVariable : restrictedVariables) {
187 helperUtilities.remove(restrictedVariable);
188 }
189
190 helperUtilitiesArray[1] = helperUtilities;
191 }
192 else {
193 helperUtilitiesArray[0] = helperUtilities;
194 }
195
196 return helperUtilities;
197 }
198
199 public Set<String> getRestrictedVariables() {
200 return Collections.emptySet();
201 }
202
203 public TemplateControlContext getTemplateControlContext() {
204 return _pacl.getTemplateControlContext();
205 }
206
207 public void prepare(Template template, HttpServletRequest request) {
208
209
210
211 template.put("request", request);
212
213
214
215 PortletConfig portletConfig = (PortletConfig)request.getAttribute(
216 JavaConstants.JAVAX_PORTLET_CONFIG);
217
218 if (portletConfig != null) {
219 template.put("portletConfig", portletConfig);
220 }
221
222
223
224 final PortletRequest portletRequest =
225 (PortletRequest)request.getAttribute(
226 JavaConstants.JAVAX_PORTLET_REQUEST);
227
228 if (portletRequest != null) {
229 if (portletRequest instanceof RenderRequest) {
230 template.put("renderRequest", portletRequest);
231 }
232 }
233
234
235
236 final PortletResponse portletResponse =
237 (PortletResponse)request.getAttribute(
238 JavaConstants.JAVAX_PORTLET_RESPONSE);
239
240 if (portletResponse != null) {
241 if (portletResponse instanceof RenderResponse) {
242 template.put("renderResponse", portletResponse);
243 }
244 }
245
246
247
248 if ((portletRequest != null) && (portletResponse != null)) {
249 template.put(
250 "xmlRequest",
251 new Object() {
252
253 @Override
254 public String toString() {
255 return PortletRequestUtil.toXML(
256 portletRequest, portletResponse);
257 }
258
259 }
260 );
261 }
262
263
264
265 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
266 WebKeys.THEME_DISPLAY);
267
268 if (themeDisplay != null) {
269 Layout layout = themeDisplay.getLayout();
270 List<Layout> layouts = themeDisplay.getLayouts();
271
272 template.put("themeDisplay", themeDisplay);
273 template.put("company", themeDisplay.getCompany());
274 template.put("user", themeDisplay.getUser());
275 template.put("realUser", themeDisplay.getRealUser());
276 template.put("layout", layout);
277 template.put("layouts", layouts);
278 template.put("plid", String.valueOf(themeDisplay.getPlid()));
279 template.put(
280 "layoutTypePortlet", themeDisplay.getLayoutTypePortlet());
281 template.put(
282 "scopeGroupId", new Long(themeDisplay.getScopeGroupId()));
283 template.put(
284 "permissionChecker", themeDisplay.getPermissionChecker());
285 template.put("locale", themeDisplay.getLocale());
286 template.put("timeZone", themeDisplay.getTimeZone());
287 template.put("colorScheme", themeDisplay.getColorScheme());
288 template.put("portletDisplay", themeDisplay.getPortletDisplay());
289
290
291
292 if (layout != null) {
293 List<NavItem> navItems = NavItem.fromLayouts(
294 request, layouts, template);
295
296 template.put("navItems", navItems);
297 }
298
299
300
301 template.put(
302 "portletGroupId", new Long(themeDisplay.getScopeGroupId()));
303 }
304
305
306
307 Theme theme = (Theme)request.getAttribute(WebKeys.THEME);
308
309 if ((theme == null) && (themeDisplay != null)) {
310 theme = themeDisplay.getTheme();
311 }
312
313 if (theme != null) {
314 template.put("theme", theme);
315 }
316
317
318
319 prepareTiles(template, request);
320
321
322
323 ListMergeable<String> pageTitleListMergeable =
324 (ListMergeable<String>)request.getAttribute(WebKeys.PAGE_TITLE);
325
326 if (pageTitleListMergeable != null) {
327 String pageTitle = pageTitleListMergeable.mergeToString(
328 StringPool.SPACE);
329
330 template.put("pageTitle", pageTitle);
331 }
332
333 ListMergeable<String> pageSubtitleListMergeable =
334 (ListMergeable<String>)request.getAttribute(WebKeys.PAGE_SUBTITLE);
335
336 if (pageSubtitleListMergeable != null) {
337 String pageSubtitle = pageSubtitleListMergeable.mergeToString(
338 StringPool.SPACE);
339
340 template.put("pageSubtitle", pageSubtitle);
341 }
342 }
343
344 public void removeAllHelperUtilities() {
345 _helperUtilitiesMaps.clear();
346 }
347
348 public void removeHelperUtilities(ClassLoader classLoader) {
349 _helperUtilitiesMaps.remove(classLoader);
350 }
351
352 public static interface PACL {
353
354 public TemplateControlContext getTemplateControlContext();
355
356 }
357
358 protected void populateCommonHelperUtilities(
359 Map<String, Object> variables) {
360
361
362
363 variables.put("arrayUtil", ArrayUtil_IW.getInstance());
364
365
366
367 try {
368 variables.put(
369 "auditMessageFactoryUtil",
370 AuditMessageFactoryUtil.getAuditMessageFactory());
371 }
372 catch (SecurityException se) {
373 _log.error(se, se);
374 }
375
376
377
378 try {
379 variables.put("auditRouterUtil", AuditRouterUtil.getAuditRouter());
380 }
381 catch (SecurityException se) {
382 _log.error(se, se);
383 }
384
385
386
387 try {
388 variables.put(
389 "browserSniffer", BrowserSnifferUtil.getBrowserSniffer());
390 }
391 catch (SecurityException se) {
392 _log.error(se, se);
393 }
394
395
396
397 try {
398 variables.put(
399 "calendarFactory", CalendarFactoryUtil.getCalendarFactory());
400 }
401 catch (SecurityException se) {
402 _log.error(se, se);
403 }
404
405
406
407 try {
408 variables.put(
409 "dateFormatFactory",
410 FastDateFormatFactoryUtil.getFastDateFormatFactory());
411 }
412 catch (SecurityException se) {
413 _log.error(se, se);
414 }
415
416
417
418 variables.put("dateUtil", DateUtil_IW.getInstance());
419
420
421
422 try {
423 variables.put("ddmUtil", DDMUtil.getDDM());
424 }
425 catch (SecurityException se) {
426 _log.error(se, se);
427 }
428
429
430
431 try {
432 variables.put("dlUtil", DLUtil.getDL());
433 }
434 catch (SecurityException se) {
435 _log.error(se, se);
436 }
437
438
439
440 try {
441 ServiceLocator serviceLocator = ServiceLocator.getInstance();
442
443
444
445 variables.put("serviceLocator", serviceLocator);
446
447 try {
448 variables.put(
449 "expandoColumnLocalService",
450 serviceLocator.findService(
451 ExpandoColumnLocalService.class.getName()));
452 }
453 catch (SecurityException se) {
454 _log.error(se, se);
455 }
456
457
458
459 try {
460 variables.put(
461 "expandoRowLocalService",
462 serviceLocator.findService(
463 ExpandoRowLocalService.class.getName()));
464 }
465 catch (SecurityException se) {
466 _log.error(se, se);
467 }
468
469
470
471 try {
472 variables.put(
473 "expandoTableLocalService",
474 serviceLocator.findService(
475 ExpandoTableLocalService.class.getName()));
476 }
477 catch (SecurityException se) {
478 _log.error(se, se);
479 }
480
481
482
483 try {
484 variables.put(
485 "expandoValueLocalService",
486 serviceLocator.findService(
487 ExpandoValueLocalService.class.getName()));
488 }
489 catch (SecurityException se) {
490 _log.error(se, se);
491 }
492 }
493 catch (SecurityException se) {
494 _log.error(se, se);
495 }
496
497
498
499 variables.put("getterUtil", GetterUtil_IW.getInstance());
500
501
502
503 try {
504 variables.put("htmlUtil", HtmlUtil.getHtml());
505 }
506 catch (SecurityException se) {
507 _log.error(se, se);
508 }
509
510
511
512 try {
513 variables.put("httpUtil", HttpUtil.getHttp());
514 }
515 catch (SecurityException se) {
516 _log.error(se, se);
517 }
518
519
520
521 try {
522 variables.put(
523 "journalContentUtil", JournalContentUtil.getJournalContent());
524 }
525 catch (SecurityException se) {
526 _log.error(se, se);
527 }
528
529
530
531 try {
532 variables.put("jsonFactoryUtil", JSONFactoryUtil.getJSONFactory());
533 }
534 catch (SecurityException se) {
535 _log.error(se, se);
536 }
537
538
539
540 try {
541 variables.put("languageUtil", LanguageUtil.getLanguage());
542 }
543 catch (SecurityException se) {
544 _log.error(se, se);
545 }
546
547 try {
548 variables.put(
549 "unicodeLanguageUtil",
550 UnicodeLanguageUtil.getUnicodeLanguage());
551 }
552 catch (SecurityException se) {
553 _log.error(se, se);
554 }
555
556
557
558 try {
559 variables.put("localeUtil", LocaleUtil.getInstance());
560 }
561 catch (SecurityException se) {
562 _log.error(se, se);
563 }
564
565
566
567 variables.put("paramUtil", ParamUtil_IW.getInstance());
568
569
570
571 try {
572 variables.put("portalUtil", PortalUtil.getPortal());
573 }
574 catch (SecurityException se) {
575 _log.error(se, se);
576 }
577
578 try {
579 variables.put("portal", PortalUtil.getPortal());
580 }
581 catch (SecurityException se) {
582 _log.error(se, se);
583 }
584
585
586
587 try {
588 variables.put("prefsPropsUtil", PrefsPropsUtil.getPrefsProps());
589 }
590 catch (SecurityException se) {
591 _log.error(se, se);
592 }
593
594
595
596 try {
597 variables.put("propsUtil", PropsUtil.getProps());
598 }
599 catch (SecurityException se) {
600 _log.error(se, se);
601 }
602
603
604
605 variables.put(
606 "portletModeFactory", PortletModeFactory_IW.getInstance());
607
608
609
610 try {
611 variables.put(
612 "portletURLFactory",
613 PortletURLFactoryUtil.getPortletURLFactory());
614 }
615 catch (SecurityException se) {
616 _log.error(se, se);
617 }
618
619
620
621 try {
622 variables.put(
623 "randomizer", Randomizer_IW.getInstance().getWrappedInstance());
624 }
625 catch (SecurityException se) {
626 _log.error(se, se);
627 }
628
629 try {
630 UtilLocator utilLocator = UtilLocator.getInstance();
631
632
633
634 variables.put("utilLocator", utilLocator);
635
636
637
638 try {
639 variables.put(
640 "saxReaderUtil",
641 utilLocator.findUtil(SAXReader.class.getName()));
642 }
643 catch (SecurityException se) {
644 _log.error(se, se);
645 }
646 }
647 catch (SecurityException se) {
648 _log.error(se, se);
649 }
650
651
652
653 variables.put("sessionClicks", SessionClicks_IW.getInstance());
654
655
656
657 variables.put("staticFieldGetter", StaticFieldGetter.getInstance());
658
659
660
661 variables.put("stringUtil", StringUtil_IW.getInstance());
662
663
664
665 variables.put("timeZoneUtil", TimeZoneUtil_IW.getInstance());
666
667
668
669 variables.put("unicodeFormatter", UnicodeFormatter_IW.getInstance());
670
671
672
673 variables.put("validator", Validator_IW.getInstance());
674
675
676
677 try {
678 Class<?> clazz = VelocityTaglibImpl.class;
679
680 Method method = clazz.getMethod(
681 "layoutIcon", new Class[] {Layout.class});
682
683 variables.put("velocityTaglib_layoutIcon", method);
684 }
685 catch (Exception e) {
686 _log.error(e, e);
687 }
688
689
690
691 try {
692 variables.put(
693 "webServerToken",
694 WebServerServletTokenUtil.getWebServerServletToken());
695 }
696 catch (SecurityException se) {
697 _log.error(se, se);
698 }
699
700
701
702 variables.put(
703 "windowStateFactory", WindowStateFactory_IW.getInstance());
704
705
706
707 try {
708 variables.put(
709 "accountPermission",
710 AccountPermissionUtil.getAccountPermission());
711 }
712 catch (SecurityException se) {
713 _log.error(se, se);
714 }
715
716 try {
717 variables.put(
718 "commonPermission", CommonPermissionUtil.getCommonPermission());
719 }
720 catch (SecurityException se) {
721 _log.error(se, se);
722 }
723
724 try {
725 variables.put(
726 "groupPermission", GroupPermissionUtil.getGroupPermission());
727 }
728 catch (SecurityException se) {
729 _log.error(se, se);
730 }
731
732 try {
733 variables.put(
734 "layoutPermission", LayoutPermissionUtil.getLayoutPermission());
735 }
736 catch (SecurityException se) {
737 _log.error(se, se);
738 }
739
740 try {
741 variables.put(
742 "organizationPermission",
743 OrganizationPermissionUtil.getOrganizationPermission());
744 }
745 catch (SecurityException se) {
746 _log.error(se, se);
747 }
748
749 try {
750 variables.put(
751 "passwordPolicyPermission",
752 PasswordPolicyPermissionUtil.getPasswordPolicyPermission());
753 }
754 catch (SecurityException se) {
755 _log.error(se, se);
756 }
757
758 try {
759 variables.put(
760 "portalPermission", PortalPermissionUtil.getPortalPermission());
761 }
762 catch (SecurityException se) {
763 _log.error(se, se);
764 }
765
766 try {
767 variables.put(
768 "portletPermission",
769 PortletPermissionUtil.getPortletPermission());
770 }
771 catch (SecurityException se) {
772 _log.error(se, se);
773 }
774
775 try {
776 variables.put(
777 "rolePermission", RolePermissionUtil.getRolePermission());
778 }
779 catch (SecurityException se) {
780 _log.error(se, se);
781 }
782
783 try {
784 variables.put(
785 "userGroupPermission",
786 UserGroupPermissionUtil.getUserGroupPermission());
787 }
788 catch (SecurityException se) {
789 _log.error(se, se);
790 }
791
792 try {
793 variables.put(
794 "userPermission", UserPermissionUtil.getUserPermission());
795 }
796 catch (SecurityException se) {
797 _log.error(se, se);
798 }
799
800
801
802 try {
803 variables.put(
804 "dateFormats",
805 FastDateFormatFactoryUtil.getFastDateFormatFactory());
806 }
807 catch (SecurityException se) {
808 _log.error(se, se);
809 }
810
811 try {
812 variables.put(
813 "imageToken",
814 WebServerServletTokenUtil.getWebServerServletToken());
815 }
816 catch (SecurityException se) {
817 _log.error(se, se);
818 }
819
820 try {
821 variables.put(
822 "locationPermission",
823 OrganizationPermissionUtil.getOrganizationPermission());
824 }
825 catch (SecurityException se) {
826 _log.error(se, se);
827 }
828 }
829
830 protected void populateExtraHelperUtilities(Map<String, Object> variables) {
831 }
832
833 protected void prepareTiles(Template template, HttpServletRequest request) {
834 ComponentContext componentContext =
835 (ComponentContext)request.getAttribute(
836 ComponentConstants.COMPONENT_CONTEXT);
837
838 if (componentContext == null) {
839 return;
840 }
841
842 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
843 WebKeys.THEME_DISPLAY);
844
845 String tilesTitle = (String)componentContext.getAttribute("title");
846
847 themeDisplay.setTilesTitle(tilesTitle);
848
849 template.put("tilesTitle", tilesTitle);
850
851 String tilesContent = (String)componentContext.getAttribute("content");
852
853 themeDisplay.setTilesContent(tilesContent);
854
855 template.put("tilesContent", tilesContent);
856
857 boolean tilesSelectable = GetterUtil.getBoolean(
858 (String)componentContext.getAttribute("selectable"));
859
860 themeDisplay.setTilesSelectable(tilesSelectable);
861
862 template.put("tilesSelectable", tilesSelectable);
863 }
864
865 private static Log _log = LogFactoryUtil.getLog(
866 TemplateContextHelper.class);
867
868 private static PACL _pacl = new NoPACL();
869
870 private Map<ClassLoader, Map<String, Object>[]> _helperUtilitiesMaps =
871 new ConcurrentHashMap<ClassLoader, Map<String, Object>[]>();
872
873 private static class NoPACL implements PACL {
874
875 @Override
876 public TemplateControlContext getTemplateControlContext() {
877 ClassLoader contextClassLoader =
878 ClassLoaderUtil.getContextClassLoader();
879
880 return new TemplateControlContext(null, contextClassLoader);
881 }
882
883 }
884
885 }