From 36542549fa5712b235eb4e7d60e3de537e84416b Mon Sep 17 00:00:00 2001 From: Sebastien Deleuze Date: Fri, 29 Aug 2014 11:53:26 +0200 Subject: [PATCH] Add HTTP status to ServletRequestHandledEvent Issue: SPR-12119 --- .../support/ServletRequestHandledEvent.java | 43 ++++++++++++++++++- .../web/servlet/FrameworkServlet.java | 6 +-- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/web/context/support/ServletRequestHandledEvent.java b/spring-web/src/main/java/org/springframework/web/context/support/ServletRequestHandledEvent.java index a2eca47b11..c08405a7cc 100644 --- a/spring-web/src/main/java/org/springframework/web/context/support/ServletRequestHandledEvent.java +++ b/spring-web/src/main/java/org/springframework/web/context/support/ServletRequestHandledEvent.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,7 +28,7 @@ package org.springframework.web.context.support; @SuppressWarnings("serial") public class ServletRequestHandledEvent extends RequestHandledEvent { - /** URL that the triggered the request */ + /** URL that triggered the request */ private final String requestUrl; /** IP address that the request came from */ @@ -40,6 +40,9 @@ public class ServletRequestHandledEvent extends RequestHandledEvent { /** Name of the servlet that handled the request */ private final String servletName; + /** HTTP status code of the response */ + private final int statusCode; + /** * Create a new ServletRequestHandledEvent. @@ -62,6 +65,7 @@ public class ServletRequestHandledEvent extends RequestHandledEvent { this.clientAddress = clientAddress; this.method = method; this.servletName = servletName; + this.statusCode = -1; } /** @@ -86,6 +90,33 @@ public class ServletRequestHandledEvent extends RequestHandledEvent { this.clientAddress = clientAddress; this.method = method; this.servletName = servletName; + this.statusCode = -1; + } + + /** + * Create a new ServletRequestHandledEvent. + * @param source the component that published the event + * @param requestUrl the URL of the request + * @param clientAddress the IP address that the request came from + * @param method the HTTP method of the request (usually GET or POST) + * @param servletName the name of the servlet that handled the request + * @param sessionId the id of the HTTP session, if any + * @param userName the name of the user that was associated with the + * request, if any (usually the UserPrincipal) + * @param processingTimeMillis the processing time of the request in milliseconds + * @param failureCause the cause of failure, if any + * @param statusCode the HTTP status code of the response + */ + public ServletRequestHandledEvent(Object source, String requestUrl, + String clientAddress, String method, String servletName, String sessionId, + String userName, long processingTimeMillis, Throwable failureCause, int statusCode) { + + super(source, sessionId, userName, processingTimeMillis, failureCause); + this.requestUrl = requestUrl; + this.clientAddress = clientAddress; + this.method = method; + this.servletName = servletName; + this.statusCode = statusCode; } @@ -117,6 +148,14 @@ public class ServletRequestHandledEvent extends RequestHandledEvent { return this.servletName; } + /** + * Return the HTTP status code of the response or -1 if the status + * code is not available. + * @since 4.1 + */ + public int getStatusCode() { + return this.statusCode; + } @Override public String getShortDescription() { diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/FrameworkServlet.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/FrameworkServlet.java index abfbe4e5c5..68218c13f8 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/FrameworkServlet.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/FrameworkServlet.java @@ -993,7 +993,7 @@ public abstract class FrameworkServlet extends HttpServletBean implements Applic } } - publishRequestHandledEvent(request, startTime, failureCause); + publishRequestHandledEvent(request, response, startTime, failureCause); } } @@ -1054,7 +1054,7 @@ public abstract class FrameworkServlet extends HttpServletBean implements Applic } } - private void publishRequestHandledEvent(HttpServletRequest request, long startTime, Throwable failureCause) { + private void publishRequestHandledEvent(HttpServletRequest request, HttpServletResponse response, long startTime, Throwable failureCause) { if (this.publishEvents) { // Whether or not we succeeded, publish an event. long processingTime = System.currentTimeMillis() - startTime; @@ -1063,7 +1063,7 @@ public abstract class FrameworkServlet extends HttpServletBean implements Applic request.getRequestURI(), request.getRemoteAddr(), request.getMethod(), getServletConfig().getServletName(), WebUtils.getSessionId(request), getUsernameForRequest(request), - processingTime, failureCause)); + processingTime, failureCause, response.getStatus())); } } -- GitLab