KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > za > org > coefficient > core > CoefficientWebContext


1
2 /*
3  * Coefficient - facilitates project based collaboration
4  * Copyright (C) 2003, Dylan Etkin, CSIR icomtek
5  * PO Box 395
6  * Pretoria 0001, RSA
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */

20
21 package za.org.coefficient.core;
22
23 import org.apache.commons.fileupload.FileUploadException;
24 import org.apache.commons.httpclient.Cookie;
25
26 import za.org.coefficient.util.common.MultipartRequest;
27
28 import java.io.IOException JavaDoc;
29 import java.io.Serializable JavaDoc;
30
31 import java.util.Enumeration JavaDoc;
32 import java.util.Iterator JavaDoc;
33
34 import javax.servlet.http.HttpServletRequest JavaDoc;
35 import javax.servlet.http.HttpServletResponse JavaDoc;
36
37 /**
38  * This is the context created by the CoefficientServlet and passed as
39  * an argument to all coefficient modules. This is used to find information
40  * such as who is logged in and what parameters have just been passed into the
41  * system.
42  * It is also used to delegate certain actions such as setting up page forwards,
43  * reporting errors, and beginning workflow operations.
44  * The class BaseCoefficientContext does all the work this class just
45  * initializes and sychronizes the context with web components, HttpServletRequest
46  * and HttpServletResponse.
47  */

48 public class CoefficientWebContext extends BaseCoefficientContext implements Serializable JavaDoc {
49
50     //~ Constructors ===========================================================
51

52     public CoefficientWebContext(HttpServletRequest JavaDoc request,
53         HttpServletResponse JavaDoc response) throws IOException JavaDoc {
54
55         // Setup the requestURL value from the request
56
this.requestURL = request.getRequestURL().toString();
57
58         // Initialize all the existing cookies into a serializable format
59
javax.servlet.http.Cookie JavaDoc [] cookies = request.getCookies();
60         if(cookies == null) {
61             cookies = new javax.servlet.http.Cookie JavaDoc[0];
62         }
63         serializableExistingCookies = new Cookie[cookies.length];
64         for(int i = 0; i < cookies.length; i++) {
65             Cookie serializableCookie = new Cookie(cookies[i].getDomain(),
66                                                    cookies[i].getName(),
67                                                    cookies[i].getValue(),
68                                                    cookies[i].getPath(),
69                                                    cookies[i].getMaxAge(),
70                                                    cookies[i].getSecure());
71             serializableExistingCookies[i] = serializableCookie;
72         }
73
74         // Initialize all the requestData from the request
75
for(Enumeration JavaDoc enumer = request.getParameterNames();
76             enumer.hasMoreElements(); ) {
77             String JavaDoc paramName = (String JavaDoc)enumer.nextElement();
78             String JavaDoc [] values = request.getParameterValues(paramName);
79             if(values.length == 1) {
80                 requestData.put(paramName, values[0]);
81             } else {
82                 requestData.put(paramName, values);
83             }
84         }
85
86         // Initialize all the requestAttributes from the request
87
for(Enumeration JavaDoc enumer = request.getAttributeNames();
88             enumer.hasMoreElements(); ) {
89             String JavaDoc attrName = (String JavaDoc)enumer.nextElement();
90             Object JavaDoc value = request.getAttribute(attrName);
91             // Only ever put serializable objects into the maps
92
if(value instanceof Serializable JavaDoc) {
93                 requestAttributes.put(attrName, value);
94             }
95         }
96
97         // Initialize all the session information from the request
98
for(Enumeration JavaDoc enumer = request.getSession().getAttributeNames();
99             enumer.hasMoreElements(); ) {
100             String JavaDoc attrName = (String JavaDoc)enumer.nextElement();
101             Object JavaDoc value = request.getSession().getAttribute(attrName);
102             // Only ever put serializable objects into the maps
103
if(value instanceof Serializable JavaDoc) {
104                 session.put(attrName, value);
105             }
106         }
107
108         try {
109             multipartRequest = new MultipartRequest(request,
110                 Integer.parseInt(Constants.MAX_FILE_UPLOAD_SIZE_BYTES));
111             requestData.putAll(multipartRequest.getParameterMap());
112         } catch (NumberFormatException JavaDoc e) {
113             setError("MAX_FILE_UPLOAD_SIZE_BYTES is not an integer!");
114         } catch(FileUploadException fux) {
115             multipartRequest = null;
116             //fux.printStackTrace();
117
}
118     }
119         
120
121     //~ Methods ================================================================
122

123     public void synchronize(HttpServletRequest JavaDoc request,
124         HttpServletResponse JavaDoc response) throws IOException JavaDoc {
125
126         // Make sure if the session has been invalidated that we make it so
127
if(invalidateSession) {
128             request.getSession().invalidate();
129         }
130
131         // First we must synch the session with what is in our session store
132
for(Iterator JavaDoc it = session.keySet().iterator(); it.hasNext(); ) {
133             String JavaDoc sessKey = (String JavaDoc)it.next();
134             Object JavaDoc value = session.get(sessKey);
135             request.getSession().setAttribute(sessKey, value);
136         }
137
138         // Next make sure we remove any session objects that were requested to
139
// be removed
140
for(Iterator JavaDoc it = removedSessionKeys.iterator(); it.hasNext(); ) {
141             String JavaDoc key = (String JavaDoc)it.next();
142             request.getSession().removeAttribute(key);
143         }
144
145         // Next we must make sure any cookies that were set are passed along
146
for(Iterator JavaDoc it = serializableNewCookies.iterator(); it.hasNext(); ) {
147             Cookie cookie = (Cookie)it.next();
148             javax.servlet.http.Cookie JavaDoc cookieToSet =
149                 new javax.servlet.http.Cookie JavaDoc(cookie.getName(), cookie.getValue());
150             long expire = cookie.getExpiryDate().getTime()
151                 - System.currentTimeMillis();
152             if(expire < 0) {
153                 expire = 0;
154             }
155             cookieToSet.setMaxAge((int)expire);
156             response.addCookie(cookieToSet);
157         }
158
159         // Last if we need to forward then do so
160
if(redirectionURL != null) {
161             response.sendRedirect(redirectionURL);
162         }
163     }
164
165 }
166
Popular Tags