001/* 002 * Copyright 2002-2019 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.context.ApplicationEvent; 020import org.springframework.lang.Nullable; 021 022/** 023 * Event raised when a request is handled within an ApplicationContext. 024 * 025 * <p>Supported by Spring's own FrameworkServlet (through a specific 026 * ServletRequestHandledEvent subclass), but can also be raised by any 027 * other web component. 028 * 029 * @author Rod Johnson 030 * @author Juergen Hoeller 031 * @since January 17, 2001 032 * @see ServletRequestHandledEvent 033 * @see org.springframework.web.servlet.FrameworkServlet 034 * @see org.springframework.context.ApplicationContext#publishEvent 035 */ 036@SuppressWarnings("serial") 037public class RequestHandledEvent extends ApplicationEvent { 038 039 /** Session id that applied to the request, if any. */ 040 @Nullable 041 private String sessionId; 042 043 /** Usually the UserPrincipal. */ 044 @Nullable 045 private String userName; 046 047 /** Request processing time. */ 048 private final long processingTimeMillis; 049 050 /** Cause of failure, if any. */ 051 @Nullable 052 private Throwable failureCause; 053 054 055 /** 056 * Create a new RequestHandledEvent with session information. 057 * @param source the component that published the event 058 * @param sessionId the id of the HTTP session, if any 059 * @param userName the name of the user that was associated with the 060 * request, if any (usually the UserPrincipal) 061 * @param processingTimeMillis the processing time of the request in milliseconds 062 */ 063 public RequestHandledEvent(Object source, @Nullable String sessionId, @Nullable String userName, 064 long processingTimeMillis) { 065 066 super(source); 067 this.sessionId = sessionId; 068 this.userName = userName; 069 this.processingTimeMillis = processingTimeMillis; 070 } 071 072 /** 073 * Create a new RequestHandledEvent with session information. 074 * @param source the component that published the event 075 * @param sessionId the id of the HTTP session, if any 076 * @param userName the name of the user that was associated with the 077 * request, if any (usually the UserPrincipal) 078 * @param processingTimeMillis the processing time of the request in milliseconds 079 * @param failureCause the cause of failure, if any 080 */ 081 public RequestHandledEvent(Object source, @Nullable String sessionId, @Nullable String userName, 082 long processingTimeMillis, @Nullable Throwable failureCause) { 083 084 this(source, sessionId, userName, processingTimeMillis); 085 this.failureCause = failureCause; 086 } 087 088 089 /** 090 * Return the processing time of the request in milliseconds. 091 */ 092 public long getProcessingTimeMillis() { 093 return this.processingTimeMillis; 094 } 095 096 /** 097 * Return the id of the HTTP session, if any. 098 */ 099 @Nullable 100 public String getSessionId() { 101 return this.sessionId; 102 } 103 104 /** 105 * Return the name of the user that was associated with the request 106 * (usually the UserPrincipal). 107 * @see javax.servlet.http.HttpServletRequest#getUserPrincipal() 108 */ 109 @Nullable 110 public String getUserName() { 111 return this.userName; 112 } 113 114 /** 115 * Return whether the request failed. 116 */ 117 public boolean wasFailure() { 118 return (this.failureCause != null); 119 } 120 121 /** 122 * Return the cause of failure, if any. 123 */ 124 @Nullable 125 public Throwable getFailureCause() { 126 return this.failureCause; 127 } 128 129 130 /** 131 * Return a short description of this event, only involving 132 * the most important context data. 133 */ 134 public String getShortDescription() { 135 StringBuilder sb = new StringBuilder(); 136 sb.append("session=[").append(this.sessionId).append("]; "); 137 sb.append("user=[").append(this.userName).append("]; "); 138 return sb.toString(); 139 } 140 141 /** 142 * Return a full description of this event, involving 143 * all available context data. 144 */ 145 public String getDescription() { 146 StringBuilder sb = new StringBuilder(); 147 sb.append("session=[").append(this.sessionId).append("]; "); 148 sb.append("user=[").append(this.userName).append("]; "); 149 sb.append("time=[").append(this.processingTimeMillis).append("ms]; "); 150 sb.append("status=["); 151 if (!wasFailure()) { 152 sb.append("OK"); 153 } 154 else { 155 sb.append("failed: ").append(this.failureCause); 156 } 157 sb.append(']'); 158 return sb.toString(); 159 } 160 161 @Override 162 public String toString() { 163 return ("RequestHandledEvent: " + getDescription()); 164 } 165 166}