01: /*
02: * Copyright 2004 The Apache Software Foundation.
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: package org.apache.myfaces.context.servlet;
17:
18: import java.util.ArrayList;
19: import java.util.Enumeration;
20: import java.util.HashMap;
21: import java.util.List;
22: import java.util.Map;
23:
24: import javax.servlet.http.HttpServletRequest;
25:
26: import org.apache.myfaces.util.AbstractAttributeMap;
27:
28: /**
29: * HttpServletRequest header values (multi-value headers) as Map of String[].
30: *
31: * @author Anton Koinov (latest modification by $Author: mbr $)
32: * @version $Revision: 512417 $ $Date: 2007-02-27 22:22:36 +0100 (Di, 27 Feb 2007) $
33: */
34: public class RequestHeaderValuesMap extends
35: AbstractAttributeMap<String[]> {
36: private final HttpServletRequest _httpServletRequest;
37: private final Map<String, String[]> _valueCache = new HashMap<String, String[]>();
38:
39: RequestHeaderValuesMap(HttpServletRequest httpServletRequest) {
40: _httpServletRequest = httpServletRequest;
41: }
42:
43: protected String[] getAttribute(String key) {
44: String[] ret = _valueCache.get(key);
45: if (ret == null) {
46: _valueCache.put(key, ret = toArray(_httpServletRequest
47: .getHeaders(key)));
48: }
49:
50: return ret;
51: }
52:
53: protected void setAttribute(String key, String[] value) {
54: throw new UnsupportedOperationException(
55: "Cannot set HttpServletRequest HeaderValues");
56: }
57:
58: protected void removeAttribute(String key) {
59: throw new UnsupportedOperationException(
60: "Cannot remove HttpServletRequest HeaderValues");
61: }
62:
63: @SuppressWarnings("unchecked")
64: protected Enumeration<String> getAttributeNames() {
65: return _httpServletRequest.getHeaderNames();
66: }
67:
68: private String[] toArray(Enumeration<String> e) {
69: List<String> ret = new ArrayList<String>();
70:
71: while (e.hasMoreElements()) {
72: ret.add(e.nextElement());
73: }
74:
75: return (String[]) ret.toArray(new String[ret.size()]);
76: }
77: }
|