01: // WebOnSwing - Web Application Framework
02: //Copyright (C) 2004 Fernando Damian Petrola
03: //
04: //This library is free software; you can redistribute it and/or
05: //modify it under the terms of the GNU Lesser General Public
06: //License as published by the Free Software Foundation; either
07: //version 2.1 of the License, or (at your option) any later version.
08: //
09: //This library is distributed in the hope that it will be useful,
10: //but WITHOUT ANY WARRANTY; without even the implied warranty of
11: //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12: //Lesser General Public License for more details.
13: //
14: //You should have received a copy of the GNU Lesser General Public
15: //License along with this library; if not, write to the Free Software
16: //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17:
18: package net.ar.webonswing.pages;
19:
20: import java.util.Enumeration;
21: import java.util.Iterator;
22: import java.util.Map;
23: import java.util.Map.Entry;
24:
25: import javax.servlet.http.HttpServletRequest;
26: import javax.servlet.http.HttpServletResponse;
27: import javax.servlet.http.HttpSession;
28:
29: import net.ar.webonswing.WosFramework;
30:
31: public class SaveContextDefaultHtmlPage extends DefaultHtmlPage {
32: protected HttpSession session;
33: protected HttpServletRequest request;
34:
35: public void dispatchEvents() {
36: super .dispatchEvents();
37:
38: copyAttributesToHttpSession(session);
39: }
40:
41: public static void copyAttributesToHttpSession(HttpSession aSession) {
42: removeLastAttributes(aSession);
43: Map sessionContext = WosFramework.getSessionContext();
44: for (Iterator i = sessionContext.entrySet().iterator(); i
45: .hasNext();) {
46: Map.Entry entry = (Entry) i.next();
47: aSession.setAttribute((String) entry.getKey(), entry
48: .getValue());
49: }
50: }
51:
52: protected static void removeLastAttributes(HttpSession aSession) {
53: Enumeration names2 = aSession.getAttributeNames();
54: while (names2.hasMoreElements())
55: aSession.removeAttribute((String) names2.nextElement());
56: }
57:
58: public void renderPage(HttpServletResponse aResponse)
59: throws Exception {
60: super .renderPage(aResponse);
61: copyAttributesToHttpSession(session);
62: }
63:
64: public void prepareFromRequest(HttpServletRequest aRequest) {
65: session = aRequest.getSession();
66:
67: copyAttributesFromHttpSession(session);
68: super .prepareFromRequest(aRequest);
69: }
70:
71: public void setRequest(HttpServletRequest aRequest) {
72: session = aRequest.getSession();
73: }
74:
75: public static void copyAttributesFromHttpSession(
76: HttpSession aSession) {
77: Map sessionContext = WosFramework.getSessionContext();
78: sessionContext.clear();
79: Enumeration sessionAttributesNames = aSession
80: .getAttributeNames();
81: while (sessionAttributesNames.hasMoreElements()) {
82: String name = (String) sessionAttributesNames.nextElement();
83: Object attribute = aSession.getAttribute(name);
84: if (attribute != null)
85: sessionContext.put(name, attribute);
86: }
87: }
88: }
|