Author: remy.maucherat(a)jboss.com
Date: 2008-09-29 11:45:21 -0400 (Mon, 29 Sep 2008)
New Revision: 796
Modified:
trunk/java/org/apache/catalina/servlets/DefaultServlet.java
trunk/java/org/apache/catalina/servlets/WebdavServlet.java
trunk/java/org/apache/naming/resources/ResourceAttributes.java
trunk/webapps/docs/changelog.xml
Log:
- More etag improvements.
Modified: trunk/java/org/apache/catalina/servlets/DefaultServlet.java
===================================================================
--- trunk/java/org/apache/catalina/servlets/DefaultServlet.java 2008-09-29 15:28:37 UTC
(rev 795)
+++ trunk/java/org/apache/catalina/servlets/DefaultServlet.java 2008-09-29 15:45:21 UTC
(rev 796)
@@ -580,24 +580,6 @@
/**
- * Get the ETag associated with a file.
- *
- * @param resourceAttributes The resource information
- */
- protected String getETag(ResourceAttributes resourceAttributes) {
- String result = null;
- if ((result = resourceAttributes.getETag(true)) != null) {
- return result;
- } else if ((result = resourceAttributes.getETag()) != null) {
- return result;
- } else {
- return "W/\"" + resourceAttributes.getContentLength() +
"-"
- + resourceAttributes.getLastModified() + "\"";
- }
- }
-
-
- /**
* URL rewriter.
*
* @param path Path which has to be rewiten
@@ -737,7 +719,7 @@
ranges = parseRange(request, response, cacheEntry.attributes);
// ETag header
- response.setHeader("ETag", getETag(cacheEntry.attributes));
+ response.setHeader("ETag", cacheEntry.attributes.getETag());
// Last-Modified header
response.setHeader("Last-Modified",
@@ -983,7 +965,7 @@
;
}
- String eTag = getETag(resourceAttributes);
+ String eTag = resourceAttributes.getETag();
long lastModified = resourceAttributes.getLastModified();
if (headerValueTime == (-1L)) {
@@ -1535,7 +1517,7 @@
ResourceAttributes resourceAttributes)
throws IOException {
- String eTag = getETag(resourceAttributes);
+ String eTag = resourceAttributes.getETag();
String headerValue = request.getHeader("If-Match");
if (headerValue != null) {
if (headerValue.indexOf('*') == -1) {
@@ -1591,6 +1573,7 @@
// The entity has not been modified since the date
// specified by the client. This is not an error case.
response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
+ response.setHeader("ETag", resourceAttributes.getETag());
return false;
}
}
@@ -1617,7 +1600,7 @@
ResourceAttributes resourceAttributes)
throws IOException {
- String eTag = getETag(resourceAttributes);
+ String eTag = resourceAttributes.getETag();
String headerValue = request.getHeader("If-None-Match");
if (headerValue != null) {
@@ -1647,6 +1630,7 @@
if ( ("GET".equals(request.getMethod()))
|| ("HEAD".equals(request.getMethod())) ) {
response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
+ response.setHeader("ETag", eTag);
return false;
} else {
response.sendError
Modified: trunk/java/org/apache/catalina/servlets/WebdavServlet.java
===================================================================
--- trunk/java/org/apache/catalina/servlets/WebdavServlet.java 2008-09-29 15:28:37 UTC
(rev 795)
+++ trunk/java/org/apache/catalina/servlets/WebdavServlet.java 2008-09-29 15:45:21 UTC
(rev 796)
@@ -2136,7 +2136,7 @@
contentType);
}
generatedXML.writeProperty(null, "getetag",
- getETag(cacheEntry.attributes));
+ cacheEntry.attributes.getETag());
generatedXML.writeElement(null, "resourcetype",
XMLWriter.NO_CONTENT);
} else {
@@ -2262,7 +2262,7 @@
propertiesNotFound.addElement(property);
} else {
generatedXML.writeProperty
- (null, "getetag", getETag(cacheEntry.attributes));
+ (null, "getetag",
cacheEntry.attributes.getETag());
}
} else if (property.equals("getlastmodified")) {
if (cacheEntry.context != null) {
Modified: trunk/java/org/apache/naming/resources/ResourceAttributes.java
===================================================================
--- trunk/java/org/apache/naming/resources/ResourceAttributes.java 2008-09-29 15:28:37 UTC
(rev 795)
+++ trunk/java/org/apache/naming/resources/ResourceAttributes.java 2008-09-29 15:45:21 UTC
(rev 796)
@@ -123,6 +123,12 @@
/**
+ * ETag.
+ */
+ public static final String ALTERNATE_ETAG = "etag";
+
+
+ /**
* Collection type.
*/
public static final String COLLECTION_TYPE = "<collection/>";
@@ -704,43 +710,38 @@
/**
* Get ETag.
*
- * @return Weak ETag
+ * @return strong ETag if available, else weak ETag.
*/
public String getETag() {
- return getETag(false);
- }
-
-
- /**
- * Get ETag.
- *
- * @param strong If true, the strong ETag will be returned
- * @return ETag
- */
- public String getETag(boolean strong) {
- if (strong) {
- // The strong ETag must always be calculated by the resources
- if (strongETag != null)
- return strongETag;
- if (attributes != null) {
- Attribute attribute = attributes.get(ETAG);
- if (attribute != null) {
- try {
- strongETag = attribute.get().toString();
- } catch (NamingException e) {
- ; // No value for the attribute
+ String result = null;
+ if (attributes != null) {
+ Attribute attribute = attributes.get(ETAG);
+ if (attribute != null) {
+ try {
+ result = attribute.get().toString();
+ } catch (NamingException e) {
+ ; // No value for the attribute
+ }
+ }
+ }
+ if (result == null) {
+ if (strongETag != null) {
+ // The strong ETag must always be calculated by the resources
+ result = strongETag;
+ } else {
+ // The weakETag is contentLength + lastModified
+ if (weakETag == null) {
+ long contentLength = getContentLength();
+ long lastModified = getLastModified();
+ if ((contentLength >= 0) || (lastModified >= 0)) {
+ weakETag = "W/\"" + contentLength + "-"
+ + lastModified + "\"";
}
}
+ result = weakETag;
}
- return strongETag;
- } else {
- // The weakETag is contentLenght + lastModified
- if (weakETag == null) {
- weakETag = "W/\"" + getContentLength() + "-"
- + getLastModified() + "\"";
- }
- return weakETag;
}
+ return result;
}
@@ -810,6 +811,14 @@
long contentLength = getContentLength();
if (contentLength < 0) return null;
return new BasicAttribute(ALTERNATE_CONTENT_LENGTH, new
Long(contentLength));
+ } else if (attrID.equals(ETAG)) {
+ String etag = getETag();
+ if (etag == null) return null;
+ return new BasicAttribute(ETAG, etag);
+ } else if (attrID.equals(ALTERNATE_ETAG)) {
+ String etag = getETag();
+ if (etag == null) return null;
+ return new BasicAttribute(ALTERNATE_ETAG, etag);
}
} else {
return attributes.get(attrID);
@@ -893,6 +902,11 @@
attributes.addElement(new BasicAttribute(CONTENT_LENGTH,
contentLengthLong));
attributes.addElement(new BasicAttribute(ALTERNATE_CONTENT_LENGTH,
contentLengthLong));
}
+ String etag = getETag();
+ if (etag != null) {
+ attributes.addElement(new BasicAttribute(ETAG, etag));
+ attributes.addElement(new BasicAttribute(ALTERNATE_ETAG, etag));
+ }
return new RecyclableNamingEnumeration(attributes);
} else {
return attributes.getAll();
@@ -929,6 +943,11 @@
attributeIDs.addElement(CONTENT_LENGTH);
attributeIDs.addElement(ALTERNATE_CONTENT_LENGTH);
}
+ String etag = getETag();
+ if (etag != null) {
+ attributeIDs.addElement(ETAG);
+ attributeIDs.addElement(ALTERNATE_ETAG);
+ }
return new RecyclableNamingEnumeration(attributeIDs);
} else {
return attributes.getIDs();
@@ -947,6 +966,7 @@
if (getName() != null) size++;
if (getResourceType() != null) size += 2;
if (getContentLength() >= 0) size += 2;
+ if (getETag() != null) size += 2;
return size;
} else {
return attributes.size();
Modified: trunk/webapps/docs/changelog.xml
===================================================================
--- trunk/webapps/docs/changelog.xml 2008-09-29 15:28:37 UTC (rev 795)
+++ trunk/webapps/docs/changelog.xml 2008-09-29 15:45:21 UTC (rev 796)
@@ -46,6 +46,9 @@
<fix>
Sync date format usage in SSI. (markt)
</fix>
+ <fix>
+ <bug>45906</bug>: Another ETag improvement. Patch provided by Chris
Hubick. (remm)
+ </fix>
</changelog>
</subsection>
<subsection name="Coyote">