KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > filter > AbstractRequestLoggingFilter


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.web.filter;
18
19 import java.io.IOException JavaDoc;
20
21 import javax.servlet.FilterChain JavaDoc;
22 import javax.servlet.ServletException JavaDoc;
23 import javax.servlet.http.HttpServletRequest JavaDoc;
24 import javax.servlet.http.HttpServletResponse JavaDoc;
25
26 /**
27  * Base class for <code>Filter</code>s that perform logging operations before and after a
28  * request is processed.
29  *
30  * <p>Subclasses should override the <code>beforeRequest(HttpServletRequest, String)</code>
31  * and <code>afterRequest(HttpServletRequest, String)</code> methods to perform the actual
32  * logging around the request.
33  *
34  * <p>Subclasses are passed the message to write to the log in the <code>beforeRequest</code>
35  * and <code>afterRequest</code> methods. By default, only the URI of the request is logged.
36  * However, setting the <code>includeQueryString</code> property to <code>true</code> will
37  * cause the query string of the request to be included also.
38  *
39  * <p>Prefixes and suffixes for the before and after messages can be configured
40  * using the <code>beforeMessagePrefix</code>, <code>afterMessagePrefix</code>,
41  * <code>beforeMessageSuffix</code> and <code>afterMessageSuffix</code> properties,
42  *
43  * @author Rob Harrop
44  * @author Juergen Hoeller
45  * @since 1.2.5
46  * @see #beforeRequest
47  * @see #afterRequest
48  */

49 public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter {
50
51     public static final String JavaDoc DEFAULT_BEFORE_MESSAGE_PREFIX = "Before request [";
52
53     public static final String JavaDoc DEFAULT_BEFORE_MESSAGE_SUFFIX = "]";
54
55     public static final String JavaDoc DEFAULT_AFTER_MESSAGE_PREFIX = "After request [";
56
57     public static final String JavaDoc DEFAULT_AFTER_MESSAGE_SUFFIX = "]";
58
59
60     private boolean includeQueryString = false;
61
62     private String JavaDoc beforeMessagePrefix = DEFAULT_BEFORE_MESSAGE_PREFIX;
63
64     private String JavaDoc beforeMessageSuffix = DEFAULT_BEFORE_MESSAGE_SUFFIX;
65
66     private String JavaDoc afterMessagePrefix = DEFAULT_AFTER_MESSAGE_PREFIX;
67
68     private String JavaDoc afterMessageSuffix = DEFAULT_AFTER_MESSAGE_SUFFIX;
69
70
71     /**
72      * Set the whether or not the query string should be included in the log message.
73      * Should be configured using an <code>&lt;init-param&gt;</code> in the filter
74      * definition in <code>web.xml</code>.
75      */

76     public void setIncludeQueryString(boolean includeQueryString) {
77         this.includeQueryString = includeQueryString;
78     }
79
80     /**
81      * Return whether or not the query string should be included in the log message.
82      */

83     protected boolean isIncludeQueryString() {
84         return includeQueryString;
85     }
86
87     /**
88      * Set the value that should be prepended to the log message written
89      * <i>before</i> a request is processed.
90      */

91     public void setBeforeMessagePrefix(String JavaDoc beforeMessagePrefix) {
92         this.beforeMessagePrefix = beforeMessagePrefix;
93     }
94
95     /**
96      * Set the value that should be apppended to the log message written
97      * <i>before</i> a request is processed.
98      */

99     public void setBeforeMessageSuffix(String JavaDoc beforeMessageSuffix) {
100         this.beforeMessageSuffix = beforeMessageSuffix;
101     }
102
103     /**
104      * Set the value that should be prepended to the log message written
105      * <i>after</i> a request is processed.
106      */

107     public void setAfterMessagePrefix(String JavaDoc afterMessagePrefix) {
108         this.afterMessagePrefix = afterMessagePrefix;
109     }
110
111     /**
112      * Set the value that should be appended to the log message written
113      * <i>after</i> a request is processed.
114      */

115     public void setAfterMessageSuffix(String JavaDoc afterMessageSuffix) {
116         this.afterMessageSuffix = afterMessageSuffix;
117     }
118
119
120     /**
121      * Forwards the request to the next filter in the chain and delegates
122      * down to the subclasses to perform the actual request logging both
123      * before and after the request is processed.
124      * @see #beforeRequest
125      * @see #afterRequest
126      */

127     protected void doFilterInternal(
128             HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, FilterChain JavaDoc filterChain)
129             throws ServletException JavaDoc, IOException JavaDoc {
130
131         beforeRequest(request, getBeforeMessage(request));
132         try {
133             filterChain.doFilter(request, response);
134         }
135         finally {
136             afterRequest(request, getAfterMessage(request));
137         }
138     }
139
140     /**
141      * Get the message to write to the log before the request.
142      * @see #createMessage
143      */

144     private String JavaDoc getBeforeMessage(HttpServletRequest JavaDoc request) {
145         return createMessage(request, this.beforeMessagePrefix, this.beforeMessageSuffix);
146     }
147
148     /**
149      * Get the message to write to the log after the request.
150      * @see #createMessage
151      */

152     private String JavaDoc getAfterMessage(HttpServletRequest JavaDoc request) {
153         return createMessage(request, this.afterMessagePrefix, this.afterMessageSuffix);
154     }
155
156     /**
157      * Create a log message for the given request, prefix and suffix.
158      * <p>If <code>includeQueryString</code> is <code>true</code> then
159      * the inner part of the log message will take the form
160      * <code>request_uri?query_string</code> otherwise the message will
161      * simply be of the form <code>request_uri</code>.
162      * <p>The final message is composed of the inner part as described
163      * and the supplied prefix and suffix.
164      */

165     protected String JavaDoc createMessage(HttpServletRequest JavaDoc request, String JavaDoc prefix, String JavaDoc suffix) {
166         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
167         buffer.append(prefix);
168         buffer.append(request.getRequestURI());
169         if (isIncludeQueryString()) {
170             buffer.append('?');
171             buffer.append(request.getQueryString());
172         }
173         buffer.append(suffix);
174         return buffer.toString();
175     }
176
177
178     /**
179      * Concrete subclasses should implement this method to write a log message
180      * <i>before</i> the request is processed.
181      * @param request current HTTP request
182      * @param message the message to log
183      */

184     protected abstract void beforeRequest(HttpServletRequest JavaDoc request, String JavaDoc message);
185
186     /**
187      * Concrete subclasses should implement this method to write a log message
188      * <i>after</i> the request is processed.
189      * @param request current HTTP request
190      * @param message the message to log
191      */

192     protected abstract void afterRequest(HttpServletRequest JavaDoc request, String JavaDoc message);
193
194 }
195
Popular Tags