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  public class MonitoringFilter extends BasePortalFilter {
48  
49      public static boolean isMonitoringPortalRequest() {
50          return _monitoringPortalRequest;
51      }
52  
53      public static void setMonitoringPortalRequest(
54          boolean monitoringPortalRequest) {
55  
56          _monitoringPortalRequest = monitoringPortalRequest;
57      }
58  
59      protected boolean isFilterEnabled() {
60          DataSampleThreadLocal.clearDataSamples();
61  
62          if (!super.isFilterEnabled()) {
63              return false;
64          }
65  
66          if (!_monitoringPortalRequest) {
67              return false;
68          }
69  
70          return true;
71      }
72  
73      protected void processFilter(
74              HttpServletRequest request, HttpServletResponse response,
75              FilterChain filterChain)
76          throws IOException, ServletException {
77  
78          long companyId = PortalUtil.getCompanyId(request);
79  
80          PortalRequestDataSample portalRequestDataSample =
81              new PortalRequestDataSample(
82                  companyId, request.getRemoteUser(), request.getRequestURI(),
83                  request.getRequestURL().toString());
84  
85          try {
86              portalRequestDataSample.prepare();
87  
88              processFilter(
89                  MonitoringFilter.class, request, response, filterChain);
90  
91              portalRequestDataSample.capture(RequestStatus.SUCCESS);
92          }
93          catch (Exception e) {
94              portalRequestDataSample.capture(RequestStatus.ERROR);
95  
96              if (e instanceof IOException) {
97                  throw (IOException)e;
98              }
99              else if (e instanceof ServletException) {
100                 throw (ServletException)e;
101             }
102             else {
103                 throw new ServletException("Unable to execute request", e);
104             }
105         }
106         finally {
107             MessageBusUtil.sendMessage(
108                 DestinationNames.MONITORING, portalRequestDataSample);
109 
110             DataSampleThreadLocal.addDataSample(portalRequestDataSample);
111         }
112     }
113 
114     private static boolean _monitoringPortalRequest =
115         PropsValues.MONITORING_PORTAL_REQUEST;
116 
117 }