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