KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > getahead > dwrdemo > filter > AuthenticationAjaxFilter


1 package org.getahead.dwrdemo.filter;
2
3 import java.lang.reflect.Method JavaDoc;
4
5 import javax.servlet.http.HttpSession JavaDoc;
6
7 import org.directwebremoting.AjaxFilter;
8 import org.directwebremoting.AjaxFilterChain;
9 import org.directwebremoting.WebContextFactory;
10
11 /**
12  * Manages the current Authentication state.
13  * @author Joe Walker [joe at getahead dot ltd dot uk]
14  */

15 public class AuthenticationAjaxFilter implements AjaxFilter
16 {
17     /* (non-Javadoc)
18      * @see org.directwebremoting.AjaxFilter#doFilter(java.lang.Object, java.lang.reflect.Method, java.lang.Object[], org.directwebremoting.AjaxFilterChain)
19      */

20     public Object JavaDoc doFilter(Object JavaDoc object, Method JavaDoc method, Object JavaDoc[] params, AjaxFilterChain chain) throws Exception JavaDoc
21     {
22         // We allow anyone to authenticate
23
if (authenticateName.equals(method.getName()))
24         {
25             return chain.doFilter(object, method, params);
26         }
27
28         Object JavaDoc user = getUser();
29         if (user != null)
30         {
31             return chain.doFilter(object, method, params);
32         }
33
34         throw new SecurityException JavaDoc("Not authenticated");
35     }
36
37     /**
38      * @return Returns the authenticateName.
39      */

40     public String JavaDoc getAuthenticateName()
41     {
42         return authenticateName;
43     }
44
45     /**
46      * @param authenticateName The authenticateName to set.
47      */

48     public void setAuthenticateName(String JavaDoc authenticateName)
49     {
50         this.authenticateName = authenticateName;
51     }
52
53     /**
54      * What is the name of the authenticate method?
55      */

56     private String JavaDoc authenticateName = "authenticate";
57
58     /**
59      * Accessor for the current user
60      * @param user The current user
61      */

62     public static void setUser(Object JavaDoc user)
63     {
64         HttpSession JavaDoc session = WebContextFactory.get().getSession(true);
65         session.setAttribute(USER, user);
66     }
67
68     /**
69      * Accessor for the current user
70      * @return The current user
71      */

72     public static Object JavaDoc getUser()
73     {
74         HttpSession JavaDoc session = WebContextFactory.get().getSession(true);
75         return session.getAttribute(USER);
76     }
77
78     /**
79      * The session key
80      */

81     private static final String JavaDoc USER = "org.directwebremoting.filter.AuthenticationAjaxFilter.USER";
82 }
83
Popular Tags