Skip to content

Instantly share code, notes, and snippets.

@alxsimo
Created October 7, 2014 14:39
Show Gist options
  • Select an option

  • Save alxsimo/3b6902cefc78e74dbb64 to your computer and use it in GitHub Desktop.

Select an option

Save alxsimo/3b6902cefc78e74dbb64 to your computer and use it in GitHub Desktop.
[Android] CookieHelper - Saves cookies retrieved from Apache HTTP Client to SharedPreferences
package com.alexsimo.utils.network;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Base64;
import android.util.Base64InputStream;
import android.util.Base64OutputStream;
import android.util.Log;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.cookie.BasicClientCookie;
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.net.CookieManager;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* Created by alexandru.simonescu on 30/09/2014.
*
* Guarda cookies en SharedPreferences para usarlas
* posteriormente en otras peticiones o webviews
*
*/
public class CookieHelper {
private static final String TAG = CookieHelper.class.getSimpleName();
private Context context;
public CookieHelper(Context c) {
this.context = c;
}
public void saveSharedPreferencesCookies(List<Cookie> cookies) {
SerializableCookie[] serializableCookies = new SerializableCookie[cookies.size()];
for (int i=0;i<cookies.size();i++){
SerializableCookie serializableCookie = new SerializableCookie(cookies.get(i));
serializableCookies[i] = serializableCookie;
}
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = preferences.edit();
ObjectOutputStream objectOutput;
ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
try {
objectOutput = new ObjectOutputStream(arrayOutputStream);
objectOutput.writeObject(serializableCookies);
byte[] data = arrayOutputStream.toByteArray();
objectOutput.close();
arrayOutputStream.close();
ByteArrayOutputStream out = new ByteArrayOutputStream();
Base64OutputStream b64 = new Base64OutputStream(out, Base64.DEFAULT);
b64.write(data);
b64.close();
out.close();
editor.putString("cookies", new String(out.toByteArray()));
editor.apply();
} catch (IOException e) {
e.printStackTrace();
}
}
public List<Cookie> loadSharedPreferencesCookie() throws Exception {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
byte[] bytes = preferences.getString("cookies", "{}").getBytes();
if (bytes.length == 0 || bytes.length==2)
return null;
ByteArrayInputStream byteArray = new ByteArrayInputStream(bytes);
Base64InputStream base64InputStream = new Base64InputStream(byteArray, Base64.DEFAULT);
ObjectInputStream in;
List<Cookie> cookies = new ArrayList<Cookie>();
SerializableCookie[] serializableCookies;
try {
in = new ObjectInputStream(base64InputStream);
serializableCookies = (SerializableCookie[]) in.readObject();
for (int i=0;i<serializableCookies.length; i++){
Cookie cookie = serializableCookies[i].getCookie();
cookies.add(cookie);
}
return cookies;
} catch (IOException e) {
e.printStackTrace();
throw new IOException(e);
} catch (ClassNotFoundException e) {
e.printStackTrace();
Log.e(TAG, e.getMessage());
//throw new ClassNotFoundException(e.getMessage());
}
return null;
}
/*
Clase usada para persistir cookies
*/
public final class SerializableCookie implements Serializable {
private static final long serialVersionUID = 6374381828722046732L;
private transient final Cookie cookie;
private transient BasicClientCookie clientCookie;
public SerializableCookie(Cookie cookie) {
this.cookie = cookie;
}
public Cookie getCookie() {
Cookie bestCookie = cookie;
if(clientCookie != null) {
bestCookie = clientCookie;
}
return bestCookie;
}
private void writeObject(ObjectOutputStream out) throws IOException {
out.writeObject(cookie.getName());
out.writeObject(cookie.getValue());
out.writeObject(cookie.getComment());
out.writeObject(cookie.getDomain());
out.writeObject(cookie.getExpiryDate());
out.writeObject(cookie.getPath());
out.writeInt(cookie.getVersion());
out.writeBoolean(cookie.isSecure());
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
String name = (String)in.readObject();
String value = (String)in.readObject();
clientCookie = new BasicClientCookie(name, value);
clientCookie.setComment((String)in.readObject());
clientCookie.setDomain((String)in.readObject());
clientCookie.setExpiryDate((Date)in.readObject());
clientCookie.setPath((String)in.readObject());
clientCookie.setVersion(in.readInt());
clientCookie.setSecure(in.readBoolean());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment