001    /**
002     * Copyright (c) 2000-present 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    
020    import java.io.Writer;
021    
022    import java.security.AccessControlContext;
023    import java.security.AccessController;
024    import java.security.PrivilegedActionException;
025    import java.security.PrivilegedExceptionAction;
026    
027    import javax.servlet.http.HttpServletRequest;
028    
029    /**
030     * @author Tina Tian
031     * @author Shuyang Zhou
032     */
033    public class PrivilegedTemplateWrapper implements Template {
034    
035            public PrivilegedTemplateWrapper(
036                    AccessControlContext accessControlContext, Template template) {
037    
038                    _accessControlContext = accessControlContext;
039                    _template = template;
040            }
041    
042            @Override
043            public void doProcessTemplate(Writer writer) throws Exception {
044                    AccessController.doPrivileged(
045                            new ProcessTemplatePrivilegedExceptionAction(_template, writer),
046                            _accessControlContext);
047            }
048    
049            @Override
050            public Object get(String key) {
051                    return _template.get(key);
052            }
053    
054            @Override
055            public String[] getKeys() {
056                    return _template.getKeys();
057            }
058    
059            @Override
060            public void prepare(HttpServletRequest request) {
061                    _template.prepare(request);
062            }
063    
064            @Override
065            public void processTemplate(Writer writer) throws TemplateException {
066                    try {
067                            doProcessTemplate(writer);
068                    }
069                    catch (PrivilegedActionException pae) {
070                            throw (TemplateException)pae.getException();
071                    }
072                    catch (Exception e) {
073                            throw new TemplateException();
074                    }
075            }
076    
077            @Override
078            public void put(String key, Object value) {
079                    _template.put(key, value);
080            }
081    
082            private final AccessControlContext _accessControlContext;
083            private Template _template;
084    
085            private static class ProcessTemplatePrivilegedExceptionAction
086                    implements PrivilegedExceptionAction<Void> {
087    
088                    public ProcessTemplatePrivilegedExceptionAction(
089                            Template template, Writer writer) {
090    
091                            _template = template;
092                            _writer = writer;
093                    }
094    
095                    @Override
096                    public Void run() throws Exception {
097                            _template.processTemplate(_writer);
098    
099                            return null;
100                    }
101    
102                    private Template _template;
103                    private final Writer _writer;
104    
105            }
106    
107    }