1
2
3
4
5
6
7
8
9
10
11
12
13 package org.abstracthorizon.danube.http.cookie;
14
15 import java.text.ParseException;
16 import java.util.Collection;
17 import java.util.HashMap;
18 import java.util.Map;
19 import java.util.StringTokenizer;
20
21 import org.abstracthorizon.danube.http.HTTPConnection;
22 import org.abstracthorizon.danube.http.util.MultiStringMap;
23
24
25
26
27
28
29 public class CookieUtilities {
30
31 public static final String RESPONSE_COOKIES_ATTRIBUTE = "org.abstracthorizon.danube.http.cookie.ResponseCookies";
32
33 public static final String RESPONSE_COOKIE_HEADER = "Set-Cookie";
34
35 public static final String REQUEST_COOKIES_ATTRIBUTE = "org.abstracthorizon.danube.http.cookie.RequestCookies";
36
37 public static final String REQUEST_COOKIE_HEADER = "Cookie";
38
39
40
41
42
43
44 @SuppressWarnings("unchecked")
45 public static Map<String, Cookie> getResponseCookies(HTTPConnection connection) {
46 return (Map<String, Cookie>)connection.getAttributes().get(RESPONSE_COOKIES_ATTRIBUTE);
47 }
48
49
50
51
52
53
54 @SuppressWarnings("unchecked")
55 public static void addResponseCookies(HTTPConnection connection, Collection<Cookie> cookies) {
56
57 Map<String, Cookie> responseCookies = (Map<String, Cookie>)connection.getAttributes().get(RESPONSE_COOKIES_ATTRIBUTE);
58
59 if (responseCookies == null) {
60 responseCookies = new HashMap<String, Cookie>();
61 connection.getAttributes().put(RESPONSE_COOKIES_ATTRIBUTE, responseCookies);
62 }
63
64 MultiStringMap responseHeaders = connection.getResponseHeaders();
65
66 Map<String, String> cookieHeaders = null;
67
68 for (Cookie cookie : cookies) {
69 if (responseCookies.containsKey(cookie.getName())) {
70 if (cookieHeaders == null) {
71 cookieHeaders = obtainCookieHeaders(responseHeaders);
72 }
73 } else {
74 responseCookies.put(cookie.getName(), cookie);
75 String cookieRepresentation = cookie.toString();
76 if (cookieHeaders != null) {
77 cookieHeaders.put(cookie.getName(), cookieRepresentation);
78 } else {
79 responseHeaders.add(RESPONSE_COOKIE_HEADER, cookieRepresentation);
80 }
81 }
82 }
83 if (cookieHeaders != null) {
84 responseHeaders.putAll(RESPONSE_COOKIE_HEADER, cookieHeaders.values());
85 }
86 }
87
88
89
90
91
92
93 protected static Map<String, String> obtainCookieHeaders(MultiStringMap responseHeaders) {
94 Map<String, String> headers = new HashMap<String, String>();
95
96 for (String header : responseHeaders.getAsList(RESPONSE_COOKIE_HEADER)) {
97 int i = header.indexOf('=');
98 if (i < 0) {
99 throw new IllegalStateException("Missing '=' in a cookie response header");
100 }
101
102 String name = header.substring(0, i);
103 headers.put(name, header);
104 }
105
106 return headers;
107 }
108
109
110
111
112
113
114 @SuppressWarnings("unchecked")
115 public static Map<String, Cookie> getRequestCookies(HTTPConnection connection) {
116 Map<String, Cookie> cookies = (Map<String, Cookie>)connection.getAttributes().get(REQUEST_COOKIES_ATTRIBUTE);
117 if (cookies == null) {
118 cookies = new HashMap<String, Cookie>();
119 connection.getAttributes().put(REQUEST_COOKIES_ATTRIBUTE, cookies);
120 }
121
122 for (String header : connection.getRequestHeaders().getAsList(REQUEST_COOKIE_HEADER)) {
123 StringTokenizer tokenizer = new StringTokenizer(header, ";");
124 while (tokenizer.hasMoreTokens()) {
125 try {
126 String token = tokenizer.nextToken();
127 Cookie cookie = new Cookie(token.trim());
128 cookies.put(cookie.getName(), cookie);
129 } catch (ParseException ignore) {
130 }
131 }
132 }
133 return cookies;
134 }
135
136
137 }