01: /*
02: * Copyright 2002-2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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:
17: package org.springframework.mock.web;
18:
19: import java.util.Collection;
20: import java.util.Collections;
21: import java.util.Iterator;
22: import java.util.LinkedList;
23: import java.util.List;
24: import java.util.Map;
25:
26: import org.springframework.util.Assert;
27: import org.springframework.util.CollectionUtils;
28:
29: /**
30: * Internal helper class that serves as value holder for request headers.
31: *
32: * @author Juergen Hoeller
33: * @author Rick Evans
34: * @since 2.0.1
35: */
36: class HeaderValueHolder {
37:
38: private final List values = new LinkedList();
39:
40: public void setValue(Object value) {
41: this .values.clear();
42: this .values.add(value);
43: }
44:
45: public void addValue(Object value) {
46: this .values.add(value);
47: }
48:
49: public void addValues(Collection values) {
50: this .values.addAll(values);
51: }
52:
53: public void addValueArray(Object values) {
54: CollectionUtils.mergeArrayIntoCollection(values, this .values);
55: }
56:
57: public List getValues() {
58: return Collections.unmodifiableList(this .values);
59: }
60:
61: public Object getValue() {
62: return (!this .values.isEmpty() ? this .values.get(0) : null);
63: }
64:
65: /**
66: * Find a HeaderValueHolder by name, ignoring casing.
67: * @param headers the Map of header names to HeaderValueHolders
68: * @param name the name of the desired header
69: * @return the corresponding HeaderValueHolder,
70: * or <code>null</code> if none found
71: */
72: public static HeaderValueHolder getByName(Map headers, String name) {
73: Assert.notNull(name, "Header name must not be null");
74: for (Iterator it = headers.keySet().iterator(); it.hasNext();) {
75: String headerName = (String) it.next();
76: if (headerName.equalsIgnoreCase(name)) {
77: return (HeaderValueHolder) headers.get(headerName);
78: }
79: }
80: return null;
81: }
82:
83: }
|