Source Code Cross Referenced for CookieUtils.java in  » Net » openfire » org » jivesoftware » util » 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 » Net » openfire » org.jivesoftware.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * $RCSfile$
003:         * $Revision
004:         * $Date: 2004-11-09 16:36:36 -0800 (Tue, 09 Nov 2004) $
005:         *
006:         * Copyright (C) 1999-2004 Jive Software. All rights reserved.
007:         *
008:         * This software is the proprietary information of Jive Software. Use is subject to license terms.
009:         */package org.jivesoftware.util;
010:
011:        import javax.servlet.http.Cookie;
012:        import javax.servlet.http.HttpServletRequest;
013:        import javax.servlet.http.HttpServletResponse;
014:
015:        public class CookieUtils {
016:
017:            /**
018:             * Returns the specified cookie, or <tt>null</tt> if the cookie
019:             * does not exist. Note: because of the way that cookies are implemented
020:             * it's possible for multiple cookies with the same name to exist (but with
021:             * different domain values). This method will return the first cookie that
022:             * has a name match.
023:             *
024:             * @param request the servlet request.
025:             * @param name the name of the cookie.
026:             * @return the Cookie object if it exists, otherwise <tt>null</tt>.
027:             */
028:            public static Cookie getCookie(HttpServletRequest request,
029:                    String name) {
030:                Cookie cookies[] = request.getCookies();
031:                // Return null if there are no cookies or the name is invalid.
032:                if (cookies == null || name == null || name.length() == 0) {
033:                    return null;
034:                }
035:                // Otherwise, we  do a linear scan for the cookie.
036:                Cookie cookie = null;
037:                for (int i = 0; i < cookies.length; i++) {
038:                    // If the current cookie name matches the one we're looking for, we've
039:                    // found a matching cookie.
040:                    if (cookies[i].getName().equals(name)) {
041:                        cookie = cookies[i];
042:                        // The best matching cookie will be the one that has the correct
043:                        // domain name. If we've found the cookie with the correct domain name,
044:                        // return it. Otherwise, we'll keep looking for a better match.
045:                        if (request.getServerName().equals(cookie.getDomain())) {
046:                            break;
047:                        }
048:                    }
049:                }
050:                return cookie;
051:            }
052:
053:            /**
054:             * Deletes the specified cookie.
055:             *
056:             * @param request the servlet request.
057:             * @param response the servlet response.
058:             * @param cookie the cookie object to be deleted.
059:             */
060:            public static void deleteCookie(HttpServletRequest request,
061:                    HttpServletResponse response, Cookie cookie) {
062:                if (cookie != null) {
063:                    // Invalidate the cookie
064:                    String path = request.getContextPath() == null ? "/"
065:                            : request.getContextPath();
066:                    if ("".equals(path)) {
067:                        path = "/";
068:                    }
069:                    cookie.setPath(path);
070:                    cookie.setValue("");
071:                    cookie.setMaxAge(0);
072:                    response.addCookie(cookie);
073:                }
074:            }
075:
076:            /**
077:             * Stores a value in a cookie. By default this cookie will persist for 30 days.
078:             *
079:             * @see #setCookie(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse,String,String,int)
080:             * @param request the servlet request.
081:             * @param response the servlet response.
082:             * @param name a name to identify the cookie.
083:             * @param value the value to store in the cookie.
084:             */
085:            public static void setCookie(HttpServletRequest request,
086:                    HttpServletResponse response, String name, String value) {
087:                // Save the cookie value for 1 month
088:                setCookie(request, response, name, value, 60 * 60 * 24 * 30);
089:            }
090:
091:            /**
092:             * Stores a value in a cookie. This cookie will persist for the amount
093:             * specified in the <tt>saveTime</tt> parameter.
094:             *
095:             * @see #setCookie(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse,String,String)
096:             * @param request the servlet request.
097:             * @param response the servlet response.
098:             * @param name a name to identify the cookie.
099:             * @param value the value to store in the cookie.
100:             * @param maxAge the time (in seconds) this cookie should live.
101:             */
102:            public static void setCookie(HttpServletRequest request,
103:                    HttpServletResponse response, String name, String value,
104:                    int maxAge) {
105:                // Check to make sure the new value is not null (appservers like Tomcat
106:                // 4 blow up if the value is null).
107:                if (value == null) {
108:                    value = "";
109:                }
110:                String path = request.getContextPath() == null ? "/" : request
111:                        .getContextPath();
112:                if ("".equals(path)) {
113:                    path = "/";
114:                }
115:                Cookie cookie = new Cookie(name, value);
116:                cookie.setMaxAge(maxAge);
117:                cookie.setPath(path);
118:                response.addCookie(cookie);
119:            }
120:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.