001: /**
002: *
003: */package clime.messadmin.providers.lifecycle;
004:
005: import java.util.LinkedList;
006:
007: import javax.servlet.ServletContext;
008: import javax.servlet.http.HttpServletRequest;
009: import javax.servlet.http.HttpServletResponse;
010:
011: import clime.messadmin.clickstream.HttpRequestInfo;
012: import clime.messadmin.clickstream.Utils;
013: import clime.messadmin.filter.MessAdminThreadLocal;
014: import clime.messadmin.model.ErrorData;
015: import clime.messadmin.model.Server;
016: import clime.messadmin.model.Session;
017: import clime.messadmin.providers.spi.RequestExceptionProvider;
018: import clime.messadmin.providers.spi.RequestLifeCycleProvider;
019: import clime.messadmin.utils.SessionUtils;
020:
021: /**
022: * Collects statistics on Servlets, like the <a href="http://www.opensymphony.com/clickstream/">ClickStream</a> monitoring utility
023: * @author Cédrik LIME
024: */
025: public class ClickStreamGatherer implements RequestLifeCycleProvider,
026: RequestExceptionProvider {
027: /**
028: * Maximum # of URLs to keep in memory. Default is {@value}.
029: */
030: public static final int MAX_SIZE = 100; //TODO externalize in .properties
031:
032: /**
033: *
034: */
035: public ClickStreamGatherer() {
036: super ();
037: }
038:
039: /**
040: * {@inheritDoc}
041: */
042: public void requestInitialized(HttpServletRequest httpRequest,
043: HttpServletResponse httpResponse,
044: ServletContext servletContext) {
045: final String servletPath = SessionUtils
046: .getRequestURLWithMethodAndQueryString(httpRequest);
047: if (httpRequest.getSession(false) != null) {
048: Session session = Server.getInstance().getSession(
049: httpRequest.getSession(false));
050: HttpRequestInfo requestInfo = new HttpRequestInfo(
051: servletPath);
052: LinkedList list = Utils.getPluginData(session);
053: if (list.size() >= MAX_SIZE) {
054: list.removeFirst();
055: }
056: list.addLast(requestInfo);
057: //request.requestInitialized(requestInfo, httpRequest, servletContext);
058: requestInfo.requestDate = MessAdminThreadLocal
059: .getStartTime().getTime();
060: }
061: }
062:
063: /**
064: * {@inheritDoc}
065: */
066: public void requestDestroyed(HttpServletRequest httpRequest,
067: HttpServletResponse httpResponse,
068: ServletContext servletContext) {
069: if (httpRequest.getSession(false) != null) {
070: Session session = Server.getInstance().getSession(
071: httpRequest.getSession(false));
072: LinkedList list = Utils.getPluginData(session);
073: HttpRequestInfo requestInfo;
074: if (list.isEmpty()) {
075: // session was created with this hit
076: requestInfo = new HttpRequestInfo(
077: SessionUtils
078: .getRequestURLWithMethodAndQueryString(httpRequest));
079: list.addLast(requestInfo);
080: requestInfo.requestDate = MessAdminThreadLocal
081: .getStartTime().getTime();
082: } else {
083: requestInfo = (HttpRequestInfo) list.getLast();
084: }
085: //request.requestDestroyed(requestInfo, (MessAdminRequestWrapper)httpRequest, (MessAdminResponseWrapper)httpResponse, servletContext);
086: requestInfo.usedTime = MessAdminThreadLocal.getUsedTime();
087: }
088: }
089:
090: /**
091: * {@inheritDoc}
092: */
093: public void requestException(Exception e,
094: HttpServletRequest httpRequest,
095: HttpServletResponse httpResponse,
096: ServletContext servletContext) {
097: requestDestroyed(httpRequest, httpResponse, servletContext);
098: if (httpRequest.getSession(false) != null) {
099: Session session = Server.getInstance().getSession(
100: httpRequest.getSession(false));
101: HttpRequestInfo requestInfo = (HttpRequestInfo) Utils
102: .getPluginData(session).getLast();
103: //request.requestException(requestInfo, e, (MessAdminRequestWrapper)httpRequest, (MessAdminResponseWrapper)httpResponse, servletContext);
104: requestInfo.error = new ErrorData(httpRequest, e);
105: }
106: }
107:
108: /**
109: * {@inheritDoc}
110: */
111: public int getPriority() {
112: // no need for a priority, really
113: return 10;
114: }
115:
116: }
|