View Javadoc

1   package net.sf.mmapps.commons.util;
2   
3   import javax.servlet.http.HttpServletRequest;
4   import javax.servlet.http.HttpSession;
5   
6   public class SessionUtil {
7      
8      /*** contains the session a object
9       * 
10      * @param name Name of session attribute
11      * @param request Http request
12      * @return true when session contains attribute
13      */
14     public static boolean contains(HttpServletRequest request, String name) {
15        if (request == null) {
16           throw new NullPointerException("Request is null. Where should the object come from?.");
17        }
18        if (name == null) {
19           throw new NullPointerException("Name is null. How did you want to check the object?.");
20        }
21  
22        HttpSession session = request.getSession(false);
23        if (session != null) {
24           return session.getAttribute(name) != null;
25        }
26        return false;
27     }
28  
29     /*** Get a object from the session
30      * 
31      * @param name Name of session attribute
32      * @param request Http request
33      * @return Object from the session
34      */
35     public static Object get(HttpServletRequest request, String name) {
36        if (request == null) {
37           throw new NullPointerException("Request is null. Where should the object come from?.");
38        }
39        if (name == null) {
40           throw new NullPointerException("Name is null. How did you want to get the object?.");
41        }
42  
43        HttpSession session = request.getSession(false);
44        if (session == null) {
45           throw new NullPointerException("Session not initialised yet. Failed to get " + name);
46        }
47        
48        Object o = session.getAttribute(name);
49        if (o == null) {
50           throw new NullPointerException(name + " has not been registered in session.");
51        }
52        return o;
53     }
54  
55     /*** Set the object in the session
56      *
57      * @param name Name of session attribute 
58      * @param request Http request
59      * @param o Object to add to the session
60      */
61     public static void set(HttpServletRequest request, String name, Object o) {
62        if (request == null) {
63           throw new NullPointerException("Request is null. Where should the object go to?.");
64        }
65        if (name == null) {
66           throw new NullPointerException("Name is null. How did you want to get the object back?.");
67        }
68        if (o == null) {
69           throw new NullPointerException("Object is null. Why should it be added?.");
70        }
71        HttpSession session = request.getSession(true);
72        session.setAttribute(name, o);
73     }
74  
75     /*** removes an object from the session
76      * 
77      * @param name Name of session attribute
78      * @param request Http request
79      */
80     public static void remove(HttpServletRequest request, String name) {
81        if (request == null) {
82           throw new NullPointerException("Request is null. Where should the object removed from?.");
83        }
84        if (name == null) {
85           throw new NullPointerException("Name is null. How did you want to remove the object?.");
86        }
87  
88        HttpSession session = request.getSession(false);
89        if (session != null) {
90           session.removeAttribute(name);
91        }
92     }
93     
94     /*** Clean the session
95      * 
96      * @param request Http request
97      */
98     public static void clean(HttpServletRequest request) {
99        if (request == null) {
100          throw new NullPointerException("Request is null. Impossible to clean the session!.");
101       }
102 
103       HttpSession session = request.getSession(false);
104       if (session != null) {
105          session.invalidate();
106       }
107    }
108 }