1 29 30 31 package com.caucho.server.webapp; 32 33 import com.caucho.server.connection.AbstractHttpRequest; 34 import com.caucho.server.connection.Connection; 35 import com.caucho.server.port.TcpConnection; 36 import com.caucho.util.Alarm; 37 import com.caucho.vfs.ClientDisconnectException; 38 39 import javax.servlet.FilterChain ; 40 import javax.servlet.ServletException ; 41 import javax.servlet.ServletRequest ; 42 import javax.servlet.ServletResponse ; 43 import java.io.IOException ; 44 45 public class StatisticsFilterChain 46 implements FilterChain 47 { 48 private final FilterChain _next; 49 private WebApp _webApp; 50 51 public StatisticsFilterChain(FilterChain next, WebApp webApp) 52 { 53 _next = next; 54 _webApp = webApp; 55 } 56 57 public void doFilter(ServletRequest request, ServletResponse response) 58 throws ServletException , IOException 59 { 60 if (request instanceof AbstractHttpRequest) { 61 AbstractHttpRequest httpRequest = (AbstractHttpRequest) request; 62 63 Connection connection = httpRequest.getConnection(); 64 65 if (connection instanceof TcpConnection) { 66 TcpConnection tcpConnection = (TcpConnection) connection; 67 68 long time = Alarm.getExactTime(); 69 70 long readBytes = -1; 71 long writeBytes = -1; 72 73 readBytes = tcpConnection.getSocket().getTotalReadBytes(); 74 writeBytes = tcpConnection.getSocket().getTotalWriteBytes(); 75 76 ClientDisconnectException clientDisconnectException = null; 77 78 try { 79 _next.doFilter(request, response); 80 } 81 catch (ClientDisconnectException ex) { 82 clientDisconnectException = ex; 83 } 84 85 time = Alarm.getExactTime() - time; 86 87 readBytes = tcpConnection.getSocket().getTotalReadBytes() - readBytes; 88 writeBytes = tcpConnection.getSocket().getTotalReadBytes() - writeBytes; 89 90 _webApp.updateStatistics(time, (int) readBytes, (int) writeBytes, clientDisconnectException != null); 91 92 if (clientDisconnectException != null) 93 throw clientDisconnectException; 94 } 95 } 96 else { 97 _next.doFilter(request, response); 98 } 99 } 100 } 101 | Popular Tags |