001: /*
002: * Copyright 1999-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.commons.chain.web.servlet;
017:
018: import java.util.ArrayList;
019: import java.util.Collection;
020: import java.util.Enumeration;
021: import java.util.HashSet;
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Map;
025: import java.util.Set;
026: import javax.servlet.http.HttpServletRequest;
027: import org.apache.commons.chain.web.MapEntry;
028:
029: /**
030: * <p>Private implementation of <code>Map</code> for servlet request
031: * name-value.</p>
032: *
033: * @author Craig R. McClanahan
034: * @version $Revision: 155403 $ $Date: 2005-02-26 12:52:46 +0000 (Sat, 26 Feb 2005) $
035: */
036:
037: final class ServletHeaderMap implements Map {
038:
039: public ServletHeaderMap(HttpServletRequest request) {
040: this .request = request;
041: }
042:
043: private HttpServletRequest request = null;
044:
045: public void clear() {
046: throw new UnsupportedOperationException();
047: }
048:
049: public boolean containsKey(Object key) {
050: return (request.getHeader(key(key)) != null);
051: }
052:
053: public boolean containsValue(Object value) {
054: Iterator values = values().iterator();
055: while (values.hasNext()) {
056: if (value.equals(values.next())) {
057: return (true);
058: }
059: }
060: return (false);
061: }
062:
063: public Set entrySet() {
064: Set set = new HashSet();
065: Enumeration keys = request.getHeaderNames();
066: String key;
067: while (keys.hasMoreElements()) {
068: key = (String) keys.nextElement();
069: set.add(new MapEntry(key, request.getHeader(key), false));
070: }
071: return (set);
072: }
073:
074: public boolean equals(Object o) {
075: return (request.equals(o));
076: }
077:
078: public Object get(Object key) {
079: return (request.getHeader(key(key)));
080: }
081:
082: public int hashCode() {
083: return (request.hashCode());
084: }
085:
086: public boolean isEmpty() {
087: return (size() < 1);
088: }
089:
090: public Set keySet() {
091: Set set = new HashSet();
092: Enumeration keys = request.getHeaderNames();
093: while (keys.hasMoreElements()) {
094: set.add(keys.nextElement());
095: }
096: return (set);
097: }
098:
099: public Object put(Object key, Object value) {
100: throw new UnsupportedOperationException();
101: }
102:
103: public void putAll(Map map) {
104: throw new UnsupportedOperationException();
105: }
106:
107: public Object remove(Object key) {
108: throw new UnsupportedOperationException();
109: }
110:
111: public int size() {
112: int n = 0;
113: Enumeration keys = request.getHeaderNames();
114: while (keys.hasMoreElements()) {
115: keys.nextElement();
116: n++;
117: }
118: return (n);
119: }
120:
121: public Collection values() {
122: List list = new ArrayList();
123: Enumeration keys = request.getHeaderNames();
124: while (keys.hasMoreElements()) {
125: list.add(request.getHeader((String) keys.nextElement()));
126: }
127: return (list);
128: }
129:
130: private String key(Object key) {
131: if (key == null) {
132: throw new IllegalArgumentException();
133: } else if (key instanceof String) {
134: return ((String) key);
135: } else {
136: return (key.toString());
137: }
138: }
139:
140: }
|