01: /* ***** BEGIN LICENSE BLOCK *****
02: * Version: MPL 1.1
03: * The contents of this file are subject to the Mozilla Public License Version
04: * 1.1 (the "License"); you may not use this file except in compliance with
05: * the License. You may obtain a copy of the License at
06: * http://www.mozilla.org/MPL/
07: *
08: * Software distributed under the License is distributed on an "AS IS" basis,
09: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10: * for the specific language governing rights and limitations under the
11: * License.
12: *
13: * The Original Code is Riot.
14: *
15: * The Initial Developer of the Original Code is
16: * Neteye GmbH.
17: * Portions created by the Initial Developer are Copyright (C) 2006
18: * the Initial Developer. All Rights Reserved.
19: *
20: * Contributor(s):
21: * Felix Gnass [fgnass at neteye dot de]
22: *
23: * ***** END LICENSE BLOCK ***** */
24: package org.riotfamily.common.web.interceptor;
25:
26: import javax.servlet.http.HttpServletRequest;
27: import javax.servlet.http.HttpServletResponse;
28:
29: public abstract class OncePerRequestInterceptor extends
30: PathMatchingInterceptor {
31:
32: private String counterAttribute = getClass().getName()
33: + ".interceptions";
34:
35: protected final boolean doPreHandle(HttpServletRequest request,
36: HttpServletResponse response, Object handler)
37: throws Exception {
38:
39: int interceptions = 0;
40: Integer counter = (Integer) request
41: .getAttribute(counterAttribute);
42: if (counter != null) {
43: interceptions = counter.intValue();
44: }
45: interceptions++;
46: request.setAttribute(counterAttribute, new Integer(
47: interceptions));
48:
49: if (interceptions == 1) {
50: return preHandleOnce(request, response, handler);
51: }
52:
53: return true;
54: }
55:
56: protected boolean preHandleOnce(HttpServletRequest request,
57: HttpServletResponse response, Object handler)
58: throws Exception {
59:
60: return true;
61: }
62:
63: public final void afterCompletion(HttpServletRequest request,
64: HttpServletResponse response, Object handler,
65: Exception exception) throws Exception {
66:
67: Integer counter = (Integer) request
68: .getAttribute(counterAttribute);
69: if (counter != null) {
70: int interceptions = counter.intValue() - 1;
71: request.setAttribute(counterAttribute, new Integer(
72: interceptions));
73: if (interceptions == 0) {
74: afterLastCompletion(request, response, handler,
75: exception);
76: }
77: }
78: }
79:
80: protected void afterLastCompletion(HttpServletRequest request,
81: HttpServletResponse response, Object handler,
82: Exception exception) throws Exception {
83: }
84:
85: }
|