Skip to content

Instantly share code, notes, and snippets.

@lezorich
Last active September 20, 2022 02:23
Show Gist options
  • Save lezorich/8f3f3a54f07515881581 to your computer and use it in GitHub Desktop.
Save lezorich/8f3f3a54f07515881581 to your computer and use it in GitHub Desktop.
Android basic persistent cookie manager for Volley. This cookie store wraps the default CookieManager store to store the session cookie. It use's Gson to json serialize the cookie and store it as a json string in SharedPreferences.
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 Lukas Zorich
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
import android.content.Context;
import android.content.SharedPreferences;
import com.google.gson.Gson;
import java.net.CookieManager;
import java.net.CookieStore;
import java.net.HttpCookie;
import java.net.URI;
import java.util.List;
/**
* Repository for cookies. CookieManager will store cookies of every incoming HTTP response into
* CookieStore, and retrieve cookies for every outgoing HTTP request.
* <p/>
* Cookies are stored in {@link android.content.SharedPreferences} and will persist on the
* user's device between application session. {@link com.google.gson.Gson} is used to serialize
* the cookies into a json string in order to be able to save the cookie to
* {@link android.content.SharedPreferences}
* <p/>
* Created by lukas on 17-11-14.
*/
public class PersistentCookieStore implements CookieStore {
/**
* The default preferences string.
*/
private final static String PREF_DEFAULT_STRING = "";
/**
* The preferences name.
*/
private final static String PREFS_NAME = PersistentCookieStore.class.getName();
/**
* The preferences session cookie key.
*/
private final static String PREF_SESSION_COOKIE = "session_cookie";
private CookieStore mStore;
private Context mContext;
/**
* @param context The application context
*/
public PersistentCookieStore(Context context) {
// prevent context leaking by getting the application context
mContext = context.getApplicationContext();
// get the default in memory store and if there is a cookie stored in shared preferences,
// we added it to the cookie store
mStore = new CookieManager().getCookieStore();
String jsonSessionCookie = getJsonSessionCookieString();
if (!jsonSessionCookie.equals(PREF_DEFAULT_STRING)) {
Gson gson = new Gson();
HttpCookie cookie = gson.fromJson(jsonSessionCookie, HttpCookie.class);
mStore.add(URI.create(cookie.getDomain()), cookie);
}
}
@Override
public void add(URI uri, HttpCookie cookie) {
if (cookie.getName().equals("sessionid")) {
// if the cookie that the cookie store attempt to add is a session cookie,
// we remove the older cookie and save the new one in shared preferences
remove(URI.create(cookie.getDomain()), cookie);
saveSessionCookie(cookie);
}
mStore.add(URI.create(cookie.getDomain()), cookie);
}
@Override
public List<HttpCookie> get(URI uri) {
return mStore.get(uri);
}
@Override
public List<HttpCookie> getCookies() {
return mStore.getCookies();
}
@Override
public List<URI> getURIs() {
return mStore.getURIs();
}
@Override
public boolean remove(URI uri, HttpCookie cookie) {
return mStore.remove(uri, cookie);
}
@Override
public boolean removeAll() {
return mStore.removeAll();
}
private String getJsonSessionCookieString() {
return getPrefs().getString(PREF_SESSION_COOKIE, PREF_DEFAULT_STRING);
}
/**
* Saves the HttpCookie to SharedPreferences as a json string.
*
* @param cookie The cookie to save in SharedPreferences.
*/
private void saveSessionCookie(HttpCookie cookie) {
Gson gson = new Gson();
String jsonSessionCookieString = gson.toJson(cookie);
SharedPreferences.Editor editor = getPrefs().edit();
editor.putString(PREF_SESSION_COOKIE, jsonSessionCookieString);
editor.apply();
}
private SharedPreferences getPrefs() {
return mContext.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
}
}
@asadullah06
Copy link

asadullah06 commented Nov 27, 2018

Hello, there is another easy way to maintain cookies session and that is to add this line in a class that is extended with APPLICATION class: CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));

@noorsyyed
Copy link

Hello, there is another easy way to maintain cookies session and that is to add this line in a class that is extended with APPLICATION class: CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));

@asadullah06 did it work for you ? so cookies are stored automatic and no need to explicitly send them in subsequent requests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment