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.TemplateException;
018    import com.liferay.portal.kernel.template.TemplateResource;
019    import com.liferay.portal.security.lang.PortalSecurityManagerThreadLocal;
020    import com.liferay.portal.security.pacl.PACLPolicy;
021    import com.liferay.portal.template.TemplateContextHelper;
022    
023    import freemarker.template.Configuration;
024    
025    import java.io.Writer;
026    
027    import java.util.Map;
028    
029    /**
030     * @author Raymond Augé
031     */
032    public class PACLFreeMarkerTemplate extends FreeMarkerTemplate {
033    
034            public PACLFreeMarkerTemplate(
035                    TemplateResource templateResource,
036                    TemplateResource errorTemplateResource, Map<String, Object> context,
037                    Configuration configuration,
038                    TemplateContextHelper templateContextHelper, PACLPolicy paclPolicy) {
039    
040                    super(
041                            templateResource, errorTemplateResource, context, configuration,
042                            templateContextHelper);
043    
044                    _paclPolicy = paclPolicy;
045            }
046    
047            @Override
048            public boolean processTemplate(Writer writer) throws TemplateException {
049                    PACLPolicy initialPolicy =
050                            PortalSecurityManagerThreadLocal.getPACLPolicy();
051    
052                    try {
053                            PortalSecurityManagerThreadLocal.setPACLPolicy(_paclPolicy);
054    
055                            return super.processTemplate(writer);
056                    }
057                    finally {
058                            PortalSecurityManagerThreadLocal.setPACLPolicy(initialPolicy);
059                    }
060            }
061    
062            private PACLPolicy _paclPolicy;
063    
064    }