Ticket #2172: ResponseData.java

File ResponseData.java, 1.7 KB (added by exactt, 14 months ago)

Cookie Handling Helper Class

Line 
1package de.exactt.controller;
2
3import java.net.HttpCookie;
4import java.net.HttpURLConnection;
5import java.util.ArrayList;
6import java.util.List;
7import java.util.Map;
8import java.util.Set;
9
10import javax.servlet.http.Cookie;
11
12public class ResponseData
13{
14        private Map<String, List<String>> data;
15       
16        public ResponseData(HttpURLConnection connection)
17        {
18                data = connection.getHeaderFields();
19        }
20       
21        public List<Cookie> getCookies()
22        {
23                List<Cookie> cookies = new ArrayList<Cookie>();
24       
25                Set<String> keys = data.keySet();
26                for (String key : keys)
27                {
28            List<String> stringData = data.get(key);
29            //String headerValue = connection.getHeaderField(i);
30           
31            String value = "";
32           
33            for (String value2 : stringData)
34                        {
35                                value += value2;
36                        }
37           
38            if (key == null && value == null) {
39                // No more headers
40                break;
41            }
42            else if (key == null) {
43                // The header value contains the server's HTTP version
44               
45            }
46            else if(key.equals("Set-Cookie"))
47            {
48                List<HttpCookie> httpCookies = HttpCookie.parse(value);
49                for (HttpCookie h : httpCookies)
50                                {
51                                        Cookie c = new Cookie(h.getName(), h.getValue());
52                                        c.setComment(h.getComment());
53                                        if(h.getDomain() != null)
54                                                c.setDomain(h.getDomain());
55                                        c.setMaxAge(new Long(h.getMaxAge()).intValue());
56                                        c.setPath(h.getPath());
57                                        c.setSecure(h.getSecure());
58                                        c.setVersion(h.getVersion());
59                                        cookies.add(c);
60                                }
61            }
62            //else
63                //System.out.println(key + " : " + value);
64        }
65                return cookies;
66        }
67}