Rewrote cookie setting to be more flexible and allow setting of path and domain.

This commit is contained in:
hns 2002-11-26 13:22:27 +00:00
parent c8690f49d3
commit a6516af320
4 changed files with 123 additions and 74 deletions

View file

@ -0,0 +1,74 @@
// CookieTrans.java
package helma.framework;
import java.io.Serializable;
import java.util.HashMap;
import javax.servlet.http.Cookie;
/**
* Cookie Transmitter. A simple, serializable representation
* of an HTTP cookie.
*/
public final class CookieTrans implements Serializable {
String name, value, path, domain;
int days;
CookieTrans (String name, String value) {
this.name = name;
this.value = value;
}
void setValue (String value) {
this.value = value;
}
void setDays (int days) {
this.days = days;
}
void setPath (String path) {
this.path = path;
}
void setDomain (String domain) {
this.domain = domain;
}
public String getName() {
return name;
}
public String getValue() {
return value;
}
public int getDays() {
return days;
}
public String getPath () {
return path;
}
public String getDomain () {
return domain;
}
public Cookie getCookie (String defaultPath, String defaultDomain) {
Cookie c = new Cookie (name, value);
if (days > 0)
// Cookie time to live, days -> seconds
c.setMaxAge (days*60*60*24);
if (path != null)
c.setPath (path);
else if (defaultPath != null)
c.setPath (defaultPath);
if (domain != null)
c.setDomain (domain);
else if (defaultDomain != null)
c.setDomain (defaultDomain);
return c;
}
}

View file

@ -26,14 +26,6 @@ public class ResponseBean implements Serializable {
res.format (what);
}
public void pushStringBuffer () {
res.pushStringBuffer ();
}
public String popStringBuffer () {
return res.popStringBuffer ();
}
public void redirect (String url) throws RedirectException {
res.redirect (url);
}
@ -43,11 +35,19 @@ public class ResponseBean implements Serializable {
}
public void setCookie (String key, String value) {
res.setCookie (key, value, -1);
res.setCookie (key, value, -1, null, null);
}
public void setCookie (String key, String value, int days) {
res.setCookie (key, value, days);
res.setCookie (key, value, days, null, null);
}
public void setCookie (String key, String value, int days, String path) {
res.setCookie (key, value, days, path, null);
}
public void setCookie (String key, String value, int days, String path, String domain) {
res.setCookie (key, value, days, path, domain);
}
public void write (Object what) {
@ -172,11 +172,5 @@ public class ResponseBean implements Serializable {
res.digestDependencies ();
}
/* public void notModified () throws RedirectException {
res.setNotModified (true);
} */
}

View file

@ -59,10 +59,7 @@ public final class ResponseTrans implements Externalizable {
private String etag = null;
// cookies
String cookieKeys[];
String cookieValues[];
int cookieDays[];
int nCookies = 0;
Map cookies;
// the buffer used to build the response
private transient StringBuffer buffer = null;
@ -260,8 +257,7 @@ public final class ResponseTrans implements Externalizable {
}
/**
* Replace special characters with entities but leave <, > and ", allowing HTML tags
* in the response.
* Replace special characters with entities but pass through HTML tags
*/
public void format (Object what) {
if (what != null) {
@ -494,61 +490,49 @@ public final class ResponseTrans implements Externalizable {
skincache.put (id, skin);
}
public synchronized void setCookie (String key, String value) {
setCookie (key, value, -1);
}
public synchronized void setCookie (String key, String value, int days) {
if (nCookies == 0) {
cookieKeys = new String [3];
cookieValues = new String [3];
cookieDays = new int [3];
public void setCookie (String key, String value, int days, String path, String domain) {
CookieTrans c = null;
if (cookies == null) {
cookies = new HashMap ();
} else {
c = (CookieTrans) cookies.get (key);
}
if (nCookies == cookieKeys.length) {
String nk[] = new String [nCookies+3];
System.arraycopy (cookieKeys, 0, nk, 0, nCookies);
String nv[] = new String [nCookies+3];
System.arraycopy (cookieValues, 0, nv, 0, nCookies);
int nd[] = new int [nCookies+3];
System.arraycopy (cookieDays, 0, nd, 0, nCookies);
cookieKeys = nk;
cookieValues = nv;
cookieDays = nd;
if (c == null) {
c = new CookieTrans (key, value);
cookies.put (key, c);
} else {
c.setValue (value);
}
cookieKeys [nCookies] = key;
cookieValues [nCookies] = value;
cookieDays [nCookies] = days;
nCookies += 1;
c.setDays (days);
c.setPath (path);
c.setDomain (domain);
}
public void resetCookies () {
nCookies = 0;
if (cookies != null)
cookies.clear ();
}
public int countCookies () {
return nCookies;
if (cookies != null)
return cookies.size();
return 0;
}
public int getDaysAt (int i) {
return cookieDays[i];
public CookieTrans[] getCookies () {
if (cookies == null)
return new CookieTrans[0];
CookieTrans[] c = new CookieTrans[cookies.size()];
cookies.values().toArray (c);
return c;
}
public String getKeyAt (int i) {
return cookieKeys[i];
}
public String getValueAt (int i) {
return cookieValues[i];
}
public void readExternal (ObjectInput s) throws ClassNotFoundException, IOException {
contentType = (String) s.readObject ();
response = (byte[]) s.readObject ();
redir = (String) s.readObject ();
cookieKeys = (String[]) s.readObject ();
cookieValues = (String[]) s.readObject ();
cookieDays = (int[]) s.readObject ();
nCookies = s.readInt ();
cookies = (Map) s.readObject ();
cache = s.readBoolean ();
status = s.readInt ();
realm = (String) s.readObject ();
@ -562,10 +546,7 @@ public final class ResponseTrans implements Externalizable {
s.writeObject (contentType);
s.writeObject (response);
s.writeObject (redir);
s.writeObject (cookieKeys);
s.writeObject (cookieValues);
s.writeObject (cookieDays);
s.writeInt (nCookies);
s.writeObject (cookies);
s.writeBoolean (cache);
s.writeInt (status);
s.writeObject (realm);

View file

@ -178,6 +178,8 @@ public abstract class AbstractServletClient extends HttpServlet {
if ( authorization != null )
reqtrans.set ("authorization", authorization );
// response.setHeader ("Server", "Helma/"+helma.main.Server.version);
reqtrans.path = getPathInfo (request);
ResponseTrans restrans = execute (reqtrans);
@ -208,16 +210,14 @@ public abstract class AbstractServletClient extends HttpServlet {
HttpServletResponse res,
ResponseTrans hopres) {
for (int i = 0; i < hopres.countCookies(); i++) try {
Cookie c = new Cookie(hopres.getKeyAt(i), hopres.getValueAt(i));
c.setPath ("/");
if (cookieDomain != null)
c.setDomain (cookieDomain);
int expires = hopres.getDaysAt(i);
if (expires > 0)
c.setMaxAge(expires * 60*60*24); // Cookie time to live, days -> seconds
res.addCookie(c);
} catch (Exception ign) {}
int ncookies = hopres.countCookies();
if (hopres.countCookies() > 0) {
CookieTrans[] cookies = hopres.getCookies ();
for (int i = 0; i < cookies.length; i++) try {
Cookie c = cookies[i].getCookie ("/", cookieDomain);
res.addCookie(c);
} catch (Exception ignore) {}
}
if (hopres.getETag() != null) {
res.setHeader ("ETag", hopres.getETag());