Sometime last week I read this piece by Sam Ruby, which summarized says this:
…don’t send Etag and Last-Modified headers unless you really mean it. But if you can support it, please do. It will save you some bandwidth and your readers some processing.
The product I’ve been working on at work (which I should be able to start talking about soon which I can talk about now) for the last couple months uses feeds (either Atom, RSS 1.0 or RSS 2.0, your choice) extensively but didn’t have Etag or Last-Modified support so I spent a couple hours working on it this past weekend. We’re using ROME, so the code ended up looking something like this:
HttpServletRequest request = ... HttpServletResponse response = .... SyndFeed feed = ... if (!isModified(request, feed)) { response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); } else { long publishDate = feed.getPublishedDate().getTime(); response.setDateHeader("Last-Modified", publishDate); response.setHeader("Etag", getEtag(feed)); } ... private String getEtag(SyndFeed feed) { return "\"" + String.valueOf(feed.getPublishedDate().getTime()) + "\""; } ... private boolean isModified(HttpServletRequest request, SyndFeed feed) { if (request.getHeader("If-Modified-Since") != null && request.getHeader("If-None-Match") != null) { String feedTag = getEtag(feed); String eTag = request.getHeader("If-None-Match"); Calendar ifModifiedSince = Calendar.getInstance(); ifModifiedSince.setTimeInMillis(request.getDateHeader("If-Modified-Since")); Calendar publishDate = Calendar.getInstance(); publishDate.setTime(feed.getPublishedDate()); publishDate.set(Calendar.MILLISECOND, 0); int diff = ifModifiedSince.compareTo(publishDate); return diff != 0 || !eTag.equalsIgnoreCase(feedTag); } else { return true; } }
There are only a two gotchas in the code:
- The value of the Etag must be quoted, hence the
getEtag(...)
method above returning a string wrapped in quotes. Not hard to do, but easy to miss. - The first block of code above uses the
setDateHeader(String name, long date)
to set the ‘Last-Modified’ HTTP header, which conveniently takes care of formatting the given date according to the RFC 822 specification for dates and times. The published date comes from ROME. Here’s where it gets tricky: if the client returns the ‘If-Modified-Since’ header and you retrieve said date from the request usinggetDateHeader(String name)
, you’ll get a Date in the GMT timezone, which means if you want to compare the date you’ll have to get the date into your own timezone. That’s relatively easy to do by creating aCalendar
instance and setting the time of the instance to the value you retrieved from the header. The Calendar instance will transparently take care of the timezone change for you. But there’s still one thing left: the date specification for RFC 822 doesn’t specify a millisecond so if thelong
value you hand tosetDateHeader(long date)
method contains a millisecond value and you then try to use the same value to compare against the ‘If-Modified-Since’ header, you’ll never get a match. The easy way around that is to manually set the millisecond bits on the date you get back from the ‘If-Modified-Since’ header to zero.
If you’re interested, there are a number of other blogs / articles about Etags and Last-Modified headers: