1 16 package org.apache.myfaces.context.servlet; 17 18 import java.util.Enumeration ; 19 20 import javax.servlet.http.Cookie ; 21 import javax.servlet.http.HttpServletRequest ; 22 23 40 public class CookieMap extends AbstractAttributeMap 41 { 42 private static final Cookie [] EMPTY_ARRAY = new Cookie [0]; 43 44 final HttpServletRequest _httpServletRequest; 45 46 CookieMap(HttpServletRequest httpServletRequest) 47 { 48 _httpServletRequest = httpServletRequest; 49 } 50 51 public void clear() 52 { 53 throw new UnsupportedOperationException ( 54 "Cannot clear HttpRequest Cookies"); 55 } 56 57 public boolean containsKey(Object key) 58 { 59 Cookie [] cookies = _httpServletRequest.getCookies(); 60 if (cookies == null) return false; 61 for (int i = 0, len = cookies.length; i < len; i++) 62 { 63 if (cookies[i].getName().equals(key)) 64 { 65 return true; 66 } 67 } 68 69 return false; 70 } 71 72 public boolean containsValue(Object findValue) 73 { 74 if (findValue == null) 75 { 76 return false; 77 } 78 79 Cookie [] cookies = _httpServletRequest.getCookies(); 80 if (cookies == null) return false; 81 for (int i = 0, len = cookies.length; i < len; i++) 82 { 83 if (findValue.equals(cookies[i].getValue())) 84 { 85 return true; 86 } 87 } 88 89 return false; 90 } 91 92 public boolean isEmpty() 93 { 94 Cookie [] cookies = _httpServletRequest.getCookies(); 95 return cookies == null || cookies.length == 0; 96 } 97 98 public int size() 99 { 100 Cookie [] cookies = _httpServletRequest.getCookies(); 101 return cookies == null ? 0 : cookies.length; 102 } 103 104 protected Object getAttribute(String key) 105 { 106 Cookie [] cookies = _httpServletRequest.getCookies(); 107 if (cookies == null) return null; 108 for (int i = 0, len = cookies.length; i < len; i++) 109 { 110 if (cookies[i].getName().equals(key)) 111 { 112 return cookies[i]; 113 } 114 } 115 116 return null; 117 } 118 119 protected void setAttribute(String key, Object value) 120 { 121 throw new UnsupportedOperationException ( 122 "Cannot set HttpRequest Cookies"); 123 } 124 125 protected void removeAttribute(String key) 126 { 127 throw new UnsupportedOperationException ( 128 "Cannot remove HttpRequest Cookies"); 129 } 130 131 protected Enumeration getAttributeNames() 132 { 133 Cookie [] cookies = _httpServletRequest.getCookies(); 134 if (cookies == null) 135 { 136 return new CookieNameEnumeration(EMPTY_ARRAY); 137 } 138 else 139 { 140 return new CookieNameEnumeration(cookies); 141 } 142 } 143 144 private static class CookieNameEnumeration implements Enumeration 145 { 146 private final Cookie [] _cookies; 147 private final int _length; 148 private int _index; 149 150 public CookieNameEnumeration(Cookie [] cookies) 151 { 152 _cookies = cookies; 153 _length = cookies.length; 154 } 155 156 public boolean hasMoreElements() 157 { 158 return _index < _length; 159 } 160 161 public Object nextElement() 162 { 163 return _cookies[_index++].getName(); 164 } 165 } 166 } 167 | Popular Tags |