001    /**
002     * Copyright (c) 2000-2013 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            @Override
038            public void destroy() {
039                    portalDestroy();
040            }
041    
042            @Override
043            public String getInitParameter(String name) {
044                    return servletConfig.getInitParameter(name);
045            }
046    
047            @Override
048            public Enumeration<String> getInitParameterNames() {
049                    return servletConfig.getInitParameterNames();
050            }
051    
052            @Override
053            public ServletConfig getServletConfig() {
054                    return servletConfig;
055            }
056    
057            @Override
058            public ServletContext getServletContext() {
059                    return servletConfig.getServletContext();
060            }
061    
062            @Override
063            public String getServletInfo() {
064                    return servlet.getServletInfo();
065            }
066    
067            @Override
068            public String getServletName() {
069                    return servletConfig.getServletName();
070            }
071    
072            @Override
073            public void init(ServletConfig servletConfig) {
074                    this.servletConfig = servletConfig;
075    
076                    registerPortalLifecycle();
077            }
078    
079            @Override
080            public void service(
081                            ServletRequest servletRequest, ServletResponse servletResponse)
082                    throws IOException, ServletException {
083    
084                    servlet.service(servletRequest, servletResponse);
085            }
086    
087            @Override
088            protected void doPortalDestroy() {
089                    servlet.destroy();
090            }
091    
092            @Override
093            protected void doPortalInit() throws Exception {
094                    ServletContext servletContext = servletConfig.getServletContext();
095    
096                    ClassLoader classLoader = (ClassLoader)servletContext.getAttribute(
097                            PluginContextListener.PLUGIN_CLASS_LOADER);
098    
099                    String servletClass = servletConfig.getInitParameter("servlet-class");
100    
101                    servlet = (Servlet)InstanceFactory.newInstance(
102                            classLoader, servletClass);
103    
104                    servlet.init(servletConfig);
105            }
106    
107            protected Servlet servlet;
108            protected ServletConfig servletConfig;
109    
110    }