Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
21480 rajender 1
package com.saholic.profittill.Utils;
2
import java.io.IOException;
3
import java.net.CookieManager;
4
import java.net.CookiePolicy;
5
import java.net.CookieStore;
6
import java.net.URI;
7
import java.util.Arrays;
8
import java.util.List;
9
import java.util.Map;
10
 
11
public class WebKitCookieManagerProxy extends CookieManager
12
{
13
    private android.webkit.CookieManager webkitCookieManager;
14
 
15
    public WebKitCookieManagerProxy()
16
    {
17
        this(null, null);
18
    }
19
 
20
    public WebKitCookieManagerProxy(CookieStore store, CookiePolicy cookiePolicy)
21
    {
22
        super(null, cookiePolicy);
23
 
24
        this.webkitCookieManager = android.webkit.CookieManager.getInstance();
25
    }
26
 
27
    @Override
28
    public void put(URI uri, Map<String, List<String>> responseHeaders) throws IOException
29
    {
30
        // make sure our args are valid
31
        if ((uri == null) || (responseHeaders == null)) return;
32
 
33
        // save our url once
34
        String url = uri.toString();
35
 
36
        // go over the headers
37
        for (String headerKey : responseHeaders.keySet())
38
        {
39
            // ignore headers which aren't cookie related
40
            if ((headerKey == null) || !(headerKey.equalsIgnoreCase("Set-Cookie2") || headerKey.equalsIgnoreCase("Set-Cookie"))) continue;
41
 
42
            // process each of the headers
43
            for (String headerValue : responseHeaders.get(headerKey))
44
            {
45
                this.webkitCookieManager.setCookie(url, headerValue);
46
            }
47
        }
48
    }
49
 
50
    @Override
51
    public Map<String, List<String>> get(URI uri, Map<String, List<String>> requestHeaders) throws IOException
52
    {
53
        // make sure our args are valid
54
        if ((uri == null) || (requestHeaders == null)) throw new IllegalArgumentException("Argument is null");
55
 
56
        // save our url once
57
        String url = uri.toString();
58
 
59
        // prepare our response
60
        Map<String, List<String>> res = new java.util.HashMap<String, List<String>>();
61
 
62
        // get the cookie
63
        String cookie = this.webkitCookieManager.getCookie(url);
64
 
65
        // return it
66
        if (cookie != null) res.put("Cookie", Arrays.asList(cookie));
67
        return res;
68
    }
69
 
70
    @Override
71
    public CookieStore getCookieStore()
72
    {
73
        // we don't want anyone to work with this cookie store directly
74
        throw new UnsupportedOperationException();
75
    }
76
}