Sam,
Here is something that I used to achieve a serializable cookie. I was
storing cookies into memcached so I wanted to minimise the footprint of the
serialized object to maximise my cache and since (for my app anyway) most of
the cookie fields were null I used a null mask to save having to store null
references/empty fields.
Hope it's useful,
Chris.
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.Date;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.cookie.BasicClientCookie;
public class SerializableCookie implements Cookie, Externalizable {
private static final int NAME = 0x01;
private static final int VALUE = 0x02;
private static final int COMMENT = 0x04;
private static final int COMMENT_URL = 0x08;
private static final int EXPIRY_DATE = 0x10;
private static final int DOMAIN = 0x20;
private static final int PATH = 0x40;
private static final int PORTS = 0x80;
private transient int nullMask = 0;
private transient Cookie cookie;
public SerializableCookie() {
super();
}
public SerializableCookie(final Cookie cookie) {
super();
this.cookie = cookie;
}
public String getName() {
return cookie.getName();
}
public String getValue() {
return cookie.getValue();
}
public String getComment() {
return cookie.getComment();
}
public String getCommentURL() {
return cookie.getCommentURL();
}
public Date getExpiryDate() {
return cookie.getExpiryDate();
}
public boolean isPersistent() {
return cookie.isPersistent();
}
public String getDomain() {
return cookie.getDomain();
}
public String getPath() {
return cookie.getPath();
}
public int[] getPorts() {
return cookie.getPorts();
}
public boolean isSecure() {
return cookie.isSecure();
}
public int getVersion() {
return cookie.getVersion();
}
public boolean isExpired(final Date date) {
return cookie.isExpired(date);
}
public void writeExternal(final ObjectOutput out) throws IOException {
nullMask |= (getName() == null) ? NAME : 0;
nullMask |= (getValue() == null) ? VALUE : 0;
nullMask |= (getComment() == null) ? COMMENT : 0;
nullMask |= (getCommentURL() == null) ? COMMENT_URL : 0;
nullMask |= (getExpiryDate() == null) ? EXPIRY_DATE : 0;
nullMask |= (getDomain() == null) ? DOMAIN : 0;
nullMask |= (getPath() == null) ? PATH : 0;
nullMask |= (getPorts() == null) ? PORTS : 0;
out.writeInt(nullMask);
if ((nullMask & NAME) == 0) {
out.writeUTF(getName());
}
if ((nullMask & VALUE) == 0) {
out.writeUTF(getValue());
}
if ((nullMask & COMMENT) == 0) {
out.writeUTF(getComment());
}
if ((nullMask & COMMENT_URL) == 0) {
out.writeUTF(getCommentURL());
}
if ((nullMask & EXPIRY_DATE) == 0) {
out.writeLong(getExpiryDate().getTime());
}
out.writeBoolean(isPersistent());
if ((nullMask & DOMAIN) == 0) {
out.writeUTF(getDomain());
}
if ((nullMask & PATH) == 0) {
out.writeUTF(getPath());
}
if ((nullMask & PORTS) == 0) {
out.writeInt(getPorts().length);
for (int p : getPorts()) {
out.writeInt(p);
}
}
out.writeBoolean(isSecure());
out.writeInt(getVersion());
}
public void readExternal(final ObjectInput in) throws IOException,
ClassNotFoundException {
nullMask = in.readInt();
String name = null;
String value = null;
String comment = null;
String commentURL = null;
Date expiryDate = null;
boolean isPersistent = false;
String domain = null;
String path = null;
int[] ports = null;
boolean isSecure = false;
int version = 0;
if ((nullMask & NAME) == 0) {
name = in.readUTF();
}
if ((nullMask & VALUE) == 0) {
value = in.readUTF();
}
if ((nullMask & COMMENT) == 0) {
comment = in.readUTF();
}
if ((nullMask & COMMENT_URL) == 0) {
commentURL = in.readUTF();
}
if ((nullMask & EXPIRY_DATE) == 0) {
expiryDate = new Date(in.readLong());
}
isPersistent = in.readBoolean();
if ((nullMask & DOMAIN) == 0) {
domain = in.readUTF();
}
if ((nullMask & PATH) == 0) {
path = in.readUTF();
}
if ((nullMask & PORTS) == 0) {
final int len = in.readInt();
ports = new int[len];
for (int i = 0; i < len; i++) {
ports[i] = in.readInt();
}
}
isSecure = in.readBoolean();
version = in.readInt();
final BasicClientCookie bc = new BasicClientCookie(name, value);
bc.setComment(comment);
bc.setDomain(domain);
bc.setExpiryDate(expiryDate);
bc.setPath(path);
bc.setSecure(isSecure);
bc.setVersion(version);
this.cookie = bc;
}
@Override
public String toString() {
if (cookie == null) {
return "null";
}
else {
return cookie.toString();
}
}
}
2009/4/8 Sam Crawford <[email protected]>
> Okay thanks for the quick response.
>
> Regards,
>
> Sam
>
>
> 2009/4/8 Oleg Kalnichevski <[email protected]>
>
> > On Tue, Apr 07, 2009 at 10:14:57PM +0100, Sam Crawford wrote:
> > > Evening,
> > >
> > > Does anyone know if any work has been conducted on serializing
> > ClientCookies
> > > and CookieStores? (specifically BasicClientCookie and BasicCookieStore
> > > implementations).
> > >
> >
> > Not that I know of. HTTP state persistence is out of scope for
> > the HttpClient project.
> >
> > Oleg
> >
> >
> > > I'm looking at writing these out to a permanent store so that I can
> > persist
> > > sessions between application instances.
> > >
> > > I appreciate it's relatively straight-forward to extend these classes
> and
> > do
> > > it manually, but I thought I'd ask the question first to see if
> anyone's
> > > seen anything similar out there (or in case I've overlooked something
> > > obvious).
> > >
> > > Thanks,
> > >
> > > Sam
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [email protected]
> > For additional commands, e-mail: [email protected]
> >
> >
>