View Javadoc

1   /*
2    * Copyright (c) 2005-2007 Creative Sphere Limited.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *
10   *   Creative Sphere - initial API and implementation
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   * Cookie utilities class
26   *
27   * @author Daniel Sendula
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       * Returns all response cookies as a map
41       * @param connection http connection
42       * @return response cookies as a map
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       * Adds response cookie
51       * @param connection http connection
52       * @param cookies cookie
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       * Obtains cookie headers
90       * @param responseHeaders response headers
91       * @return map of cookie headers
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      * Obtains request cookies as a map
111      * @param connection http connection
112      * @return request cookies as a map
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 }