Skip to content

Commit

Permalink
Fixes #4467 - Add logging to HttpWebApplicationServer for debugging p…
Browse files Browse the repository at this point in the history
…urposes (#4481)
  • Loading branch information
mnriem authored Jan 7, 2025
1 parent 2936183 commit 793c12c
Showing 1 changed file with 37 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import java.lang.System.Logger;
import static java.lang.System.Logger.Level.DEBUG;
import static java.lang.System.Logger.Level.ERROR;
import static java.lang.System.Logger.Level.TRACE;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

Expand Down Expand Up @@ -89,6 +90,9 @@ public HttpWebApplicationServer() {
* @param contextPath the context path.
*/
public void addMapping(String servletContextName, String contextPath) {
if (LOGGER.isLoggable(TRACE)) {
LOGGER.log(TRACE, "Adding context path: %s for: %s", contextPath, servletContextName);
}
for (WebApplication webApp : webApplications.values()) {
if (webApp.getServletContextName().equals(servletContextName)) {
requestMapper.addMapping(webApp, contextPath);
Expand All @@ -99,7 +103,9 @@ public void addMapping(String servletContextName, String contextPath) {

@Override
public void addWebApplication(WebApplication webApplication) {
LOGGER.log(DEBUG, () -> "Adding web application with context path: " + webApplication.getContextPath());
if (LOGGER.isLoggable(DEBUG)) {
LOGGER.log(DEBUG, () -> "Adding web application with context path: " + webApplication.getContextPath());
}
webApplications.put(webApplication.getContextPath(), webApplication);
requestMapper.addMapping(webApplication, webApplication.getContextPath());
}
Expand All @@ -111,17 +117,24 @@ public WebApplicationServerRequestMapper getRequestMapper() {

@Override
public void initialize() {
LOGGER.log(DEBUG, "Starting initialization of {0} web application(s)", webApplications.size());
if (LOGGER.isLoggable(DEBUG)) {
LOGGER.log(DEBUG, "Starting initialization of {0} web application(s)", webApplications.size());
}
for (WebApplication webApp : webApplications.values()) {
ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(webApp.getClassLoader());
if (LOGGER.isLoggable(TRACE)) {
LOGGER.log(TRACE, "Initializing web application at: %s", webApp.getContextPath());
}
webApp.initialize();
} finally {
Thread.currentThread().setContextClassLoader(oldClassLoader);
}
}
LOGGER.log(DEBUG, "Finished initialization of {0} web application(s)", webApplications.size());
if (LOGGER.isLoggable(DEBUG)) {
LOGGER.log(DEBUG, "Finished initialization of {0} web application(s)", webApplications.size());
}
}

@Override
Expand All @@ -140,6 +153,11 @@ public HttpServerProcessorEndState process(HttpServerRequest request, HttpServer
} catch (Throwable t) {
LOGGER.log(ERROR, "An error occurred while processing the request", t);
}

if (LOGGER.isLoggable(TRACE)) {
LOGGER.log(TRACE, "Processor end state: %s", state.name());
}

return state;
}

Expand All @@ -154,13 +172,24 @@ public HttpServerProcessorEndState process(HttpServerRequest request, HttpServer
@Override
public void service(WebApplicationRequest request, WebApplicationResponse response) throws IOException, ServletException {
String requestUri = request.getRequestURI();

if (requestUri == null) {
if (LOGGER.isLoggable(TRACE)) {
LOGGER.log(TRACE, "Request URI is invalid");
}
response.sendError(500);
return;
}

if (LOGGER.isLoggable(TRACE)) {
LOGGER.log(TRACE, "Request URI: %s", requestUri);
}

WebApplication webApplication = requestMapper.findMapping(requestUri);
if (webApplication == null) {
if (LOGGER.isLoggable(TRACE)) {
LOGGER.log(TRACE, "No web application found for request URI: %s", requestUri);
}
response.sendError(404);
return;
}
Expand All @@ -174,6 +203,11 @@ public void service(WebApplicationRequest request, WebApplicationResponse respon
request.setWebApplication(webApplication);
response.setWebApplication(webApplication);

if (LOGGER.isLoggable(TRACE)) {
LOGGER.log(TRACE, "Context Path: %s", contextPath);
LOGGER.log(TRACE, "Servlet Path: %s", request.getServletPath());
}

webApplication.service(request, response);

// Make sure the request is fully read wrt parameters (if any still)
Expand Down

0 comments on commit 793c12c

Please sign in to comment.