DRY out common functions between servlets
H2MigrationServlet and AutoAdjustCachesServlet
had two static methods in common candidate to be
DRY-ed out.
Introduce a new utility class HttpServletOps
that allows to centralise some common utility
methods to be reused across classes.
Change-Id: I1a74cf972ca31f9e1b2b2cccdf1c806e17f458bb
diff --git a/src/main/java/com/googlesource/gerrit/modules/cache/chroniclemap/AutoAdjustCachesServlet.java b/src/main/java/com/googlesource/gerrit/modules/cache/chroniclemap/AutoAdjustCachesServlet.java
index 47f0a4d..b217b0e 100644
--- a/src/main/java/com/googlesource/gerrit/modules/cache/chroniclemap/AutoAdjustCachesServlet.java
+++ b/src/main/java/com/googlesource/gerrit/modules/cache/chroniclemap/AutoAdjustCachesServlet.java
@@ -14,8 +14,8 @@
package com.googlesource.gerrit.modules.cache.chroniclemap;
-import static org.apache.http.HttpHeaders.ACCEPT;
-import static org.eclipse.jgit.util.HttpSupport.TEXT_PLAIN;
+import static com.googlesource.gerrit.modules.cache.chroniclemap.HttpServletOps.checkAcceptHeader;
+import static com.googlesource.gerrit.modules.cache.chroniclemap.HttpServletOps.setResponse;
import com.google.common.flogger.FluentLogger;
import com.google.gerrit.extensions.restapi.AuthException;
@@ -24,8 +24,6 @@
import com.google.inject.Provider;
import com.google.inject.Singleton;
import java.io.IOException;
-import java.io.PrintWriter;
-import java.util.Arrays;
import java.util.Optional;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
@@ -49,11 +47,7 @@
@Override
protected void doPut(HttpServletRequest req, HttpServletResponse rsp) throws IOException {
AutoAdjustCaches autoAdjustCachesEngine = autoAdjustCachesProvider.get();
- if (hasInvalidAcceptHeader(req)) {
- setResponse(
- rsp,
- HttpServletResponse.SC_BAD_REQUEST,
- "No advertised 'Accept' headers can be honoured. 'text/plain' should be provided in the request 'Accept' header.");
+ if (!checkAcceptHeader(req, rsp)) {
return;
}
@@ -82,17 +76,4 @@
return;
}
}
-
- private static void setResponse(HttpServletResponse httpResponse, int statusCode, String value)
- throws IOException {
- httpResponse.setContentType(TEXT_PLAIN);
- httpResponse.setStatus(statusCode);
- PrintWriter writer = httpResponse.getWriter();
- writer.print(value);
- }
-
- private static boolean hasInvalidAcceptHeader(HttpServletRequest req) {
- return req.getHeader(ACCEPT) != null
- && !Arrays.asList("text/plain", "text/*", "*/*").contains(req.getHeader(ACCEPT));
- }
}
diff --git a/src/main/java/com/googlesource/gerrit/modules/cache/chroniclemap/H2MigrationServlet.java b/src/main/java/com/googlesource/gerrit/modules/cache/chroniclemap/H2MigrationServlet.java
index 63cf659..f35f0ee 100644
--- a/src/main/java/com/googlesource/gerrit/modules/cache/chroniclemap/H2MigrationServlet.java
+++ b/src/main/java/com/googlesource/gerrit/modules/cache/chroniclemap/H2MigrationServlet.java
@@ -17,8 +17,8 @@
import static com.googlesource.gerrit.modules.cache.chroniclemap.H2CacheCommand.H2_SUFFIX;
import static com.googlesource.gerrit.modules.cache.chroniclemap.H2CacheCommand.getStats;
import static com.googlesource.gerrit.modules.cache.chroniclemap.H2CacheCommand.jdbcUrl;
-import static org.apache.http.HttpHeaders.ACCEPT;
-import static org.eclipse.jgit.util.HttpSupport.TEXT_PLAIN;
+import static com.googlesource.gerrit.modules.cache.chroniclemap.HttpServletOps.checkAcceptHeader;
+import static com.googlesource.gerrit.modules.cache.chroniclemap.HttpServletOps.setResponse;
import com.google.common.flogger.FluentLogger;
import com.google.gerrit.entities.Account;
@@ -51,14 +51,12 @@
import com.google.inject.name.Named;
import java.io.File;
import java.io.IOException;
-import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
-import java.util.Arrays;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
@@ -138,11 +136,7 @@
@Override
protected void doPut(HttpServletRequest req, HttpServletResponse rsp) throws IOException {
- if (hasInvalidAcceptHeader(req)) {
- setResponse(
- rsp,
- HttpServletResponse.SC_BAD_REQUEST,
- "No advertised 'Accept' headers can be honoured. 'text/plain' should be provided in the request 'Accept' header.");
+ if (!checkAcceptHeader(req, rsp)) {
return;
}
@@ -341,19 +335,6 @@
return typeLiteral.getRawType().getSimpleName().equals("String");
}
- private void setResponse(HttpServletResponse httpResponse, int statusCode, String value)
- throws IOException {
- httpResponse.setContentType(TEXT_PLAIN);
- httpResponse.setStatus(statusCode);
- PrintWriter writer = httpResponse.getWriter();
- writer.print(value);
- }
-
- private boolean hasInvalidAcceptHeader(HttpServletRequest req) {
- return req.getHeader(ACCEPT) != null
- && !Arrays.asList("text/plain", "text/*", "*/*").contains(req.getHeader(ACCEPT));
- }
-
private static void copyExistingCacheSettingsToConfig(
Config outputConfig, ChronicleMapCacheConfig cacheConfig) {
String cacheName = cacheConfig.getConfigKey();
diff --git a/src/main/java/com/googlesource/gerrit/modules/cache/chroniclemap/HttpServletOps.java b/src/main/java/com/googlesource/gerrit/modules/cache/chroniclemap/HttpServletOps.java
new file mode 100644
index 0000000..dec758c
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/modules/cache/chroniclemap/HttpServletOps.java
@@ -0,0 +1,49 @@
+// Copyright (C) 2021 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.googlesource.gerrit.modules.cache.chroniclemap;
+
+import static org.apache.http.HttpHeaders.ACCEPT;
+import static org.eclipse.jgit.util.HttpSupport.TEXT_PLAIN;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.util.Arrays;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+class HttpServletOps {
+
+ static boolean checkAcceptHeader(HttpServletRequest req, HttpServletResponse rsp)
+ throws IOException {
+ if (req.getHeader(ACCEPT) != null
+ && !Arrays.asList("text/plain", "text/*", "*/*").contains(req.getHeader(ACCEPT))) {
+ setResponse(
+ rsp,
+ HttpServletResponse.SC_BAD_REQUEST,
+ "No advertised 'Accept' headers can be honoured. 'text/plain' should be provided in the request 'Accept' header.");
+ return false;
+ }
+
+ return true;
+ }
+
+ static void setResponse(HttpServletResponse httpResponse, int statusCode, String value)
+ throws IOException {
+ httpResponse.setContentType(TEXT_PLAIN);
+ httpResponse.setStatus(statusCode);
+ PrintWriter writer = httpResponse.getWriter();
+ writer.print(value);
+ }
+}