001/* 002 * Copyright 2002-2018 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.web.context.support; 018 019import org.springframework.lang.Nullable; 020 021/** 022 * Servlet-specific subclass of RequestHandledEvent, 023 * adding servlet-specific context information. 024 * 025 * @author Juergen Hoeller 026 * @since 2.0 027 * @see org.springframework.web.servlet.FrameworkServlet 028 * @see org.springframework.context.ApplicationContext#publishEvent 029 */ 030@SuppressWarnings("serial") 031public class ServletRequestHandledEvent extends RequestHandledEvent { 032 033 /** URL that triggered the request. */ 034 private final String requestUrl; 035 036 /** IP address that the request came from. */ 037 private final String clientAddress; 038 039 /** Usually GET or POST. */ 040 private final String method; 041 042 /** Name of the servlet that handled the request. */ 043 private final String servletName; 044 045 /** HTTP status code of the response. */ 046 private final int statusCode; 047 048 049 /** 050 * Create a new ServletRequestHandledEvent. 051 * @param source the component that published the event 052 * @param requestUrl the URL of the request 053 * @param clientAddress the IP address that the request came from 054 * @param method the HTTP method of the request (usually GET or POST) 055 * @param servletName the name of the servlet that handled the request 056 * @param sessionId the id of the HTTP session, if any 057 * @param userName the name of the user that was associated with the 058 * request, if any (usually the UserPrincipal) 059 * @param processingTimeMillis the processing time of the request in milliseconds 060 */ 061 public ServletRequestHandledEvent(Object source, String requestUrl, 062 String clientAddress, String method, String servletName, 063 @Nullable String sessionId, @Nullable String userName, long processingTimeMillis) { 064 065 super(source, sessionId, userName, processingTimeMillis); 066 this.requestUrl = requestUrl; 067 this.clientAddress = clientAddress; 068 this.method = method; 069 this.servletName = servletName; 070 this.statusCode = -1; 071 } 072 073 /** 074 * Create a new ServletRequestHandledEvent. 075 * @param source the component that published the event 076 * @param requestUrl the URL of the request 077 * @param clientAddress the IP address that the request came from 078 * @param method the HTTP method of the request (usually GET or POST) 079 * @param servletName the name of the servlet that handled the request 080 * @param sessionId the id of the HTTP session, if any 081 * @param userName the name of the user that was associated with the 082 * request, if any (usually the UserPrincipal) 083 * @param processingTimeMillis the processing time of the request in milliseconds 084 * @param failureCause the cause of failure, if any 085 */ 086 public ServletRequestHandledEvent(Object source, String requestUrl, 087 String clientAddress, String method, String servletName, @Nullable String sessionId, 088 @Nullable String userName, long processingTimeMillis, @Nullable Throwable failureCause) { 089 090 super(source, sessionId, userName, processingTimeMillis, failureCause); 091 this.requestUrl = requestUrl; 092 this.clientAddress = clientAddress; 093 this.method = method; 094 this.servletName = servletName; 095 this.statusCode = -1; 096 } 097 098 /** 099 * Create a new ServletRequestHandledEvent. 100 * @param source the component that published the event 101 * @param requestUrl the URL of the request 102 * @param clientAddress the IP address that the request came from 103 * @param method the HTTP method of the request (usually GET or POST) 104 * @param servletName the name of the servlet that handled the request 105 * @param sessionId the id of the HTTP session, if any 106 * @param userName the name of the user that was associated with the 107 * request, if any (usually the UserPrincipal) 108 * @param processingTimeMillis the processing time of the request in milliseconds 109 * @param failureCause the cause of failure, if any 110 * @param statusCode the HTTP status code of the response 111 */ 112 public ServletRequestHandledEvent(Object source, String requestUrl, 113 String clientAddress, String method, String servletName, @Nullable String sessionId, 114 @Nullable String userName, long processingTimeMillis, @Nullable Throwable failureCause, int statusCode) { 115 116 super(source, sessionId, userName, processingTimeMillis, failureCause); 117 this.requestUrl = requestUrl; 118 this.clientAddress = clientAddress; 119 this.method = method; 120 this.servletName = servletName; 121 this.statusCode = statusCode; 122 } 123 124 125 /** 126 * Return the URL of the request. 127 */ 128 public String getRequestUrl() { 129 return this.requestUrl; 130 } 131 132 /** 133 * Return the IP address that the request came from. 134 */ 135 public String getClientAddress() { 136 return this.clientAddress; 137 } 138 139 /** 140 * Return the HTTP method of the request (usually GET or POST). 141 */ 142 public String getMethod() { 143 return this.method; 144 } 145 146 /** 147 * Return the name of the servlet that handled the request. 148 */ 149 public String getServletName() { 150 return this.servletName; 151 } 152 153 /** 154 * Return the HTTP status code of the response or -1 if the status 155 * code is not available. 156 * @since 4.1 157 */ 158 public int getStatusCode() { 159 return this.statusCode; 160 } 161 162 @Override 163 public String getShortDescription() { 164 StringBuilder sb = new StringBuilder(); 165 sb.append("url=[").append(getRequestUrl()).append("]; "); 166 sb.append("client=[").append(getClientAddress()).append("]; "); 167 sb.append(super.getShortDescription()); 168 return sb.toString(); 169 } 170 171 @Override 172 public String getDescription() { 173 StringBuilder sb = new StringBuilder(); 174 sb.append("url=[").append(getRequestUrl()).append("]; "); 175 sb.append("client=[").append(getClientAddress()).append("]; "); 176 sb.append("method=[").append(getMethod()).append("]; "); 177 sb.append("servlet=[").append(getServletName()).append("]; "); 178 sb.append(super.getDescription()); 179 return sb.toString(); 180 } 181 182 @Override 183 public String toString() { 184 return "ServletRequestHandledEvent: " + getDescription(); 185 } 186 187}