001    /**
002     * Copyright (c) 2000-2013 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.freemarker;
016    
017    import com.liferay.portal.kernel.template.Template;
018    import com.liferay.portal.kernel.template.TemplateConstants;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.SetUtil;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.model.Theme;
024    import com.liferay.portal.template.TemplateContextHelper;
025    import com.liferay.portal.template.TemplatePortletPreferences;
026    import com.liferay.portal.theme.ThemeDisplay;
027    import com.liferay.portal.util.PropsValues;
028    import com.liferay.portal.util.WebKeys;
029    
030    import freemarker.ext.beans.BeansWrapper;
031    
032    import java.util.Map;
033    import java.util.Set;
034    
035    import javax.servlet.http.HttpServletRequest;
036    
037    /**
038     * @author Mika Koivisto
039     * @author Raymond Aug??
040     */
041    public class FreeMarkerTemplateContextHelper extends TemplateContextHelper {
042    
043            @Override
044            public Set<String> getRestrictedVariables() {
045                    return SetUtil.fromArray(
046                            PropsValues.FREEMARKER_ENGINE_RESTRICTED_VARIABLES);
047            }
048    
049            @Override
050            public void prepare(Template template, HttpServletRequest request) {
051                    super.prepare(template, request);
052    
053                    // Theme display
054    
055                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
056                            WebKeys.THEME_DISPLAY);
057    
058                    if (themeDisplay != null) {
059                            Theme theme = themeDisplay.getTheme();
060    
061                            // Full css and templates path
062    
063                            String servletContextName = GetterUtil.getString(
064                                    theme.getServletContextName());
065    
066                            template.put(
067                                    "fullCssPath",
068                                    StringPool.SLASH + servletContextName +
069                                            theme.getFreeMarkerTemplateLoader() + theme.getCssPath());
070    
071                            template.put(
072                                    "fullTemplatesPath",
073                                    StringPool.SLASH + servletContextName +
074                                            theme.getFreeMarkerTemplateLoader() +
075                                                    theme.getTemplatesPath());
076    
077                            // Init
078    
079                            template.put(
080                                    "init",
081                                    StringPool.SLASH + themeDisplay.getPathContext() +
082                                            TemplateConstants.SERVLET_SEPARATOR +
083                                                    "/html/themes/_unstyled/templates/init.ftl");
084                    }
085    
086                    // Insert custom ftl variables
087    
088                    Map<String, Object> ftlVariables =
089                            (Map<String, Object>)request.getAttribute(WebKeys.FTL_VARIABLES);
090    
091                    if (ftlVariables != null) {
092                            for (Map.Entry<String, Object> entry : ftlVariables.entrySet()) {
093                                    String key = entry.getKey();
094                                    Object value = entry.getValue();
095    
096                                    if (Validator.isNotNull(key)) {
097                                            template.put(key, value);
098                                    }
099                            }
100                    }
101            }
102    
103            @Override
104            protected void populateExtraHelperUtilities(
105                    Map<String, Object> helperUtilities) {
106    
107                    // Enum util
108    
109                    helperUtilities.put(
110                            "enumUtil", BeansWrapper.getDefaultInstance().getEnumModels());
111    
112                    // Object util
113    
114                    helperUtilities.put("objectUtil", new LiferayObjectConstructor());
115    
116                    // Portlet preferences
117    
118                    helperUtilities.put(
119                            "freeMarkerPortletPreferences", new TemplatePortletPreferences());
120    
121                    // Static class util
122    
123                    helperUtilities.put(
124                            "staticUtil", BeansWrapper.getDefaultInstance().getStaticModels());
125            }
126    
127    }