001: /*
002: * Copyright (c) 1998-2005 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Sam
028: */
029:
030: package com.caucho.server.webapp;
031:
032: import com.caucho.server.connection.AbstractHttpRequest;
033: import com.caucho.server.connection.Connection;
034: import com.caucho.server.port.TcpConnection;
035: import com.caucho.util.Alarm;
036: import com.caucho.vfs.ClientDisconnectException;
037:
038: import javax.servlet.FilterChain;
039: import javax.servlet.ServletException;
040: import javax.servlet.ServletRequest;
041: import javax.servlet.ServletResponse;
042: import java.io.IOException;
043:
044: public class StatisticsFilterChain implements FilterChain {
045: private final FilterChain _next;
046: private WebApp _webApp;
047:
048: public StatisticsFilterChain(FilterChain next, WebApp webApp) {
049: _next = next;
050: _webApp = webApp;
051: }
052:
053: public void doFilter(ServletRequest request,
054: ServletResponse response) throws ServletException,
055: IOException {
056: if (request instanceof AbstractHttpRequest) {
057: AbstractHttpRequest httpRequest = (AbstractHttpRequest) request;
058:
059: Connection connection = httpRequest.getConnection();
060:
061: if (connection instanceof TcpConnection) {
062: TcpConnection tcpConnection = (TcpConnection) connection;
063:
064: long time = Alarm.getExactTime();
065:
066: long readBytes = -1;
067: long writeBytes = -1;
068:
069: readBytes = tcpConnection.getSocket()
070: .getTotalReadBytes();
071: writeBytes = tcpConnection.getSocket()
072: .getTotalWriteBytes();
073:
074: ClientDisconnectException clientDisconnectException = null;
075:
076: try {
077: _next.doFilter(request, response);
078: } catch (ClientDisconnectException ex) {
079: clientDisconnectException = ex;
080: }
081:
082: time = Alarm.getExactTime() - time;
083:
084: readBytes = tcpConnection.getSocket()
085: .getTotalReadBytes()
086: - readBytes;
087: writeBytes = tcpConnection.getSocket()
088: .getTotalReadBytes()
089: - writeBytes;
090:
091: _webApp.updateStatistics(time, (int) readBytes,
092: (int) writeBytes,
093: clientDisconnectException != null);
094:
095: if (clientDisconnectException != null)
096: throw clientDisconnectException;
097: }
098: } else {
099: _next.doFilter(request, response);
100: }
101: }
102: }
|