KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > acegi > SavedRequest


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.blandware.atleap.webapp.acegi;
17
18 import javax.servlet.http.Cookie JavaDoc;
19 import javax.servlet.http.HttpServletRequest JavaDoc;
20 import java.util.*;
21
22 /**
23  * <p>
24  * Object that saves the critical information from a request so that
25  * form-based authentication can reproduce it once the user has been
26  * authenticated.
27  * <p>
28  * <b>IMPLEMENTATION NOTE</b> - It is assumed that this object is accessed
29  * only from the context of a single thread, so no synchronization around
30  * internal collection classes is performed.
31  * <p>
32  * <b>FIXME</b> - Currently, this object has no mechanism to save or
33  * restore the data content of the request, although it does save
34  * request parameters so that a POST transaction can be faithfully
35  * duplicated.
36  * </p>
37  * <p><a HREF="SavedRequest.java.htm"><i>View Source</i></a></p>
38  * <p>The original source code is got from Apache Tomcat<p>
39  *
40  * @author Craig R. McClanahan
41  * @author Andrey Grebnev <a HREF="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
42  * @version $Revision: 1.3 $ $Date: 2005/08/11 18:27:41 $
43  */

44 public class SavedRequest {
45
46     /**
47      * The set of Cookies associated with this Request.
48      */

49     private ArrayList cookies = new ArrayList();
50
51     /**
52      * Adds cookie to list of cookies
53      *
54      * @param cookie cookie to add
55      */

56     public void addCookie(Cookie JavaDoc cookie) {
57         cookies.add(cookie);
58     }
59
60     /**
61      * Returns list of cookies
62      *
63      * @return list of cookies
64      */

65     public List getCookies() {
66         return cookies;
67     }
68
69
70     /**
71      * The set of Headers associated with this Request. Each key is a header
72      * name, while the value is a ArrayList containing one or more actual
73      * values for this header. The values are returned as an Iterator when
74      * you ask for them.
75      */

76     private HashMap headers = new HashMap();
77
78     /**
79      * Adds header
80      *
81      * @param name header name
82      * @param value header value
83      */

84     public void addHeader(String JavaDoc name, String JavaDoc value) {
85         ArrayList values = (ArrayList) headers.get(name);
86         if (values == null) {
87             values = new ArrayList();
88             headers.put(name, values);
89         }
90         values.add(value);
91     }
92
93     /**
94      * Returns iterator over header names
95      *
96      * @return iterator over header names
97      */

98     public Iterator getHeaderNames() {
99         return (headers.keySet().iterator());
100     }
101
102     /**
103      * Returns iterator over header values
104      *
105      * @param name header name
106      * @return iterator over header values
107      */

108     public Iterator getHeaderValues(String JavaDoc name) {
109         ArrayList values = (ArrayList) headers.get(name);
110         if (values == null)
111             return ((new ArrayList()).iterator());
112         else
113             return (values.iterator());
114     }
115
116
117     /**
118      * The set of Locales associated with this Request.
119      */

120     private ArrayList locales = new ArrayList();
121
122     /**
123      * Adds locale
124      *
125      * @param locale locale to add
126      */

127     public void addLocale(Locale locale) {
128         locales.add(locale);
129     }
130
131     /**
132      * Returns iterator over locales
133      *
134      * @return iterator over locales
135      */

136     public Iterator getLocales() {
137         return (locales.iterator());
138     }
139
140
141     /**
142      * The request method used on this Request.
143      */

144     private String JavaDoc method = null;
145
146     /**
147      * Returns request method
148      *
149      * @return request method
150      */

151     public String JavaDoc getMethod() {
152         return (this.method);
153     }
154
155     /**
156      * Sets request method
157      *
158      * @param method request method to set
159      */

160     public void setMethod(String JavaDoc method) {
161         this.method = method;
162     }
163
164
165
166     /**
167      * The set of request parameters associated with this Request. Each
168      * entry is keyed by the parameter name, pointing at a String array of
169      * the corresponding values.
170      */

171     private HashMap parameters = new HashMap();
172
173     /**
174      * Adds parameter
175      *
176      * @param name parameter name
177      * @param values parameter values
178      */

179     public void addParameter(String JavaDoc name, String JavaDoc values[]) {
180         parameters.put(name, values);
181     }
182
183     /**
184      * Returns iterator over parameter names
185      *
186      * @return iterator over parameter names
187      */

188     public Iterator getParameterNames() {
189         return (parameters.keySet().iterator());
190     }
191
192     /**
193      * Returns parameter values
194      *
195      * @param name parameter name
196      * @return parameter values
197      */

198     public String JavaDoc[] getParameterValues(String JavaDoc name) {
199         return ((String JavaDoc[]) parameters.get(name));
200     }
201
202     /**
203      * Returns parameters
204      *
205      * @return parameters map
206      */

207     public Map getParameterMap() {
208         return parameters;
209     }
210
211
212     /**
213      * The query string associated with this Request.
214      */

215     private String JavaDoc queryString = null;
216
217     /**
218      * Returns query string
219      *
220      * @return query string
221      */

222     public String JavaDoc getQueryString() {
223         return (this.queryString);
224     }
225
226     /**
227      * Sets query string
228      *
229      * @param queryString query string to set
230      */

231     public void setQueryString(String JavaDoc queryString) {
232         this.queryString = queryString;
233     }
234
235
236     /**
237      * The request URI associated with this Request.
238      */

239     private String JavaDoc requestURI = null;
240
241     /**
242      * Returns request URI
243      *
244      * @return request URI
245      */

246     public String JavaDoc getRequestURI() {
247         return (this.requestURI);
248     }
249
250     /**
251      * Sets request URI
252      *
253      * @param requestURI request URI to set
254      */

255     public void setRequestURI(String JavaDoc requestURI) {
256         this.requestURI = requestURI;
257     }
258
259     /**
260      * The request pathInfo associated with this Request.
261      */

262     private String JavaDoc pathInfo = null;
263
264     /**
265      * Returns path info
266      *
267      * @return path info
268      */

269     public String JavaDoc getPathInfo() {
270         return pathInfo;
271     }
272
273     /**
274      * Sets path info
275      *
276      * @param pathInfo path info to set
277      */

278     public void setPathInfo(String JavaDoc pathInfo) {
279         this.pathInfo = pathInfo;
280     }
281
282     /**
283      * Gets uri with path info and query string
284      *
285      * @return uri with path info and query string
286      */

287     public String JavaDoc getRequestURL() {
288         if (getRequestURI() == null)
289             return null;
290
291         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(getRequestURI());
292 // if (getPathInfo() != null) {
293
// sb.append(getPathInfo());
294
// }
295
if (getQueryString() != null) {
296             sb.append('?');
297             sb.append(getQueryString());
298         }
299         return sb.toString();
300     }
301
302     /**
303      * This method provides ability to create SavedRequest from HttpServletRequest
304      * @param request request to be saved
305      * @return saved request resulting SavedRequest
306      */

307     public static SavedRequest saveRequest(HttpServletRequest JavaDoc request) {
308         if (request.getRequestURI() == null)
309             return null;
310
311         // Create and populate a SavedRequest object for this request
312
SavedRequest saved = new SavedRequest();
313         Cookie JavaDoc cookies[] = request.getCookies();
314         if (cookies != null) {
315             for (int i = 0; i < cookies.length; i++)
316                 saved.addCookie(cookies[i]);
317         }
318         Enumeration names = request.getHeaderNames();
319         while (names.hasMoreElements()) {
320             String JavaDoc name = (String JavaDoc) names.nextElement();
321             Enumeration values = request.getHeaders(name);
322             while (values.hasMoreElements()) {
323                 String JavaDoc value = (String JavaDoc) values.nextElement();
324                 saved.addHeader(name, value);
325             }
326         }
327         Enumeration locales = request.getLocales();
328         while (locales.hasMoreElements()) {
329             Locale locale = (Locale) locales.nextElement();
330             saved.addLocale(locale);
331         }
332         Map parameters = request.getParameterMap();
333         Iterator paramNames = parameters.keySet().iterator();
334         while (paramNames.hasNext()) {
335             String JavaDoc paramName = (String JavaDoc) paramNames.next();
336             String JavaDoc paramValues[] = (String JavaDoc[]) parameters.get(paramName);
337             saved.addParameter(paramName, paramValues);
338         }
339         saved.setMethod(request.getMethod());
340         saved.setQueryString(request.getQueryString());
341         saved.setRequestURI(request.getRequestURI());
342 // saved.setPathInfo(request.getPathInfo());
343

344         return saved;
345     }
346
347
348 }
349
Popular Tags