KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2002-2007 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.ServletRequest JavaDoc;
24 import javax.servlet.ServletResponse JavaDoc;
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import javax.servlet.http.HttpServletResponse JavaDoc;
27
28 /**
29  * Filter base class that guarantees to be just executed once per request,
30  * on any servlet container. It provides a {@link #doFilterInternal}
31  * method with HttpServletRequest and HttpServletResponse arguments.
32  *
33  * <p>The {@link #getAlreadyFilteredAttributeName} method determines how
34  * to identify that a request is already filtered. The default implementation
35  * is based on the configured name of the concrete filter instance.
36  *
37  * @author Juergen Hoeller
38  * @since 06.12.2003
39  */

40 public abstract class OncePerRequestFilter extends GenericFilterBean {
41
42     /**
43      * Suffix that gets appended to the filter name for the
44      * "already filtered" request attribute.
45      * @see #getAlreadyFilteredAttributeName
46      */

47     public static final String JavaDoc ALREADY_FILTERED_SUFFIX = ".FILTERED";
48
49
50     /**
51      * This <code>doFilter</code> implementation stores a request attribute for
52      * "already filtered", proceeding without filtering again if the
53      * attribute is already there.
54      * @see #getAlreadyFilteredAttributeName
55      * @see #shouldNotFilter
56      * @see #doFilterInternal
57      */

58     public final void doFilter(ServletRequest JavaDoc request, ServletResponse JavaDoc response, FilterChain JavaDoc filterChain)
59             throws ServletException JavaDoc, IOException JavaDoc {
60
61         if (!(request instanceof HttpServletRequest JavaDoc) || !(response instanceof HttpServletResponse JavaDoc)) {
62             throw new ServletException JavaDoc("OncePerRequestFilter just supports HTTP requests");
63         }
64         HttpServletRequest JavaDoc httpRequest = (HttpServletRequest JavaDoc) request;
65         HttpServletResponse JavaDoc httpResponse = (HttpServletResponse JavaDoc) response;
66
67         String JavaDoc alreadyFilteredAttributeName = getAlreadyFilteredAttributeName();
68         if (request.getAttribute(alreadyFilteredAttributeName) != null || shouldNotFilter(httpRequest)) {
69             // Proceed without invoking this filter...
70
filterChain.doFilter(request, response);
71         }
72         else {
73             // Do invoke this filter...
74
request.setAttribute(alreadyFilteredAttributeName, Boolean.TRUE);
75             doFilterInternal(httpRequest, httpResponse, filterChain);
76         }
77     }
78
79     /**
80      * Return the name of the request attribute that identifies that a request
81      * is already filtered.
82      * <p>Default implementation takes the configured name of the concrete filter
83      * instance and appends ".FILTERED". If the filter is not fully initialized,
84      * it falls back to its class name.
85      * @see #getFilterName
86      * @see #ALREADY_FILTERED_SUFFIX
87      */

88     protected String JavaDoc getAlreadyFilteredAttributeName() {
89         String JavaDoc name = getFilterName();
90         if (name == null) {
91             name = getClass().getName();
92         }
93         return name + ALREADY_FILTERED_SUFFIX;
94     }
95
96     /**
97      * Can be overridden in subclasses for custom filtering control,
98      * returning <code>true</code> to avoid filtering of the given request.
99      * <p>The default implementation always returns <code>false</code>.
100      * @param request current HTTP request
101      * @return whether the given request should <i>not</i> be filtered
102      * @throws ServletException in case of errors
103      */

104     protected boolean shouldNotFilter(HttpServletRequest JavaDoc request) throws ServletException JavaDoc {
105         return false;
106     }
107
108
109     /**
110      * Same contract as for <code>doFilter</code>, but guaranteed to be
111      * just invoked once per request. Provides HttpServletRequest and
112      * HttpServletResponse arguments instead of the default ServletRequest
113      * and ServletResponse ones.
114      */

115     protected abstract void doFilterInternal(
116             HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, FilterChain JavaDoc filterChain)
117             throws ServletException JavaDoc, IOException JavaDoc;
118
119 }
120
Popular Tags