001    /**
002     * Copyright (c) 2000-2012 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.TemplateResource;
018    import com.liferay.portal.kernel.templateparser.TemplateContext;
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 Map<String, Object> getHelperUtilities() {
045                    Map<String, Object> helperUtilities = super.getHelperUtilities();
046    
047                    // Enum util
048    
049                    helperUtilities.put(
050                            "enumUtil", BeansWrapper.getDefaultInstance().getEnumModels());
051    
052                    // Object util
053    
054                    helperUtilities.put("objectUtil", new LiferayObjectConstructor());
055    
056                    // Portlet preferences
057    
058                    helperUtilities.put(
059                            "freeMarkerPortletPreferences", new TemplatePortletPreferences());
060    
061                    // Static class util
062    
063                    helperUtilities.put(
064                            "staticUtil", BeansWrapper.getDefaultInstance().getStaticModels());
065    
066                    return helperUtilities;
067            }
068    
069            @Override
070            public Set<String> getRestrictedVariables() {
071                    return SetUtil.fromArray(
072                            PropsValues.JOURNAL_TEMPLATE_FREEMARKER_RESTRICTED_VARIABLES);
073            }
074    
075            @Override
076            public void prepare(
077                    TemplateContext templateContext, HttpServletRequest request) {
078    
079                    super.prepare(templateContext, request);
080    
081                    // Theme display
082    
083                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
084                            WebKeys.THEME_DISPLAY);
085    
086                    if (themeDisplay != null) {
087                            Theme theme = themeDisplay.getTheme();
088    
089                            // Full css and templates path
090    
091                            String servletContextName = GetterUtil.getString(
092                                    theme.getServletContextName());
093    
094                            templateContext.put(
095                                    "fullCssPath",
096                                    StringPool.SLASH + servletContextName +
097                                            theme.getFreeMarkerTemplateLoader() + theme.getCssPath());
098    
099                            templateContext.put(
100                                    "fullTemplatesPath",
101                                    StringPool.SLASH + servletContextName +
102                                            theme.getFreeMarkerTemplateLoader() +
103                                                    theme.getTemplatesPath());
104    
105                            // Init
106    
107                            templateContext.put(
108                                    "init",
109                                    StringPool.SLASH + themeDisplay.getPathContext() +
110                                            TemplateResource.SERVLET_SEPARATOR +
111                                                    "/html/themes/_unstyled/templates/init.ftl");
112                    }
113    
114                    // Insert custom ftl variables
115    
116                    Map<String, Object> ftlVariables =
117                            (Map<String, Object>)request.getAttribute(WebKeys.FTL_VARIABLES);
118    
119                    if (ftlVariables != null) {
120                            for (Map.Entry<String, Object> entry : ftlVariables.entrySet()) {
121                                    String key = entry.getKey();
122                                    Object value = entry.getValue();
123    
124                                    if (Validator.isNotNull(key)) {
125                                            templateContext.put(key, value);
126                                    }
127                            }
128                    }
129            }
130    
131    }