KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > javabb > interceptor > DelayInterceptor


1 package org.javabb.interceptor;
2
3 import java.util.Map JavaDoc;
4
5 import org.apache.commons.logging.Log;
6 import org.apache.commons.logging.LogFactory;
7 import org.javabb.infra.ConfigurationFactory;
8
9 import com.opensymphony.webwork.ServletActionContext;
10 import com.opensymphony.xwork.ActionContext;
11 import com.opensymphony.xwork.ActionInvocation;
12 import com.opensymphony.xwork.interceptor.AroundInterceptor;
13
14 /*
15  * Copyright 2004 JavaFree.org
16  *
17  * Licensed under the Apache License, Version 2.0 (the "License");
18  * you may not use this file except in compliance with the License.
19  * You may obtain a copy of the License at
20  *
21  * http://www.apache.org/licenses/LICENSE-2.0
22  *
23  * Unless required by applicable law or agreed to in writing, software
24  * distributed under the License is distributed on an "AS IS" BASIS,
25  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26  * See the License for the specific language governing permissions and
27  * limitations under the License.
28  */

29
30 /**
31  * $Id: DelayInterceptor.java,v 1.3.2.1.4.2 2006/04/17 17:47:08 daltoncamargo Exp $
32  *
33  * @author Dalton Camargo -
34  */

35 public class DelayInterceptor extends AroundInterceptor {
36
37     protected final Log _log = LogFactory.getLog(this.getClass());
38     private boolean _isFlood = false;
39
40
41     /**
42      * @param invocation
43      * @throws Exception
44      * @see com.opensymphony.xwork.interceptor.AroundInterceptor#before(com.opensymphony.xwork.ActionInvocation)
45      */

46     protected void before(ActionInvocation invocation) throws Exception JavaDoc {
47         Map JavaDoc session = ActionContext.getContext().getSession();
48
49         boolean createDelay = false;
50         Long JavaDoc userLastVisit = (Long JavaDoc) session.get("user_last_visit");
51         if (session.get("user_last_visit") == null) {
52             userLastVisit = new Long JavaDoc(System.currentTimeMillis());
53             session.put("user_last_visit", userLastVisit);
54         } else {
55             createDelay = true;
56         }
57
58         String JavaDoc userIP = (String JavaDoc) session.get("user_ip");
59         if (userIP == null) {
60             userIP = ServletActionContext.getRequest().getRemoteAddr();
61             session.put("user_ip", userIP);
62         }
63
64         if (userIP.equals(ServletActionContext.getRequest().getRemoteAddr())) {
65             long currTime = System.currentTimeMillis();
66             long timeDiff = currTime - userLastVisit.longValue();
67             if (createDelay && (timeDiff < 1500)) {
68                 _log.debug("LAST FLOOD: |" + userLastVisit.longValue());
69                 _log.debug("IP FLOODING: |"
70                         + ServletActionContext.getRequest().getRemoteAddr());
71                 _isFlood = true;
72             }
73         }
74         //updating the session
75
session.put("user_last_visit", new Long JavaDoc(System.currentTimeMillis()));
76     }
77     
78     /**
79      * @param invocation
80      * @param result
81      * @throws Exception
82      * @see com.opensymphony.xwork.interceptor.AroundInterceptor#after(com.opensymphony.xwork.ActionInvocation,
83      * java.lang.String)
84      */

85     protected void after(ActionInvocation invocation, String JavaDoc result)
86             throws Exception JavaDoc {
87         //
88
}
89
90     /**
91      * @see com.opensymphony.xwork.interceptor.Interceptor#intercept(com.opensymphony.xwork.ActionInvocation)
92      */

93     public String JavaDoc intercept(ActionInvocation invocation) throws Exception JavaDoc {
94         before(invocation);
95         if (_isFlood && !"0".equals(ConfigurationFactory.getConf().floodControl)) {
96             _isFlood = false;
97             return "flood_control";
98         }
99         return invocation.invoke();
100     }
101 }
Popular Tags