View Javadoc
1   /*
2    * Copyright 2002-2012 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.test;
18  
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.Collections;
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<Object> values = new LinkedList<Object>();
39  
40  
41  	public void setValue(Object value) {
42  		this.values.clear();
43  		this.values.add(value);
44  	}
45  
46  	public void addValue(Object value) {
47  		this.values.add(value);
48  	}
49  
50  	public void addValues(Collection<?> values) {
51  		this.values.addAll(values);
52  	}
53  
54  	public void addValueArray(Object values) {
55  		CollectionUtils.mergeArrayIntoCollection(values, this.values);
56  	}
57  
58  	public List<Object> getValues() {
59  		return Collections.unmodifiableList(this.values);
60  	}
61  
62  	public List<String> getStringValues() {
63  		List<String> stringList = new ArrayList<String>(this.values.size());
64  		for (Object value : this.values) {
65  			stringList.add(value.toString());
66  		}
67  		return Collections.unmodifiableList(stringList);
68  	}
69  
70  	public Object getValue() {
71  		return (!this.values.isEmpty() ? this.values.get(0) : null);
72  	}
73  
74  	public String getStringValue() {
75  		return (!this.values.isEmpty() ? this.values.get(0).toString() : null);
76  	}
77  
78  
79  	/**
80  	 * Find a HeaderValueHolder by name, ignoring casing.
81  	 * @param headers the Map of header names to HeaderValueHolders
82  	 * @param name the name of the desired header
83  	 * @return the corresponding HeaderValueHolder,
84  	 * or {@code null} if none found
85  	 */
86  	public static HeaderValueHolder getByName(Map<String, HeaderValueHolder> headers, String name) {
87  		Assert.notNull(name, "Header name must not be null");
88  		for (String headerName : headers.keySet()) {
89  			if (headerName.equalsIgnoreCase(name)) {
90  				return headers.get(headerName);
91  			}
92  		}
93  		return null;
94  	}
95  
96  }