Added support for an application checksum which is automatically included in
dependency checksums/fingerprints/ETags generated via res.dependsOn() and res.digest().
This commit is contained in:
parent
78309c02aa
commit
35d0389d8e
1 changed files with 31 additions and 21 deletions
|
@ -96,8 +96,13 @@ public final class ResponseTrans implements Externalizable {
|
||||||
// the request trans for this response
|
// the request trans for this response
|
||||||
private transient RequestTrans reqtrans;
|
private transient RequestTrans reqtrans;
|
||||||
|
|
||||||
|
// the message digest used to generate composed digests for ETag headers
|
||||||
private transient MessageDigest digest;
|
private transient MessageDigest digest;
|
||||||
|
|
||||||
|
// the appliciation checksum to make ETag headers sensitive to app changes
|
||||||
|
long applicationChecksum;
|
||||||
|
|
||||||
|
|
||||||
public ResponseTrans () {
|
public ResponseTrans () {
|
||||||
super ();
|
super ();
|
||||||
title = head = body = message = error = null;
|
title = head = body = message = error = null;
|
||||||
|
@ -321,7 +326,7 @@ public final class ResponseTrans implements Externalizable {
|
||||||
// if charset is not set, use western encoding
|
// if charset is not set, use western encoding
|
||||||
if (charset == null)
|
if (charset == null)
|
||||||
charset = "ISO-8859-1";
|
charset = "ISO-8859-1";
|
||||||
boolean error = false;
|
boolean encodingError = false;
|
||||||
if (response == null) {
|
if (response == null) {
|
||||||
if (buffer != null) {
|
if (buffer != null) {
|
||||||
if (debugBuffer != null)
|
if (debugBuffer != null)
|
||||||
|
@ -329,18 +334,22 @@ public final class ResponseTrans implements Externalizable {
|
||||||
try {
|
try {
|
||||||
response = buffer.toString ().getBytes (charset);
|
response = buffer.toString ().getBytes (charset);
|
||||||
} catch (UnsupportedEncodingException uee) {
|
} catch (UnsupportedEncodingException uee) {
|
||||||
error = true;
|
encodingError = true;
|
||||||
response = buffer.toString ().getBytes ();
|
response = buffer.toString ().getBytes ();
|
||||||
}
|
}
|
||||||
// if etag is not set, calc MD5 digest and check it
|
// if etag is not set, calc MD5 digest and check it
|
||||||
if (etag == null && !notModified && redir == null) try {
|
if (etag == null && lastModified == -1 &&
|
||||||
String digest = MD5Encoder.encode (response);
|
redir == null && error == null) try {
|
||||||
etag = "\""+digest+"\"";
|
digest = MessageDigest.getInstance("MD5");
|
||||||
|
byte[] b = digest.digest(response);
|
||||||
|
etag = "\""+new String (Base64.encode(b))+"\"";
|
||||||
if (reqtrans != null && reqtrans.hasETag (etag)) {
|
if (reqtrans != null && reqtrans.hasETag (etag)) {
|
||||||
response = new byte[0];
|
response = new byte[0];
|
||||||
notModified = true;
|
notModified = true;
|
||||||
}
|
}
|
||||||
} catch (Exception ignore) {}
|
} catch (Exception ignore) {
|
||||||
|
// Etag creation failed for some reason. Ignore.
|
||||||
|
}
|
||||||
buffer = null; // make sure this is done only once, even with more requsts attached
|
buffer = null; // make sure this is done only once, even with more requsts attached
|
||||||
} else {
|
} else {
|
||||||
response = new byte[0];
|
response = new byte[0];
|
||||||
|
@ -348,7 +357,7 @@ public final class ResponseTrans implements Externalizable {
|
||||||
}
|
}
|
||||||
notifyAll ();
|
notifyAll ();
|
||||||
// if there was a problem with the encoding, let the app know
|
// if there was a problem with the encoding, let the app know
|
||||||
if (error)
|
if (encodingError)
|
||||||
throw new UnsupportedEncodingException (charset);
|
throw new UnsupportedEncodingException (charset);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -393,12 +402,6 @@ public final class ResponseTrans implements Externalizable {
|
||||||
return lastModified;
|
return lastModified;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* public void setNotModified (boolean notmod) throws RedirectException {
|
|
||||||
notModified = notmod;
|
|
||||||
if (notmod)
|
|
||||||
throw new RedirectException (null);
|
|
||||||
} */
|
|
||||||
|
|
||||||
public void setETag (String value) {
|
public void setETag (String value) {
|
||||||
etag = value == null ? null : "\""+value+"\"";
|
etag = value == null ? null : "\""+value+"\"";
|
||||||
if (etag != null && reqtrans != null && reqtrans.hasETag (etag)) {
|
if (etag != null && reqtrans != null && reqtrans.hasETag (etag)) {
|
||||||
|
@ -423,6 +426,8 @@ public final class ResponseTrans implements Externalizable {
|
||||||
}
|
}
|
||||||
if (what == null) {
|
if (what == null) {
|
||||||
digest.update (new byte[0]);
|
digest.update (new byte[0]);
|
||||||
|
} else if (what instanceof Date) {
|
||||||
|
digest.update (MD5Encoder.toBytes((Date) what).getTime());
|
||||||
} else if (what instanceof byte[]) {
|
} else if (what instanceof byte[]) {
|
||||||
digest.update ((byte[]) what);
|
digest.update ((byte[]) what);
|
||||||
} else {
|
} else {
|
||||||
|
@ -437,16 +442,21 @@ public final class ResponseTrans implements Externalizable {
|
||||||
public void digestDependencies () {
|
public void digestDependencies () {
|
||||||
if (digest == null)
|
if (digest == null)
|
||||||
return;
|
return;
|
||||||
byte[] b = digest.digest();
|
byte[] b = digest.digest(MD5Encoder.toBytes (applicationChecksum));
|
||||||
/* StringBuffer buf = new StringBuffer(b.length*2);
|
/* StringBuffer buf = new StringBuffer(b.length*2);
|
||||||
for ( int i=0; i<b.length; i++ ) {
|
for ( int i=0; i<b.length; i++ ) {
|
||||||
int j = (b[i]<0) ? 256+b[i] : b[i];
|
int j = (b[i]<0) ? 256+b[i] : b[i];
|
||||||
if ( j<16 ) buf.append("0");
|
if ( j<16 ) buf.append("0");
|
||||||
buf.append(Integer.toHexString(j));
|
buf.append(Integer.toHexString(j));
|
||||||
} */
|
}
|
||||||
|
setETag (buf.toString ()); */
|
||||||
setETag (new String (Base64.encode(b)));
|
setETag (new String (Base64.encode(b)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setApplicationChecksum (long n) {
|
||||||
|
applicationChecksum = n;
|
||||||
|
}
|
||||||
|
|
||||||
public void setSkinpath (Object[] arr) {
|
public void setSkinpath (Object[] arr) {
|
||||||
this.skinpath = arr;
|
this.skinpath = arr;
|
||||||
skincache = null;
|
skincache = null;
|
||||||
|
|
Loading…
Add table
Reference in a new issue