Instantly share code, notes, and snippets.
Forked from franmontiel/PersistentCookieStore.java
Last active
August 29, 2015 14:14
-
Save tibro4u/7ae579c301f4a4e8262a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.net.CookieStore; | |
import java.net.HttpCookie; | |
import java.net.URI; | |
import java.net.URISyntaxException; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.HashSet; | |
import java.util.Iterator; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.Map.Entry; | |
import java.util.Set; | |
import android.content.Context; | |
import android.content.SharedPreferences; | |
public class PersistentCookieStore implements CookieStore { | |
private static final String TAG = PersistentCookieStore.class | |
.getSimpleName(); | |
// Persistence | |
private static final String SP_COOKIE_STORE = "cookieStore"; | |
private static final String SP_KEY_DELIMITER = "|"; // Unusual char in URL | |
private static final String SP_KEY_DELIMITER_REGEX = "\\" | |
+ SP_KEY_DELIMITER; | |
private SharedPreferences sharedPreferences; | |
// In memory | |
private Map<URI, Set<HttpCookie>> allCookies; | |
public PersistentCookieStore(Context context) { | |
sharedPreferences = context.getSharedPreferences(SP_COOKIE_STORE, | |
Context.MODE_PRIVATE); | |
loadAllFromPersistence(); | |
} | |
private void loadAllFromPersistence() { | |
allCookies = new HashMap<URI, Set<HttpCookie>>(); | |
Map<String, ?> allPairs = sharedPreferences.getAll(); | |
for (Entry<String, ?> entry : allPairs.entrySet()) { | |
String[] uriAndName = entry.getKey().split(SP_KEY_DELIMITER_REGEX, | |
2); | |
try { | |
URI uri = new URI(uriAndName[0]); | |
String encodedCookie = (String) entry.getValue(); | |
HttpCookie cookie = new SerializableHttpCookie() | |
.decode(encodedCookie); | |
Set<HttpCookie> targetCookies = allCookies.get(uri); | |
if (targetCookies == null) { | |
targetCookies = new HashSet<HttpCookie>(); | |
allCookies.put(uri, targetCookies); | |
} | |
// Repeated cookies cannot exist in persistence | |
// targetCookies.remove(cookie) | |
targetCookies.add(cookie); | |
} catch (URISyntaxException e) { | |
Log.w(TAG, e); | |
} | |
} | |
} | |
@Override | |
public synchronized void add(URI uri, HttpCookie cookie) { | |
uri = cookieUri(uri, cookie); | |
Set<HttpCookie> targetCookies = allCookies.get(uri); | |
if (targetCookies == null) { | |
targetCookies = new HashSet<HttpCookie>(); | |
allCookies.put(uri, targetCookies); | |
} | |
targetCookies.remove(cookie); | |
targetCookies.add(cookie); | |
saveToPersistence(uri, cookie); | |
} | |
/** | |
* Get the real URI from the cookie "domain" and "path" attributes, if they | |
* are not set then uses the URI provided (coming from the response) | |
* | |
* @param uri | |
* @param cookie | |
* @return | |
*/ | |
private static URI cookieUri(URI uri, HttpCookie cookie) { | |
URI cookieUri = uri; | |
if (cookie.getDomain() != null) { | |
try { | |
cookieUri = new URI(uri.getScheme() == null ? "http" | |
: uri.getScheme(), cookie.getDomain(), | |
cookie.getPath() == null ? "/" : cookie.getPath(), null); | |
} catch (URISyntaxException e) { | |
Log.w(TAG, e); | |
} | |
} | |
return cookieUri; | |
} | |
private void saveToPersistence(URI uri, HttpCookie cookie) { | |
SharedPreferences.Editor editor = sharedPreferences.edit(); | |
editor.putString(uri.toString() + SP_KEY_DELIMITER + cookie.getName(), | |
new SerializableHttpCookie().encode(cookie)); | |
editor.apply(); | |
} | |
@Override | |
public synchronized List<HttpCookie> get(URI uri) { | |
return getValidCookies(uri); | |
} | |
@Override | |
public synchronized List<HttpCookie> getCookies() { | |
List<HttpCookie> allValidCookies = new ArrayList<HttpCookie>(); | |
for (Iterator<URI> it = allCookies.keySet().iterator(); it.hasNext();) { | |
allValidCookies.addAll(getValidCookies(it.next())); | |
} | |
return allValidCookies; | |
} | |
private List<HttpCookie> getValidCookies(URI uri) { | |
Set<HttpCookie> targetCookies = new HashSet<HttpCookie>(); | |
// If the stored URI does not have a path then it must match any URI in | |
// the same domain | |
for (Iterator<URI> it = allCookies.keySet().iterator(); it.hasNext();) { | |
URI storedUri = it.next(); | |
// Check if the two domains are the same | |
if (storedUri.getHost().equals(uri.getHost())) { | |
// Check if the stored path is null or "/" | |
// OR | |
// if the two the paths are the same | |
if ((storedUri.getPath() == null || storedUri.getPath().equals( | |
"/")) | |
|| storedUri.getPath().equals(uri.getPath()) | |
|| uri.getPath().startsWith(storedUri.getPath())) //checking for subdomain | |
{ | |
targetCookies.addAll(allCookies.get(storedUri)); | |
} | |
} | |
} | |
// Check it there are expired cookies and remove them | |
if (targetCookies != null) { | |
List<HttpCookie> cookiesToRemoveFromPersistence = new ArrayList<HttpCookie>(); | |
for (Iterator<HttpCookie> it = targetCookies.iterator(); it | |
.hasNext();) { | |
HttpCookie currentCookie = it.next(); | |
if (currentCookie.hasExpired()) { | |
cookiesToRemoveFromPersistence.add(currentCookie); | |
it.remove(); | |
} | |
} | |
if (!cookiesToRemoveFromPersistence.isEmpty()) { | |
removeFromPersistence(uri, cookiesToRemoveFromPersistence); | |
} | |
} | |
return new ArrayList<HttpCookie>(targetCookies); | |
} | |
private void removeFromPersistence(URI uri, List<HttpCookie> cookiesToRemove) { | |
SharedPreferences.Editor editor = sharedPreferences.edit(); | |
for (HttpCookie cookieToRemove : cookiesToRemove) { | |
editor.remove(uri.toString() + SP_KEY_DELIMITER | |
+ cookieToRemove.getName()); | |
} | |
editor.apply(); | |
} | |
@Override | |
public synchronized List<URI> getURIs() { | |
return new ArrayList<URI>(allCookies.keySet()); | |
} | |
@Override | |
public synchronized boolean remove(URI uri, HttpCookie cookie) { | |
Set<HttpCookie> targetCookies = allCookies.get(uri); | |
boolean cookieRemoved = targetCookies != null ? targetCookies | |
.remove(cookie) : false; | |
if (cookieRemoved) { | |
removeFromPersistence(uri, cookie); | |
} | |
return cookieRemoved; | |
} | |
private void removeFromPersistence(URI uri, HttpCookie cookieToRemove) { | |
SharedPreferences.Editor editor = sharedPreferences.edit(); | |
editor.remove(uri.toString() + SP_KEY_DELIMITER | |
+ cookieToRemove.getName()); | |
editor.apply(); | |
} | |
@Override | |
public synchronized boolean removeAll() { | |
allCookies.clear(); | |
removeAllFromPersistence(); | |
return true; | |
} | |
private void removeAllFromPersistence() { | |
sharedPreferences.edit().clear().apply(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Based on the code from this stackoverflow answer http://stackoverflow.com/a/25462286/980387 by janoliver | |
* Modifications in the structure of the class and addition of serialization of httpOnly attribute | |
*/ | |
import java.io.ByteArrayInputStream; | |
import java.io.ByteArrayOutputStream; | |
import java.io.IOException; | |
import java.io.ObjectInputStream; | |
import java.io.ObjectOutputStream; | |
import java.io.Serializable; | |
import java.lang.reflect.Field; | |
import java.net.HttpCookie; | |
public class SerializableHttpCookie implements Serializable { | |
private static final String TAG = SerializableHttpCookie.class | |
.getSimpleName(); | |
private static final long serialVersionUID = 6374381323722046732L; | |
private transient HttpCookie cookie; | |
// Workaround httpOnly: The httpOnly attribute is not accessible so when we | |
// serialize and deserialize the cookie it not preserve the same value. We | |
// need to access it using reflection | |
private Field fieldHttpOnly; | |
public SerializableHttpCookie() { | |
} | |
public String encode(HttpCookie cookie) { | |
this.cookie = cookie; | |
ByteArrayOutputStream os = new ByteArrayOutputStream(); | |
try { | |
ObjectOutputStream outputStream = new ObjectOutputStream(os); | |
outputStream.writeObject(this); | |
} catch (IOException e) { | |
AppLog.d(TAG, "IOException in encodeCookie", e); | |
return null; | |
} | |
return byteArrayToHexString(os.toByteArray()); | |
} | |
public HttpCookie decode(String encodedCookie) { | |
byte[] bytes = hexStringToByteArray(encodedCookie); | |
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream( | |
bytes); | |
HttpCookie cookie = null; | |
try { | |
ObjectInputStream objectInputStream = new ObjectInputStream( | |
byteArrayInputStream); | |
cookie = ((SerializableHttpCookie) objectInputStream.readObject()).cookie; | |
} catch (IOException e) { | |
Log.d(TAG, "IOException in decodeCookie", e); | |
} catch (ClassNotFoundException e) { | |
Log.d(TAG, "ClassNotFoundException in decodeCookie", e); | |
} | |
return cookie; | |
} | |
// Workaround httpOnly (getter) | |
private boolean getHttpOnly() { | |
try { | |
initFieldHttpOnly(); | |
return (boolean) fieldHttpOnly.get(cookie); | |
} catch (Exception e) { | |
// NoSuchFieldException || IllegalAccessException || | |
// IllegalArgumentException | |
Log.w(TAG, e); | |
} | |
return false; | |
} | |
// Workaround httpOnly (setter) | |
private void setHttpOnly(boolean httpOnly) { | |
try { | |
initFieldHttpOnly(); | |
fieldHttpOnly.set(cookie, httpOnly); | |
} catch (Exception e) { | |
// NoSuchFieldException || IllegalAccessException || | |
// IllegalArgumentException | |
Log.w(TAG, e); | |
} | |
} | |
private void initFieldHttpOnly() throws NoSuchFieldException { | |
Field fieldHttpOnly = cookie.getClass().getDeclaredField("httpOnly"); | |
fieldHttpOnly.setAccessible(true); | |
} | |
private void writeObject(ObjectOutputStream out) throws IOException { | |
out.writeObject(cookie.getName()); | |
out.writeObject(cookie.getValue()); | |
out.writeObject(cookie.getComment()); | |
out.writeObject(cookie.getCommentURL()); | |
out.writeObject(cookie.getDomain()); | |
out.writeLong(cookie.getMaxAge()); | |
out.writeObject(cookie.getPath()); | |
out.writeObject(cookie.getPortlist()); | |
out.writeInt(cookie.getVersion()); | |
out.writeBoolean(cookie.getSecure()); | |
out.writeBoolean(cookie.getDiscard()); | |
out.writeBoolean(getHttpOnly()); | |
} | |
private void readObject(ObjectInputStream in) throws IOException, | |
ClassNotFoundException { | |
String name = (String) in.readObject(); | |
String value = (String) in.readObject(); | |
cookie = new HttpCookie(name, value); | |
cookie.setComment((String) in.readObject()); | |
cookie.setCommentURL((String) in.readObject()); | |
cookie.setDomain((String) in.readObject()); | |
cookie.setMaxAge(in.readLong()); | |
cookie.setPath((String) in.readObject()); | |
cookie.setPortlist((String) in.readObject()); | |
cookie.setVersion(in.readInt()); | |
cookie.setSecure(in.readBoolean()); | |
cookie.setDiscard(in.readBoolean()); | |
setHttpOnly(in.readBoolean()); | |
} | |
/** | |
* Using some super basic byte array <-> hex conversions so we don't | |
* have to rely on any large Base64 libraries. Can be overridden if you | |
* like! | |
* | |
* @param bytes | |
* byte array to be converted | |
* @return string containing hex values | |
*/ | |
private String byteArrayToHexString(byte[] bytes) { | |
StringBuilder sb = new StringBuilder(bytes.length * 2); | |
for (byte element : bytes) { | |
int v = element & 0xff; | |
if (v < 16) { | |
sb.append('0'); | |
} | |
sb.append(Integer.toHexString(v)); | |
} | |
return sb.toString(); | |
} | |
/** | |
* Converts hex values from strings to byte array | |
* | |
* @param hexString | |
* string of hex-encoded values | |
* @return decoded byte array | |
*/ | |
private byte[] hexStringToByteArray(String hexString) { | |
int len = hexString.length(); | |
byte[] data = new byte[len / 2]; | |
for (int i = 0; i < len; i += 2) { | |
data[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4) + Character | |
.digit(hexString.charAt(i + 1), 16)); | |
} | |
return data; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment