Source Code Cross Referenced for CookieManager.java in  » Forum » nemesis-forum » org » nemesis » forum » 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 » Forum » nemesis forum » org.nemesis.forum.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * NEMESIS-FORUM.
003:         * Copyright (C) 2002  David Laurent(lithium2@free.fr). All rights reserved.
004:         * 
005:         * Copyright (c) 2000 The Apache Software Foundation. All rights reserved.
006:         * 
007:         * Copyright (C) 2001 Yasna.com. All rights reserved.
008:         * 
009:         * Copyright (C) 2000 CoolServlets.com. All rights reserved.
010:         * 
011:         * NEMESIS-FORUM. is free software; you can redistribute it and/or
012:         * modify it under the terms of the Apache Software License, Version 1.1,
013:         * or (at your option) any later version.
014:         * 
015:         * NEMESIS-FORUM core framework, NEMESIS-FORUM backoffice, NEMESIS-FORUM frontoffice
016:         * application are parts of NEMESIS-FORUM and are distributed under
017:         * same terms of licence.
018:         * 
019:         * 
020:         * NEMESIS-FORUM includes software developed by the Apache Software Foundation (http://www.apache.org/)
021:         * and software developed by CoolServlets.com (http://www.coolservlets.com).
022:         * and software developed by Yasna.com (http://www.yasna.com).
023:         * 
024:         */
025:        package org.nemesis.forum.util;
026:
027:        import javax.servlet.http.Cookie;
028:        import javax.servlet.http.HttpServletRequest;
029:        import javax.servlet.http.HttpServletResponse;
030:
031:        /**
032:         * @author dlaurent
033:         * 
034:         * :TODO:passer en config
035:         */
036:        public class CookieManager {
037:
038:            private static final long SECOND = 1000;
039:            private static final long MINUTE = 60 * SECOND;
040:            private static final long HOUR = 60 * MINUTE;
041:            private static final long DAY = 24 * HOUR;
042:            private static final long WEEK = 7 * DAY;
043:            public static final int MAX_COOKIE_AGE = (int) (WEEK / 1000) * 8;
044:
045:            private final static int ENCODE_XORMASK = 0x5A;
046:            private final static char ENCODE_DELIMETER = '\002';
047:            private final static char ENCODE_CHAR_OFFSET1 = 'A';
048:            private final static char ENCODE_CHAR_OFFSET2 = 'h';
049:
050:            public static void setCookie(HttpServletResponse res, String name,
051:                    String value, int maxAge) {
052:                Cookie oneCookie = new Cookie(name, value);
053:                oneCookie.setMaxAge(maxAge);
054:                oneCookie.setPath("/");
055:                res.addCookie(oneCookie);
056:            }
057:
058:            /**
059:             * Returns the specified Cookie object, or null if the cookie does not exist.
060:             *
061:             * @param request The HttpServletRequest object, known as "request" in a
062:             *      JSP page.
063:             * @param name the name of the cookie.
064:             * @return the Cookie object if it exists, otherwise null.
065:             */
066:            public static Cookie getCookie(HttpServletRequest request,
067:                    String name) {
068:                Cookie cookies[] = request.getCookies();
069:                if (cookies == null || name == null || name.length() == 0) {
070:                    return null;
071:                }
072:                //Otherwise, we have to do a linear scan for the cookie.
073:                for (int i = 0; i < cookies.length; i++) {
074:                    if (cookies[i].getName().equals(name)) {
075:                        return cookies[i];
076:                    }
077:                }
078:                return null;
079:            }
080:
081:            /**
082:             * Returns the value of the specified cookie as a String. If the cookie
083:             * does not exist, the method returns null.
084:             *
085:             * @param request the HttpServletRequest object, known as "request" in a
086:             *      JSP page.
087:             * @param name the name of the cookie
088:             * @return the value of the cookie, or null if the cookie does not exist.
089:             */
090:            public static String getCookieValue(HttpServletRequest request,
091:                    String name) {
092:                Cookie cookie = getCookie(request, name);
093:                if (cookie != null) {
094:                    return cookie.getValue();
095:                }
096:                return null;
097:            }
098:
099:            /**
100:             * Invalidate the specified cookie and delete it from the response object.
101:             *
102:             * @param request The HttpServletRequest object, known as "request" in a JSP page.
103:             * @param response The HttpServletResponse object, known as "response" in a JSP page.
104:             * @param cookieName The name of the cookie you want to delete.
105:             */
106:            public static void invalidateCookie(HttpServletRequest request,
107:                    HttpServletResponse response, String cookieName) {
108:                Cookie cookie = new Cookie(cookieName, null); // invalidate cookie
109:                cookie.setMaxAge(0); // deletes cookie
110:                cookie.setPath("/");
111:                response.addCookie(cookie);
112:            }
113:
114:            /**
115:             * Builds a cookie string containing a username and password.<p>
116:             *
117:             * Note: with open source this is not really secure, but it prevents users
118:             * from snooping the cookie file of others and by changing the XOR mask and
119:             * character offsets, you can easily tweak results.
120:             *
121:             * @param username The username.
122:             * @param password The password.
123:             * @return String encoding the input parameters, an empty string if one of
124:             *      the arguments equals <code>null</code>.
125:             */
126:            public static String encodePasswordCookie(String username,
127:                    String password) {
128:                StringBuffer buf = new StringBuffer();
129:                if (username != null && password != null) {
130:                    byte[] bytes = (username + ENCODE_DELIMETER + password)
131:                            .getBytes();
132:                    int b;
133:
134:                    for (int n = 0; n < bytes.length; n++) {
135:                        b = bytes[n] ^ (ENCODE_XORMASK + n);
136:                        buf.append((char) (ENCODE_CHAR_OFFSET1 + (b & 0x0F)));
137:                        buf
138:                                .append((char) (ENCODE_CHAR_OFFSET2 + ((b >> 4) & 0x0F)));
139:                    }
140:                }
141:                return buf.toString();
142:            }
143:
144:            /**
145:             * Unrafels a cookie string containing a username and password.
146:             * @param value The cookie value.
147:             * @return String[] containing the username at index 0 and the password at
148:             *      index 1, or <code>{ null, null }</code> if cookieVal equals
149:             *      <code>null</code> or the empty string.
150:             */
151:            public static String[] decodePasswordCookie(String cookieVal) {
152:
153:                // check that the cookie value isn't null or zero-length
154:                if (cookieVal == null || cookieVal.length() <= 0) {
155:                    return null;
156:                }
157:
158:                // unrafel the cookie value
159:                char[] chars = cookieVal.toCharArray();
160:                byte[] bytes = new byte[chars.length / 2];
161:                int b;
162:                for (int n = 0, m = 0; n < bytes.length; n++) {
163:                    b = chars[m++] - ENCODE_CHAR_OFFSET1;
164:                    b |= (chars[m++] - ENCODE_CHAR_OFFSET2) << 4;
165:                    bytes[n] = (byte) (b ^ (ENCODE_XORMASK + n));
166:                }
167:                cookieVal = new String(bytes);
168:                int pos = cookieVal.indexOf(ENCODE_DELIMETER);
169:                String username = (pos < 0) ? "" : cookieVal.substring(0, pos);
170:                String password = (pos < 0) ? "" : cookieVal.substring(pos + 1);
171:
172:                return new String[] { username, password };
173:            }
174:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.