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