Accept HEAD in RestApiServlet
HEAD should be treated like GET, but omit the content body. Change-Id: I7f83829f4850e8717191ca3f1772af2792a0adee
This commit is contained in:
@@ -206,7 +206,7 @@ public class RestApiServlet extends HttpServlet {
|
|||||||
RestResource rsrc = TopLevelResource.INSTANCE;
|
RestResource rsrc = TopLevelResource.INSTANCE;
|
||||||
ViewData viewData = new ViewData(null, null);
|
ViewData viewData = new ViewData(null, null);
|
||||||
if (path.isEmpty()) {
|
if (path.isEmpty()) {
|
||||||
if ("GET".equals(req.getMethod())) {
|
if (isGetOrHead(req)) {
|
||||||
viewData = new ViewData(null, rc.list());
|
viewData = new ViewData(null, rc.list());
|
||||||
} else if (rc instanceof AcceptsPost && "POST".equals(req.getMethod())) {
|
} else if (rc instanceof AcceptsPost && "POST".equals(req.getMethod())) {
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
@@ -247,7 +247,7 @@ public class RestApiServlet extends HttpServlet {
|
|||||||
(RestCollection<RestResource, RestResource>) viewData.view;
|
(RestCollection<RestResource, RestResource>) viewData.view;
|
||||||
|
|
||||||
if (path.isEmpty()) {
|
if (path.isEmpty()) {
|
||||||
if ("GET".equals(req.getMethod())) {
|
if (isGetOrHead(req)) {
|
||||||
viewData = new ViewData(null, c.list());
|
viewData = new ViewData(null, c.list());
|
||||||
} else if (c instanceof AcceptsPost && "POST".equals(req.getMethod())) {
|
} else if (c instanceof AcceptsPost && "POST".equals(req.getMethod())) {
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
@@ -363,7 +363,7 @@ public class RestApiServlet extends HttpServlet {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static boolean notModified(HttpServletRequest req, RestResource rsrc) {
|
private static boolean notModified(HttpServletRequest req, RestResource rsrc) {
|
||||||
if (!"GET".equals(req.getMethod())) {
|
if (!isGetOrHead(req)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -386,7 +386,7 @@ public class RestApiServlet extends HttpServlet {
|
|||||||
|
|
||||||
private static <T> void configureCaching(HttpServletRequest req,
|
private static <T> void configureCaching(HttpServletRequest req,
|
||||||
HttpServletResponse res, RestResource rsrc, CacheControl c) {
|
HttpServletResponse res, RestResource rsrc, CacheControl c) {
|
||||||
if ("GET".equals(req.getMethod())) {
|
if (isGetOrHead(req)) {
|
||||||
switch (c.getType()) {
|
switch (c.getType()) {
|
||||||
case NONE:
|
case NONE:
|
||||||
default:
|
default:
|
||||||
@@ -716,12 +716,14 @@ public class RestApiServlet extends HttpServlet {
|
|||||||
res.setHeader("Content-Length", Long.toString(len));
|
res.setHeader("Content-Length", Long.toString(len));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!"HEAD".equals(req.getMethod())) {
|
||||||
OutputStream dst = res.getOutputStream();
|
OutputStream dst = res.getOutputStream();
|
||||||
try {
|
try {
|
||||||
bin.writeTo(dst);
|
bin.writeTo(dst);
|
||||||
} finally {
|
} finally {
|
||||||
dst.close();
|
dst.close();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} finally {
|
} finally {
|
||||||
appResult.close();
|
appResult.close();
|
||||||
}
|
}
|
||||||
@@ -787,6 +789,8 @@ public class RestApiServlet extends HttpServlet {
|
|||||||
// is chosen, look for the projection based upon GET as the method as
|
// is chosen, look for the projection based upon GET as the method as
|
||||||
// the client thinks it is a nested collection.
|
// the client thinks it is a nested collection.
|
||||||
method = "GET";
|
method = "GET";
|
||||||
|
} else if ("HEAD".equals(method)) {
|
||||||
|
method = "GET";
|
||||||
}
|
}
|
||||||
|
|
||||||
List<String> p = splitProjection(projection);
|
List<String> p = splitProjection(projection);
|
||||||
@@ -879,9 +883,12 @@ public class RestApiServlet extends HttpServlet {
|
|||||||
user.setAccessPath(AccessPath.REST_API);
|
user.setAccessPath(AccessPath.REST_API);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static boolean isGetOrHead(HttpServletRequest req) {
|
||||||
|
return "GET".equals(req.getMethod()) || "HEAD".equals(req.getMethod());
|
||||||
|
}
|
||||||
|
|
||||||
private static boolean isStateChange(HttpServletRequest req) {
|
private static boolean isStateChange(HttpServletRequest req) {
|
||||||
String method = req.getMethod();
|
return !isGetOrHead(req);
|
||||||
return !("GET".equals(method) || "HEAD".equals(method));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkRequiresCapability(ViewData viewData) throws AuthException {
|
private void checkRequiresCapability(ViewData viewData) throws AuthException {
|
||||||
@@ -918,7 +925,7 @@ public class RestApiServlet extends HttpServlet {
|
|||||||
|
|
||||||
static void replyText(@Nullable HttpServletRequest req,
|
static void replyText(@Nullable HttpServletRequest req,
|
||||||
HttpServletResponse res, String text) throws IOException {
|
HttpServletResponse res, String text) throws IOException {
|
||||||
if ((req == null || "GET".equals(req.getMethod())) && isMaybeHTML(text)) {
|
if ((req == null || isGetOrHead(req)) && isMaybeHTML(text)) {
|
||||||
replyJson(req, res, ImmutableMultimap.of("pp", "0"), new JsonPrimitive(text));
|
replyJson(req, res, ImmutableMultimap.of("pp", "0"), new JsonPrimitive(text));
|
||||||
} else {
|
} else {
|
||||||
if (!text.endsWith("\n")) {
|
if (!text.endsWith("\n")) {
|
||||||
|
Reference in New Issue
Block a user