Refactor XDocServlet: Move ETag check into helper method
Change-Id: If893c54a1463e54e13d52b551b3c66d87b6199d5
Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
diff --git a/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocServlet.java b/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocServlet.java
index 2e83484..78520f7 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocServlet.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocServlet.java
@@ -136,14 +136,9 @@
validateCanReadCommit(repo, projectControl, revId);
}
- String eTag = null;
- String receivedETag = req.getHeader(HttpHeaders.IF_NONE_MATCH);
- if (receivedETag != null) {
- eTag = computeETag(key.project, revId, key.file);
- if (eTag.equals(receivedETag)) {
- res.sendError(SC_NOT_MODIFIED);
- return;
- }
+ if (isResourceNotModified(req, key, revId)) {
+ res.sendError(SC_NOT_MODIFIED);
+ return;
}
Resource rsc;
@@ -156,10 +151,8 @@
}
if (rsc != Resource.NOT_FOUND) {
- res.setHeader(
- HttpHeaders.ETAG,
- MoreObjects.firstNonNull(eTag,
- computeETag(key.project, revId, key.file)));
+ res.setHeader(HttpHeaders.ETAG,
+ computeETag(key.project, revId, key.file));
}
CacheHeaders.setCacheablePrivate(res, 7, TimeUnit.DAYS, false);
rsc.send(req, res);
@@ -290,6 +283,15 @@
}
}
+ private static boolean isResourceNotModified(HttpServletRequest req,
+ ResourceKey key, ObjectId revId) {
+ String receivedETag = req.getHeader(HttpHeaders.IF_NONE_MATCH);
+ if (receivedETag != null) {
+ return receivedETag.equals(computeETag(key.project, revId, key.file));
+ }
+ return false;
+ }
+
private static String computeETag(Project.NameKey project, ObjectId revId,
String file) {
return Hashing.md5().newHasher()