KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portlet > forums > interceptors > FloodControlInterceptor


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Forums JBoss Portlet *
6  * *
7  * Distributable under GPL license. *
8  * See terms of license at gnu.org. *
9  * *
10  *****************************************/

11 package org.jboss.portlet.forums.interceptors;
12
13 import java.util.Date JavaDoc;
14
15 import org.jboss.portal.common.command.Command;
16 import org.jboss.portal.common.command.CommandException;
17 import org.jboss.portal.common.command.filter.AbstractCommandFilter;
18 import org.jboss.portal.common.command.result.Result;
19 import org.jboss.portal.core.model.User;
20 import org.jboss.portal.core.modules.ModuleException;
21 import org.jboss.portlet.forums.commands.CommandConstants;
22 import org.jboss.portlet.forums.commands.post.ForumCommand;
23
24 /**
25  * Interceptor for implementing flood control to stop evil users from flooding
26  * the forums.
27  *
28  * @author <a HREF="kabirkhan@bigfoot.com">Kabir Khan </a>
29  * @author <a HREF="theute@jboss.org">Thomas Heute </a>
30  */

31 public class FloodControlInterceptor
32       extends AbstractCommandFilter
33 {
34    private long floodInterval;
35
36    /**
37     * DOCUMENT_ME
38     *
39     * @param cmd
40     * DOCUMENT_ME
41     *
42     * @return DOCUMENT_ME
43     *
44     * @throws CommandException
45     * DOCUMENT_ME
46     */

47    public Result filter(Command cmd) throws CommandException
48    {
49       if (cmd instanceof ForumCommand)
50       {
51          ForumCommand command = (ForumCommand) cmd;
52
53          if (command.poster.getID() != null)
54          {
55             User user = command.poster.getUser();
56
57             long lastPostTime = 0;
58
59             try
60             {
61                Date JavaDoc lastPost = command.forumsModule
62                      .findLastPostDateForUser(user);
63                if (lastPost != null)
64                {
65                   lastPostTime = lastPost.getTime();
66                }
67             }
68             catch (ModuleException e)
69             {
70                e.printStackTrace();
71             }
72             if (lastPostTime > 0)
73             {
74                long now = new Date JavaDoc().getTime();
75                long interval = (now - lastPostTime) / 1000;
76
77                System.out.println("INTERVAL:" + interval);
78
79                if (interval < floodInterval)
80                {
81                   return CommandConstants.TYPE_FLOODING_CANNOT_CREATE;
82                }
83             }
84          }
85       }
86
87       return getNext().filter(cmd);
88    }
89
90    /**
91     * DOCUMENT_ME
92     *
93     * @return DOCUMENT_ME
94     */

95    public long getFloodInterval()
96    {
97       return floodInterval;
98    }
99
100    /**
101     * DOCUMENT_ME
102     *
103     * @param floodInterval
104     * DOCUMENT_ME
105     */

106    public void setFloodInterval(long floodInterval)
107    {
108       this.floodInterval = floodInterval;
109    }
110 }
Popular Tags