Coverage Report - org.apache.myfaces.context.servlet.CookieMap
 
Classes in this File Line Coverage Branch Coverage Complexity
CookieMap
0%
0/31
0%
0/22
0
CookieMap$CookieNameEnumeration
0%
0/9
0%
0/4
0
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements.  See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership.  The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License.  You may obtain a copy of the License at
 9  
  *
 10  
  *   http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing,
 13  
  * software distributed under the License is distributed on an
 14  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  * KIND, either express or implied.  See the License for the
 16  
  * specific language governing permissions and limitations
 17  
  * under the License.
 18  
  */
 19  
 package org.apache.myfaces.context.servlet;
 20  
 
 21  
 import java.util.Enumeration;
 22  
 import java.util.Map;
 23  
 import java.util.NoSuchElementException;
 24  
 
 25  
 import javax.servlet.http.Cookie;
 26  
 import javax.servlet.http.HttpServletRequest;
 27  
 
 28  
 import org.apache.myfaces.util.AbstractAttributeMap;
 29  
 
 30  
 /**
 31  
  * HttpServletRequest Cookies as Map.
 32  
  *
 33  
  * @author Dimitry D'hondt
 34  
  * @author Anton Koinov
 35  
  * @version $Revision: 693059 $ $Date: 2008-09-08 06:42:28 -0500 (Mon, 08 Sep 2008) $
 36  
  */
 37  0
 public final class CookieMap extends AbstractAttributeMap<Cookie>
 38  
 {
 39  0
     private static final Cookie[] EMPTY_ARRAY = new Cookie[0];
 40  
 
 41  
     private final HttpServletRequest _httpServletRequest;
 42  
 
 43  
     CookieMap(final HttpServletRequest httpServletRequest)
 44  0
     {
 45  0
         _httpServletRequest = httpServletRequest;
 46  0
     }
 47  
 
 48  
     public void clear()
 49  
     {
 50  0
         throw new UnsupportedOperationException(
 51  
             "Cannot clear HttpRequest Cookies");
 52  
     }
 53  
 
 54  
     @Override
 55  
     public boolean containsValue(final Object findValue)
 56  
     {
 57  0
         if (findValue == null)
 58  
         {
 59  0
             return false;
 60  
         }
 61  
 
 62  0
         final Cookie[] cookies = _httpServletRequest.getCookies();
 63  0
         if (cookies == null)
 64  0
             return false;
 65  0
         for (int i = 0, len = cookies.length; i < len; i++)
 66  
         {
 67  0
             if (findValue.equals(cookies[i]))
 68  
             {
 69  0
                 return true;
 70  
             }
 71  
         }
 72  
 
 73  0
         return false;
 74  
     }
 75  
 
 76  
     @Override
 77  
     public boolean isEmpty()
 78  
     {
 79  0
         final Cookie[] cookies = _httpServletRequest.getCookies();
 80  0
         return cookies == null || cookies.length == 0;
 81  
     }
 82  
 
 83  
     @Override
 84  
     public int size()
 85  
     {
 86  0
         final Cookie[] cookies = _httpServletRequest.getCookies();
 87  0
         return cookies == null ? 0 : cookies.length;
 88  
     }
 89  
 
 90  
     @Override
 91  
     public void putAll(final Map t)
 92  
     {
 93  0
         throw new UnsupportedOperationException();
 94  
     }
 95  
 
 96  
     @Override
 97  
     protected Cookie getAttribute(final String key)
 98  
     {
 99  0
         final Cookie[] cookies = _httpServletRequest.getCookies();
 100  0
         if (cookies == null)
 101  0
             return null;
 102  0
         for (int i = 0, len = cookies.length; i < len; i++)
 103  
         {
 104  0
             if (cookies[i].getName().equals(key))
 105  
             {
 106  0
                 return cookies[i];
 107  
             }
 108  
         }
 109  
 
 110  0
         return null;
 111  
     }
 112  
 
 113  
     protected void setAttribute(final String key, final Cookie value)
 114  
     {
 115  0
         throw new UnsupportedOperationException(
 116  
             "Cannot set HttpRequest Cookies");
 117  
     }
 118  
 
 119  
     protected void removeAttribute(final String key)
 120  
     {
 121  0
         throw new UnsupportedOperationException(
 122  
             "Cannot remove HttpRequest Cookies");
 123  
     }
 124  
 
 125  
     protected Enumeration<String> getAttributeNames()
 126  
     {
 127  0
         final Cookie[] cookies = _httpServletRequest.getCookies();
 128  
 
 129  0
         return cookies == null ? new CookieNameEnumeration(EMPTY_ARRAY) : new CookieNameEnumeration(cookies);
 130  
   
 131  
     }
 132  
 
 133  0
     private static class CookieNameEnumeration implements Enumeration<String>
 134  
     {
 135  
         private final Cookie[] _cookies;
 136  
         private final int _length;
 137  
         private int _index;
 138  
 
 139  
         public CookieNameEnumeration(final Cookie[] cookies)
 140  0
         {
 141  0
             _cookies = cookies;
 142  0
             _length = cookies.length;
 143  0
         }
 144  
 
 145  
         public boolean hasMoreElements()
 146  
         {
 147  0
             return _index < _length;
 148  
         }
 149  
 
 150  
         public String nextElement()
 151  
         {
 152  0
             if (!hasMoreElements())
 153  
             {
 154  0
                 throw new NoSuchElementException();
 155  
             }
 156  0
             return _cookies[_index++].getName();
 157  
         }
 158  
     }
 159  
 }