001: /*
002: * Copyright 2003 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package velosurf.util;
018:
019: import javax.servlet.http.Cookie;
020: import javax.servlet.http.HttpServletRequest;
021: import java.util.*;
022:
023: /**
024: * <p>
025: * Object that saves the critical information from a request so that
026: * form-based authentication can reproduce it once the user has been
027: * authenticated.
028: * <p>
029: * <b>IMPLEMENTATION NOTE</b> - It is assumed that this object is accessed
030: * only from the context of a single thread, so no synchronization around
031: * internal collection classes is performed.
032: * <p>
033: * <b>FIXME</b> - Currently, this object has no mechanism to save or
034: * restore the data content of the request, although it does save
035: * request parameters so that a POST transaction can be faithfully
036: * duplicated.
037: * </p>
038: * <p>The original source code is got from Apache Tomcat<p>
039: *
040: * @author Craig R. McClanahan
041: * @author Andrey Grebnev <a href="mailto:andrey.grebnev@blandware.com"><andrey.grebnev@blandware.com></a>
042: */
043:
044: public class SavedRequest {
045:
046: /**
047: * The set of Cookies associated with this Request.
048: */
049: private ArrayList cookies = new ArrayList();
050:
051: /**
052: * Adds cookie to list of cookies.
053: *
054: * @param cookie cookie to add
055: */
056: public void addCookie(Cookie cookie) {
057: cookies.add(cookie);
058: }
059:
060: /**
061: * Returns list of cookies.
062: *
063: * @return list of cookies
064: */
065: public List getCookies() {
066: return cookies;
067: }
068:
069: /**
070: * The set of Headers associated with this Request. Each key is a header
071: * name, while the value is a ArrayList containing one or more actual
072: * values for this header. The values are returned as an Iterator when
073: * you ask for them.
074: */
075: private HashMap headers = new HashMap();
076:
077: /**
078: * Adds header.
079: *
080: * @param name header name
081: * @param value header value
082: */
083: public void addHeader(String name, String value) {
084: name = name.toLowerCase();
085: ArrayList values = (ArrayList) headers.get(name);
086: if (values == null) {
087: values = new ArrayList();
088: headers.put(name, values);
089: }
090: values.add(value);
091: }
092:
093: /**
094: * Returns iterator over header names.
095: *
096: * @return iterator over header names
097: */
098: public Iterator getHeaderNames() {
099: 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 name) {
109: name = name.toLowerCase();
110: ArrayList values = (ArrayList) headers.get(name);
111: if (values == null)
112: return ((new ArrayList()).iterator());
113: else
114: return (values.iterator());
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: * The request method used on this Request.
142: */
143: private String method = null;
144:
145: /**
146: * Returns request method.
147: *
148: * @return request method
149: */
150: public String getMethod() {
151: return (this .method);
152: }
153:
154: /**
155: * Sets request method.
156: *
157: * @param method request method to set
158: */
159: public void setMethod(String method) {
160: this .method = method;
161: }
162:
163: /**
164: * The set of request parameters associated with this Request. Each
165: * entry is keyed by the parameter name, pointing at a String array of
166: * the corresponding values.
167: */
168: private HashMap parameters = new HashMap();
169:
170: /**
171: * Adds parameter.
172: *
173: * @param name parameter name
174: * @param values parameter values
175: */
176: public void addParameter(String name, String values[]) {
177: parameters.put(name, values);
178: }
179:
180: /**
181: * Returns iterator over parameter names.
182: *
183: * @return iterator over parameter names
184: */
185: public Iterator getParameterNames() {
186: return (parameters.keySet().iterator());
187: }
188:
189: /**
190: * Returns parameter values.
191: *
192: * @param name parameter name
193: * @return parameter values
194: */
195: public String[] getParameterValues(String name) {
196: return ((String[]) parameters.get(name));
197: }
198:
199: /**
200: * Returns parameters.
201: *
202: * @return parameters map
203: */
204: public Map getParameterMap() {
205: return parameters;
206: }
207:
208: /**
209: * The query string associated with this Request.
210: */
211: private String queryString = null;
212:
213: /**
214: * Returns query string.
215: *
216: * @return query string
217: */
218: public String getQueryString() {
219: return (this .queryString);
220: }
221:
222: /**
223: * Sets query string.
224: *
225: * @param queryString query string to set
226: */
227: public void setQueryString(String queryString) {
228: this .queryString = queryString;
229: }
230:
231: /**
232: * The request URI associated with this Request.
233: */
234: private String requestURI = null;
235:
236: /**
237: * Returns request URI.
238: *
239: * @return request URI
240: */
241: public String getRequestURI() {
242: return (this .requestURI);
243: }
244:
245: /**
246: * Sets request URI.
247: *
248: * @param requestURI request URI to set
249: */
250: public void setRequestURI(String requestURI) {
251: this .requestURI = requestURI;
252: }
253:
254: /**
255: * The request pathInfo associated with this Request.
256: */
257: private String pathInfo = null;
258:
259: /**
260: * Returns path info.
261: *
262: * @return path info
263: */
264: public String getPathInfo() {
265: return pathInfo;
266: }
267:
268: /**
269: * Sets path info.
270: *
271: * @param pathInfo path info to set
272: */
273: public void setPathInfo(String pathInfo) {
274: this .pathInfo = pathInfo;
275: }
276:
277: /**
278: * Gets uri with path info and query string.
279: *
280: * @return uri with path info and query string
281: */
282: public String getRequestURL() {
283: if (getRequestURI() == null)
284: return null;
285:
286: StringBuffer sb = new StringBuffer(getRequestURI());
287: // if (getPathInfo() != null) {
288: // sb.append(getPathInfo());
289: // }
290: if (getQueryString() != null) {
291: sb.append('?');
292: sb.append(getQueryString());
293: }
294: return sb.toString();
295: }
296:
297: /**
298: * This method provides ability to create SavedRequest from HttpServletRequest.
299: * @param request request to be saved
300: * @return saved request resulting SavedRequest
301: */
302: public static SavedRequest saveRequest(HttpServletRequest request) {
303: if (request.getRequestURI() == null)
304: return null;
305:
306: // Create and populate a SavedRequest object for this request
307: SavedRequest saved = new SavedRequest();
308: Cookie cookies[] = request.getCookies();
309: if (cookies != null) {
310: for (int i = 0; i < cookies.length; i++)
311: saved.addCookie(cookies[i]);
312: }
313: Enumeration names = request.getHeaderNames();
314: while (names.hasMoreElements()) {
315: String name = (String) names.nextElement();
316: Enumeration values = request.getHeaders(name);
317: while (values.hasMoreElements()) {
318: String value = (String) values.nextElement();
319: saved.addHeader(name, value);
320: }
321: }
322: Enumeration locales = request.getLocales();
323: while (locales.hasMoreElements()) {
324: Locale locale = (Locale) locales.nextElement();
325: saved.addLocale(locale);
326: }
327: Map parameters = request.getParameterMap();
328: for (Map.Entry entry : (Set<Map.Entry>) parameters.entrySet()) {
329: saved.addParameter((String) entry.getKey(),
330: (String[]) entry.getValue());
331: }
332: saved.setMethod(request.getMethod());
333: saved.setQueryString(request.getQueryString());
334: saved.setRequestURI(request.getRequestURI());
335: // saved.setPathInfo(request.getPathInfo());
336:
337: return saved;
338: }
339:
340: }
|