Add new ETagView interface
This allows views to have an ETag. Change-Id: I6cb4cfe9ad2a4386f38cd662cda6ce260964f687
This commit is contained in:
@@ -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<R extends RestResource> extends RestReadView<R> {
|
||||
public String getETag(R rsrc);
|
||||
}
|
||||
@@ -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<RestResource> 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) {
|
||||
|
||||
Reference in New Issue
Block a user