Allow a REST view to decide how long its response should be cached

Change-Id: I1bb924cbe341d577656df8099214433c734a1563
Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
This commit is contained in:
Edwin Kempin
2013-07-08 16:32:15 +02:00
parent 53456c2ac7
commit 872cd35978
4 changed files with 65 additions and 11 deletions

View File

@@ -0,0 +1,56 @@
// Copyright (C) 2013 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;
import java.util.concurrent.TimeUnit;
public class CacheControl {
public enum Type {
NONE, PUBLIC, PRIVATE;
}
public final static CacheControl NONE = new CacheControl(Type.NONE, 0, null);
public static CacheControl PUBLIC(long age, TimeUnit unit) {
return new CacheControl(Type.PUBLIC, age, unit);
}
public static CacheControl PRIVATE(long age, TimeUnit unit) {
return new CacheControl(Type.PRIVATE, age, unit);
}
private final Type type;
private final long age;
private final TimeUnit unit;
private CacheControl(Type type, long age, TimeUnit unit) {
this.type = type;
this.age = age;
this.unit = unit;
}
public Type getType() {
return type;
}
public long getAge() {
return age;
}
public TimeUnit getUnit() {
return unit;
}
}

View File

@@ -19,10 +19,6 @@ public abstract class Response<T> {
@SuppressWarnings({"rawtypes"})
private static final Response NONE = new None();
public enum CacheControl {
NONE, PUBLIC, PRIVATE;
}
/** HTTP 200 OK: pointless wrapper for type safety. */
public static <T> Response<T> ok(T value) {
return new Impl<T>(200, value);

View File

@@ -52,6 +52,7 @@ import com.google.gerrit.extensions.restapi.AcceptsPost;
import com.google.gerrit.extensions.restapi.AuthException;
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.IdString;
import com.google.gerrit.extensions.restapi.MethodNotAllowedException;
@@ -112,7 +113,6 @@ import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import java.util.zip.GZIPOutputStream;
@@ -302,7 +302,7 @@ public class RestApiServlet extends HttpServlet {
@SuppressWarnings("rawtypes")
Response r = (Response) result;
status = r.statusCode();
configureCaching(req, res, r);
configureCaching(req, res, r.caching());
} else if (result instanceof Response.Redirect) {
CacheHeaders.setNotCacheable(res);
res.sendRedirect(((Response.Redirect) result).location());
@@ -354,18 +354,18 @@ public class RestApiServlet extends HttpServlet {
}
private static <T> void configureCaching(HttpServletRequest req,
HttpServletResponse res, Response<T> r) {
HttpServletResponse res, CacheControl c) {
if ("GET".equals(req.getMethod())) {
switch (r.caching()) {
switch (c.getType()) {
case NONE:
default:
CacheHeaders.setNotCacheable(res);
break;
case PRIVATE:
CacheHeaders.setCacheablePrivate(res, 7, TimeUnit.DAYS);
CacheHeaders.setCacheablePrivate(res, c.getAge(), c.getUnit());
break;
case PUBLIC:
CacheHeaders.setCacheable(req, res, 7, TimeUnit.DAYS);
CacheHeaders.setCacheable(req, res, c.getAge(), c.getUnit());
break;
}
} else {

View File

@@ -22,6 +22,7 @@ import com.google.common.collect.Lists;
import com.google.gerrit.common.data.PatchScript;
import com.google.gerrit.common.data.PatchScript.DisplayMethod;
import com.google.gerrit.common.data.PatchScript.FileMode;
import com.google.gerrit.extensions.restapi.CacheControl;
import com.google.gerrit.extensions.restapi.IdString;
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
import com.google.gerrit.extensions.restapi.Response;
@@ -49,6 +50,7 @@ import org.kohsuke.args4j.spi.Parameters;
import org.kohsuke.args4j.spi.Setter;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class GetDiff implements RestReadView<FileResource> {
private final PatchScriptFactory.Factory patchScriptFactoryFactory;
@@ -150,7 +152,7 @@ public class GetDiff implements RestReadView<FileResource> {
result.diffHeader = ps.getPatchHeader();
}
result.content = content.lines;
return Response.ok(result).caching(Response.CacheControl.PRIVATE);
return Response.ok(result).caching(CacheControl.PRIVATE(7, TimeUnit.DAYS));
}
static class Result {