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.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.ClassUtil;
020    import com.liferay.portal.kernel.util.InstanceFactory;
021    import com.liferay.portal.kernel.util.StringUtil;
022    
023    import java.util.List;
024    import java.util.concurrent.CopyOnWriteArrayList;
025    
026    import javax.servlet.ServletContextEvent;
027    import javax.servlet.ServletContextListener;
028    import javax.servlet.ServletRequestAttributeEvent;
029    import javax.servlet.ServletRequestAttributeListener;
030    import javax.servlet.ServletRequestEvent;
031    import javax.servlet.ServletRequestListener;
032    import javax.servlet.http.HttpSessionActivationListener;
033    import javax.servlet.http.HttpSessionAttributeListener;
034    import javax.servlet.http.HttpSessionBindingEvent;
035    import javax.servlet.http.HttpSessionBindingListener;
036    import javax.servlet.http.HttpSessionEvent;
037    import javax.servlet.http.HttpSessionListener;
038    
039    /**
040     * @author Brian Wing Shun Chan
041     */
042    public class SecurePluginContextListener
043            extends PluginContextListener
044            implements HttpSessionActivationListener, HttpSessionAttributeListener,
045                               HttpSessionBindingListener, HttpSessionListener,
046                               ServletRequestAttributeListener, ServletRequestListener {
047    
048            @Override
049            public void attributeAdded(
050                    HttpSessionBindingEvent httpSessionBindingEvent) {
051    
052                    if (_httpSessionAttributeListeners == null) {
053                            return;
054                    }
055    
056                    for (HttpSessionAttributeListener httpSessionAttributeListener :
057                                    _httpSessionAttributeListeners) {
058    
059                            httpSessionAttributeListener.attributeAdded(
060                                    httpSessionBindingEvent);
061                    }
062            }
063    
064            @Override
065            public void attributeAdded(
066                    ServletRequestAttributeEvent servletRequestAttributeEvent) {
067    
068                    if (_servletRequestAttributeListeners == null) {
069                            return;
070                    }
071    
072                    for (ServletRequestAttributeListener servletRequestAttributeListener :
073                                    _servletRequestAttributeListeners) {
074    
075                            servletRequestAttributeListener.attributeAdded(
076                                    servletRequestAttributeEvent);
077                    }
078            }
079    
080            @Override
081            public void attributeRemoved(
082                    HttpSessionBindingEvent httpSessionBindingEvent) {
083    
084                    if (_httpSessionAttributeListeners == null) {
085                            return;
086                    }
087    
088                    for (HttpSessionAttributeListener httpSessionAttributeListener :
089                                    _httpSessionAttributeListeners) {
090    
091                            httpSessionAttributeListener.attributeRemoved(
092                                    httpSessionBindingEvent);
093                    }
094            }
095    
096            @Override
097            public void attributeRemoved(
098                    ServletRequestAttributeEvent servletRequestAttributeEvent) {
099    
100                    if (_servletRequestAttributeListeners == null) {
101                            return;
102                    }
103    
104                    for (ServletRequestAttributeListener servletRequestAttributeListener :
105                                    _servletRequestAttributeListeners) {
106    
107                            servletRequestAttributeListener.attributeRemoved(
108                                    servletRequestAttributeEvent);
109                    }
110            }
111    
112            @Override
113            public void attributeReplaced(
114                    HttpSessionBindingEvent httpSessionBindingEvent) {
115    
116                    if (_httpSessionAttributeListeners == null) {
117                            return;
118                    }
119    
120                    for (HttpSessionAttributeListener httpSessionAttributeListener :
121                                    _httpSessionAttributeListeners) {
122    
123                            httpSessionAttributeListener.attributeReplaced(
124                                    httpSessionBindingEvent);
125                    }
126            }
127    
128            @Override
129            public void attributeReplaced(
130                    ServletRequestAttributeEvent servletRequestAttributeEvent) {
131    
132                    if (_servletRequestAttributeListeners == null) {
133                            return;
134                    }
135    
136                    for (ServletRequestAttributeListener servletRequestAttributeListener :
137                                    _servletRequestAttributeListeners) {
138    
139                            servletRequestAttributeListener.attributeReplaced(
140                                    servletRequestAttributeEvent);
141                    }
142            }
143    
144            @Override
145            public void contextInitialized(ServletContextEvent servletContextEvent) {
146                    servletContext = servletContextEvent.getServletContext();
147    
148                    servletContext.setAttribute(
149                            SecurePluginContextListener.class.getName(), this);
150    
151                    super.contextInitialized(servletContextEvent);
152            }
153    
154            public void instantiatingListeners() throws Exception {
155                    if (_servletRequestListeners != null) {
156                            return;
157                    }
158    
159                    String[] listenerClassNames = StringUtil.split(
160                            servletContext.getInitParameter("portalListenerClasses"));
161    
162                    for (String listenerClassName : listenerClassNames) {
163                            instantiatingListener(listenerClassName);
164                    }
165            }
166    
167            @Override
168            public void requestDestroyed(ServletRequestEvent servletRequestEvent) {
169                    if (_servletRequestListeners == null) {
170                            return;
171                    }
172    
173                    for (ServletRequestListener servletRequestListener :
174                                    _servletRequestListeners) {
175    
176                            servletRequestListener.requestDestroyed(servletRequestEvent);
177                    }
178            }
179    
180            @Override
181            public void requestInitialized(ServletRequestEvent servletRequestEvent) {
182                    if (_servletRequestListeners == null) {
183                            return;
184                    }
185    
186                    for (ServletRequestListener servletRequestListener :
187                                    _servletRequestListeners) {
188    
189                            servletRequestListener.requestInitialized(servletRequestEvent);
190                    }
191            }
192    
193            @Override
194            public void sessionCreated(HttpSessionEvent httpSessionEvent) {
195                    if (_httpSessionListeners == null) {
196                            return;
197                    }
198    
199                    for (HttpSessionListener httpSessionListener : _httpSessionListeners) {
200                            httpSessionListener.sessionCreated(httpSessionEvent);
201                    }
202            }
203    
204            @Override
205            public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
206                    if (_httpSessionListeners == null) {
207                            return;
208                    }
209    
210                    for (HttpSessionListener httpSessionListener : _httpSessionListeners) {
211                            httpSessionListener.sessionDestroyed(httpSessionEvent);
212                    }
213            }
214    
215            @Override
216            public void sessionDidActivate(HttpSessionEvent httpSessionEvent) {
217                    if (_httpSessionActivationListeners == null) {
218                            return;
219                    }
220    
221                    for (HttpSessionActivationListener httpSessionActivationListener :
222                                    _httpSessionActivationListeners) {
223    
224                            httpSessionActivationListener.sessionDidActivate(httpSessionEvent);
225                    }
226            }
227    
228            @Override
229            public void sessionWillPassivate(HttpSessionEvent httpSessionEvent) {
230                    if (_httpSessionActivationListeners == null) {
231                            return;
232                    }
233    
234                    for (HttpSessionActivationListener httpSessionActivationListener :
235                                    _httpSessionActivationListeners) {
236    
237                            httpSessionActivationListener.sessionWillPassivate(
238                                    httpSessionEvent);
239                    }
240            }
241    
242            @Override
243            public void valueBound(HttpSessionBindingEvent httpSessionBindingEvent) {
244                    if (_httpSessionBindingListeners == null) {
245                            return;
246                    }
247    
248                    for (HttpSessionBindingListener httpSessionBindingListener :
249                                    _httpSessionBindingListeners) {
250    
251                            httpSessionBindingListener.valueBound(httpSessionBindingEvent);
252                    }
253            }
254    
255            @Override
256            public void valueUnbound(HttpSessionBindingEvent httpSessionBindingEvent) {
257                    if (_httpSessionBindingListeners == null) {
258                            return;
259                    }
260    
261                    for (HttpSessionBindingListener httpSessionBindingListener :
262                                    _httpSessionBindingListeners) {
263    
264                            httpSessionBindingListener.valueUnbound(httpSessionBindingEvent);
265                    }
266            }
267    
268            @Override
269            protected void fireUndeployEvent() {
270                    if (_servletContextListeners != null) {
271                            ServletContextEvent servletContextEvent = new ServletContextEvent(
272                                    servletContext);
273    
274                            for (ServletContextListener servletContextListener :
275                                            _servletContextListeners) {
276    
277                                    try {
278                                            servletContextListener.contextDestroyed(
279                                                    servletContextEvent);
280                                    }
281                                    catch (Throwable t) {
282                                            String className = ClassUtil.getClassName(
283                                                    servletContextListener.getClass());
284    
285                                            _log.error(
286                                                    className + " is unable to process a context " +
287                                                            "destroyed event for " +
288                                                                    servletContext.getServletContextName(),
289                                                    t);
290                                    }
291                            }
292                    }
293    
294                    super.fireUndeployEvent();
295            }
296    
297            protected void instantiatingListener(String listenerClassName)
298                    throws Exception {
299    
300                    if (_log.isDebugEnabled()) {
301                            _log.debug("Instantiating " + listenerClassName);
302                    }
303    
304                    Object listener = InstanceFactory.newInstance(
305                            pluginClassLoader, listenerClassName);
306    
307                    if (listener instanceof HttpSessionActivationListener) {
308                            if (_httpSessionActivationListeners == null) {
309                                    _httpSessionActivationListeners =
310                                            new CopyOnWriteArrayList<HttpSessionActivationListener>();
311                            }
312    
313                            _httpSessionActivationListeners.add(
314                                    (HttpSessionActivationListener)listener);
315                    }
316    
317                    if (listener instanceof HttpSessionAttributeListener) {
318                            if (_httpSessionAttributeListeners == null) {
319                                    _httpSessionAttributeListeners =
320                                            new CopyOnWriteArrayList<HttpSessionAttributeListener>();
321                            }
322    
323                            _httpSessionAttributeListeners.add(
324                                    (HttpSessionAttributeListener)listener);
325                    }
326    
327                    if (listener instanceof HttpSessionBindingListener) {
328                            if (_httpSessionBindingListeners == null) {
329                                    _httpSessionBindingListeners =
330                                            new CopyOnWriteArrayList<HttpSessionBindingListener>();
331                            }
332    
333                            _httpSessionBindingListeners.add(
334                                    (HttpSessionBindingListener)listener);
335                    }
336    
337                    if (listener instanceof HttpSessionListener) {
338                            if (_httpSessionListeners == null) {
339                                    _httpSessionListeners =
340                                            new CopyOnWriteArrayList<HttpSessionListener>();
341                            }
342    
343                            _httpSessionListeners.add((HttpSessionListener)listener);
344                    }
345    
346                    if (listener instanceof ServletContextListener) {
347                            if (_servletContextListeners == null) {
348                                    _servletContextListeners =
349                                            new CopyOnWriteArrayList<ServletContextListener>();
350                            }
351    
352                            ServletContextListener servletContextListener =
353                                    (ServletContextListener)listener;
354    
355                            _servletContextListeners.add(servletContextListener);
356    
357                            ServletContextEvent servletContextEvent = new ServletContextEvent(
358                                    servletContext);
359    
360                            servletContextListener.contextInitialized(servletContextEvent);
361                    }
362    
363                    if (listener instanceof ServletRequestAttributeListener) {
364                            if (_servletRequestAttributeListeners == null) {
365                                    _servletRequestAttributeListeners =
366                                            new CopyOnWriteArrayList<ServletRequestAttributeListener>();
367                            }
368    
369                            _servletRequestAttributeListeners.add(
370                                    (ServletRequestAttributeListener)listener);
371                    }
372    
373                    if (listener instanceof ServletRequestListener) {
374                            if (_servletRequestListeners == null) {
375                                    _servletRequestListeners =
376                                            new CopyOnWriteArrayList<ServletRequestListener>();
377                            }
378    
379                            _servletRequestListeners.add((ServletRequestListener)listener);
380                    }
381            }
382    
383            private static Log _log = LogFactoryUtil.getLog(
384                    SecurePluginContextListener.class);
385    
386            private List<HttpSessionActivationListener> _httpSessionActivationListeners;
387            private List<HttpSessionAttributeListener> _httpSessionAttributeListeners;
388            private List<HttpSessionBindingListener> _httpSessionBindingListeners;
389            private List<HttpSessionListener> _httpSessionListeners;
390            private List<ServletContextListener> _servletContextListeners;
391            private List<ServletRequestAttributeListener>
392                    _servletRequestAttributeListeners;
393            private List<ServletRequestListener> _servletRequestListeners;
394    
395    }