001    /**
002     * Copyright (c) 2000-2010 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.servlet.filters.monitoring;
016    
017    import com.liferay.portal.kernel.messaging.DestinationNames;
018    import com.liferay.portal.kernel.messaging.MessageBusUtil;
019    import com.liferay.portal.monitoring.RequestStatus;
020    import com.liferay.portal.monitoring.statistics.DataSampleThreadLocal;
021    import com.liferay.portal.monitoring.statistics.portal.PortalRequestDataSample;
022    import com.liferay.portal.servlet.filters.BasePortalFilter;
023    import com.liferay.portal.util.PortalUtil;
024    import com.liferay.portal.util.PropsValues;
025    
026    import java.io.IOException;
027    
028    import javax.servlet.FilterChain;
029    import javax.servlet.ServletException;
030    import javax.servlet.http.HttpServletRequest;
031    import javax.servlet.http.HttpServletResponse;
032    
033    /**
034     * @author Rajesh Thiagarajan
035     * @author Michael C. Han
036     */
037    public class MonitoringFilter extends BasePortalFilter {
038    
039            public static boolean isMonitoringPortalRequest() {
040                    return _monitoringPortalRequest;
041            }
042    
043            public static void setMonitoringPortalRequest(
044                    boolean monitoringPortalRequest) {
045    
046                    _monitoringPortalRequest = monitoringPortalRequest;
047            }
048    
049            protected boolean isFilterEnabled() {
050                    if (!super.isFilterEnabled()) {
051                            return false;
052                    }
053    
054                    if (!_monitoringPortalRequest) {
055                            return false;
056                    }
057    
058                    return true;
059            }
060    
061            protected void processFilter(
062                            HttpServletRequest request, HttpServletResponse response,
063                            FilterChain filterChain)
064                    throws IOException, ServletException {
065    
066                    long companyId = PortalUtil.getCompanyId(request);
067    
068                    PortalRequestDataSample portalRequestDataSample =
069                            new PortalRequestDataSample(
070                                    companyId, request.getRemoteUser(), request.getRequestURI(),
071                                    request.getRequestURL().toString());
072    
073                    try {
074                            portalRequestDataSample.prepare();
075    
076                            processFilter(
077                                    MonitoringFilter.class, request, response, filterChain);
078    
079                            portalRequestDataSample.capture(RequestStatus.SUCCESS);
080                    }
081                    catch (Exception e) {
082                            portalRequestDataSample.capture(RequestStatus.ERROR);
083    
084                            if (e instanceof IOException) {
085                                    throw (IOException)e;
086                            }
087                            else if (e instanceof ServletException) {
088                                    throw (ServletException)e;
089                            }
090                            else {
091                                    throw new ServletException("Unable to execute request", e);
092                            }
093                    }
094                    finally {
095                            MessageBusUtil.sendMessage(
096                                    DestinationNames.MONITORING, portalRequestDataSample);
097    
098                            DataSampleThreadLocal.addDataSample(portalRequestDataSample);
099                    }
100            }
101    
102            private static boolean _monitoringPortalRequest =
103                    PropsValues.MONITORING_PORTAL_REQUEST;
104    
105    }