RestResponse: Use HTTP status constants from HttpServletResponse

Use the HttpServletResponse constants, consistently with RestApiServlet,
instead of the ones from Apache's HttpStatus.

Also make RestApiServlet.SC_UNPROCESSABLE_ENTITY public so that can also
be referenced in RestResponse. (Note that HttpServletResponse doesn't
have this constant for some reason, but HttpStatus does). Also remove the
TODO in RestApiServlet; it's been there for years and it's not likely to
get added in the upstream HttpServletResponse soon.

Change-Id: I51e985892e192358acee951ffdb7c04ef87c34a9
This commit is contained in:
David Pursehouse
2019-09-27 16:13:19 +09:00
parent 432a4c255c
commit 9d108cde06
2 changed files with 23 additions and 15 deletions

View File

@@ -17,13 +17,23 @@ package com.google.gerrit.acceptance;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;
import static com.google.gerrit.httpd.restapi.RestApiServlet.JSON_MAGIC;
import static com.google.gerrit.httpd.restapi.RestApiServlet.SC_UNPROCESSABLE_ENTITY;
import static java.nio.charset.StandardCharsets.UTF_8;
import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST;
import static javax.servlet.http.HttpServletResponse.SC_CONFLICT;
import static javax.servlet.http.HttpServletResponse.SC_CREATED;
import static javax.servlet.http.HttpServletResponse.SC_FORBIDDEN;
import static javax.servlet.http.HttpServletResponse.SC_METHOD_NOT_ALLOWED;
import static javax.servlet.http.HttpServletResponse.SC_MOVED_TEMPORARILY;
import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND;
import static javax.servlet.http.HttpServletResponse.SC_NO_CONTENT;
import static javax.servlet.http.HttpServletResponse.SC_OK;
import static javax.servlet.http.HttpServletResponse.SC_PRECONDITION_FAILED;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URI;
import org.apache.http.HttpStatus;
public class RestResponse extends HttpResponse {
@@ -47,47 +57,47 @@ public class RestResponse extends HttpResponse {
}
public void assertOK() throws Exception {
assertStatus(HttpStatus.SC_OK);
assertStatus(SC_OK);
}
public void assertNotFound() throws Exception {
assertStatus(HttpStatus.SC_NOT_FOUND);
assertStatus(SC_NOT_FOUND);
}
public void assertConflict() throws Exception {
assertStatus(HttpStatus.SC_CONFLICT);
assertStatus(SC_CONFLICT);
}
public void assertForbidden() throws Exception {
assertStatus(HttpStatus.SC_FORBIDDEN);
assertStatus(SC_FORBIDDEN);
}
public void assertNoContent() throws Exception {
assertStatus(HttpStatus.SC_NO_CONTENT);
assertStatus(SC_NO_CONTENT);
}
public void assertBadRequest() throws Exception {
assertStatus(HttpStatus.SC_BAD_REQUEST);
assertStatus(SC_BAD_REQUEST);
}
public void assertUnprocessableEntity() throws Exception {
assertStatus(HttpStatus.SC_UNPROCESSABLE_ENTITY);
assertStatus(SC_UNPROCESSABLE_ENTITY);
}
public void assertMethodNotAllowed() throws Exception {
assertStatus(HttpStatus.SC_METHOD_NOT_ALLOWED);
assertStatus(SC_METHOD_NOT_ALLOWED);
}
public void assertCreated() throws Exception {
assertStatus(HttpStatus.SC_CREATED);
assertStatus(SC_CREATED);
}
public void assertPreconditionFailed() throws Exception {
assertStatus(HttpStatus.SC_PRECONDITION_FAILED);
assertStatus(SC_PRECONDITION_FAILED);
}
public void assertTemporaryRedirect(String path) throws Exception {
assertStatus(HttpStatus.SC_MOVED_TEMPORARILY);
assertStatus(SC_MOVED_TEMPORARILY);
assertThat(URI.create(getHeader("Location")).getPath()).isEqualTo(path);
}
}

View File

@@ -195,9 +195,6 @@ public class RestApiServlet extends HttpServlet {
@VisibleForTesting public static final String X_GERRIT_TRACE = "X-Gerrit-Trace";
// HTTP 422 Unprocessable Entity.
// TODO: Remove when HttpServletResponse.SC_UNPROCESSABLE_ENTITY is available
private static final int SC_UNPROCESSABLE_ENTITY = 422;
private static final String X_REQUESTED_WITH = "X-Requested-With";
private static final String X_GERRIT_AUTH = "X-Gerrit-Auth";
static final ImmutableSet<String> ALLOWED_CORS_METHODS =
@@ -210,6 +207,7 @@ public class RestApiServlet extends HttpServlet {
public static final String XD_AUTHORIZATION = "access_token";
public static final String XD_CONTENT_TYPE = "$ct";
public static final String XD_METHOD = "$m";
public static final int SC_UNPROCESSABLE_ENTITY = 422;
public static final int SC_TOO_MANY_REQUESTS = 429;
private static final int HEAP_EST_SIZE = 10 * 8 * 1024; // Presize 10 blocks.