View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.portals.bridges.jsf;
18  
19  import java.util.ArrayList;
20  import java.util.Enumeration;
21  import java.util.HashMap;
22  import java.util.List;
23  import java.util.Map;
24  
25  import javax.servlet.http.HttpServletRequest;
26  
27  
28  /***
29   * HttpServletRequest header values (multi-value headers) as Map of String[].
30   * @author Apache MyFaces team
31   */
32  public class ServletRequestHeaderValuesMap extends AbstractAttributeMap
33  {
34      private final HttpServletRequest _httpServletRequest;
35      private final Map                _valueCache = new HashMap();
36  
37      ServletRequestHeaderValuesMap(HttpServletRequest httpServletRequest)
38      {
39          _httpServletRequest = httpServletRequest;
40      }
41  
42      protected Object getAttribute(String key)
43      {
44          Object ret = _valueCache.get(key);
45          if (ret == null)
46          {
47              _valueCache.put(key, ret = toArray(_httpServletRequest
48                  .getHeaders(key)));
49          }
50  
51          return ret;
52      }
53  
54      protected void setAttribute(String key, Object value)
55      {
56          throw new UnsupportedOperationException(
57              "Cannot set HttpServletRequest HeaderValues");
58      }
59  
60      protected void removeAttribute(String key)
61      {
62          throw new UnsupportedOperationException(
63              "Cannot remove HttpServletRequest HeaderValues");
64      }
65  
66      protected Enumeration getAttributeNames()
67      {
68          return _httpServletRequest.getHeaderNames();
69      }
70  
71      private String[] toArray(Enumeration e)
72      {
73          List ret = new ArrayList();
74  
75          while (e.hasMoreElements())
76          {
77              ret.add(e.nextElement());
78          }
79  
80          return (String[]) ret.toArray(new String[ret.size()]);
81      }
82  }