Source Code Cross Referenced for CookieUtils.java in  » ERP-CRM-Financial » sakai » edu » indiana » lib » twinpeaks » 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 » ERP CRM Financial » sakai » edu.indiana.lib.twinpeaks.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**********************************************************************************
002:         *
003:         * Copyright (c) 2003, 2004 The Regents of the University of Michigan, Trustees of Indiana University,
004:         *                  Board of Trustees of the Leland Stanford, Jr., University, and The MIT Corporation
005:         *
006:         * Licensed under the Educational Community License Version 1.0 (the "License");
007:         * By obtaining, using and/or copying this Original Work, you agree that you have read,
008:         * understand, and will comply with the terms and conditions of the Educational Community License.
009:         * You may obtain a copy of the License at:
010:         *
011:         *      http://cvs.sakaiproject.org/licenses/license_1_0.html
012:         *
013:         * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
014:         * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
015:         * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
016:         * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
017:         * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
018:         *
019:         **********************************************************************************/package edu.indiana.lib.twinpeaks.util;
020:
021:        import java.io.*;
022:        import java.net.*;
023:        import java.util.*;
024:
025:        public class CookieUtils {
026:
027:            private static org.apache.commons.logging.Log _log = LogUtils
028:                    .getLog(CookieUtils.class);
029:
030:            private CookieUtils() {
031:            }
032:
033:            /**
034:             * Get a new (empty) CookieData list
035:             * return An empty ArrayList
036:             */
037:            public static List newCookieList() {
038:                return new ArrayList();
039:            }
040:
041:            /**
042:             * Parse an HTTP cookie
043:             * @param value Cookie value
044:             * @return A CookieData object representing this cookie
045:             */
046:            public static CookieData parseCookie(URL url, String value) {
047:                String[] cookieFields;
048:                CookieData cookie;
049:
050:                cookieFields = value.split(";\\s*");
051:                cookie = makeCookieData(url, cookieFields[0]);
052:
053:                for (int j = 1; j < cookieFields.length; j++) {
054:                    if ("secure".equalsIgnoreCase(cookieFields[j])) {
055:                        cookie.setSecure(true);
056:                        continue;
057:                    }
058:
059:                    if (cookieFields[j].indexOf('=') > 0) {
060:                        String[] field = cookieFields[j].split("=");
061:
062:                        if ("expires".equalsIgnoreCase(field[0])) {
063:                            cookie.setExpires(field[1]);
064:                        } else if ("domain".equalsIgnoreCase(field[0])) {
065:                            cookie.setDomain(field[1]);
066:                        } else if ("path".equalsIgnoreCase(field[0])) {
067:                            cookie.setPath(field[1]);
068:                        } else if ("version".equalsIgnoreCase(field[0])) {
069:                            cookie.setVersion(field[1]);
070:                        } else if ("max-age".equalsIgnoreCase(field[0])) {
071:                            cookie.setMaxAge(field[1]);
072:                        }
073:                    }
074:                }
075:                return cookie;
076:            }
077:
078:            /**
079:             * Build a CookieData object from cookieName=cookieValue text
080:             * @param url URL this cookie belongs to
081:             * @param cookieText cookieName[=cookieValue] text
082:             * @return A new CookieData object
083:             */
084:            private static CookieData makeCookieData(URL url, String cookieText) {
085:
086:                for (int i = 0; i < cookieText.length(); i++) {
087:
088:                    if (cookieText.charAt(i) == '=') {
089:                        String name = cookieText.substring(0, i);
090:                        String value = "";
091:
092:                        if (i + 1 <= cookieText.length()) {
093:                            value = cookieText.substring(i + 1);
094:                        }
095:                        return new CookieData(url, name, value);
096:                    }
097:                }
098:                return new CookieData(url, cookieText, "");
099:            }
100:
101:            /**
102:             * Maintain a list of CookieData objects (add, replace, or delete a cookie)
103:             * @param cookieList CookieData list
104:             * @param cookie A CookieData object
105:             */
106:            public static void storeCookie(List cookieList, CookieData cookie) {
107:                int size = cookieList.size();
108:
109:                for (int i = 0; i < size; i++) {
110:                    CookieData cd = (CookieData) cookieList.get(i);
111:
112:                    if (cookie.equals(cd)) {
113:                        if (cookie.getMaxAge() == 0) {
114:                            cookieList.remove(i);
115:                            return;
116:                        }
117:                        cookieList.set(i, cookie);
118:                        return;
119:                    }
120:                }
121:                if (cookie.getMaxAge() != 0) {
122:                    cookieList.add(cookie);
123:                }
124:            }
125:
126:            /**
127:             * Does the cookie domain match the URL?
128:             * @param urlString URL String to match
129:             * @param cookie CookieData object (the cookie)
130:             * @return true if the cookie domain matches the URL
131:             */
132:            public static boolean inDomain(String urlString, CookieData cookie) {
133:                URL url;
134:
135:                try {
136:                    url = new URL(urlString);
137:                } catch (MalformedURLException exception) {
138:                    return false;
139:                }
140:                return inDomain(url, cookie);
141:            }
142:
143:            /**
144:             * Does the cookie domain match the URL?
145:             * @param url URL to match
146:             * @param cookie CookieData object (the cookie)
147:             * @return true if the cookie domain matches the URL
148:             */
149:            public static boolean inDomain(URL url, CookieData cookie) {
150:                String domain = cookie.getDomain();
151:
152:                return url.getHost().toLowerCase().endsWith(
153:                        domain.toLowerCase());
154:            }
155:
156:            /**
157:             * Does the cookie path match the URL "file"?
158:             * @param urlString String URL to match
159:             * @param cookie CookieData object (the cookie)
160:             * @return true if the cookie domain matches the URL
161:             */
162:            public static boolean inPath(String urlString, CookieData cookie) {
163:                URL url;
164:
165:                try {
166:                    url = new URL(urlString);
167:                } catch (MalformedURLException exception) {
168:                    return false;
169:                }
170:                return inPath(url, cookie);
171:            }
172:
173:            /**
174:             * Does the cookie path match the URL "file"?
175:             * @param url URL to match
176:             * @param cookie CookieData object (the cookie)
177:             * @return true if the cookie domain matches the URL
178:             */
179:            public static boolean inPath(URL url, CookieData cookie) {
180:                return url.getFile().startsWith(cookie.getPath());
181:            }
182:
183:            /**
184:             * Find all stored cookies which associated with this server
185:             * @param cookieList List of stored cookies (CookieData objects)
186:             * @param url URL representing the request to lookup (server)
187:             * @return A List of associated cookies
188:             */
189:            public static List findCookiesForServer(List cookieList, URL url) {
190:                Iterator iterator = cookieList.iterator();
191:                ArrayList list = new ArrayList();
192:
193:                while (iterator.hasNext()) {
194:                    CookieData cookie = (CookieData) iterator.next();
195:
196:                    if ((inDomain(url, cookie)) && (inPath(url, cookie))) {
197:                        list.add(cookie);
198:                    }
199:                }
200:                return list;
201:            }
202:
203:            /**
204:             * Find cookies associated with this server (by name)
205:             * @param cookieList List of stored cookies (CookieData objects)
206:             * @param url URL representing the request to lookup (server)
207:             * @param name Cookie name
208:             * @param exact true for exact name match, false to match on name prefix
209:             * @return A List of associated cookies
210:             */
211:            public static List getCookies(List cookieList, URL url,
212:                    String name, boolean exact) {
213:
214:                List serverCookies = findCookiesForServer(cookieList, url);
215:                Iterator iterator = serverCookies.iterator();
216:                ArrayList list = new ArrayList();
217:
218:                while (iterator.hasNext()) {
219:                    CookieData cookie = (CookieData) iterator.next();
220:
221:                    if (exact) {
222:                        if (cookie.getName().equals(name)) {
223:                            list.add(cookie);
224:                        }
225:                        continue;
226:                    }
227:
228:                    if (cookie.getName().startsWith(name)) {
229:                        list.add(cookie);
230:                    }
231:                }
232:                return list;
233:            }
234:
235:            /**
236:             * Find cookies associated with this server (exact name match)
237:             * @param cookieList List of stored cookies (CookieData objects)
238:             * @param url URL representing the request to lookup (server)
239:             * @param name Cookie name
240:             * @return A List of associated cookies
241:             */
242:            public static List getCookiesByName(List cookieList, URL url,
243:                    String name) {
244:                return getCookies(cookieList, url, name, true);
245:            }
246:
247:            /**
248:             * Find cookies associated with this server (match on name "prefix")
249:             * @param cookieList List of stored cookies (CookieData objects)
250:             * @param url URL representing the request to lookup (server)
251:             * @param name Cookie name
252:             * @return A List of associated cookies
253:             */
254:            public static List getCookiesByPrefix(List cookieList, URL url,
255:                    String name) {
256:                return getCookies(cookieList, url, name, false);
257:            }
258:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.