mirror of
https://github.com/google/nomulus.git
synced 2025-05-15 00:47:11 +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
|
@ -27,6 +27,7 @@ java_library(
|
||||||
"//java/google/registry/keyring/kms",
|
"//java/google/registry/keyring/kms",
|
||||||
"//java/google/registry/mapreduce",
|
"//java/google/registry/mapreduce",
|
||||||
"//java/google/registry/model",
|
"//java/google/registry/model",
|
||||||
|
"//java/google/registry/module",
|
||||||
"//java/google/registry/monitoring/whitebox",
|
"//java/google/registry/monitoring/whitebox",
|
||||||
"//java/google/registry/rde",
|
"//java/google/registry/rde",
|
||||||
"//java/google/registry/rde/imports",
|
"//java/google/registry/rde/imports",
|
||||||
|
|
|
@ -14,66 +14,18 @@
|
||||||
|
|
||||||
package google.registry.module.backend;
|
package google.registry.module.backend;
|
||||||
|
|
||||||
import com.google.appengine.api.LifecycleManager;
|
|
||||||
import com.google.common.flogger.FluentLogger;
|
|
||||||
import com.google.monitoring.metrics.MetricReporter;
|
import com.google.monitoring.metrics.MetricReporter;
|
||||||
import dagger.Lazy;
|
import dagger.Lazy;
|
||||||
import google.registry.util.SystemClock;
|
import google.registry.module.ServletBase;
|
||||||
import java.io.IOException;
|
|
||||||
import java.security.Security;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
import java.util.concurrent.TimeoutException;
|
|
||||||
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;
|
|
||||||
|
|
||||||
/** Servlet that should handle all requests to our "backend" App Engine module. */
|
/** Servlet that should handle all requests to our "backend" App Engine module. */
|
||||||
public final class BackendServlet extends HttpServlet {
|
public final class BackendServlet extends ServletBase {
|
||||||
|
|
||||||
private static final BackendComponent component = DaggerBackendComponent.create();
|
private static final BackendComponent component = DaggerBackendComponent.create();
|
||||||
private static final BackendRequestHandler requestHandler = component.requestHandler();
|
private static final BackendRequestHandler requestHandler = component.requestHandler();
|
||||||
private static final Lazy<MetricReporter> metricReporter = component.metricReporter();
|
private static final Lazy<MetricReporter> metricReporter = component.metricReporter();
|
||||||
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
|
|
||||||
private static final SystemClock clock = new SystemClock();
|
|
||||||
|
|
||||||
@Override
|
public BackendServlet() {
|
||||||
public void init() {
|
super(requestHandler, metricReporter);
|
||||||
Security.addProvider(new BouncyCastleProvider());
|
|
||||||
|
|
||||||
// If metric reporter failed to instantiate for any reason (bad keyring, bad json credential,
|
|
||||||
// etc), we log the error but keep the main thread running. Also the shutdown hook will only be
|
|
||||||
// registered if metric reporter starts up correctly.
|
|
||||||
try {
|
|
||||||
metricReporter.get().startAsync().awaitRunning(10, TimeUnit.SECONDS);
|
|
||||||
logger.atInfo().log("Started up MetricReporter");
|
|
||||||
LifecycleManager.getInstance()
|
|
||||||
.setShutdownHook(
|
|
||||||
() -> {
|
|
||||||
try {
|
|
||||||
metricReporter.get().stopAsync().awaitTerminated(10, TimeUnit.SECONDS);
|
|
||||||
logger.atInfo().log("Shut down MetricReporter");
|
|
||||||
} catch (TimeoutException timeoutException) {
|
|
||||||
logger.atSevere().withCause(timeoutException).log(
|
|
||||||
"Failed to stop MetricReporter: %s", timeoutException);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} catch (Exception e) {
|
|
||||||
logger.atSevere().withCause(e).log("Failed to initialize MetricReporter.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void service(HttpServletRequest req, HttpServletResponse rsp) throws IOException {
|
|
||||||
logger.atInfo().log("Received backend request");
|
|
||||||
DateTime startTime = clock.nowUtc();
|
|
||||||
try {
|
|
||||||
requestHandler.handleRequest(req, rsp);
|
|
||||||
} finally {
|
|
||||||
logger.atInfo().log(
|
|
||||||
"Finished backend request. Latency: %.3fs",
|
|
||||||
(clock.nowUtc().getMillis() - startTime.getMillis()) / 1000d);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,6 +14,7 @@ java_library(
|
||||||
"//java/google/registry/keyring",
|
"//java/google/registry/keyring",
|
||||||
"//java/google/registry/keyring/api",
|
"//java/google/registry/keyring/api",
|
||||||
"//java/google/registry/keyring/kms",
|
"//java/google/registry/keyring/kms",
|
||||||
|
"//java/google/registry/module",
|
||||||
"//java/google/registry/monitoring/whitebox",
|
"//java/google/registry/monitoring/whitebox",
|
||||||
"//java/google/registry/request",
|
"//java/google/registry/request",
|
||||||
"//java/google/registry/request:modules",
|
"//java/google/registry/request:modules",
|
||||||
|
|
|
@ -14,65 +14,18 @@
|
||||||
|
|
||||||
package google.registry.module.frontend;
|
package google.registry.module.frontend;
|
||||||
|
|
||||||
import com.google.appengine.api.LifecycleManager;
|
|
||||||
import com.google.common.flogger.FluentLogger;
|
|
||||||
import com.google.monitoring.metrics.MetricReporter;
|
import com.google.monitoring.metrics.MetricReporter;
|
||||||
import dagger.Lazy;
|
import dagger.Lazy;
|
||||||
import google.registry.util.SystemClock;
|
import google.registry.module.ServletBase;
|
||||||
import java.io.IOException;
|
|
||||||
import java.security.Security;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
import java.util.concurrent.TimeoutException;
|
|
||||||
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;
|
|
||||||
|
|
||||||
/** Servlet that should handle all requests to our "default" App Engine module. */
|
/** Servlet that should handle all requests to our "default" App Engine module. */
|
||||||
public final class FrontendServlet extends HttpServlet {
|
public final class FrontendServlet extends ServletBase {
|
||||||
|
|
||||||
private static final FrontendComponent component = DaggerFrontendComponent.create();
|
private static final FrontendComponent component = DaggerFrontendComponent.create();
|
||||||
private static final FrontendRequestHandler requestHandler = component.requestHandler();
|
private static final FrontendRequestHandler requestHandler = component.requestHandler();
|
||||||
private static final Lazy<MetricReporter> metricReporter = component.metricReporter();
|
private static final Lazy<MetricReporter> metricReporter = component.metricReporter();
|
||||||
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
|
|
||||||
private static final SystemClock clock = new SystemClock();
|
|
||||||
|
|
||||||
@Override
|
public FrontendServlet() {
|
||||||
public void init() {
|
super(requestHandler, metricReporter);
|
||||||
Security.addProvider(new BouncyCastleProvider());
|
|
||||||
|
|
||||||
// If metric reporter failed to instantiate for any reason (bad keyring, bad json credential,
|
|
||||||
// etc), we log the error but keep the main thread running. Also the shutdown hook will only be
|
|
||||||
// registered if metric reporter starts up correctly.
|
|
||||||
try {
|
|
||||||
metricReporter.get().startAsync().awaitRunning(10, TimeUnit.SECONDS);
|
|
||||||
logger.atInfo().log("Started up MetricReporter");
|
|
||||||
LifecycleManager.getInstance()
|
|
||||||
.setShutdownHook(
|
|
||||||
() -> {
|
|
||||||
try {
|
|
||||||
metricReporter.get().stopAsync().awaitTerminated(10, TimeUnit.SECONDS);
|
|
||||||
logger.atInfo().log("Shut down MetricReporter");
|
|
||||||
} catch (TimeoutException e) {
|
|
||||||
logger.atSevere().withCause(e).log("Failed to stop MetricReporter.");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} catch (Exception e) {
|
|
||||||
logger.atSevere().withCause(e).log("Failed to initialize MetricReporter.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void service(HttpServletRequest req, HttpServletResponse rsp) throws IOException {
|
|
||||||
logger.atInfo().log("Received frontend request");
|
|
||||||
DateTime startTime = clock.nowUtc();
|
|
||||||
try {
|
|
||||||
requestHandler.handleRequest(req, rsp);
|
|
||||||
} finally {
|
|
||||||
logger.atInfo().log(
|
|
||||||
"Finished frontend request. Latency: %.3fs",
|
|
||||||
(clock.nowUtc().getMillis() - startTime.getMillis()) / 1000d);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,6 +14,7 @@ java_library(
|
||||||
"//java/google/registry/keyring",
|
"//java/google/registry/keyring",
|
||||||
"//java/google/registry/keyring/api",
|
"//java/google/registry/keyring/api",
|
||||||
"//java/google/registry/keyring/kms",
|
"//java/google/registry/keyring/kms",
|
||||||
|
"//java/google/registry/module",
|
||||||
"//java/google/registry/monitoring/whitebox",
|
"//java/google/registry/monitoring/whitebox",
|
||||||
"//java/google/registry/rdap",
|
"//java/google/registry/rdap",
|
||||||
"//java/google/registry/request",
|
"//java/google/registry/request",
|
||||||
|
|
|
@ -47,12 +47,12 @@ import javax.inject.Singleton;
|
||||||
CredentialModule.class,
|
CredentialModule.class,
|
||||||
CustomLogicFactoryModule.class,
|
CustomLogicFactoryModule.class,
|
||||||
DummyKeyringModule.class,
|
DummyKeyringModule.class,
|
||||||
PubApiRequestComponentModule.class,
|
|
||||||
Jackson2Module.class,
|
Jackson2Module.class,
|
||||||
KeyModule.class,
|
KeyModule.class,
|
||||||
KeyringModule.class,
|
KeyringModule.class,
|
||||||
KmsModule.class,
|
KmsModule.class,
|
||||||
NetHttpTransportModule.class,
|
NetHttpTransportModule.class,
|
||||||
|
PubApiRequestComponentModule.class,
|
||||||
ServerTridProviderModule.class,
|
ServerTridProviderModule.class,
|
||||||
StackdriverModule.class,
|
StackdriverModule.class,
|
||||||
SystemClockModule.class,
|
SystemClockModule.class,
|
||||||
|
|
|
@ -14,65 +14,18 @@
|
||||||
|
|
||||||
package google.registry.module.pubapi;
|
package google.registry.module.pubapi;
|
||||||
|
|
||||||
import com.google.appengine.api.LifecycleManager;
|
|
||||||
import com.google.common.flogger.FluentLogger;
|
|
||||||
import com.google.monitoring.metrics.MetricReporter;
|
import com.google.monitoring.metrics.MetricReporter;
|
||||||
import dagger.Lazy;
|
import dagger.Lazy;
|
||||||
import google.registry.util.SystemClock;
|
import google.registry.module.ServletBase;
|
||||||
import java.io.IOException;
|
|
||||||
import java.security.Security;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
import java.util.concurrent.TimeoutException;
|
|
||||||
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;
|
|
||||||
|
|
||||||
/** Servlet that should handle all requests to our "default" App Engine module. */
|
/** Servlet that should handle all requests to our "default" App Engine module. */
|
||||||
public final class PubApiServlet extends HttpServlet {
|
public final class PubApiServlet extends ServletBase {
|
||||||
|
|
||||||
private static final PubApiComponent component = DaggerPubApiComponent.create();
|
private static final PubApiComponent component = DaggerPubApiComponent.create();
|
||||||
private static final PubApiRequestHandler requestHandler = component.requestHandler();
|
private static final PubApiRequestHandler requestHandler = component.requestHandler();
|
||||||
private static final Lazy<MetricReporter> metricReporter = component.metricReporter();
|
private static final Lazy<MetricReporter> metricReporter = component.metricReporter();
|
||||||
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
|
|
||||||
private static final SystemClock clock = new SystemClock();
|
|
||||||
|
|
||||||
@Override
|
public PubApiServlet() {
|
||||||
public void init() {
|
super(requestHandler, metricReporter);
|
||||||
Security.addProvider(new BouncyCastleProvider());
|
|
||||||
|
|
||||||
// If metric reporter failed to instantiate for any reason (bad keyring, bad json credential,
|
|
||||||
// etc), we log the error but keep the main thread running. Also the shutdown hook will only be
|
|
||||||
// registered if metric reporter starts up correctly.
|
|
||||||
try {
|
|
||||||
metricReporter.get().startAsync().awaitRunning(10, TimeUnit.SECONDS);
|
|
||||||
logger.atInfo().log("Started up MetricReporter");
|
|
||||||
LifecycleManager.getInstance()
|
|
||||||
.setShutdownHook(
|
|
||||||
() -> {
|
|
||||||
try {
|
|
||||||
metricReporter.get().stopAsync().awaitTerminated(10, TimeUnit.SECONDS);
|
|
||||||
logger.atInfo().log("Shut down MetricReporter");
|
|
||||||
} catch (TimeoutException e) {
|
|
||||||
logger.atSevere().withCause(e).log("Failed to stop MetricReporter.");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} catch (Exception e) {
|
|
||||||
logger.atSevere().withCause(e).log("Failed to initialize MetricReporter.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void service(HttpServletRequest req, HttpServletResponse rsp) throws IOException {
|
|
||||||
logger.atInfo().log("Received pubapi request");
|
|
||||||
DateTime startTime = clock.nowUtc();
|
|
||||||
try {
|
|
||||||
requestHandler.handleRequest(req, rsp);
|
|
||||||
} finally {
|
|
||||||
logger.atInfo().log(
|
|
||||||
"Finished pubapi request. Latency: %.3fs",
|
|
||||||
(clock.nowUtc().getMillis() - startTime.getMillis()) / 1000d);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,6 +20,7 @@ java_library(
|
||||||
"//java/google/registry/keyring/kms",
|
"//java/google/registry/keyring/kms",
|
||||||
"//java/google/registry/loadtest",
|
"//java/google/registry/loadtest",
|
||||||
"//java/google/registry/mapreduce",
|
"//java/google/registry/mapreduce",
|
||||||
|
"//java/google/registry/module",
|
||||||
"//java/google/registry/monitoring/whitebox",
|
"//java/google/registry/monitoring/whitebox",
|
||||||
"//java/google/registry/request",
|
"//java/google/registry/request",
|
||||||
"//java/google/registry/request:modules",
|
"//java/google/registry/request:modules",
|
||||||
|
@ -30,6 +31,7 @@ java_library(
|
||||||
"@com_google_dagger",
|
"@com_google_dagger",
|
||||||
"@com_google_flogger",
|
"@com_google_flogger",
|
||||||
"@com_google_flogger_system_backend",
|
"@com_google_flogger_system_backend",
|
||||||
|
"@com_google_monitoring_client_metrics",
|
||||||
"@javax_inject",
|
"@javax_inject",
|
||||||
"@javax_servlet_api",
|
"@javax_servlet_api",
|
||||||
"@joda_time",
|
"@joda_time",
|
||||||
|
|
|
@ -14,7 +14,9 @@
|
||||||
|
|
||||||
package google.registry.module.tools;
|
package google.registry.module.tools;
|
||||||
|
|
||||||
|
import com.google.monitoring.metrics.MetricReporter;
|
||||||
import dagger.Component;
|
import dagger.Component;
|
||||||
|
import dagger.Lazy;
|
||||||
import google.registry.config.CredentialModule;
|
import google.registry.config.CredentialModule;
|
||||||
import google.registry.config.RegistryConfig.ConfigModule;
|
import google.registry.config.RegistryConfig.ConfigModule;
|
||||||
import google.registry.export.DriveModule;
|
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.api.KeyModule;
|
||||||
import google.registry.keyring.kms.KmsModule;
|
import google.registry.keyring.kms.KmsModule;
|
||||||
import google.registry.module.tools.ToolsRequestComponent.ToolsRequestComponentModule;
|
import google.registry.module.tools.ToolsRequestComponent.ToolsRequestComponentModule;
|
||||||
|
import google.registry.monitoring.whitebox.StackdriverModule;
|
||||||
import google.registry.request.Modules.DatastoreServiceModule;
|
import google.registry.request.Modules.DatastoreServiceModule;
|
||||||
import google.registry.request.Modules.Jackson2Module;
|
import google.registry.request.Modules.Jackson2Module;
|
||||||
import google.registry.request.Modules.NetHttpTransportModule;
|
import google.registry.request.Modules.NetHttpTransportModule;
|
||||||
|
@ -62,6 +65,7 @@ import javax.inject.Singleton;
|
||||||
KmsModule.class,
|
KmsModule.class,
|
||||||
NetHttpTransportModule.class,
|
NetHttpTransportModule.class,
|
||||||
ServerTridProviderModule.class,
|
ServerTridProviderModule.class,
|
||||||
|
StackdriverModule.class,
|
||||||
SystemClockModule.class,
|
SystemClockModule.class,
|
||||||
SystemSleeperModule.class,
|
SystemSleeperModule.class,
|
||||||
ToolsRequestComponentModule.class,
|
ToolsRequestComponentModule.class,
|
||||||
|
@ -70,4 +74,6 @@ import javax.inject.Singleton;
|
||||||
})
|
})
|
||||||
interface ToolsComponent {
|
interface ToolsComponent {
|
||||||
ToolsRequestHandler requestHandler();
|
ToolsRequestHandler requestHandler();
|
||||||
|
|
||||||
|
Lazy<MetricReporter> metricReporter();
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,39 +14,18 @@
|
||||||
|
|
||||||
package google.registry.module.tools;
|
package google.registry.module.tools;
|
||||||
|
|
||||||
import com.google.common.flogger.FluentLogger;
|
import com.google.monitoring.metrics.MetricReporter;
|
||||||
import google.registry.util.SystemClock;
|
import dagger.Lazy;
|
||||||
import java.io.IOException;
|
import google.registry.module.ServletBase;
|
||||||
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;
|
|
||||||
|
|
||||||
/** Servlet that should handle all requests to our "tools" App Engine module. */
|
/** 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 ToolsComponent component = DaggerToolsComponent.create();
|
||||||
private static final ToolsRequestHandler requestHandler = component.requestHandler();
|
private static final ToolsRequestHandler requestHandler = component.requestHandler();
|
||||||
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
|
private static final Lazy<MetricReporter> metricReporter = component.metricReporter();
|
||||||
private static final SystemClock clock = new SystemClock();
|
|
||||||
|
|
||||||
@Override
|
public ToolsServlet() {
|
||||||
public void init() {
|
super(requestHandler, metricReporter);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue