01: package com.ibatis.struts.httpmap;
02:
03: import java.util.Enumeration;
04:
05: import javax.servlet.http.Cookie;
06: import javax.servlet.http.HttpServletRequest;
07:
08: /**
09: * Map to wrap cookie names and values (READ ONLY). <p/>Date: Mar 11, 2004
10: * 11:31:35 PM
11: *
12: * @author Clinton Begin
13: */
14: public class CookieMap extends BaseHttpMap {
15:
16: private Cookie[] cookies;
17:
18: public CookieMap(HttpServletRequest request) {
19: cookies = request.getCookies();
20: }
21:
22: protected Enumeration getNames() {
23: return new CookieEnumerator(cookies);
24: }
25:
26: protected Object getValue(Object key) {
27: for (int i = 0; i < cookies.length; i++) {
28: if (key.equals(cookies[i].getName())) {
29: return cookies[i].getValue();
30: }
31: }
32: return null;
33: }
34:
35: protected void putValue(Object key, Object value) {
36: throw new UnsupportedOperationException();
37: }
38:
39: protected void removeValue(Object key) {
40: throw new UnsupportedOperationException();
41: }
42:
43: /**
44: * Cookie Enumerator Class
45: */
46: private class CookieEnumerator implements Enumeration {
47:
48: private int i = 0;
49:
50: private Cookie[] cookieArray;
51:
52: public CookieEnumerator(Cookie[] cookies) {
53: this .cookieArray = cookies;
54: }
55:
56: public synchronized boolean hasMoreElements() {
57: return cookieArray.length > i;
58: }
59:
60: public synchronized Object nextElement() {
61: Object element = cookieArray[i];
62: i++;
63: return element;
64: }
65:
66: }
67:
68: }
|