KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > mock > web > HeaderValueHolder


1 /*
2  * Copyright 2002-2007 the original author or authors.
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
17 package org.springframework.mock.web;
18
19 import java.util.Collection JavaDoc;
20 import java.util.Collections JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.LinkedList JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
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 JavaDoc values = new LinkedList JavaDoc();
39
40
41     public void setValue(Object JavaDoc value) {
42         this.values.clear();
43         this.values.add(value);
44     }
45
46     public void addValue(Object JavaDoc value) {
47         this.values.add(value);
48     }
49
50     public void addValues(Collection JavaDoc values) {
51         this.values.addAll(values);
52     }
53
54     public void addValueArray(Object JavaDoc values) {
55         CollectionUtils.mergeArrayIntoCollection(values, this.values);
56     }
57
58     public List JavaDoc getValues() {
59         return Collections.unmodifiableList(this.values);
60     }
61
62     public Object JavaDoc getValue() {
63         return (!this.values.isEmpty() ? this.values.get(0) : null);
64     }
65
66
67     /**
68      * Find a HeaderValueHolder by name, ignoring casing.
69      * @param headers the Map of header names to HeaderValueHolders
70      * @param name the name of the desired header
71      * @return the corresponding HeaderValueHolder,
72      * or <code>null</code> if none found
73      */

74     public static HeaderValueHolder getByName(Map JavaDoc headers, String JavaDoc name) {
75         Assert.notNull(name, "Header name must not be null");
76         for (Iterator JavaDoc it = headers.keySet().iterator(); it.hasNext();) {
77             String JavaDoc headerName = (String JavaDoc) it.next();
78             if (headerName.equalsIgnoreCase(name)) {
79                 return (HeaderValueHolder) headers.get(headerName);
80             }
81         }
82         return null;
83     }
84
85 }
86
Popular Tags