001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.velocity;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.template.Template;
020    import com.liferay.portal.kernel.template.TemplateConstants;
021    import com.liferay.portal.kernel.util.GetterUtil;
022    import com.liferay.portal.kernel.util.SetUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.model.Theme;
025    import com.liferay.portal.service.permission.RolePermissionUtil;
026    import com.liferay.portal.template.TemplateContextHelper;
027    import com.liferay.portal.template.TemplatePortletPreferences;
028    import com.liferay.portal.theme.ThemeDisplay;
029    import com.liferay.portal.util.PropsValues;
030    import com.liferay.portal.util.WebKeys;
031    
032    import java.util.Map;
033    import java.util.Set;
034    
035    import javax.servlet.http.HttpServletRequest;
036    
037    import org.apache.velocity.tools.generic.DateTool;
038    import org.apache.velocity.tools.generic.EscapeTool;
039    import org.apache.velocity.tools.generic.IteratorTool;
040    import org.apache.velocity.tools.generic.ListTool;
041    import org.apache.velocity.tools.generic.MathTool;
042    import org.apache.velocity.tools.generic.NumberTool;
043    import org.apache.velocity.tools.generic.SortTool;
044    
045    /**
046     * @author Brian Wing Shun Chan
047     * @author Raymond Aug??
048     */
049    public class VelocityTemplateContextHelper extends TemplateContextHelper {
050    
051            @Override
052            public Set<String> getRestrictedVariables() {
053                    return SetUtil.fromArray(
054                            PropsValues.VELOCITY_ENGINE_RESTRICTED_VARIABLES);
055            }
056    
057            @Override
058            public void prepare(Template template, HttpServletRequest request) {
059                    super.prepare(template, request);
060    
061                    // Theme display
062    
063                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
064                            WebKeys.THEME_DISPLAY);
065    
066                    if (themeDisplay != null) {
067    
068                            // Init
069    
070                            template.put(
071                                    "init",
072                                    themeDisplay.getPathContext() +
073                                            TemplateConstants.SERVLET_SEPARATOR +
074                                                    "/html/themes/_unstyled/templates/init.vm");
075                    }
076    
077                    // Theme
078    
079                    Theme theme = (Theme)request.getAttribute(WebKeys.THEME);
080    
081                    if ((theme == null) && (themeDisplay != null)) {
082                            theme = themeDisplay.getTheme();
083                    }
084    
085                    if (theme != null) {
086    
087                            // Full css and templates path
088    
089                            String servletContextName = GetterUtil.getString(
090                                    theme.getServletContextName());
091    
092                            template.put(
093                                    "fullCssPath",
094                                    servletContextName + theme.getVelocityResourceListener() +
095                                            theme.getCssPath());
096    
097                            template.put(
098                                    "fullTemplatesPath",
099                                    servletContextName + theme.getVelocityResourceListener() +
100                                            theme.getTemplatesPath());
101                    }
102    
103                    // Insert custom vm variables
104    
105                    Map<String, Object> vmVariables =
106                            (Map<String, Object>)request.getAttribute(WebKeys.VM_VARIABLES);
107    
108                    if (vmVariables != null) {
109                            for (Map.Entry<String, Object> entry : vmVariables.entrySet()) {
110                                    String key = entry.getKey();
111                                    Object value = entry.getValue();
112    
113                                    if (Validator.isNotNull(key)) {
114                                            template.put(key, value);
115                                    }
116                            }
117                    }
118            }
119    
120            @Override
121            protected void populateExtraHelperUtilities(
122                    Map<String, Object> velocityContext) {
123    
124                    // Date tool
125    
126                    velocityContext.put("dateTool", new DateTool());
127    
128                    // Escape tool
129    
130                    velocityContext.put("escapeTool", new EscapeTool());
131    
132                    // Iterator tool
133    
134                    velocityContext.put("iteratorTool", new IteratorTool());
135    
136                    // List tool
137    
138                    velocityContext.put("listTool", new ListTool());
139    
140                    // Math tool
141    
142                    velocityContext.put("mathTool", new MathTool());
143    
144                    // Number tool
145    
146                    velocityContext.put("numberTool", new NumberTool());
147    
148                    // Portlet preferences
149    
150                    velocityContext.put(
151                            "velocityPortletPreferences", new TemplatePortletPreferences());
152    
153                    // Sort tool
154    
155                    velocityContext.put("sortTool", new SortTool());
156    
157                    // Permissions
158    
159                    try {
160                            velocityContext.put(
161                                    "rolePermission", RolePermissionUtil.getRolePermission());
162                    }
163                    catch (SecurityException se) {
164                            _log.error(se, se);
165                    }
166            }
167    
168            private static final Log _log = LogFactoryUtil.getLog(
169                    VelocityTemplateContextHelper.class);
170    
171    }