001    /**
002     * Copyright (c) 2000-2010 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.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    }