1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.servlet.filters.monitoring;
24  
25  import com.liferay.portal.kernel.messaging.DestinationNames;
26  import com.liferay.portal.kernel.messaging.MessageBusUtil;
27  import com.liferay.portal.monitoring.RequestStatus;
28  import com.liferay.portal.monitoring.statistics.DataSampleThreadLocal;
29  import com.liferay.portal.monitoring.statistics.portal.PortalRequestDataSample;
30  import com.liferay.portal.servlet.filters.BasePortalFilter;
31  import com.liferay.portal.util.PortalUtil;
32  import com.liferay.portal.util.PropsValues;
33  
34  import java.io.IOException;
35  
36  import javax.servlet.FilterChain;
37  import javax.servlet.ServletException;
38  import javax.servlet.http.HttpServletRequest;
39  import javax.servlet.http.HttpServletResponse;
40  
41  /**
42   * <a href="MonitoringFilter.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Rajesh Thiagarajan
45   * @author Michael C. Han
46   *
47   */
48  public class MonitoringFilter extends BasePortalFilter {
49  
50      public static boolean isMonitoringPortalRequest() {
51          return _monitoringPortalRequest;
52      }
53  
54      public static void setMonitoringPortalRequest(
55          boolean monitoringPortalRequest) {
56  
57          _monitoringPortalRequest = monitoringPortalRequest;
58      }
59  
60      protected boolean isFilterEnabled() {
61          DataSampleThreadLocal.clearDataSamples();
62  
63          if (!super.isFilterEnabled()) {
64              return false;
65          }
66  
67          if (!_monitoringPortalRequest) {
68              return false;
69          }
70  
71          return true;
72      }
73  
74      protected void processFilter(
75              HttpServletRequest request, HttpServletResponse response,
76              FilterChain filterChain)
77          throws IOException, ServletException {
78  
79          long companyId = PortalUtil.getCompanyId(request);
80  
81          PortalRequestDataSample portalRequestDataSample =
82              new PortalRequestDataSample(
83                  companyId, request.getRemoteUser(), request.getRequestURI(),
84                  request.getRequestURL().toString());
85  
86          try {
87              portalRequestDataSample.prepare();
88  
89              processFilter(
90                  MonitoringFilter.class, request, response, filterChain);
91  
92              portalRequestDataSample.capture(RequestStatus.SUCCESS);
93          }
94          catch (Exception e) {
95              portalRequestDataSample.capture(RequestStatus.ERROR);
96  
97              if (e instanceof IOException) {
98                  throw (IOException)e;
99              }
100             else if (e instanceof ServletException) {
101                 throw (ServletException)e;
102             }
103             else {
104                 throw new ServletException("Unable to execute request", e);
105             }
106         }
107         finally {
108             MessageBusUtil.sendMessage(
109                 DestinationNames.MONITORING, portalRequestDataSample);
110 
111             DataSampleThreadLocal.addDataSample(portalRequestDataSample);
112         }
113     }
114 
115     private static boolean _monitoringPortalRequest =
116         PropsValues.MONITORING_PORTAL_REQUEST;
117 
118 }