View Javadoc

1   /*
2    * Copyright 2004 The Apache Software Foundation.
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  package org.apache.portals.bridges.jsf;
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.portlet.PortletRequest;
25  
26  
27  /***
28   * <p>
29   * This must be the set of properties available via the javax.portlet.PortletRequest methods getProperties()
30   * and getPropertyNames(). As such, HTTP headers will only be included if they were provided by the portlet
31   * container, and additional properties provided by the portlet container may also be included.
32   * </p>
33   * <p>
34   * See MyFaces project for servlet implementation.
35   * </p>
36   * 
37   * @author <a href="dlestrat@apache.org">David Le Strat </a>
38   */
39  public class RequestHeaderValuesMap extends AbstractAttributeMap
40  {
41      /*** The portlet request. */
42      private final PortletRequest portletRequest;
43      
44      /*** Value cache. */
45      private final Map valueCache = new HashMap();
46  
47      /***
48       * @param portletRequest The {@link PortletRequest}.
49       */
50      RequestHeaderValuesMap(PortletRequest portletRequest)
51      {
52          this.portletRequest = portletRequest;
53      }
54  
55      /***
56       * @see org.apache.portals.bridges.jsf.AbstractAttributeMap#getAttribute(java.lang.String)
57       */
58      protected Object getAttribute(String key)
59      {
60          Object ret = valueCache.get(key);
61          if (ret == null)
62          {
63              valueCache.put(key, ret = toArray(portletRequest.getProperties(key)));
64          }
65          return ret;
66      }
67  
68      /***
69       * @see org.apache.portals.bridges.jsf.AbstractAttributeMap#setAttribute(java.lang.String, java.lang.Object)
70       */
71      protected void setAttribute(String key, Object value)
72      {
73          throw new UnsupportedOperationException(
74              "Cannot set PortletRequest Properties");
75      }
76  
77      /***
78       * @see org.apache.portals.bridges.jsf.AbstractAttributeMap#removeAttribute(java.lang.String)
79       */
80      protected void removeAttribute(String key)
81      {
82          throw new UnsupportedOperationException(
83              "Cannot remove PortletRequest Properties");
84      }
85  
86      /***
87       * @see org.apache.portals.bridges.jsf.AbstractAttributeMap#getAttributeNames()
88       */
89      protected Enumeration getAttributeNames()
90      {
91          return portletRequest.getPropertyNames();
92      }
93  
94      /***
95       * @param e The enumeration.
96       * @return An array of strings.
97       */
98      private String[] toArray(Enumeration e)
99      {
100         List ret = new ArrayList();
101 
102         while (e.hasMoreElements())
103         {
104             ret.add(e.nextElement());
105         }
106 
107         return (String[]) ret.toArray(new String[ret.size()]);
108     }
109 }