001: /*
002: * Copyright 2002-2007 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: * http://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:
017: package org.springframework.web.filter;
018:
019: import java.io.IOException;
020:
021: import javax.servlet.FilterChain;
022: import javax.servlet.ServletException;
023: import javax.servlet.ServletRequest;
024: import javax.servlet.ServletResponse;
025: import javax.servlet.http.HttpServletRequest;
026: import javax.servlet.http.HttpServletResponse;
027:
028: /**
029: * Filter base class that guarantees to be just executed once per request,
030: * on any servlet container. It provides a {@link #doFilterInternal}
031: * method with HttpServletRequest and HttpServletResponse arguments.
032: *
033: * <p>The {@link #getAlreadyFilteredAttributeName} method determines how
034: * to identify that a request is already filtered. The default implementation
035: * is based on the configured name of the concrete filter instance.
036: *
037: * @author Juergen Hoeller
038: * @since 06.12.2003
039: */
040: public abstract class OncePerRequestFilter extends GenericFilterBean {
041:
042: /**
043: * Suffix that gets appended to the filter name for the
044: * "already filtered" request attribute.
045: * @see #getAlreadyFilteredAttributeName
046: */
047: public static final String ALREADY_FILTERED_SUFFIX = ".FILTERED";
048:
049: /**
050: * This <code>doFilter</code> implementation stores a request attribute for
051: * "already filtered", proceeding without filtering again if the
052: * attribute is already there.
053: * @see #getAlreadyFilteredAttributeName
054: * @see #shouldNotFilter
055: * @see #doFilterInternal
056: */
057: public final void doFilter(ServletRequest request,
058: ServletResponse response, FilterChain filterChain)
059: throws ServletException, IOException {
060:
061: if (!(request instanceof HttpServletRequest)
062: || !(response instanceof HttpServletResponse)) {
063: throw new ServletException(
064: "OncePerRequestFilter just supports HTTP requests");
065: }
066: HttpServletRequest httpRequest = (HttpServletRequest) request;
067: HttpServletResponse httpResponse = (HttpServletResponse) response;
068:
069: String alreadyFilteredAttributeName = getAlreadyFilteredAttributeName();
070: if (request.getAttribute(alreadyFilteredAttributeName) != null
071: || shouldNotFilter(httpRequest)) {
072: // Proceed without invoking this filter...
073: filterChain.doFilter(request, response);
074: } else {
075: // Do invoke this filter...
076: request.setAttribute(alreadyFilteredAttributeName,
077: Boolean.TRUE);
078: doFilterInternal(httpRequest, httpResponse, filterChain);
079: }
080: }
081:
082: /**
083: * Return the name of the request attribute that identifies that a request
084: * is already filtered.
085: * <p>Default implementation takes the configured name of the concrete filter
086: * instance and appends ".FILTERED". If the filter is not fully initialized,
087: * it falls back to its class name.
088: * @see #getFilterName
089: * @see #ALREADY_FILTERED_SUFFIX
090: */
091: protected String getAlreadyFilteredAttributeName() {
092: String name = getFilterName();
093: if (name == null) {
094: name = getClass().getName();
095: }
096: return name + ALREADY_FILTERED_SUFFIX;
097: }
098:
099: /**
100: * Can be overridden in subclasses for custom filtering control,
101: * returning <code>true</code> to avoid filtering of the given request.
102: * <p>The default implementation always returns <code>false</code>.
103: * @param request current HTTP request
104: * @return whether the given request should <i>not</i> be filtered
105: * @throws ServletException in case of errors
106: */
107: protected boolean shouldNotFilter(HttpServletRequest request)
108: throws ServletException {
109: return false;
110: }
111:
112: /**
113: * Same contract as for <code>doFilter</code>, but guaranteed to be
114: * just invoked once per request. Provides HttpServletRequest and
115: * HttpServletResponse arguments instead of the default ServletRequest
116: * and ServletResponse ones.
117: */
118: protected abstract void doFilterInternal(
119: HttpServletRequest request, HttpServletResponse response,
120: FilterChain filterChain) throws ServletException,
121: IOException;
122:
123: }
|