From ea34214362fe9b70e9d5af85c5a21f6fffef87c7 Mon Sep 17 00:00:00 2001 From: Stefan Beller Date: Thu, 5 Feb 2015 10:57:11 -0800 Subject: [PATCH] Add new ETagView interface This allows views to have an ETag. Change-Id: I6cb4cfe9ad2a4386f38cd662cda6ce260964f687 --- .../gerrit/extensions/restapi/ETagView.java | 22 +++++++++++++++++++ .../gerrit/httpd/restapi/RestApiServlet.java | 14 ++++++++++-- 2 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 gerrit-extension-api/src/main/java/com/google/gerrit/extensions/restapi/ETagView.java diff --git a/gerrit-extension-api/src/main/java/com/google/gerrit/extensions/restapi/ETagView.java b/gerrit-extension-api/src/main/java/com/google/gerrit/extensions/restapi/ETagView.java new file mode 100644 index 0000000000..f95161d00e --- /dev/null +++ b/gerrit-extension-api/src/main/java/com/google/gerrit/extensions/restapi/ETagView.java @@ -0,0 +1,22 @@ +// Copyright (C) 2015 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.gerrit.extensions.restapi; + +/** + * A view which may change, although the underlying resource did not change + */ +public interface ETagView extends RestReadView { + public String getETag(R rsrc); +} diff --git a/gerrit-httpd/src/main/java/com/google/gerrit/httpd/restapi/RestApiServlet.java b/gerrit-httpd/src/main/java/com/google/gerrit/httpd/restapi/RestApiServlet.java index dd36495671..8f34d391f1 100644 --- a/gerrit-httpd/src/main/java/com/google/gerrit/httpd/restapi/RestApiServlet.java +++ b/gerrit-httpd/src/main/java/com/google/gerrit/httpd/restapi/RestApiServlet.java @@ -58,6 +58,7 @@ import com.google.gerrit.extensions.restapi.BadRequestException; import com.google.gerrit.extensions.restapi.BinaryResult; import com.google.gerrit.extensions.restapi.CacheControl; import com.google.gerrit.extensions.restapi.DefaultInput; +import com.google.gerrit.extensions.restapi.ETagView; import com.google.gerrit.extensions.restapi.IdString; import com.google.gerrit.extensions.restapi.MethodNotAllowedException; import com.google.gerrit.extensions.restapi.PreconditionFailedException; @@ -299,7 +300,7 @@ public class RestApiServlet extends HttpServlet { checkRequiresCapability(viewData); } - if (notModified(req, rsrc)) { + if (notModified(req, rsrc, viewData.view)) { res.sendError(SC_NOT_MODIFIED); return; } @@ -392,11 +393,20 @@ public class RestApiServlet extends HttpServlet { return defaultMessage; } - private static boolean notModified(HttpServletRequest req, RestResource rsrc) { + @SuppressWarnings({"unchecked", "rawtypes"}) + private static boolean notModified(HttpServletRequest req, RestResource rsrc, + RestView view) { if (!isGetOrHead(req)) { return false; } + if (view instanceof ETagView) { + String have = req.getHeader(HttpHeaders.IF_NONE_MATCH); + if (have != null) { + return have.equals(((ETagView) view).getETag(rsrc)); + } + } + if (rsrc instanceof RestResource.HasETag) { String have = req.getHeader(HttpHeaders.IF_NONE_MATCH); if (have != null) {