Skip to content

Commit

Permalink
Merge pull request #115 from GoogleCloudPlatform:AppEngineWebAppConte…
Browse files Browse the repository at this point in the history
…xt-startWebapp

PiperOrigin-RevId: 629254889
Change-Id: Icad130ead65d0e9f54f7adf93a6c82e6a55c8225
  • Loading branch information
gae-java-bot committed Apr 30, 2024
2 parents 6fb1d45 + 81dca45 commit b79b467
Showing 1 changed file with 50 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -234,64 +234,58 @@ public void doStart() throws Exception {
addEventListener(new TransactionCleanupListener(getClassLoader()));
}

@Override
protected void startContext() throws Exception {
ServletHandler servletHandler = getServletHandler();
getServletHandler().addListener(new ListenerHolder() {
@Override
public void doStart() throws Exception {
// This Listener doStart is called after the web.xml metadata has been resolved, so we can
// clean configuration here:
// - Removed deprecated filters and servlets
// - Ensure known runtime filters/servlets are instantiated from this classloader
// - Ensure known runtime mappings exist.
setListener(new EventListener() {});

TrimmedFilters trimmedFilters =
new TrimmedFilters(
servletHandler.getFilters(),
servletHandler.getFilterMappings(),
DEPRECATED_SERVLETS_FILTERS);
trimmedFilters.ensure(
"CloudSqlConnectionCleanupFilter", JdbcMySqlConnectionCleanupFilter.class, "/*");

TrimmedServlets trimmedServlets =
new TrimmedServlets(
servletHandler.getServlets(),
servletHandler.getServletMappings(),
DEPRECATED_SERVLETS_FILTERS);
trimmedServlets.ensure("_ah_warmup", WarmupServlet.class, "/_ah/warmup");
trimmedServlets.ensure(
"_ah_sessioncleanup", SessionCleanupServlet.class, "/_ah/sessioncleanup");
trimmedServlets.ensure(
"_ah_queue_deferred", DeferredTaskServlet.class, "/_ah/queue/__deferred__");
trimmedServlets.ensure("_ah_snapshot", SnapshotServlet.class, "/_ah/snapshot");
trimmedServlets.ensure("_ah_default", ResourceFileServlet.class, "/");
trimmedServlets.ensure("default", NamedDefaultServlet.class);
trimmedServlets.ensure("jsp", NamedJspServlet.class);

trimmedServlets.instantiateJettyServlets();
trimmedFilters.instantiateJettyFilters();
instantiateJettyListeners();

servletHandler.setFilters(trimmedFilters.getHolders());
servletHandler.setFilterMappings(trimmedFilters.getMappings());
servletHandler.setServlets(trimmedServlets.getHolders());
servletHandler.setServletMappings(trimmedServlets.getMappings());
servletHandler.setAllowDuplicateMappings(true);

// Protect deferred task queue with constraint
ConstraintSecurityHandler security = (ConstraintSecurityHandler) getSecurityHandler();
ConstraintMapping cm = new ConstraintMapping();
cm.setConstraint(
Constraint.from("deferred_queue", Constraint.Authorization.SPECIFIC_ROLE, "admin"));
cm.setPathSpec("/_ah/queue/__deferred__");
security.addConstraintMapping(cm);
}
});
@Override
protected void startWebapp() throws Exception {
// startWebapp is called after the web.xml metadata has been resolved, so we can
// clean configuration here:
// - Removed deprecated filters and servlets
// - Ensure known runtime filters/servlets are instantiated from this classloader
// - Ensure known runtime mappings exist.

ServletHandler servletHandler = getServletHandler();
TrimmedFilters trimmedFilters =
new TrimmedFilters(
servletHandler.getFilters(),
servletHandler.getFilterMappings(),
DEPRECATED_SERVLETS_FILTERS);
trimmedFilters.ensure(
"CloudSqlConnectionCleanupFilter", JdbcMySqlConnectionCleanupFilter.class, "/*");

TrimmedServlets trimmedServlets =
new TrimmedServlets(
servletHandler.getServlets(),
servletHandler.getServletMappings(),
DEPRECATED_SERVLETS_FILTERS);
trimmedServlets.ensure("_ah_warmup", WarmupServlet.class, "/_ah/warmup");
trimmedServlets.ensure(
"_ah_sessioncleanup", SessionCleanupServlet.class, "/_ah/sessioncleanup");
trimmedServlets.ensure(
"_ah_queue_deferred", DeferredTaskServlet.class, "/_ah/queue/__deferred__");
trimmedServlets.ensure("_ah_snapshot", SnapshotServlet.class, "/_ah/snapshot");
trimmedServlets.ensure("_ah_default", ResourceFileServlet.class, "/");
trimmedServlets.ensure("default", NamedDefaultServlet.class);
trimmedServlets.ensure("jsp", NamedJspServlet.class);

trimmedServlets.instantiateJettyServlets();
trimmedFilters.instantiateJettyFilters();
instantiateJettyListeners();

servletHandler.setFilters(trimmedFilters.getHolders());
servletHandler.setFilterMappings(trimmedFilters.getMappings());
servletHandler.setServlets(trimmedServlets.getHolders());
servletHandler.setServletMappings(trimmedServlets.getMappings());
servletHandler.setAllowDuplicateMappings(true);

// Protect deferred task queue with constraint
ConstraintSecurityHandler security = (ConstraintSecurityHandler) getSecurityHandler();
ConstraintMapping cm = new ConstraintMapping();
cm.setConstraint(
Constraint.from("deferred_queue", Constraint.Authorization.SPECIFIC_ROLE, "admin"));
cm.setPathSpec("/_ah/queue/__deferred__");
security.addConstraintMapping(cm);

// continue starting the webapp
super.startContext();
super.startWebapp();
}

@Override
Expand Down

0 comments on commit b79b467

Please sign in to comment.