KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > chain > web > servlet > ServletHeaderValuesMap


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.chain.web.servlet;
17
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.Enumeration JavaDoc;
22 import java.util.HashSet JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Set JavaDoc;
27 import javax.servlet.http.HttpServletRequest JavaDoc;
28 import org.apache.commons.chain.web.MapEntry;
29
30
31 /**
32  * <p>Private implementation of <code>Map</code> for servlet request
33  * name-values[].</p>
34  *
35  * @author Craig R. McClanahan
36  * @version $Revision: 1.5 $ $Date: 2004/11/30 05:52:23 $
37  */

38
39 final class ServletHeaderValuesMap implements Map JavaDoc {
40
41
42     public ServletHeaderValuesMap(HttpServletRequest JavaDoc request) {
43         this.request = request;
44     }
45
46
47     private HttpServletRequest JavaDoc request = null;
48
49
50     public void clear() {
51         throw new UnsupportedOperationException JavaDoc();
52     }
53
54
55     public boolean containsKey(Object JavaDoc key) {
56         return (request.getHeader(key(key)) != null);
57     }
58
59
60     public boolean containsValue(Object JavaDoc value) {
61         if (!(value instanceof String JavaDoc[])) {
62             return (false);
63         }
64         String JavaDoc[] test = (String JavaDoc[]) value;
65         Iterator JavaDoc values = values().iterator();
66         while (values.hasNext()) {
67             String JavaDoc[] actual = (String JavaDoc[]) values.next();
68             if (test.length == actual.length) {
69                 boolean matched = true;
70                 for (int i = 0; i < test.length; i++) {
71                     if (!test[i].equals(actual[i])) {
72                         matched = false;
73                         break;
74                     }
75                 }
76                 if (matched) {
77                     return (true);
78                 }
79             }
80         }
81         return (false);
82     }
83
84
85     public Set JavaDoc entrySet() {
86         Set JavaDoc set = new HashSet JavaDoc();
87         Enumeration JavaDoc keys = request.getHeaderNames();
88         String JavaDoc key;
89         while (keys.hasMoreElements()) {
90             key = (String JavaDoc) keys.nextElement();
91             set.add(new MapEntry(key, request.getHeaders(key), false));
92         }
93         return (set);
94     }
95
96
97     public boolean equals(Object JavaDoc o) {
98         return (request.equals(o));
99     }
100
101
102     public Object JavaDoc get(Object JavaDoc key) {
103         List JavaDoc list = new ArrayList JavaDoc();
104         Enumeration JavaDoc values = request.getHeaders(key(key));
105         while (values.hasMoreElements()) {
106             list.add((String JavaDoc) values.nextElement());
107         }
108         return (((String JavaDoc[]) list.toArray(new String JavaDoc[list.size()])));
109     }
110
111
112     public int hashCode() {
113         return (request.hashCode());
114     }
115
116
117     public boolean isEmpty() {
118         return (size() < 1);
119     }
120
121
122     public Set JavaDoc keySet() {
123         Set JavaDoc set = new HashSet JavaDoc();
124         Enumeration JavaDoc keys = request.getHeaderNames();
125         while (keys.hasMoreElements()) {
126             set.add(keys.nextElement());
127         }
128         return (set);
129     }
130
131
132     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
133         throw new UnsupportedOperationException JavaDoc();
134     }
135
136
137     public void putAll(Map JavaDoc map) {
138         throw new UnsupportedOperationException JavaDoc();
139     }
140
141
142     public Object JavaDoc remove(Object JavaDoc key) {
143         throw new UnsupportedOperationException JavaDoc();
144     }
145
146
147     public int size() {
148         int n = 0;
149         Enumeration JavaDoc keys = request.getHeaderNames();
150         while (keys.hasMoreElements()) {
151             keys.nextElement();
152             n++;
153         }
154         return (n);
155     }
156
157
158     public Collection JavaDoc values() {
159         List JavaDoc list = new ArrayList JavaDoc();
160         Enumeration JavaDoc keys = request.getHeaderNames();
161         while (keys.hasMoreElements()) {
162             String JavaDoc key = (String JavaDoc) keys.nextElement();
163             List JavaDoc list1 = new ArrayList JavaDoc();
164             Enumeration JavaDoc values = request.getHeaders(key);
165             while (values.hasMoreElements()) {
166                 list1.add((String JavaDoc) values.nextElement());
167             }
168             list.add(((String JavaDoc[]) list1.toArray(new String JavaDoc[list1.size()])));
169         }
170         return (list);
171     }
172
173
174     private String JavaDoc key(Object JavaDoc key) {
175         if (key == null) {
176             throw new IllegalArgumentException JavaDoc();
177         } else if (key instanceof String JavaDoc) {
178             return ((String JavaDoc) key);
179         } else {
180             return (key.toString());
181         }
182     }
183
184
185 }
186
Popular Tags