View Javadoc

1   /*
2    * WebFlow Navigation Manager: webflow definiton, server side navigation history and automatic session cleaning.
3    * Distributed under LGPL license at web site http://wfnm.sourceforge.net .
4    */
5   package net.sf.wfnm.web;
6   
7   import net.sf.wfnm.AttributeContainer;
8   import net.sf.wfnm.NavigationContextFactory;
9   
10  import java.util.Enumeration;
11  
12  import javax.servlet.ServletContext;
13  import javax.servlet.http.HttpSession;
14  import javax.servlet.http.HttpSessionContext;
15  
16  
17  /***
18   * An attribute container wrapper for http session. Implements the AttributeContainer abstraction of the http session
19   * and handles the binding/unbinding of objects to/from the original http session
20   *
21   * @author <a href="mailto:malbari@users.sourceforge.net">Maurizio Albari</a>
22   * @version 1.0.6
23   */
24  public class HttpSessionWrapper implements AttributeContainer, HttpSession {
25      /*** 
26       * the wrapped http session.
27       */
28      private HttpSession session;
29  
30      /***
31       * Creates a new HttpSessionWrapper object.
32       *
33       * @param session the wrapped http session
34       */
35      private HttpSessionWrapper(HttpSession session) {
36          this.session = session;
37      }
38  
39      /***
40       * Wraps the session with the HttpSessionWrapper if necessary
41       *
42       * @param session the http session
43       *
44       * @return the wrapped http session
45       */
46      public static HttpSession wrapItIfNecessary(HttpSession session) {
47          if (session == null || session instanceof HttpSessionWrapper) {
48              return session;
49          } else {
50              return new HttpSessionWrapper(session);
51          }
52      }
53  
54      /***
55       * Set the value of an attribute AND notify the event to WFNM.
56       *
57       * @param key the key of the attribute
58       * @param value the value of the attribute
59       */
60      public void setAttribute(String key, Object value) {
61          session.setAttribute(key, value);
62          NavigationContextFactory.getInstance().setAttribute(this, key);
63      }
64  
65      /***
66       * Gets the value of an attribute.
67       *
68       * @param key the attribute key
69       *
70       * @return the value of the attribute
71       */
72      public Object getAttribute(String key) {
73          return session.getAttribute(key);
74      }
75  
76      /***
77       * Returns an enumeration of attribute names.
78       *
79       * @return an enumeration of attribute names
80       */
81      public Enumeration getAttributeNames() {
82          return session.getAttributeNames();
83      }
84  
85      /***
86       * Set the value of an attribute.
87       *
88       * @param key the attribute key
89       * @param value the attribute value
90       */
91      public void setAttributeValue(String key, Object value) {
92          session.setAttribute(key, value);
93      }
94  
95      /***
96       * Returns the creation time of the session.
97       *
98       * @return the creation time of the session
99       */
100     public long getCreationTime() {
101         return session.getCreationTime();
102     }
103 
104     /***
105      * Get the unique identifier of this attribute container.
106      *
107      * @return the unique identifier of this attribute container
108      */
109     public String getId() {
110         return session.getId();
111     }
112 
113     /***
114      * Returns the last accessed time of the session.
115      *
116      * @return the last accessed time of the session
117      */
118     public long getLastAccessedTime() {
119         return session.getLastAccessedTime();
120     }
121 
122     /***
123      * Set the max inactive interval of the session.
124      *
125      * @param maxInactiveInterval the max inactive interval of the session
126      */
127     public void setMaxInactiveInterval(int maxInactiveInterval) {
128         session.setMaxInactiveInterval(maxInactiveInterval);
129     }
130 
131     /***
132      * Returns the max inactive interval of the session.
133      *
134      * @return the max inactive interval of the session
135      */
136     public int getMaxInactiveInterval() {
137         return session.getMaxInactiveInterval();
138     }
139 
140     /***
141      * Returns whether the session is new.
142      *
143      * @return true if the session is new
144      */
145     public boolean isNew() {
146         return session.isNew();
147     }
148 
149     /***
150      * Returns the servlet context.
151      *
152      * @return the servlet context
153      */
154     public ServletContext getServletContext() {
155         return session.getServletContext();
156     }
157 
158     /***
159      * Returns the http session context.
160      *
161      * @return the http session context
162      */
163     public HttpSessionContext getSessionContext() {
164         return session.getSessionContext();
165     }
166 
167     /***
168      * Returns the value of an object.
169      *
170      * @param key the key of the object
171      *
172      * @return the value of the object
173      */
174     public Object getValue(String key) {
175         return session.getValue(key);
176     }
177 
178     /***
179      * Returns an array of the value names
180      *
181      * @return an array of the value names
182      */
183     public String[] getValueNames() {
184         return session.getValueNames();
185     }
186 
187     /***
188      * Invalidate the session
189      */
190     public void invalidate() {
191         session.invalidate();
192     }
193 
194     /***
195      * Set the value of an attribute AND notify the event to WFNM.
196      *
197      * @param key the key of the attrubute
198      * @param value the value of the attribute
199      */
200     public void putValue(String key, Object value) {
201         session.putValue(key, value);
202         NavigationContextFactory.getInstance().setAttribute(this, key);
203     }
204 
205     /***
206      * Removes the attribute AND notify the event to WFNM.
207      *
208      * @param key the key of the attribute
209      */
210     public void removeAttribute(String key) {
211         session.removeAttribute(key);
212         NavigationContextFactory.getInstance().removeAttribute(this, key);
213     }
214 
215     /***
216      * Removes an attribute.
217      *
218      * @param key the attribute key
219      */
220     public void removeAttributeValue(String key) {
221         session.removeAttribute(key);
222     }
223 
224     /***
225      * Removes the attribute AND notify the event to WFNM.
226      *
227      * @param key the key of the attribute
228      */
229     public void removeValue(String key) {
230         session.removeValue(key);
231         NavigationContextFactory.getInstance().removeAttribute(this, key);
232     }
233 }