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