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-values[].</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 ServletHeaderValuesMap implements Map {
038:
039: public ServletHeaderValuesMap(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: if (!(value instanceof String[])) {
055: return (false);
056: }
057: String[] test = (String[]) value;
058: Iterator values = values().iterator();
059: while (values.hasNext()) {
060: String[] actual = (String[]) values.next();
061: if (test.length == actual.length) {
062: boolean matched = true;
063: for (int i = 0; i < test.length; i++) {
064: if (!test[i].equals(actual[i])) {
065: matched = false;
066: break;
067: }
068: }
069: if (matched) {
070: return (true);
071: }
072: }
073: }
074: return (false);
075: }
076:
077: public Set entrySet() {
078: Set set = new HashSet();
079: Enumeration keys = request.getHeaderNames();
080: String key;
081: while (keys.hasMoreElements()) {
082: key = (String) keys.nextElement();
083: set.add(new MapEntry(key, request.getHeaders(key), false));
084: }
085: return (set);
086: }
087:
088: public boolean equals(Object o) {
089: return (request.equals(o));
090: }
091:
092: public Object get(Object key) {
093: List list = new ArrayList();
094: Enumeration values = request.getHeaders(key(key));
095: while (values.hasMoreElements()) {
096: list.add((String) values.nextElement());
097: }
098: return (((String[]) list.toArray(new String[list.size()])));
099: }
100:
101: public int hashCode() {
102: return (request.hashCode());
103: }
104:
105: public boolean isEmpty() {
106: return (size() < 1);
107: }
108:
109: public Set keySet() {
110: Set set = new HashSet();
111: Enumeration keys = request.getHeaderNames();
112: while (keys.hasMoreElements()) {
113: set.add(keys.nextElement());
114: }
115: return (set);
116: }
117:
118: public Object put(Object key, Object value) {
119: throw new UnsupportedOperationException();
120: }
121:
122: public void putAll(Map map) {
123: throw new UnsupportedOperationException();
124: }
125:
126: public Object remove(Object key) {
127: throw new UnsupportedOperationException();
128: }
129:
130: public int size() {
131: int n = 0;
132: Enumeration keys = request.getHeaderNames();
133: while (keys.hasMoreElements()) {
134: keys.nextElement();
135: n++;
136: }
137: return (n);
138: }
139:
140: public Collection values() {
141: List list = new ArrayList();
142: Enumeration keys = request.getHeaderNames();
143: while (keys.hasMoreElements()) {
144: String key = (String) keys.nextElement();
145: List list1 = new ArrayList();
146: Enumeration values = request.getHeaders(key);
147: while (values.hasMoreElements()) {
148: list1.add((String) values.nextElement());
149: }
150: list.add(((String[]) list1
151: .toArray(new String[list1.size()])));
152: }
153: return (list);
154: }
155:
156: private String key(Object key) {
157: if (key == null) {
158: throw new IllegalArgumentException();
159: } else if (key instanceof String) {
160: return ((String) key);
161: } else {
162: return (key.toString());
163: }
164: }
165:
166: }
|