Author: jonesde
Date: Fri Apr 27 02:39:09 2007 New Revision: 533037 URL: http://svn.apache.org/viewvc?view=rev&rev=533037 Log: Some fair refactoring of the VisitHandler class to make it more resilient and centralize some of the data gathering in the ControlServlet; along with this are fixes for a couple of problems that caused the initial request data to be missing from the Visit especially on logout which cleared the session; this has been broken for a long time so is hopefully resolved now Modified: ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/UtilHttp.java ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/ControlServlet.java ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/RequestHandler.java ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/stats/VisitHandler.java Modified: ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/UtilHttp.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/UtilHttp.java?view=diff&rev=533037&r1=533036&r2=533037 ============================================================================== --- ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/UtilHttp.java (original) +++ ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/UtilHttp.java Fri Apr 27 02:39:09 2007 @@ -287,6 +287,27 @@ } return appName; } + + public static void setInitialRequestInfo(HttpServletRequest request) { + HttpSession session = request.getSession(); + if (UtilValidate.isNotEmpty((String) session.getAttribute("_WEBAPP_NAME_"))) { + // oops, info already in place... + return; + } + + StringBuffer fullRequestUrl = UtilHttp.getFullRequestUrl(request); + + session.setAttribute("_WEBAPP_NAME_", UtilHttp.getApplicationName(request)); + session.setAttribute("_CLIENT_LOCALE_", request.getLocale()); + session.setAttribute("_CLIENT_REQUEST_", fullRequestUrl.toString()); + session.setAttribute("_CLIENT_USER_AGENT_", request.getHeader("User-Agent") != null ? request.getHeader("User-Agent") : ""); + session.setAttribute("_CLIENT_REFERER_", request.getHeader("Referer") != null ? request.getHeader("Referer") : ""); + + session.setAttribute("_CLIENT_FORWARDED_FOR_", request.getHeader("X-Forwarded-For")); + session.setAttribute("_CLIENT_REMOTE_ADDR_", request.getRemoteAddr()); + session.setAttribute("_CLIENT_REMOTE_HOST_", request.getRemoteHost()); + session.setAttribute("_CLIENT_REMOTE_USER_", request.getRemoteUser()); + } /** * Put request parameters in request object as attributes. Modified: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/ControlServlet.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/ControlServlet.java?view=diff&rev=533037&r1=533036&r2=533037 ============================================================================== --- ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/ControlServlet.java (original) +++ ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/ControlServlet.java Fri Apr 27 02:39:09 2007 @@ -43,6 +43,7 @@ import org.ofbiz.security.Security; import org.ofbiz.service.LocalDispatcher; import org.ofbiz.webapp.stats.ServerHitBin; +import org.ofbiz.webapp.stats.VisitHandler; /** * ControlServlet.java - Master servlet for the web application. @@ -81,8 +82,10 @@ * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + long requestStartTime = System.currentTimeMillis(); RequestHandler requestHandler = this.getRequestHandler(); - + HttpSession session = request.getSession(); + // setup DEFAULT chararcter encoding and content type, this will be overridden in the RequestHandler for view rendering String charset = getServletContext().getInitParameter("charset"); if (charset == null || charset.length() == 0) charset = request.getCharacterEncoding(); @@ -102,9 +105,6 @@ response.setContentType(contentType); } - long requestStartTime = System.currentTimeMillis(); - HttpSession session = request.getSession(); - GenericValue userLogin = (GenericValue) session.getAttribute("userLogin"); //Debug.log("Cert Chain: " + request.getAttribute("javax.servlet.request.X509Certificate"), module); @@ -173,6 +173,10 @@ request.setAttribute("_REQUEST_HANDLER_", requestHandler); + // setup some things that should always be there + UtilHttp.setInitialRequestInfo(request); + VisitHandler.getVisitor(request, response); + // display details on the servlet objects if (Debug.verboseOn()) { logRequestInfo(request); @@ -252,6 +256,10 @@ Debug.logWarning(e, module); } + // run these two again before the ServerHitBin.countRequest call because on a logout this will end up creating a new visit + UtilHttp.setInitialRequestInfo(request); + VisitHandler.getVisitor(request, response); + ServerHitBin.countRequest(webappName + "." + rname, request, requestStartTime, System.currentTimeMillis() - requestStartTime, userLogin, delegator); if (Debug.timingOn()) timer.timerString("[" + rname + "] Done rendering page, Servlet Finished", module); } Modified: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/RequestHandler.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/RequestHandler.java?view=diff&rev=533037&r1=533036&r2=533037 ============================================================================== --- ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/RequestHandler.java (original) +++ ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/RequestHandler.java Fri Apr 27 02:39:09 2007 @@ -176,7 +176,7 @@ if (session.getAttribute("visit") == null) { Debug.logInfo("This is the first request in this visit." + " sessionId=" + UtilHttp.getSessionId(request), module); // This isn't an event because it is required to run. We do not want to make it optional. - VisitHandler.setInitialVisit(request, response); + VisitHandler.getVisit(session); Collection events = requestManager.getFirstVisitEvents(); if (events != null) { Modified: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/stats/VisitHandler.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/stats/VisitHandler.java?view=diff&rev=533037&r1=533036&r2=533037 ============================================================================== --- ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/stats/VisitHandler.java (original) +++ ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/stats/VisitHandler.java Fri Apr 27 02:39:09 2007 @@ -20,6 +20,7 @@ import java.net.InetAddress; import java.sql.Timestamp; +import java.util.Locale; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; @@ -45,52 +46,6 @@ public static final String visitorCookieName = "OFBiz.Visitor"; - // this is not an event because it is required to run; as an event it could be disabled. - public static void setInitialVisit(HttpServletRequest request, HttpServletResponse response) { - HttpSession session = request.getSession(); - - // init the visitor - getVisitor(request, response); - - String webappName = UtilHttp.getApplicationName(request); - StringBuffer fullRequestUrl = UtilHttp.getFullRequestUrl(request); - String initialLocale = request.getLocale() != null ? request.getLocale().toString() : ""; - String initialRequest = fullRequestUrl.toString(); - String initialReferrer = request.getHeader("Referer") != null ? request.getHeader("Referer") : ""; - String initialUserAgent = request.getHeader("User-Agent") != null ? request.getHeader("User-Agent") : ""; - - session.setAttribute("_CLIENT_LOCALE_", request.getLocale()); - session.setAttribute("_CLIENT_REQUEST_", initialRequest); - session.setAttribute("_CLIENT_USER_AGENT_", initialUserAgent); - session.setAttribute("_CLIENT_REFERER_", initialUserAgent); - VisitHandler.setInitials(request, session, initialLocale, initialRequest, initialReferrer, initialUserAgent, webappName); - } - - public static void setInitials(HttpServletRequest request, HttpSession session, String initialLocale, String initialRequest, String initialReferrer, String initialUserAgent, String webappName) { - GenericValue visit = getVisit(session); - - if (visit != null) { - visit.set("initialLocale", initialLocale); - if (initialRequest != null) visit.set("initialRequest", initialRequest.length() > 250 ? initialRequest.substring(0, 250) : initialRequest); - if (initialReferrer != null) visit.set("initialReferrer", initialReferrer.length() > 250 ? initialReferrer.substring(0, 250) : initialReferrer); - if (initialUserAgent != null) visit.set("initialUserAgent", initialUserAgent.length() > 250 ? initialUserAgent.substring(0, 250) : initialUserAgent); - visit.set("webappName", webappName); - if (UtilProperties.propertyValueEquals("serverstats", "stats.proxy.enabled", "true")){ - visit.set("clientIpAddress", request.getHeader("X-Forwarded-For")); - } else { - visit.set("clientIpAddress", request.getRemoteAddr()); - } - visit.set("clientHostName", request.getRemoteHost()); - visit.set("clientUser", request.getRemoteUser()); - - try { - visit.store(); - } catch (GenericEntityException e) { - Debug.logError(e, "Could not update visit:", module); - } - } - } - public static void setUserLogin(HttpSession session, GenericValue userLogin, boolean userCreated) { if (userLogin == null) return; ModelEntity modelUserLogin = userLogin.getModelEntity(); @@ -131,7 +86,6 @@ public static String getVisitId(HttpSession session) { GenericValue visit = getVisit(session); - if (visit != null) { return visit.getString("visitId"); } else { @@ -144,134 +98,177 @@ // this defaults to true: ie if anything but "false" it will be true if (!UtilProperties.propertyValueEqualsIgnoreCase("serverstats", "stats.persist.visit", "false")) { GenericValue visit = (GenericValue) session.getAttribute("visit"); - if (visit == null) { - GenericDelegator delegator = null; - - // first try the session attribute delegatorName - String delegatorName = (String) session.getAttribute("delegatorName"); - if (UtilValidate.isNotEmpty(delegatorName)) { - delegator = GenericDelegator.getGenericDelegator(delegatorName); - } - - // then try the ServletContext attribute delegator, should always be there... - if (delegator == null) { - delegator = (GenericDelegator) session.getServletContext().getAttribute("delegator"); - } - - if (delegator == null) { - Debug.logError("Could not find delegator with delegatorName [" + delegatorName + "] in session, or a delegator attribute in the ServletContext, not creating Visit entity", module); - } else { - visit = delegator.makeValue("Visit", null); - visit.set("visitId", delegator.getNextSeqId("Visit")); - visit.set("sessionId", session.getId()); - visit.set("fromDate", new Timestamp(session.getCreationTime())); - - // get the visitorId - GenericValue visitor = (GenericValue) session.getAttribute("visitor"); - if (visitor != null) { - visit.set("visitorId", visitor.get("visitorId")); - } - - // get localhost ip address and hostname to store - try { - InetAddress address = InetAddress.getLocalHost(); - - if (address != null) { - visit.set("serverIpAddress", address.getHostAddress()); - visit.set("serverHostName", address.getHostName()); + synchronized (session) { + visit = (GenericValue) session.getAttribute("visit"); + if (visit == null) { + GenericDelegator delegator = null; + + // first try the session attribute delegatorName + String delegatorName = (String) session.getAttribute("delegatorName"); + if (UtilValidate.isNotEmpty(delegatorName)) { + delegator = GenericDelegator.getGenericDelegator(delegatorName); + } + + // then try the ServletContext attribute delegator, should always be there... + if (delegator == null) { + delegator = (GenericDelegator) session.getServletContext().getAttribute("delegator"); + } + + if (delegator == null) { + Debug.logError("Could not find delegator with delegatorName [" + delegatorName + "] in session, or a delegator attribute in the ServletContext, not creating Visit entity", module); } else { - Debug.logError("Unable to get localhost internet address, was null", module); + String webappName = (String) session.getAttribute("_WEBAPP_NAME_"); + Locale initialLocaleObj = (Locale) session.getAttribute("_CLIENT_LOCALE_"); + String initialRequest = (String) session.getAttribute("_CLIENT_REQUEST_"); + String initialReferrer = (String) session.getAttribute("_CLIENT_USER_AGENT_"); + String initialUserAgent = (String) session.getAttribute("_CLIENT_REFERER_"); + + String initialLocale = initialLocaleObj != null ? initialLocaleObj.toString() : ""; + + if (UtilValidate.isEmpty(webappName)) { + Debug.logInfo(new Exception(), "The webappName was empty, somehow the initial request settings were missing.", module); + } + + visit = delegator.makeValue("Visit", null); + visit.set("visitId", delegator.getNextSeqId("Visit")); + visit.set("sessionId", session.getId()); + visit.set("fromDate", new Timestamp(session.getCreationTime())); + + visit.set("initialLocale", initialLocale); + if (initialRequest != null) visit.set("initialRequest", initialRequest.length() > 250 ? initialRequest.substring(0, 250) : initialRequest); + if (initialReferrer != null) visit.set("initialReferrer", initialReferrer.length() > 250 ? initialReferrer.substring(0, 250) : initialReferrer); + if (initialUserAgent != null) visit.set("initialUserAgent", initialUserAgent.length() > 250 ? initialUserAgent.substring(0, 250) : initialUserAgent); + visit.set("webappName", webappName); + if (UtilProperties.propertyValueEquals("serverstats", "stats.proxy.enabled", "true")){ + visit.set("clientIpAddress", session.getAttribute("_CLIENT_FORWARDED_FOR_")); + } else { + visit.set("clientIpAddress", session.getAttribute("_CLIENT_REMOTE_ADDR_")); + } + visit.set("clientHostName", session.getAttribute("_CLIENT_REMOTE_HOST_")); + visit.set("clientUser", session.getAttribute("_CLIENT_REMOTE_USER_")); + + // get the visitorId + GenericValue visitor = (GenericValue) session.getAttribute("visitor"); + if (visitor != null) { + visit.set("visitorId", visitor.get("visitorId")); + } + + // get localhost ip address and hostname to store + try { + InetAddress address = InetAddress.getLocalHost(); + if (address != null) { + visit.set("serverIpAddress", address.getHostAddress()); + visit.set("serverHostName", address.getHostName()); + } else { + Debug.logError("Unable to get localhost internet address, was null", module); + } + } catch (java.net.UnknownHostException e) { + Debug.logError("Unable to get localhost internet address: " + e.toString(), module); + } + try { + visit.create(); + session.setAttribute("visit", visit); + } catch (GenericEntityException e) { + Debug.logError(e, "Could not create new visit:", module); + visit = null; + } } - } catch (java.net.UnknownHostException e) { - Debug.logError("Unable to get localhost internet address: " + e.toString(), module); - } - try { - visit.create(); - session.setAttribute("visit", visit); - } catch (GenericEntityException e) { - Debug.logError(e, "Could not create new visit:", module); - visit = null; } - } + } } + if (visit == null) { Debug.logWarning("Could not find or create the visit...", module); } return visit; - } else { - return null; } + return null; } public static GenericValue getVisitor(HttpServletRequest request, HttpServletResponse response) { // this defaults to true: ie if anything but "false" it will be true if (!UtilProperties.propertyValueEqualsIgnoreCase("serverstats", "stats.persist.visitor", "false")) { HttpSession session = request.getSession(); + GenericValue visitor = (GenericValue) session.getAttribute("visitor"); - if (visitor == null) { - GenericDelegator delegator = null; - String delegatorName = (String) session.getAttribute("delegatorName"); - - if (UtilValidate.isNotEmpty(delegatorName)) { - delegator = GenericDelegator.getGenericDelegator(delegatorName); - } - if (delegator == null) { - Debug.logError("Could not find delegator with delegatorName [" + delegatorName + "] in session, not creating/getting Visitor entity", module); - } else { - // first try to get the current ID from the visitor cookie - String visitorId = null; - Cookie[] cookies = request.getCookies(); - if (Debug.verboseOn()) Debug.logVerbose("Cookies:" + cookies, module); - if (cookies != null) { - for (int i = 0; i < cookies.length; i++) { - if (cookies[i].getName().equals(visitorCookieName)) { - visitorId = cookies[i].getValue(); - break; + synchronized (session) { + visitor = (GenericValue) session.getAttribute("visitor"); + if (visitor == null) { + GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); + + String delegatorName = (String) session.getAttribute("delegatorName"); + if (delegator == null && UtilValidate.isNotEmpty(delegatorName)) { + delegator = GenericDelegator.getGenericDelegator(delegatorName); + } + + if (delegator == null) { + Debug.logError("Could not find delegator in request or with delegatorName [" + delegatorName + "] in session, not creating/getting Visitor entity", module); + } else { + // first try to get the current ID from the visitor cookie + String visitorId = null; + Cookie[] cookies = request.getCookies(); + if (Debug.verboseOn()) Debug.logVerbose("Cookies:" + cookies, module); + if (cookies != null) { + for (int i = 0; i < cookies.length; i++) { + if (cookies[i].getName().equals(visitorCookieName)) { + visitorId = cookies[i].getValue(); + break; + } + } + } + + if (Debug.infoOn()) Debug.logInfo("Found visitorId [" + visitorId + "] in cookie", module); + + if (UtilValidate.isEmpty(visitorId)) { + // no visitor cookie? create visitor and send back cookie too + visitorId = delegator.getNextSeqId("Visitor"); + + visitor = delegator.makeValue("Visitor", null); + visitor.set("visitorId", visitorId); + try { + visitor.create(); + } catch (GenericEntityException e) { + Debug.logError(e, "Could not create new visitor:", module); + visitor = null; + } + } else { + try { + visitor = delegator.findByPrimaryKey("Visitor", UtilMisc.toMap("visitorId", visitorId)); + if (visitor == null) { + // looks like we have an ID that doesn't exist in our database, so we'll create a new one + String cookieVisitorId = visitorId; + visitorId = delegator.getNextSeqId("Visitor"); + visitor = delegator.makeValue("Visitor", UtilMisc.toMap("visitorId", visitorId)); + delegator.create(visitor); + if (Debug.infoOn()) Debug.logInfo("The visitorId [" + cookieVisitorId + "] found in cookie was invalid, creating new Visitor with ID [" + visitorId + "]", module); + } + } catch (GenericEntityException e) { + Debug.logError(e, "Error finding visitor with ID from cookie: " + visitorId, module); + visitor = null; + } } } - } - - if (visitorId == null) { - // no visitor cookie? create visitor and send back cookie too - visitorId = delegator.getNextSeqId("Visitor"); - - visitor = delegator.makeValue("Visitor", null); - visitor.set("visitorId", visitorId); - try { - visitor.create(); - } catch (GenericEntityException e) { - Debug.logError(e, "Could not create new visitor:", module); - visitor = null; - } - } else { - try { - visitor = delegator.findByPrimaryKey("Visitor", UtilMisc.toMap("visitorId", visitorId)); - } catch (GenericEntityException e) { - Debug.logError(e, "Could not find visitor with ID from cookie: " + visitorId, module); - visitor = null; + + if (visitor != null) { + // we got one, and it's a new one since it was null before + session.setAttribute("visitor", visitor); + + // create the cookie and send it back, this may be done over and over, in effect frequently refreshing the cookie + Cookie visitorCookie = new Cookie(visitorCookieName, visitor.getString("visitorId")); + visitorCookie.setMaxAge(60 * 60 * 24 * 365); + visitorCookie.setPath("/"); + response.addCookie(visitorCookie); } } } - - if (visitor != null) { - // we got one, and it's a new one since it was null before - session.setAttribute("visitor", visitor); - - // create the cookie and send it back, this may be done over and over, in effect frequently refreshing the cookie - Cookie visitorCookie = new Cookie(visitorCookieName, visitor.getString("visitorId")); - visitorCookie.setMaxAge(60 * 60 * 24 * 365); - visitorCookie.setPath("/"); - response.addCookie(visitorCookie); - } } if (visitor == null) { - Debug.logWarning("Could not find or create the visitor...", module); + Debug.logWarning(new Exception(), "Could not find or create the visitor...", module); } return visitor; - } else { - return null; } + return null; } } |
Free forum by Nabble | Edit this page |