Source Code Cross Referenced for VisitHandler.java in  » ERP-CRM-Financial » ofbiz » org » ofbiz » webapp » stats » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » ERP CRM Financial » ofbiz » org.ofbiz.webapp.stats 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*******************************************************************************
002:         * Licensed to the Apache Software Foundation (ASF) under one
003:         * or more contributor license agreements.  See the NOTICE file
004:         * distributed with this work for additional information
005:         * regarding copyright ownership.  The ASF licenses this file
006:         * to you under the Apache License, Version 2.0 (the
007:         * "License"); you may not use this file except in compliance
008:         * with the License.  You may obtain a copy of the License at
009:         * 
010:         * http://www.apache.org/licenses/LICENSE-2.0
011:         * 
012:         * Unless required by applicable law or agreed to in writing,
013:         * software distributed under the License is distributed on an
014:         * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015:         * KIND, either express or implied.  See the License for the
016:         * specific language governing permissions and limitations
017:         * under the License.
018:         *******************************************************************************/package org.ofbiz.webapp.stats;
019:
020:        import java.net.InetAddress;
021:        import java.sql.Timestamp;
022:
023:        import javax.servlet.http.Cookie;
024:        import javax.servlet.http.HttpServletRequest;
025:        import javax.servlet.http.HttpServletResponse;
026:        import javax.servlet.http.HttpSession;
027:
028:        import org.ofbiz.base.util.Debug;
029:        import org.ofbiz.base.util.UtilHttp;
030:        import org.ofbiz.base.util.UtilMisc;
031:        import org.ofbiz.base.util.UtilProperties;
032:        import org.ofbiz.base.util.UtilValidate;
033:        import org.ofbiz.entity.GenericDelegator;
034:        import org.ofbiz.entity.GenericEntityException;
035:        import org.ofbiz.entity.GenericValue;
036:        import org.ofbiz.entity.model.ModelEntity;
037:
038:        /**
039:         * Handles saving and maintaining visit information
040:         */
041:        public class VisitHandler {
042:            // Debug module name
043:            public static final String module = VisitHandler.class.getName();
044:
045:            public static final String visitorCookieName = "OFBiz.Visitor";
046:
047:            // this is not an event because it is required to run; as an event it could be disabled.
048:            public static void setInitialVisit(HttpServletRequest request,
049:                    HttpServletResponse response) {
050:                HttpSession session = request.getSession();
051:
052:                // init the visitor
053:                getVisitor(request, response);
054:
055:                String webappName = UtilHttp.getApplicationName(request);
056:                StringBuffer fullRequestUrl = UtilHttp
057:                        .getFullRequestUrl(request);
058:                String initialLocale = request.getLocale() != null ? request
059:                        .getLocale().toString() : "";
060:                String initialRequest = fullRequestUrl.toString();
061:                String initialReferrer = request.getHeader("Referer") != null ? request
062:                        .getHeader("Referer")
063:                        : "";
064:                String initialUserAgent = request.getHeader("User-Agent") != null ? request
065:                        .getHeader("User-Agent")
066:                        : "";
067:
068:                session.setAttribute("_CLIENT_LOCALE_", request.getLocale());
069:                session.setAttribute("_CLIENT_REQUEST_", initialRequest);
070:                session.setAttribute("_CLIENT_USER_AGENT_", initialUserAgent);
071:                session.setAttribute("_CLIENT_REFERER_", initialUserAgent);
072:                VisitHandler.setInitials(request, session, initialLocale,
073:                        initialRequest, initialReferrer, initialUserAgent,
074:                        webappName);
075:            }
076:
077:            public static void setInitials(HttpServletRequest request,
078:                    HttpSession session, String initialLocale,
079:                    String initialRequest, String initialReferrer,
080:                    String initialUserAgent, String webappName) {
081:                GenericValue visit = getVisit(session);
082:
083:                if (visit != null) {
084:                    visit.set("initialLocale", initialLocale);
085:                    if (initialRequest != null)
086:                        visit.set("initialRequest",
087:                                initialRequest.length() > 250 ? initialRequest
088:                                        .substring(0, 250) : initialRequest);
089:                    if (initialReferrer != null)
090:                        visit
091:                                .set("initialReferrer", initialReferrer
092:                                        .length() > 250 ? initialReferrer
093:                                        .substring(0, 250) : initialReferrer);
094:                    if (initialUserAgent != null)
095:                        visit
096:                                .set("initialUserAgent", initialUserAgent
097:                                        .length() > 250 ? initialUserAgent
098:                                        .substring(0, 250) : initialUserAgent);
099:                    visit.set("webappName", webappName);
100:                    if (UtilProperties.propertyValueEquals("serverstats",
101:                            "stats.proxy.enabled", "true")) {
102:                        visit.set("clientIpAddress", request
103:                                .getHeader("X-Forwarded-For"));
104:                    } else {
105:                        visit.set("clientIpAddress", request.getRemoteAddr());
106:                    }
107:                    visit.set("clientHostName", request.getRemoteHost());
108:                    visit.set("clientUser", request.getRemoteUser());
109:
110:                    try {
111:                        visit.store();
112:                    } catch (GenericEntityException e) {
113:                        Debug.logError(e, "Could not update visit:", module);
114:                    }
115:                }
116:            }
117:
118:            public static void setUserLogin(HttpSession session,
119:                    GenericValue userLogin, boolean userCreated) {
120:                if (userLogin == null)
121:                    return;
122:                ModelEntity modelUserLogin = userLogin.getModelEntity();
123:
124:                GenericValue visitor = (GenericValue) session
125:                        .getAttribute("visitor");
126:                if (visitor != null) {
127:                    visitor.set("userLoginId", userLogin.get("userLoginId"));
128:                    if (modelUserLogin.isField("partyId")) {
129:                        visitor.set("partyId", userLogin.get("partyId"));
130:                    }
131:                    try {
132:                        visitor.store();
133:                    } catch (GenericEntityException e) {
134:                        Debug.logError(e, "Could not update visitor: ", module);
135:                    }
136:                }
137:
138:                GenericValue visit = getVisit(session);
139:                if (visit != null) {
140:                    visit.set("userLoginId", userLogin.get("userLoginId"));
141:                    if (modelUserLogin.isField("partyId")) {
142:                        visit.set("partyId", userLogin.get("partyId"));
143:                    }
144:                    visit.set("userCreated", new Boolean(userCreated));
145:
146:                    // make sure the visitorId is still in place
147:                    if (visitor != null) {
148:                        visit.set("visitorId", visitor.get("visitorId"));
149:                    }
150:
151:                    try {
152:                        visit.store();
153:                    } catch (GenericEntityException e) {
154:                        Debug.logError(e, "Could not update visit: ", module);
155:                    }
156:                }
157:            }
158:
159:            public static String getVisitId(HttpSession session) {
160:                GenericValue visit = getVisit(session);
161:
162:                if (visit != null) {
163:                    return visit.getString("visitId");
164:                } else {
165:                    return null;
166:                }
167:            }
168:
169:            /** Get the visit from the session, or create if missing */
170:            public static GenericValue getVisit(HttpSession session) {
171:                // this defaults to true: ie if anything but "false" it will be true
172:                if (!UtilProperties.propertyValueEqualsIgnoreCase(
173:                        "serverstats", "stats.persist.visit", "false")) {
174:                    GenericValue visit = (GenericValue) session
175:                            .getAttribute("visit");
176:
177:                    if (visit == null) {
178:                        GenericDelegator delegator = null;
179:
180:                        // first try the session attribute delegatorName
181:                        String delegatorName = (String) session
182:                                .getAttribute("delegatorName");
183:                        if (UtilValidate.isNotEmpty(delegatorName)) {
184:                            delegator = GenericDelegator
185:                                    .getGenericDelegator(delegatorName);
186:                        }
187:
188:                        // then try the ServletContext attribute delegator, should always be there...
189:                        if (delegator == null) {
190:                            delegator = (GenericDelegator) session
191:                                    .getServletContext().getAttribute(
192:                                            "delegator");
193:                        }
194:
195:                        if (delegator == null) {
196:                            Debug
197:                                    .logError(
198:                                            "Could not find delegator with delegatorName ["
199:                                                    + delegatorName
200:                                                    + "] in session, or a delegator attribute in the ServletContext, not creating Visit entity",
201:                                            module);
202:                        } else {
203:                            visit = delegator.makeValue("Visit", null);
204:                            visit.set("visitId", delegator
205:                                    .getNextSeqId("Visit"));
206:                            visit.set("sessionId", session.getId());
207:                            visit.set("fromDate", new Timestamp(session
208:                                    .getCreationTime()));
209:
210:                            // get the visitorId
211:                            GenericValue visitor = (GenericValue) session
212:                                    .getAttribute("visitor");
213:                            if (visitor != null) {
214:                                visit
215:                                        .set("visitorId", visitor
216:                                                .get("visitorId"));
217:                            }
218:
219:                            // get localhost ip address and hostname to store
220:                            try {
221:                                InetAddress address = InetAddress
222:                                        .getLocalHost();
223:
224:                                if (address != null) {
225:                                    visit.set("serverIpAddress", address
226:                                            .getHostAddress());
227:                                    visit.set("serverHostName", address
228:                                            .getHostName());
229:                                } else {
230:                                    Debug
231:                                            .logError(
232:                                                    "Unable to get localhost internet address, was null",
233:                                                    module);
234:                                }
235:                            } catch (java.net.UnknownHostException e) {
236:                                Debug.logError(
237:                                        "Unable to get localhost internet address: "
238:                                                + e.toString(), module);
239:                            }
240:                            try {
241:                                visit.create();
242:                                session.setAttribute("visit", visit);
243:                            } catch (GenericEntityException e) {
244:                                Debug.logError(e,
245:                                        "Could not create new visit:", module);
246:                                visit = null;
247:                            }
248:                        }
249:                    }
250:                    if (visit == null) {
251:                        Debug
252:                                .logWarning(
253:                                        "Could not find or create the visit...",
254:                                        module);
255:                    }
256:                    return visit;
257:                } else {
258:                    return null;
259:                }
260:            }
261:
262:            public static GenericValue getVisitor(HttpServletRequest request,
263:                    HttpServletResponse response) {
264:                // this defaults to true: ie if anything but "false" it will be true
265:                if (!UtilProperties.propertyValueEqualsIgnoreCase(
266:                        "serverstats", "stats.persist.visitor", "false")) {
267:                    HttpSession session = request.getSession();
268:                    GenericValue visitor = (GenericValue) session
269:                            .getAttribute("visitor");
270:
271:                    if (visitor == null) {
272:                        GenericDelegator delegator = null;
273:                        String delegatorName = (String) session
274:                                .getAttribute("delegatorName");
275:
276:                        if (UtilValidate.isNotEmpty(delegatorName)) {
277:                            delegator = GenericDelegator
278:                                    .getGenericDelegator(delegatorName);
279:                        }
280:                        if (delegator == null) {
281:                            Debug
282:                                    .logError(
283:                                            "Could not find delegator with delegatorName ["
284:                                                    + delegatorName
285:                                                    + "] in session, not creating/getting Visitor entity",
286:                                            module);
287:                        } else {
288:                            // first try to get the current ID from the visitor cookie
289:                            String visitorId = null;
290:                            Cookie[] cookies = request.getCookies();
291:                            if (Debug.verboseOn())
292:                                Debug.logVerbose("Cookies:" + cookies, module);
293:                            if (cookies != null) {
294:                                for (int i = 0; i < cookies.length; i++) {
295:                                    if (cookies[i].getName().equals(
296:                                            visitorCookieName)) {
297:                                        visitorId = cookies[i].getValue();
298:                                        break;
299:                                    }
300:                                }
301:                            }
302:
303:                            if (visitorId == null) {
304:                                // no visitor cookie? create visitor and send back cookie too
305:                                visitorId = delegator.getNextSeqId("Visitor");
306:
307:                                visitor = delegator.makeValue("Visitor", null);
308:                                visitor.set("visitorId", visitorId);
309:                                try {
310:                                    visitor.create();
311:                                } catch (GenericEntityException e) {
312:                                    Debug.logError(e,
313:                                            "Could not create new visitor:",
314:                                            module);
315:                                    visitor = null;
316:                                }
317:                            } else {
318:                                try {
319:                                    visitor = delegator.findByPrimaryKey(
320:                                            "Visitor", UtilMisc.toMap(
321:                                                    "visitorId", visitorId));
322:                                } catch (GenericEntityException e) {
323:                                    Debug.logError(e,
324:                                            "Could not find visitor with ID from cookie: "
325:                                                    + visitorId, module);
326:                                    visitor = null;
327:                                }
328:                            }
329:                        }
330:
331:                        if (visitor != null) {
332:                            // we got one, and it's a new one since it was null before
333:                            session.setAttribute("visitor", visitor);
334:
335:                            // create the cookie and send it back, this may be done over and over, in effect frequently refreshing the cookie
336:                            Cookie visitorCookie = new Cookie(
337:                                    visitorCookieName, visitor
338:                                            .getString("visitorId"));
339:                            visitorCookie.setMaxAge(60 * 60 * 24 * 365);
340:                            visitorCookie.setPath("/");
341:                            response.addCookie(visitorCookie);
342:                        }
343:                    }
344:                    if (visitor == null) {
345:                        Debug.logWarning(
346:                                "Could not find or create the visitor...",
347:                                module);
348:                    }
349:                    return visitor;
350:                } else {
351:                    return null;
352:                }
353:            }
354:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.