tags:

views:

304

answers:

1

Im using

// this is a DefaultHttpClient
List<Cookie> cookies = this.getCookieStore().getCookies();

Now, since Cookie does not implement serializeable, I can't serialize that List.

EDIT: (specified my goal, not only the problem)

My goal is to use the DefaultHttpClient with persistent cookies.

Anyone with experience that could lead me on the right track here? There might be another best practice that I haven't discovered...

+1  A: 

Create your own SerializableCookie class which implements Serializable and just copy the Cookie properties during its construction. Something like this:

public class SerializableCookie implements Serializable {

    private String name;
    private String path;
    private String domain;
    // ...

    public SerializableCookie(Cookie cookie) {
        this.name = cookie.getName();
        this.path = cookie.getPath();
        this.domain = cookie.getDomain();
        // ...
    }

    public String getName() {
        return name;
    }

    // ...

}

Ensure that all properties itself are also serializable. Apart from the primitives, the String class for example itself already implements Serializable, so you don't have to worry about that.

Alternatively you can also wrap/decorate the Cookie as a transient property (so that it doesn't get serialized) and override the writeObject() and readObject() methods accordingly. Something like:

public class SerializableCookie implements Serializable {

    private transient Cookie cookie;

    public SerializableCookie(Cookie cookie) {
        this.cookie = cookie;
    }

    public Cookie getCookie() {
        return cookie;
    }

    private void writeObject(ObjectOutputStream oos) throws IOException {
        oos.defaultWriteObject();
        oos.writeObject(cookie.getName());
        oos.writeObject(cookie.getPath());
        oos.writeObject(cookie.getDomain());
        // ...
    }

    private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
        ois.defaultReadObject();
        cookie = new Cookie();
        cookie.setName((String) ois.readObject());
        cookie.setPath((String) ois.readObject());
        cookie.setDomain((String) ois.readObject());
        // ...
    }

}

Finally use that class instead in the List.

BalusC
Thanks, I think this might work, but i was hoping there was easier way to achieve my goal. I was hoping there would be some ready made feature in the SDK so i didn't have to invent the wheel again, but since its Java, I might have to. Using persistent cookies is typical issues i think. Sorry, my question was abit unclear in the first place, but I have updated it. Anyway, Im thankful for your suggestion.
PHP_Jedi
It makes sense to not serialize real cookies since they represents a state which may be invalid at the moment you deserialize it. Just keep them in memory or store only name/value pairs in a `Map` and serialize it instead.
BalusC
Thanks alot for a very good answer!
PHP_Jedi