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.kernel.servlet;
016    
017    import com.liferay.portal.kernel.util.BasePortalLifecycle;
018    import com.liferay.portal.kernel.util.InstanceFactory;
019    
020    import java.io.IOException;
021    
022    import java.util.Enumeration;
023    
024    import javax.servlet.Servlet;
025    import javax.servlet.ServletConfig;
026    import javax.servlet.ServletContext;
027    import javax.servlet.ServletException;
028    import javax.servlet.ServletRequest;
029    import javax.servlet.ServletResponse;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     */
034    public class SecureServlet
035            extends BasePortalLifecycle implements ServletConfig, Servlet {
036    
037            public void destroy() {
038                    portalDestroy();
039            }
040    
041            public String getInitParameter(String name) {
042                    return _servletConfig.getInitParameter(name);
043            }
044    
045            public Enumeration<String> getInitParameterNames() {
046                    return _servletConfig.getInitParameterNames();
047            }
048    
049            public ServletConfig getServletConfig() {
050                    return _servletConfig;
051            }
052    
053            public ServletContext getServletContext() {
054                    return _servletConfig.getServletContext();
055            }
056    
057            public String getServletInfo() {
058                    return _servlet.getServletInfo();
059            }
060    
061            public String getServletName() {
062                    return _servletConfig.getServletName();
063            }
064    
065            public void init(ServletConfig servletConfig) {
066                    _servletConfig = servletConfig;
067    
068                    registerPortalLifecycle();
069            }
070    
071            public void service(
072                            ServletRequest servletRequest, ServletResponse servletResponse)
073                    throws IOException, ServletException {
074    
075                    _servlet.service(servletRequest, servletResponse);
076            }
077    
078            @Override
079            protected void doPortalDestroy() {
080                    _servlet.destroy();
081            }
082    
083            @Override
084            protected void doPortalInit() throws Exception {
085                    ServletContext servletContext = _servletConfig.getServletContext();
086    
087                    ClassLoader classLoader = (ClassLoader)servletContext.getAttribute(
088                            PluginContextListener.PLUGIN_CLASS_LOADER);
089    
090                    String servletClass = _servletConfig.getInitParameter("servlet-class");
091    
092                    _servlet = (Servlet)InstanceFactory.newInstance(
093                            classLoader, servletClass);
094    
095                    _servlet.init(_servletConfig);
096            }
097    
098            private Servlet _servlet;
099            private ServletConfig _servletConfig;
100    
101    }