001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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    }