aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/CookieJar.java
blob: 1b421d4276c59566530e8e1f8e3fb182977cdf2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package cgeo.geocaching;

import org.apache.commons.lang3.StringUtils;

import android.content.SharedPreferences;
import android.util.Log;

import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Handle cookies obtained from web sites.
 *
 * No other place should touch cookies directly, as we want to make sure
 * that the stored information is up-to-date.
 *
 */
final public class CookieJar {

    static private boolean cookiesLoaded = false;
    final static private HashMap<String, String> cookies = new HashMap<String, String>();

    static private String cache = null; // Cache information, or null if it has been invalidated

    static private void loadCookiesIfNeeded(final SharedPreferences prefs) {
        if (!cookiesLoaded) {
            cookies.clear();
            for (final Map.Entry<String, ?> entry : prefs.getAll().entrySet()) {
                if (entry.getKey().startsWith("cookie_")) {
                    cookies.put(entry.getKey().substring(7), (String) entry.getValue());
                }
            }
            cookiesLoaded = true;
        }
    }

    static public synchronized void setCookie(final SharedPreferences prefs, final String name, final String value) {
        loadCookiesIfNeeded(prefs);
        if (!cookies.containsKey(name) || !cookies.get(name).equals(value)) {
            final SharedPreferences.Editor editor = prefs.edit();
            cookies.put(name, value);
            editor.putString(name, value);
            cache = null;
            editor.commit();
        }
    }

    static public synchronized void setCookie(final SharedPreferences prefs, final String headerField) {
        final int semiIndex = headerField.indexOf(';');
        final String cookie = semiIndex == -1 ? headerField : headerField.substring(0, semiIndex);
        final int equalIndex = headerField.indexOf('=');
        if (equalIndex > 0) {
            setCookie(prefs, cookie.substring(0, equalIndex), cookie.substring(equalIndex + 1));
        } else {
            Log.w(cgSettings.tag, "CookieJar.setCookie: ignoring header " + headerField);
        }
    }

    static public synchronized void setCookies(final SharedPreferences prefs, final URLConnection uc) {
        final Map<String, List<String>> headers = uc.getHeaderFields();
        for (final Map.Entry<String, List<String>> entry : headers.entrySet()) {
            if ("set-cookie".equalsIgnoreCase(entry.getKey())) {
                for (final String field : entry.getValue()) {
                    setCookie(prefs, field);
                }
            }
        }
    }

    static public synchronized String getCookiesAsString(final SharedPreferences prefs) {
        if (cache == null) {
            loadCookiesIfNeeded(prefs);
            final ArrayList<String> built = new ArrayList<String>();
            for (final Map.Entry<String, String> entry : cookies.entrySet()) {
                built.add(entry.getKey() + "=" + entry.getValue());
            }
            cache = StringUtils.join(built, ';');
        }
        return cache;
    }

    static public synchronized void deleteCookies(final SharedPreferences prefs) {
        loadCookiesIfNeeded(prefs);
        final SharedPreferences.Editor editor = prefs.edit();
        for (final String key : cookies.keySet()) {
            editor.remove("cookie_" + key);
        }
        editor.commit();
        cookies.clear();
        cache = "";
    }

}