mirror of
https://github.com/google/nomulus.git
synced 2025-06-28 23:33:36 +02:00
Reduce duplicate code in the servlets
Currently, all 4 servlets (backend, frontend, pubapi, tools) have duplicates of the same exact code. That's an anti-pattern! Created a ServletBase they can all extend which has the duplicate code. As a bonus, the tools servlet now runs the metric reporter, meaning tool related metrics will now be reported! ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=219792176
This commit is contained in:
parent
545b68ad9a
commit
3f6585fccc
10 changed files with 31 additions and 183 deletions
|
@ -20,6 +20,7 @@ java_library(
|
|||
"//java/google/registry/keyring/kms",
|
||||
"//java/google/registry/loadtest",
|
||||
"//java/google/registry/mapreduce",
|
||||
"//java/google/registry/module",
|
||||
"//java/google/registry/monitoring/whitebox",
|
||||
"//java/google/registry/request",
|
||||
"//java/google/registry/request:modules",
|
||||
|
@ -30,6 +31,7 @@ java_library(
|
|||
"@com_google_dagger",
|
||||
"@com_google_flogger",
|
||||
"@com_google_flogger_system_backend",
|
||||
"@com_google_monitoring_client_metrics",
|
||||
"@javax_inject",
|
||||
"@javax_servlet_api",
|
||||
"@joda_time",
|
||||
|
|
|
@ -14,7 +14,9 @@
|
|||
|
||||
package google.registry.module.tools;
|
||||
|
||||
import com.google.monitoring.metrics.MetricReporter;
|
||||
import dagger.Component;
|
||||
import dagger.Lazy;
|
||||
import google.registry.config.CredentialModule;
|
||||
import google.registry.config.RegistryConfig.ConfigModule;
|
||||
import google.registry.export.DriveModule;
|
||||
|
@ -29,6 +31,7 @@ import google.registry.keyring.api.DummyKeyringModule;
|
|||
import google.registry.keyring.api.KeyModule;
|
||||
import google.registry.keyring.kms.KmsModule;
|
||||
import google.registry.module.tools.ToolsRequestComponent.ToolsRequestComponentModule;
|
||||
import google.registry.monitoring.whitebox.StackdriverModule;
|
||||
import google.registry.request.Modules.DatastoreServiceModule;
|
||||
import google.registry.request.Modules.Jackson2Module;
|
||||
import google.registry.request.Modules.NetHttpTransportModule;
|
||||
|
@ -62,6 +65,7 @@ import javax.inject.Singleton;
|
|||
KmsModule.class,
|
||||
NetHttpTransportModule.class,
|
||||
ServerTridProviderModule.class,
|
||||
StackdriverModule.class,
|
||||
SystemClockModule.class,
|
||||
SystemSleeperModule.class,
|
||||
ToolsRequestComponentModule.class,
|
||||
|
@ -70,4 +74,6 @@ import javax.inject.Singleton;
|
|||
})
|
||||
interface ToolsComponent {
|
||||
ToolsRequestHandler requestHandler();
|
||||
|
||||
Lazy<MetricReporter> metricReporter();
|
||||
}
|
||||
|
|
|
@ -14,39 +14,18 @@
|
|||
|
||||
package google.registry.module.tools;
|
||||
|
||||
import com.google.common.flogger.FluentLogger;
|
||||
import google.registry.util.SystemClock;
|
||||
import java.io.IOException;
|
||||
import java.security.Security;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
||||
import org.joda.time.DateTime;
|
||||
import com.google.monitoring.metrics.MetricReporter;
|
||||
import dagger.Lazy;
|
||||
import google.registry.module.ServletBase;
|
||||
|
||||
/** Servlet that should handle all requests to our "tools" App Engine module. */
|
||||
public final class ToolsServlet extends HttpServlet {
|
||||
public final class ToolsServlet extends ServletBase {
|
||||
|
||||
private static final ToolsComponent component = DaggerToolsComponent.create();
|
||||
private static final ToolsRequestHandler requestHandler = component.requestHandler();
|
||||
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
|
||||
private static final SystemClock clock = new SystemClock();
|
||||
private static final Lazy<MetricReporter> metricReporter = component.metricReporter();
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
Security.addProvider(new BouncyCastleProvider());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void service(HttpServletRequest req, HttpServletResponse rsp) throws IOException {
|
||||
logger.atInfo().log("Received tools request");
|
||||
DateTime startTime = clock.nowUtc();
|
||||
try {
|
||||
requestHandler.handleRequest(req, rsp);
|
||||
} finally {
|
||||
logger.atInfo().log(
|
||||
"Finished tools request. Latency: %.3fs",
|
||||
(clock.nowUtc().getMillis() - startTime.getMillis()) / 1000d);
|
||||
}
|
||||
public ToolsServlet() {
|
||||
super(requestHandler, metricReporter);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue