Rewrote cookie setting to be more flexible and allow setting of path and domain.
This commit is contained in:
parent
c8690f49d3
commit
a6516af320
4 changed files with 123 additions and 74 deletions
74
src/helma/framework/CookieTrans.java
Normal file
74
src/helma/framework/CookieTrans.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -26,14 +26,6 @@ public class ResponseBean implements Serializable {
|
||||||
res.format (what);
|
res.format (what);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void pushStringBuffer () {
|
|
||||||
res.pushStringBuffer ();
|
|
||||||
}
|
|
||||||
|
|
||||||
public String popStringBuffer () {
|
|
||||||
return res.popStringBuffer ();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void redirect (String url) throws RedirectException {
|
public void redirect (String url) throws RedirectException {
|
||||||
res.redirect (url);
|
res.redirect (url);
|
||||||
}
|
}
|
||||||
|
@ -43,11 +35,19 @@ public class ResponseBean implements Serializable {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setCookie (String key, String value) {
|
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) {
|
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) {
|
public void write (Object what) {
|
||||||
|
@ -172,11 +172,5 @@ public class ResponseBean implements Serializable {
|
||||||
res.digestDependencies ();
|
res.digestDependencies ();
|
||||||
}
|
}
|
||||||
|
|
||||||
/* public void notModified () throws RedirectException {
|
|
||||||
res.setNotModified (true);
|
|
||||||
} */
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -59,10 +59,7 @@ public final class ResponseTrans implements Externalizable {
|
||||||
private String etag = null;
|
private String etag = null;
|
||||||
|
|
||||||
// cookies
|
// cookies
|
||||||
String cookieKeys[];
|
Map cookies;
|
||||||
String cookieValues[];
|
|
||||||
int cookieDays[];
|
|
||||||
int nCookies = 0;
|
|
||||||
|
|
||||||
// the buffer used to build the response
|
// the buffer used to build the response
|
||||||
private transient StringBuffer buffer = null;
|
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
|
* Replace special characters with entities but pass through HTML tags
|
||||||
* in the response.
|
|
||||||
*/
|
*/
|
||||||
public void format (Object what) {
|
public void format (Object what) {
|
||||||
if (what != null) {
|
if (what != null) {
|
||||||
|
@ -494,61 +490,49 @@ public final class ResponseTrans implements Externalizable {
|
||||||
skincache.put (id, skin);
|
skincache.put (id, skin);
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void setCookie (String key, String value) {
|
public void setCookie (String key, String value, int days, String path, String domain) {
|
||||||
setCookie (key, value, -1);
|
CookieTrans c = null;
|
||||||
|
if (cookies == null) {
|
||||||
|
cookies = new HashMap ();
|
||||||
|
} else {
|
||||||
|
c = (CookieTrans) cookies.get (key);
|
||||||
}
|
}
|
||||||
|
if (c == null) {
|
||||||
public synchronized void setCookie (String key, String value, int days) {
|
c = new CookieTrans (key, value);
|
||||||
if (nCookies == 0) {
|
cookies.put (key, c);
|
||||||
cookieKeys = new String [3];
|
} else {
|
||||||
cookieValues = new String [3];
|
c.setValue (value);
|
||||||
cookieDays = new int [3];
|
|
||||||
}
|
}
|
||||||
if (nCookies == cookieKeys.length) {
|
c.setDays (days);
|
||||||
String nk[] = new String [nCookies+3];
|
c.setPath (path);
|
||||||
System.arraycopy (cookieKeys, 0, nk, 0, nCookies);
|
c.setDomain (domain);
|
||||||
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;
|
|
||||||
}
|
|
||||||
cookieKeys [nCookies] = key;
|
|
||||||
cookieValues [nCookies] = value;
|
|
||||||
cookieDays [nCookies] = days;
|
|
||||||
nCookies += 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void resetCookies () {
|
public void resetCookies () {
|
||||||
nCookies = 0;
|
if (cookies != null)
|
||||||
|
cookies.clear ();
|
||||||
}
|
}
|
||||||
|
|
||||||
public int countCookies () {
|
public int countCookies () {
|
||||||
return nCookies;
|
if (cookies != null)
|
||||||
|
return cookies.size();
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getDaysAt (int i) {
|
public CookieTrans[] getCookies () {
|
||||||
return cookieDays[i];
|
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 {
|
public void readExternal (ObjectInput s) throws ClassNotFoundException, IOException {
|
||||||
contentType = (String) s.readObject ();
|
contentType = (String) s.readObject ();
|
||||||
response = (byte[]) s.readObject ();
|
response = (byte[]) s.readObject ();
|
||||||
redir = (String) s.readObject ();
|
redir = (String) s.readObject ();
|
||||||
cookieKeys = (String[]) s.readObject ();
|
cookies = (Map) s.readObject ();
|
||||||
cookieValues = (String[]) s.readObject ();
|
|
||||||
cookieDays = (int[]) s.readObject ();
|
|
||||||
nCookies = s.readInt ();
|
|
||||||
cache = s.readBoolean ();
|
cache = s.readBoolean ();
|
||||||
status = s.readInt ();
|
status = s.readInt ();
|
||||||
realm = (String) s.readObject ();
|
realm = (String) s.readObject ();
|
||||||
|
@ -562,10 +546,7 @@ public final class ResponseTrans implements Externalizable {
|
||||||
s.writeObject (contentType);
|
s.writeObject (contentType);
|
||||||
s.writeObject (response);
|
s.writeObject (response);
|
||||||
s.writeObject (redir);
|
s.writeObject (redir);
|
||||||
s.writeObject (cookieKeys);
|
s.writeObject (cookies);
|
||||||
s.writeObject (cookieValues);
|
|
||||||
s.writeObject (cookieDays);
|
|
||||||
s.writeInt (nCookies);
|
|
||||||
s.writeBoolean (cache);
|
s.writeBoolean (cache);
|
||||||
s.writeInt (status);
|
s.writeInt (status);
|
||||||
s.writeObject (realm);
|
s.writeObject (realm);
|
||||||
|
|
|
@ -178,6 +178,8 @@ public abstract class AbstractServletClient extends HttpServlet {
|
||||||
if ( authorization != null )
|
if ( authorization != null )
|
||||||
reqtrans.set ("authorization", authorization );
|
reqtrans.set ("authorization", authorization );
|
||||||
|
|
||||||
|
// response.setHeader ("Server", "Helma/"+helma.main.Server.version);
|
||||||
|
|
||||||
reqtrans.path = getPathInfo (request);
|
reqtrans.path = getPathInfo (request);
|
||||||
ResponseTrans restrans = execute (reqtrans);
|
ResponseTrans restrans = execute (reqtrans);
|
||||||
|
|
||||||
|
@ -208,16 +210,14 @@ public abstract class AbstractServletClient extends HttpServlet {
|
||||||
HttpServletResponse res,
|
HttpServletResponse res,
|
||||||
ResponseTrans hopres) {
|
ResponseTrans hopres) {
|
||||||
|
|
||||||
for (int i = 0; i < hopres.countCookies(); i++) try {
|
int ncookies = hopres.countCookies();
|
||||||
Cookie c = new Cookie(hopres.getKeyAt(i), hopres.getValueAt(i));
|
if (hopres.countCookies() > 0) {
|
||||||
c.setPath ("/");
|
CookieTrans[] cookies = hopres.getCookies ();
|
||||||
if (cookieDomain != null)
|
for (int i = 0; i < cookies.length; i++) try {
|
||||||
c.setDomain (cookieDomain);
|
Cookie c = cookies[i].getCookie ("/", cookieDomain);
|
||||||
int expires = hopres.getDaysAt(i);
|
|
||||||
if (expires > 0)
|
|
||||||
c.setMaxAge(expires * 60*60*24); // Cookie time to live, days -> seconds
|
|
||||||
res.addCookie(c);
|
res.addCookie(c);
|
||||||
} catch (Exception ign) {}
|
} catch (Exception ignore) {}
|
||||||
|
}
|
||||||
|
|
||||||
if (hopres.getETag() != null) {
|
if (hopres.getETag() != null) {
|
||||||
res.setHeader ("ETag", hopres.getETag());
|
res.setHeader ("ETag", hopres.getETag());
|
||||||
|
|
Loading…
Add table
Reference in a new issue