From 6f2370649a27b88f7f3bff98bd7aa9056cae0522 Mon Sep 17 00:00:00 2001 From: Sebastien Deleuze Date: Fri, 29 Aug 2014 11:53:26 +0200 Subject: [PATCH] Include HTTP status code in 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 a2eca47b119f..54e3bf190554 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 @@ @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 ServletRequestHandledEvent(Object source, String requestUrl, this.clientAddress = clientAddress; this.method = method; this.servletName = servletName; + this.statusCode = -1; } /** @@ -86,6 +90,33 @@ public ServletRequestHandledEvent(Object source, String requestUrl, 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 String getServletName() { return this.servletName; } + /** + * Return the HTTP status code of the response. + * If the status code is not defined, return -1. + * @since 4.1 + */ + public int getStatusCode() { + return 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 abfbe4e5c5f2..68218c13f8f5 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 @@ protected final void processRequest(HttpServletRequest request, HttpServletRespo } } - publishRequestHandledEvent(request, startTime, failureCause); + publishRequestHandledEvent(request, response, startTime, failureCause); } } @@ -1054,7 +1054,7 @@ private void resetContextHolders(HttpServletRequest request, } } - 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 @@ private void publishRequestHandledEvent(HttpServletRequest request, long startTi request.getRequestURI(), request.getRemoteAddr(), request.getMethod(), getServletConfig().getServletName(), WebUtils.getSessionId(request), getUsernameForRequest(request), - processingTime, failureCause)); + processingTime, failureCause, response.getStatus())); } }