001: /*
002: * Copyright 2005-2006 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.commons.chain.web.servlet;
017:
018: import java.util.ArrayList;
019: import java.util.Collection;
020: import java.util.HashSet;
021: import java.util.List;
022: import java.util.Map;
023: import java.util.Set;
024: import javax.servlet.http.HttpServletRequest;
025: import javax.servlet.http.Cookie;
026: import org.apache.commons.chain.web.MapEntry;
027:
028: /**
029: * <p>Private implementation of <code>Map</code> for servlet cookies</p>
030: *
031: * @author Niall Pemberton
032: * @version $Revision: 411948 $ $Date: 2006-06-06 00:29:02 +0100 (Tue, 06 Jun 2006) $
033: * @since Chain 1.1
034: */
035:
036: final class ServletCookieMap implements Map {
037:
038: public ServletCookieMap(HttpServletRequest request) {
039: this .request = request;
040: }
041:
042: private HttpServletRequest request = null;
043:
044: public void clear() {
045: throw new UnsupportedOperationException();
046: }
047:
048: public boolean containsKey(Object key) {
049: return (get(key) != null);
050: }
051:
052: public boolean containsValue(Object value) {
053: Cookie[] cookies = request.getCookies();
054: if (cookies != null) {
055: for (int i = 0; i < cookies.length; i++) {
056: if (cookies[i].equals(value)) {
057: return true;
058: }
059: }
060: }
061: return (false);
062: }
063:
064: public Set entrySet() {
065: Set set = new HashSet();
066: Cookie[] cookies = request.getCookies();
067: if (cookies != null) {
068: for (int i = 0; i < cookies.length; i++) {
069: set.add(new MapEntry(cookies[i].getName(), cookies[i],
070: false));
071: }
072: }
073: return (set);
074: }
075:
076: public boolean equals(Object o) {
077: return (request.equals(o));
078: }
079:
080: public Object get(Object key) {
081: Cookie[] cookies = request.getCookies();
082: if (cookies != null) {
083: for (int i = 0; i < cookies.length; i++) {
084: if (cookies[i].getName().equals(key(key))) {
085: return cookies[i];
086: }
087: }
088: }
089: return null;
090: }
091:
092: public int hashCode() {
093: return (request.hashCode());
094: }
095:
096: public boolean isEmpty() {
097: return (size() < 1);
098: }
099:
100: public Set keySet() {
101: Set set = new HashSet();
102: Cookie[] cookies = request.getCookies();
103: if (cookies != null) {
104: for (int i = 0; i < cookies.length; i++) {
105: set.add(cookies[i].getName());
106: }
107: }
108: return (set);
109: }
110:
111: public Object put(Object key, Object value) {
112: throw new UnsupportedOperationException();
113: }
114:
115: public void putAll(Map map) {
116: throw new UnsupportedOperationException();
117: }
118:
119: public Object remove(Object key) {
120: throw new UnsupportedOperationException();
121: }
122:
123: public int size() {
124: Cookie[] cookies = request.getCookies();
125: return (cookies == null ? 0 : cookies.length);
126: }
127:
128: public Collection values() {
129: List list = new ArrayList(size());
130: Cookie[] cookies = request.getCookies();
131: if (cookies != null) {
132: for (int i = 0; i < cookies.length; i++) {
133: list.add(cookies[i]);
134: }
135: }
136: return (list);
137: }
138:
139: private String key(Object key) {
140: if (key == null) {
141: throw new IllegalArgumentException();
142: } else if (key instanceof String) {
143: return ((String) key);
144: } else {
145: return (key.toString());
146: }
147: }
148:
149: }
|