KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > common > web > util > OncePerRequestInterceptor


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * 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.util;
25
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27 import javax.servlet.http.HttpServletResponse JavaDoc;
28
29 public abstract class OncePerRequestInterceptor extends PathMatchingInterceptor {
30
31     private String JavaDoc counterAttribute = getClass().getName() + ".interceptions";
32
33     protected final boolean doPreHandle(HttpServletRequest JavaDoc request,
34             HttpServletResponse JavaDoc response, Object JavaDoc handler) throws Exception JavaDoc {
35
36         int interceptions = 0;
37         Integer JavaDoc counter = (Integer JavaDoc) request.getAttribute(counterAttribute);
38         if (counter != null) {
39             interceptions = counter.intValue();
40         }
41         interceptions++;
42         request.setAttribute(counterAttribute, new Integer JavaDoc(interceptions));
43
44         if (interceptions == 1) {
45             return preHandleOnce(request, response, handler);
46         }
47         
48         return true;
49     }
50
51     protected boolean preHandleOnce(HttpServletRequest JavaDoc request,
52             HttpServletResponse JavaDoc response, Object JavaDoc handler) throws Exception JavaDoc {
53         
54         return true;
55     }
56     
57     public final void afterCompletion(HttpServletRequest JavaDoc request,
58             HttpServletResponse JavaDoc response, Object JavaDoc handler, Exception JavaDoc exception)
59             throws Exception JavaDoc {
60         
61         Integer JavaDoc counter = (Integer JavaDoc) request.getAttribute(counterAttribute);
62         if (counter != null) {
63             int interceptions = counter.intValue() - 1;
64             request.setAttribute(counterAttribute, new Integer JavaDoc(interceptions));
65             if (interceptions == 0) {
66                 afterLastCompletion(request, response, handler, exception);
67             }
68         }
69     }
70
71     protected void afterLastCompletion(HttpServletRequest JavaDoc request,
72             HttpServletResponse JavaDoc response, Object JavaDoc handler, Exception JavaDoc exception)
73             throws Exception JavaDoc {
74     }
75
76 }
77
Popular Tags