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.velocity;
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 java.io.Writer;
024    
025    import org.apache.velocity.VelocityContext;
026    import org.apache.velocity.app.VelocityEngine;
027    
028    /**
029     * @author Raymond Augé
030     */
031    public class PACLVelocityTemplate extends VelocityTemplate {
032    
033            public PACLVelocityTemplate(
034                    TemplateResource templateResource,
035                    TemplateResource errorTemplateResource, VelocityContext velocityContext,
036                    VelocityEngine velocityEngine,
037                    TemplateContextHelper templateContextHelper, PACLPolicy paclPolicy) {
038    
039                    super(
040                            templateResource, errorTemplateResource, velocityContext,
041                            velocityEngine, templateContextHelper);
042    
043                    _paclPolicy = paclPolicy;
044            }
045    
046            @Override
047            public boolean processTemplate(Writer writer) throws TemplateException {
048                    PACLPolicy initialPolicy =
049                            PortalSecurityManagerThreadLocal.getPACLPolicy();
050    
051                    try {
052                            PortalSecurityManagerThreadLocal.setPACLPolicy(_paclPolicy);
053    
054                            return super.processTemplate(writer);
055                    }
056                    finally {
057                            PortalSecurityManagerThreadLocal.setPACLPolicy(initialPolicy);
058                    }
059            }
060    
061            private PACLPolicy _paclPolicy;
062    
063    }