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.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.kernel.monitoring.RequestStatus;
020    import com.liferay.portal.kernel.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            @Override
050            public boolean isFilterEnabled() {
051                    if (!super.isFilterEnabled()) {
052                            return false;
053                    }
054    
055                    if (!_monitoringPortalRequest) {
056                            return false;
057                    }
058    
059                    return true;
060            }
061    
062            @Override
063            protected void processFilter(
064                            HttpServletRequest request, HttpServletResponse response,
065                            FilterChain filterChain)
066                    throws IOException, ServletException {
067    
068                    long companyId = PortalUtil.getCompanyId(request);
069    
070                    PortalRequestDataSample portalRequestDataSample =
071                            new PortalRequestDataSample(
072                                    companyId, request.getRemoteUser(), request.getRequestURI(),
073                                    request.getRequestURL().toString());
074    
075                    try {
076                            portalRequestDataSample.prepare();
077    
078                            processFilter(
079                                    MonitoringFilter.class, request, response, filterChain);
080    
081                            portalRequestDataSample.capture(RequestStatus.SUCCESS);
082                    }
083                    catch (Exception e) {
084                            portalRequestDataSample.capture(RequestStatus.ERROR);
085    
086                            if (e instanceof IOException) {
087                                    throw (IOException)e;
088                            }
089                            else if (e instanceof ServletException) {
090                                    throw (ServletException)e;
091                            }
092                            else {
093                                    throw new ServletException("Unable to execute request", e);
094                            }
095                    }
096                    finally {
097                            MessageBusUtil.sendMessage(
098                                    DestinationNames.MONITORING, portalRequestDataSample);
099    
100                            DataSampleThreadLocal.addDataSample(portalRequestDataSample);
101                    }
102            }
103    
104            private static boolean _monitoringPortalRequest =
105                    PropsValues.MONITORING_PORTAL_REQUEST;
106    
107    }