1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.servlet.filters.monitoring;
16  
17  import com.liferay.portal.kernel.messaging.DestinationNames;
18  import com.liferay.portal.kernel.messaging.MessageBusUtil;
19  import com.liferay.portal.monitoring.RequestStatus;
20  import com.liferay.portal.monitoring.statistics.DataSampleThreadLocal;
21  import com.liferay.portal.monitoring.statistics.portal.PortalRequestDataSample;
22  import com.liferay.portal.servlet.filters.BasePortalFilter;
23  import com.liferay.portal.util.PortalUtil;
24  import com.liferay.portal.util.PropsValues;
25  
26  import java.io.IOException;
27  
28  import javax.servlet.FilterChain;
29  import javax.servlet.ServletException;
30  import javax.servlet.http.HttpServletRequest;
31  import javax.servlet.http.HttpServletResponse;
32  
33  /**
34   * <a href="MonitoringFilter.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Rajesh Thiagarajan
37   * @author Michael C. Han
38   */
39  public class MonitoringFilter extends BasePortalFilter {
40  
41      public static boolean isMonitoringPortalRequest() {
42          return _monitoringPortalRequest;
43      }
44  
45      public static void setMonitoringPortalRequest(
46          boolean monitoringPortalRequest) {
47  
48          _monitoringPortalRequest = monitoringPortalRequest;
49      }
50  
51      protected boolean isFilterEnabled() {
52          if (!super.isFilterEnabled()) {
53              return false;
54          }
55  
56          if (!_monitoringPortalRequest) {
57              return false;
58          }
59  
60          return true;
61      }
62  
63      protected void processFilter(
64              HttpServletRequest request, HttpServletResponse response,
65              FilterChain filterChain)
66          throws IOException, ServletException {
67  
68          long companyId = PortalUtil.getCompanyId(request);
69  
70          PortalRequestDataSample portalRequestDataSample =
71              new PortalRequestDataSample(
72                  companyId, request.getRemoteUser(), request.getRequestURI(),
73                  request.getRequestURL().toString());
74  
75          try {
76              portalRequestDataSample.prepare();
77  
78              processFilter(
79                  MonitoringFilter.class, request, response, filterChain);
80  
81              portalRequestDataSample.capture(RequestStatus.SUCCESS);
82          }
83          catch (Exception e) {
84              portalRequestDataSample.capture(RequestStatus.ERROR);
85  
86              if (e instanceof IOException) {
87                  throw (IOException)e;
88              }
89              else if (e instanceof ServletException) {
90                  throw (ServletException)e;
91              }
92              else {
93                  throw new ServletException("Unable to execute request", e);
94              }
95          }
96          finally {
97              MessageBusUtil.sendMessage(
98                  DestinationNames.MONITORING, portalRequestDataSample);
99  
100             DataSampleThreadLocal.addDataSample(portalRequestDataSample);
101         }
102     }
103 
104     private static boolean _monitoringPortalRequest =
105         PropsValues.MONITORING_PORTAL_REQUEST;
106 
107 }