KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > struts > httpmap > CookieMap


1 package com.ibatis.struts.httpmap;
2
3 import com.ibatis.struts.httpmap.BaseHttpMap;
4
5 import javax.servlet.http.Cookie JavaDoc;
6 import javax.servlet.http.HttpServletRequest JavaDoc;
7 import java.util.Enumeration JavaDoc;
8
9 /**
10  * Map to wrap cookie names and values (READ ONLY).
11  * <p/>
12  * Date: Mar 11, 2004 11:31:35 PM
13  *
14  * @author Clinton Begin
15  */

16 public class CookieMap extends BaseHttpMap {
17
18   private Cookie JavaDoc[] cookies;
19
20   public CookieMap(HttpServletRequest JavaDoc request) {
21     cookies = request.getCookies();
22   }
23
24   protected Enumeration JavaDoc getNames() {
25     return new CookieEnumerator(cookies);
26   }
27
28   protected Object JavaDoc getValue(Object JavaDoc key) {
29     for (int i = 0; i < cookies.length; i++) {
30       if (key.equals(cookies[i].getName())) {
31         return cookies[i].getValue();
32       }
33     }
34     return null;
35   }
36
37   protected void putValue(Object JavaDoc key, Object JavaDoc value) {
38     throw new UnsupportedOperationException JavaDoc();
39   }
40
41   protected void removeValue(Object JavaDoc key) {
42     throw new UnsupportedOperationException JavaDoc();
43   }
44
45   /**
46    * Cookie Enumerator Class
47    */

48   private class CookieEnumerator implements Enumeration JavaDoc {
49
50     private int i = 0;
51
52     private Cookie JavaDoc[] cookieArray;
53
54     public CookieEnumerator(Cookie JavaDoc[] cookies) {
55       this.cookieArray = cookies;
56     }
57
58     public synchronized boolean hasMoreElements() {
59       return cookieArray.length > i;
60     }
61
62     public synchronized Object JavaDoc nextElement() {
63       Object JavaDoc element = cookieArray[i];
64       i++;
65       return element;
66     }
67
68   }
69
70 }
71
Popular Tags