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 javax.servlet.http.Cookie;
    20  import javax.servlet.http.HttpServletRequest;
    21  import java.util.Enumeration;
    22  import java.util.Map;
    23
    24  /***
    25   * @author Apache MyFaces team
    26   */
    27  public class ServletCookieMap extends AbstractAttributeMap
    28  {
    29      private static final Cookie[] EMPTY_ARRAY = new Cookie[0];
    30
    31      final HttpServletRequest _httpServletRequest;
    32
    33      ServletCookieMap(HttpServletRequest httpServletRequest)
    34      {
    35          _httpServletRequest = httpServletRequest;
    36      }
    37
    38      public void clear()
    39      {
    40          throw new UnsupportedOperationException(
    41              "Cannot clear HttpRequest Cookies");
    42      }
    43
    44      public boolean containsKey(Object key)
    45      {
    46          Cookie[] cookies = _httpServletRequest.getCookies();
    47          if (cookies == null) return false;
    48          for (int i = 0, len = cookies.length; i < len; i++)
    49          {
    50              if (cookies[i].getName().equals(key))
    51              {
    52                  return true;
    53              }
    54          }
    55
    56          return false;
    57      }
    58
    59      public boolean containsValue(Object findValue)
    60      {
    61          if (findValue == null)
    62          {
    63              return false;
    64          }
    65
    66          Cookie[] cookies = _httpServletRequest.getCookies();
    67          if (cookies == null) return false;
    68          for (int i = 0, len = cookies.length; i < len; i++)
    69          {
    70              if (findValue.equals(cookies[i]))
    71              {
    72                  return true;
    73              }
    74          }
    75
    76          return false;
    77      }
    78
    79      public boolean isEmpty()
    80      {
    81          Cookie[] cookies = _httpServletRequest.getCookies();
    82          return cookies == null || cookies.length == 0;
    83      }
    84
    85      public int size()
    86      {
    87          Cookie[] cookies = _httpServletRequest.getCookies();
    88          return cookies == null ? 0 : cookies.length;
    89      }
    90
    91      public void putAll(Map t)
    92      {
    93          throw new UnsupportedOperationException();
    94      }
    95
    96
    97      protected Object getAttribute(String key)
    98      {
    99          Cookie[] cookies = _httpServletRequest.getCookies();
    100         if (cookies == null) return null;
    101         for (int i = 0, len = cookies.length; i < len; i++)
    102         {
    103             if (cookies[i].getName().equals(key))
    104             {
    105                 return cookies[i];
    106             }
    107         }
    108
    109         return null;
    110     }
    111
    112     protected void setAttribute(String key, Object value)
    113     {
    114         throw new UnsupportedOperationException(
    115             "Cannot set HttpRequest Cookies");
    116     }
    117
    118     protected void removeAttribute(String key)
    119     {
    120         throw new UnsupportedOperationException(
    121             "Cannot remove HttpRequest Cookies");
    122     }
    123
    124     protected Enumeration getAttributeNames()
    125     {
    126         Cookie[] cookies = _httpServletRequest.getCookies();
    127         if (cookies == null)
    128         {
    129             return new CookieNameEnumeration(EMPTY_ARRAY);
    130         }
    131         else
    132         {
    133             return new CookieNameEnumeration(cookies);
    134         }
    135     }
    136
    137     private static class CookieNameEnumeration implements Enumeration {
    138         private final Cookie[] _cookies;
    139         private final int _length;
    140         private int _index;
    141
    142         public CookieNameEnumeration(Cookie[] cookies)
    143         {
    144             _cookies = cookies;
    145             _length = cookies.length;
    146         }
    147
    148         public boolean hasMoreElements()
    149         {
    150             return _index < _length;
    151         }
    152
    153         public Object nextElement()
    154         {
    155             return _cookies[_index++].getName();
    156         }
    157     }
    158 }