001: /*
002: * $Id: VisitHandler.java,v 1.3 2004/02/11 06:04:20 jonesde Exp $
003: *
004: * Copyright (c) 2001-2003 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: */
024: package org.ofbiz.content.stats;
025:
026: import java.net.InetAddress;
027: import java.sql.Timestamp;
028:
029: import javax.servlet.http.HttpServletRequest;
030: import javax.servlet.http.HttpSession;
031:
032: import org.ofbiz.base.util.Debug;
033: import org.ofbiz.base.util.UtilHttp;
034: import org.ofbiz.base.util.UtilProperties;
035: import org.ofbiz.base.util.UtilValidate;
036: import org.ofbiz.entity.GenericDelegator;
037: import org.ofbiz.entity.GenericEntityException;
038: import org.ofbiz.entity.GenericValue;
039:
040: /**
041: * Handles saving and maintaining visit information
042: *
043: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
044: * @version $Revision: 1.3 $
045: * @since 2.0
046: */
047: public class VisitHandler {
048: // Debug module name
049: public static final String module = VisitHandler.class.getName();
050:
051: // this is not an event because it is required to run; as an event it could be disabled.
052: public static void setInitialVisit(HttpServletRequest request) {
053: HttpSession session = request.getSession();
054: String webappName = UtilHttp.getApplicationName(request);
055: StringBuffer fullRequestUrl = UtilHttp
056: .getFullRequestUrl(request);
057: String initialLocale = request.getLocale() != null ? request
058: .getLocale().toString() : "";
059: String initialRequest = fullRequestUrl.toString();
060: String initialReferrer = request.getHeader("Referer") != null ? request
061: .getHeader("Referer")
062: : "";
063: String initialUserAgent = request.getHeader("User-Agent") != null ? request
064: .getHeader("User-Agent")
065: : "";
066:
067: session.setAttribute("_CLIENT_LOCALE_", request.getLocale());
068: session.setAttribute("_CLIENT_REQUEST_", initialRequest);
069: session.setAttribute("_CLIENT_USER_AGENT_", initialUserAgent);
070: session.setAttribute("_CLIENT_REFERER_", initialUserAgent);
071: VisitHandler.setInitials(request, session, initialLocale,
072: initialRequest, initialReferrer, initialUserAgent,
073: webappName);
074: }
075:
076: public static void setInitials(HttpServletRequest request,
077: HttpSession session, String initialLocale,
078: String initialRequest, String initialReferrer,
079: String initialUserAgent, String webappName) {
080: GenericValue visit = getVisit(session);
081:
082: if (visit != null) {
083: visit.set("initialLocale", initialLocale);
084: if (initialRequest != null)
085: visit.set("initialRequest",
086: initialRequest.length() > 250 ? initialRequest
087: .substring(0, 250) : initialRequest);
088: if (initialReferrer != null)
089: visit
090: .set("initialReferrer", initialReferrer
091: .length() > 250 ? initialReferrer
092: .substring(0, 250) : initialReferrer);
093: if (initialUserAgent != null)
094: visit
095: .set("initialUserAgent", initialUserAgent
096: .length() > 250 ? initialUserAgent
097: .substring(0, 250) : initialUserAgent);
098: visit.set("webappName", webappName);
099: visit.set("clientIpAddress", request.getRemoteAddr());
100: visit.set("clientHostName", request.getRemoteHost());
101: visit.set("clientUser", request.getRemoteUser());
102:
103: try {
104: visit.store();
105: } catch (GenericEntityException e) {
106: Debug.logError(e, "Could not update visit:", module);
107: }
108: }
109: }
110:
111: public static void setUserLogin(HttpSession session,
112: GenericValue userLogin, boolean userCreated) {
113: if (userLogin == null)
114: return;
115: GenericValue visit = getVisit(session);
116:
117: if (visit != null) {
118: visit.set("userLoginId", userLogin.get("userLoginId"));
119: visit.set("partyId", userLogin.get("partyId"));
120: visit.set("userCreated", new Boolean(userCreated));
121:
122: try {
123: visit.store();
124: } catch (GenericEntityException e) {
125: Debug.logError(e, "Could not update visit:", module);
126: }
127: }
128: }
129:
130: public static String getVisitId(HttpSession session) {
131: GenericValue visit = getVisit(session);
132:
133: if (visit != null) {
134: return visit.getString("visitId");
135: } else {
136: return null;
137: }
138: }
139:
140: /** Get the visit from the session, or create if missing */
141: public static GenericValue getVisit(HttpSession session) {
142: // this defaults to true: ie if anything but "false" it will be true
143: if (!UtilProperties.propertyValueEqualsIgnoreCase(
144: "serverstats", "stats.persist.visit", "false")) {
145: GenericValue visit = (GenericValue) session
146: .getAttribute("visit");
147:
148: if (visit == null) {
149: GenericDelegator delegator = null;
150: String delegatorName = (String) session
151: .getAttribute("delegatorName");
152:
153: if (UtilValidate.isNotEmpty(delegatorName)) {
154: delegator = GenericDelegator
155: .getGenericDelegator(delegatorName);
156: }
157: if (delegator == null) {
158: Debug
159: .logError(
160: "Could not find delegator with delegatorName ["
161: + delegatorName
162: + "] in session, not creating Visit entity",
163: module);
164: } else {
165: visit = delegator.makeValue("Visit", null);
166: Long nextId = delegator.getNextSeqId("Visit");
167:
168: if (nextId == null) {
169: Debug
170: .logError(
171: "Not persisting visit, could not get next seq id",
172: module);
173: visit = null;
174: } else {
175: visit.set("visitId", nextId.toString());
176: visit.set("sessionId", session.getId());
177: visit.set("fromDate", new Timestamp(session
178: .getCreationTime()));
179: // get localhost ip address and hostname to store
180: try {
181: InetAddress address = InetAddress
182: .getLocalHost();
183:
184: if (address != null) {
185: visit.set("serverIpAddress", address
186: .getHostAddress());
187: visit.set("serverHostName", address
188: .getHostName());
189: } else {
190: Debug
191: .logError(
192: "Unable to get localhost internet address, was null",
193: module);
194: }
195: } catch (java.net.UnknownHostException e) {
196: Debug.logError(
197: "Unable to get localhost internet address: "
198: + e.toString(), module);
199: }
200: try {
201: visit.create();
202: session.setAttribute("visit", visit);
203: } catch (GenericEntityException e) {
204: Debug.logError(e,
205: "Could not create new visit:",
206: module);
207: visit = null;
208: }
209: }
210: }
211: }
212: if (visit == null) {
213: Debug
214: .logWarning(
215: "Could not find or create the visit...",
216: module);
217: }
218: return visit;
219: } else {
220: return null;
221: }
222: }
223: }
|