001/*
002 * Copyright 2002-2014 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
019/**
020 * Servlet-specific subclass of RequestHandledEvent,
021 * adding servlet-specific context information.
022 *
023 * @author Juergen Hoeller
024 * @since 2.0
025 * @see org.springframework.web.servlet.FrameworkServlet
026 * @see org.springframework.context.ApplicationContext#publishEvent
027 */
028@SuppressWarnings("serial")
029public class ServletRequestHandledEvent extends RequestHandledEvent {
030
031        /** URL that triggered the request */
032        private final String requestUrl;
033
034        /** IP address that the request came from */
035        private final String clientAddress;
036
037        /** Usually GET or POST */
038        private final String method;
039
040        /** Name of the servlet that handled the request */
041        private final String servletName;
042
043        /** HTTP status code of the response */
044        private final int statusCode;
045
046
047        /**
048         * Create a new ServletRequestHandledEvent.
049         * @param source the component that published the event
050         * @param requestUrl the URL of the request
051         * @param clientAddress the IP address that the request came from
052         * @param method the HTTP method of the request (usually GET or POST)
053         * @param servletName the name of the servlet that handled the request
054         * @param sessionId the id of the HTTP session, if any
055         * @param userName the name of the user that was associated with the
056         * request, if any (usually the UserPrincipal)
057         * @param processingTimeMillis the processing time of the request in milliseconds
058         */
059        public ServletRequestHandledEvent(Object source, String requestUrl,
060                        String clientAddress, String method, String servletName,
061                        String sessionId, String userName, long processingTimeMillis) {
062
063                super(source, sessionId, userName, processingTimeMillis);
064                this.requestUrl = requestUrl;
065                this.clientAddress = clientAddress;
066                this.method = method;
067                this.servletName = servletName;
068                this.statusCode = -1;
069        }
070
071        /**
072         * Create a new ServletRequestHandledEvent.
073         * @param source the component that published the event
074         * @param requestUrl the URL of the request
075         * @param clientAddress the IP address that the request came from
076         * @param method the HTTP method of the request (usually GET or POST)
077         * @param servletName the name of the servlet that handled the request
078         * @param sessionId the id of the HTTP session, if any
079         * @param userName the name of the user that was associated with the
080         * request, if any (usually the UserPrincipal)
081         * @param processingTimeMillis the processing time of the request in milliseconds
082         * @param failureCause the cause of failure, if any
083         */
084        public ServletRequestHandledEvent(Object source, String requestUrl,
085                        String clientAddress, String method, String servletName, String sessionId,
086                        String userName, long processingTimeMillis, Throwable failureCause) {
087
088                super(source, sessionId, userName, processingTimeMillis, failureCause);
089                this.requestUrl = requestUrl;
090                this.clientAddress = clientAddress;
091                this.method = method;
092                this.servletName = servletName;
093                this.statusCode = -1;
094        }
095
096        /**
097         * Create a new ServletRequestHandledEvent.
098         * @param source the component that published the event
099         * @param requestUrl the URL of the request
100         * @param clientAddress the IP address that the request came from
101         * @param method the HTTP method of the request (usually GET or POST)
102         * @param servletName the name of the servlet that handled the request
103         * @param sessionId the id of the HTTP session, if any
104         * @param userName the name of the user that was associated with the
105         * request, if any (usually the UserPrincipal)
106         * @param processingTimeMillis the processing time of the request in milliseconds
107         * @param failureCause the cause of failure, if any
108         * @param statusCode the HTTP status code of the response
109         */
110        public ServletRequestHandledEvent(Object source, String requestUrl,
111                        String clientAddress, String method, String servletName, String sessionId,
112                        String userName, long processingTimeMillis, Throwable failureCause, int statusCode) {
113
114                super(source, sessionId, userName, processingTimeMillis, failureCause);
115                this.requestUrl = requestUrl;
116                this.clientAddress = clientAddress;
117                this.method = method;
118                this.servletName = servletName;
119                this.statusCode = statusCode;
120        }
121
122
123        /**
124         * Return the URL of the request.
125         */
126        public String getRequestUrl() {
127                return this.requestUrl;
128        }
129
130        /**
131         * Return the IP address that the request came from.
132         */
133        public String getClientAddress() {
134                return this.clientAddress;
135        }
136
137        /**
138         * Return the HTTP method of the request (usually GET or POST).
139         */
140        public String getMethod() {
141                return this.method;
142        }
143
144        /**
145         * Return the name of the servlet that handled the request.
146         */
147        public String getServletName() {
148                return this.servletName;
149        }
150
151        /**
152         * Return the HTTP status code of the response or -1 if the status
153         * code is not available.
154         * @since 4.1
155         */
156        public int getStatusCode() {
157                return this.statusCode;
158        }
159
160        @Override
161        public String getShortDescription() {
162                StringBuilder sb = new StringBuilder();
163                sb.append("url=[").append(getRequestUrl()).append("]; ");
164                sb.append("client=[").append(getClientAddress()).append("]; ");
165                sb.append(super.getShortDescription());
166                return sb.toString();
167        }
168
169        @Override
170        public String getDescription() {
171                StringBuilder sb = new StringBuilder();
172                sb.append("url=[").append(getRequestUrl()).append("]; ");
173                sb.append("client=[").append(getClientAddress()).append("]; ");
174                sb.append("method=[").append(getMethod()).append("]; ");
175                sb.append("servlet=[").append(getServletName()).append("]; ");
176                sb.append(super.getDescription());
177                return sb.toString();
178        }
179
180        @Override
181        public String toString() {
182                return "ServletRequestHandledEvent: " + getDescription();
183        }
184
185}