Source Code Cross Referenced for CookieUtils.java in  » Web-Services » restlet-1.0.8 » com » noelios » restlet » 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 » Web Services » restlet 1.0.8 » com.noelios.restlet.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2005-2007 Noelios Consulting.
003:         * 
004:         * The contents of this file are subject to the terms of the Common Development
005:         * and Distribution License (the "License"). You may not use this file except in
006:         * compliance with the License.
007:         * 
008:         * You can obtain a copy of the license at
009:         * http://www.opensource.org/licenses/cddl1.txt See the License for the specific
010:         * language governing permissions and limitations under the License.
011:         * 
012:         * When distributing Covered Code, include this CDDL HEADER in each file and
013:         * include the License file at http://www.opensource.org/licenses/cddl1.txt If
014:         * applicable, add the following below this CDDL HEADER, with the fields
015:         * enclosed by brackets "[]" replaced with your own identifying information:
016:         * Portions Copyright [yyyy] [name of copyright owner]
017:         */
018:
019:        package com.noelios.restlet.util;
020:
021:        import java.io.IOException;
022:        import java.util.Date;
023:        import java.util.Iterator;
024:        import java.util.List;
025:        import java.util.Map;
026:
027:        import org.restlet.data.Cookie;
028:        import org.restlet.data.CookieSetting;
029:        import org.restlet.util.DateUtils;
030:
031:        import com.noelios.restlet.http.HttpUtils;
032:
033:        /**
034:         * Cookie manipulation utilities.
035:         * 
036:         * @author Jerome Louvel (contact@noelios.com)
037:         */
038:        public class CookieUtils {
039:            /**
040:             * Formats a list of cookies as an HTTP header.
041:             * 
042:             * @param cookies
043:             *            The list of cookies to format.
044:             * @return The HTTP header.
045:             */
046:            public static String format(List<Cookie> cookies) {
047:                StringBuilder sb = new StringBuilder();
048:
049:                Cookie cookie;
050:                for (int i = 0; i < cookies.size(); i++) {
051:                    cookie = cookies.get(i);
052:
053:                    if (i == 0) {
054:                        if (cookie.getVersion() > 0) {
055:                            sb.append("$Version=\"")
056:                                    .append(cookie.getVersion()).append("\"; ");
057:                        }
058:                    } else {
059:                        sb.append("; ");
060:                    }
061:
062:                    format(cookie, sb);
063:                }
064:
065:                return sb.toString();
066:            }
067:
068:            /**
069:             * Formats a cookie setting.
070:             * 
071:             * @param cookieSetting
072:             *            The cookie setting to format.
073:             * @return The formatted cookie setting.
074:             */
075:            public static String format(CookieSetting cookieSetting) {
076:                StringBuilder sb = new StringBuilder();
077:
078:                try {
079:                    format(cookieSetting, sb);
080:                } catch (IOException e) {
081:                    // log error
082:                }
083:
084:                return sb.toString();
085:            }
086:
087:            /**
088:             * Formats a cookie setting.
089:             * 
090:             * @param cookieSetting
091:             *            The cookie setting to format.
092:             * @param destination
093:             *            The appendable destination.
094:             */
095:            public static void format(CookieSetting cookieSetting,
096:                    Appendable destination) throws IOException {
097:                String name = cookieSetting.getName();
098:                String value = cookieSetting.getValue();
099:                int version = cookieSetting.getVersion();
100:
101:                if ((name == null) || (name.length() == 0)) {
102:                    throw new IllegalArgumentException(
103:                            "Can't write cookie. Invalid name detected");
104:                } else {
105:                    destination.append(name).append('=');
106:
107:                    // Append the value
108:                    if ((value != null) && (value.length() > 0)) {
109:                        appendValue(value, version, destination);
110:                    }
111:
112:                    // Append the version
113:                    if (version > 0) {
114:                        destination.append("; Version=");
115:                        appendValue(Integer.toString(version), version,
116:                                destination);
117:                    }
118:
119:                    // Append the path
120:                    String path = cookieSetting.getPath();
121:                    if ((path != null) && (path.length() > 0)) {
122:                        destination.append("; Path=");
123:
124:                        if (version == 0) {
125:                            destination.append(path);
126:                        } else {
127:                            HttpUtils.appendQuote(path, destination);
128:                        }
129:                    }
130:
131:                    // Append the expiration date
132:                    int maxAge = cookieSetting.getMaxAge();
133:                    if (maxAge >= 0) {
134:                        if (version == 0) {
135:                            long currentTime = System.currentTimeMillis();
136:                            long maxTime = ((long) maxAge * 1000L);
137:                            long expiresTime = currentTime + maxTime;
138:                            Date expires = new Date(expiresTime);
139:                            destination.append("; Expires=");
140:                            appendValue(DateUtils.format(expires,
141:                                    DateUtils.FORMAT_RFC_1036.get(0)), version,
142:                                    destination);
143:                        } else {
144:                            destination.append("; Max-Age=");
145:                            appendValue(Integer.toString(cookieSetting
146:                                    .getMaxAge()), version, destination);
147:                        }
148:                    } else if ((maxAge == -1) && (version > 0)) {
149:                        // Discard the cookie at the end of the user's session (RFC
150:                        // 2965)
151:                        destination.append("; Discard");
152:                    } else {
153:                        // Netscape cookies automatically expire at the end of the
154:                        // user's session
155:                    }
156:
157:                    // Append the domain
158:                    String domain = cookieSetting.getDomain();
159:                    if ((domain != null) && (domain.length() > 0)) {
160:                        destination.append("; Domain=");
161:                        appendValue(domain.toLowerCase(), version, destination);
162:                    }
163:
164:                    // Append the secure flag
165:                    if (cookieSetting.isSecure()) {
166:                        destination.append("; Secure");
167:                    }
168:
169:                    // Append the comment
170:                    if (version > 0) {
171:                        String comment = cookieSetting.getComment();
172:                        if ((comment != null) && (comment.length() > 0)) {
173:                            destination.append("; Comment=");
174:                            appendValue(comment, version, destination);
175:                        }
176:                    }
177:                }
178:            }
179:
180:            /**
181:             * Formats a cookie.
182:             * 
183:             * @param cookie
184:             *            The cookie to format.
185:             * @return The formatted cookie.
186:             */
187:            public static String format(Cookie cookie) {
188:                StringBuilder sb = new StringBuilder();
189:                format(cookie, sb);
190:                return sb.toString();
191:            }
192:
193:            /**
194:             * Formats a cookie setting.
195:             * 
196:             * @param cookie
197:             *            The cookie to format.
198:             * @param destination
199:             *            The appendable destination.
200:             */
201:            public static void format(Cookie cookie, Appendable destination) {
202:                String name = cookie.getName();
203:                String value = cookie.getValue();
204:                int version = cookie.getVersion();
205:
206:                if ((name == null) || (name.length() == 0)) {
207:                    throw new IllegalArgumentException(
208:                            "Can't write cookie. Invalid name detected");
209:                } else {
210:                    try {
211:                        appendValue(name, 0, destination).append('=');
212:
213:                        // Append the value
214:                        if ((value != null) && (value.length() > 0)) {
215:                            appendValue(value, version, destination);
216:                        }
217:
218:                        if (version > 0) {
219:                            // Append the path
220:                            String path = cookie.getPath();
221:                            if ((path != null) && (path.length() > 0)) {
222:                                destination.append("; $Path=");
223:                                HttpUtils.appendQuote(path, destination);
224:                            }
225:
226:                            // Append the domain
227:                            String domain = cookie.getDomain();
228:                            if ((domain != null) && (domain.length() > 0)) {
229:                                destination.append("; $Domain=");
230:                                HttpUtils.appendQuote(domain, destination);
231:                            }
232:                        }
233:                    } catch (IOException e) {
234:                        // log error
235:                    }
236:                }
237:            }
238:
239:            /**
240:             * Appends a source string as an HTTP comment.
241:             * 
242:             * @param value
243:             *            The source string to format.
244:             * @param version
245:             *            The cookie version.
246:             * @param destination
247:             *            The appendable destination.
248:             * @throws IOException
249:             */
250:            private static Appendable appendValue(CharSequence value,
251:                    int version, Appendable destination) throws IOException {
252:                if (version == 0) {
253:                    destination.append(value.toString());
254:                } else {
255:                    HttpUtils.appendQuote(value, destination);
256:                }
257:
258:                return destination;
259:            }
260:
261:            /**
262:             * Gets the cookies whose name is a key in the given map. If a matching
263:             * cookie is found, its value is put in the map.
264:             * 
265:             * @param source
266:             *            The source list of cookies.
267:             * @param destination
268:             *            The cookies map controlling the reading.
269:             */
270:            public static void getCookies(List<Cookie> source,
271:                    Map<String, Cookie> destination) {
272:                Cookie cookie;
273:
274:                for (Iterator<Cookie> iter = source.iterator(); iter.hasNext();) {
275:                    cookie = iter.next();
276:
277:                    if (destination.containsKey(cookie.getName())) {
278:                        destination.put(cookie.getName(), cookie);
279:                    }
280:                }
281:            }
282:
283:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.