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.template;
016    
017    import com.liferay.portal.kernel.template.Template;
018    import com.liferay.portal.kernel.template.TemplateException;
019    import com.liferay.portal.security.lang.PortalSecurityManagerThreadLocal;
020    import com.liferay.portal.security.pacl.PACLPolicy;
021    import com.liferay.portal.security.pacl.PACLPolicyManager;
022    import com.liferay.portal.util.ClassLoaderUtil;
023    
024    import java.io.Writer;
025    
026    import javax.servlet.http.HttpServletRequest;
027    
028    /**
029     * @author Tina Tian
030     */
031    public class PACLTemplateWrapper implements Template {
032    
033            public static Template getTemplate(Template template) {
034                    ClassLoader contextClassLoader =
035                            ClassLoaderUtil.getContextClassLoader();
036                    ClassLoader portalClassLoder = ClassLoaderUtil.getPortalClassLoader();
037    
038                    if (contextClassLoader == portalClassLoder) {
039                            return template;
040                    }
041    
042                    PACLPolicy paclPolicy = PACLPolicyManager.getPACLPolicy(
043                            contextClassLoader);
044    
045                    return new PACLTemplateWrapper(template, paclPolicy);
046            }
047    
048            public PACLTemplateWrapper(Template template, PACLPolicy policy) {
049                    if (template == null) {
050                            throw new IllegalArgumentException("Template is null");
051                    }
052    
053                    _template = template;
054                    _paclPolicy = policy;
055            }
056    
057            public Object get(String key) {
058                    return _template.get(key);
059            }
060    
061            public void prepare(HttpServletRequest request) {
062                    _template.prepare(request);
063            }
064    
065            public boolean processTemplate(Writer writer) throws TemplateException {
066                    PACLPolicy paclPolicy =
067                            PortalSecurityManagerThreadLocal.getPACLPolicy();
068    
069                    try {
070                            PortalSecurityManagerThreadLocal.setPACLPolicy(_paclPolicy);
071    
072                            return _template.processTemplate(writer);
073                    }
074                    finally {
075                            PortalSecurityManagerThreadLocal.setPACLPolicy(paclPolicy);
076                    }
077            }
078    
079            public void put(String key, Object value) {
080                    _template.put(key, value);
081            }
082    
083            private PACLPolicy _paclPolicy;
084            private Template _template;
085    
086    }