001: /*
002: * Copyright 2004 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: package org.apache.myfaces.context.servlet;
017:
018: import java.util.Enumeration;
019: import java.util.Map;
020: import java.util.NoSuchElementException;
021:
022: import javax.servlet.http.Cookie;
023: import javax.servlet.http.HttpServletRequest;
024:
025: import org.apache.myfaces.util.AbstractAttributeMap;
026:
027: /**
028: * HttpServletRequest Cookies as Map.
029: *
030: * @author Dimitry D'hondt
031: * @author Anton Koinov
032: * @version $Revision: 512417 $ $Date: 2007-02-27 22:22:36 +0100 (Di, 27 Feb 2007) $
033: */
034: public class CookieMap extends AbstractAttributeMap<Cookie> {
035: private static final Cookie[] EMPTY_ARRAY = new Cookie[0];
036:
037: final HttpServletRequest _httpServletRequest;
038:
039: CookieMap(HttpServletRequest httpServletRequest) {
040: _httpServletRequest = httpServletRequest;
041: }
042:
043: public void clear() {
044: throw new UnsupportedOperationException(
045: "Cannot clear HttpRequest Cookies");
046: }
047:
048: public boolean containsKey(Object key) {
049: Cookie[] cookies = _httpServletRequest.getCookies();
050: if (cookies == null)
051: return false;
052: for (int i = 0, len = cookies.length; i < len; i++) {
053: if (cookies[i].getName().equals(key)) {
054: return true;
055: }
056: }
057:
058: return false;
059: }
060:
061: @Override
062: public boolean containsValue(Object findValue) {
063: if (findValue == null) {
064: return false;
065: }
066:
067: Cookie[] cookies = _httpServletRequest.getCookies();
068: if (cookies == null)
069: return false;
070: for (int i = 0, len = cookies.length; i < len; i++) {
071: if (findValue.equals(cookies[i])) {
072: return true;
073: }
074: }
075:
076: return false;
077: }
078:
079: @Override
080: public boolean isEmpty() {
081: Cookie[] cookies = _httpServletRequest.getCookies();
082: return cookies == null || cookies.length == 0;
083: }
084:
085: @Override
086: public int size() {
087: Cookie[] cookies = _httpServletRequest.getCookies();
088: return cookies == null ? 0 : cookies.length;
089: }
090:
091: @Override
092: public void putAll(Map t) {
093: throw new UnsupportedOperationException();
094: }
095:
096: @Override
097: protected Cookie getAttribute(String key) {
098: Cookie[] cookies = _httpServletRequest.getCookies();
099: if (cookies == null)
100: return null;
101: for (int i = 0, len = cookies.length; i < len; i++) {
102: if (cookies[i].getName().equals(key)) {
103: return cookies[i];
104: }
105: }
106:
107: return null;
108: }
109:
110: protected void setAttribute(String key, Cookie value) {
111: throw new UnsupportedOperationException(
112: "Cannot set HttpRequest Cookies");
113: }
114:
115: protected void removeAttribute(String key) {
116: throw new UnsupportedOperationException(
117: "Cannot remove HttpRequest Cookies");
118: }
119:
120: protected Enumeration<String> getAttributeNames() {
121: Cookie[] cookies = _httpServletRequest.getCookies();
122:
123: return cookies == null ? new CookieNameEnumeration(EMPTY_ARRAY)
124: : new CookieNameEnumeration(cookies);
125:
126: }
127:
128: private static class CookieNameEnumeration implements
129: Enumeration<String> {
130: private final Cookie[] _cookies;
131: private final int _length;
132: private int _index;
133:
134: public CookieNameEnumeration(Cookie[] cookies) {
135: _cookies = cookies;
136: _length = cookies.length;
137: }
138:
139: public boolean hasMoreElements() {
140: return _index < _length;
141: }
142:
143: public String nextElement() {
144: if (!hasMoreElements()) {
145: throw new NoSuchElementException();
146: }
147: return _cookies[_index++].getName();
148: }
149: }
150: }
|