Coverage Report - org.apache.myfaces.context.servlet.CookieMap
 
Classes in this File Line Coverage Branch Coverage Complexity
CookieMap
0%
0/30
0%
0/24
2.692
CookieMap$CookieNameEnumeration
0%
0/9
0%
0/4
2.692
 
 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$ $Date$
 36  
  */
 37  
 public final class CookieMap extends AbstractAttributeMap<Object>
 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  
     @Override
 49  
     public void clear()
 50  
     {
 51  0
         throw new UnsupportedOperationException(
 52  
             "Cannot clear HttpRequest Cookies");
 53  
     }
 54  
 
 55  
     @Override
 56  
     public boolean containsValue(final Object findValue)
 57  
     {
 58  0
         if (findValue == null)
 59  
         {
 60  0
             return false;
 61  
         }
 62  
 
 63  0
         final Cookie[] cookies = _httpServletRequest.getCookies();
 64  0
         if (cookies == null)
 65  
         {
 66  0
             return false;
 67  
         }
 68  0
         for (int i = 0, len = cookies.length; i < len; i++)
 69  
         {
 70  0
             if (findValue.equals(cookies[i]))
 71  
             {
 72  0
                 return true;
 73  
             }
 74  
         }
 75  
 
 76  0
         return false;
 77  
     }
 78  
 
 79  
     @Override
 80  
     public boolean isEmpty()
 81  
     {
 82  0
         final Cookie[] cookies = _httpServletRequest.getCookies();
 83  0
         return cookies == null || cookies.length == 0;
 84  
     }
 85  
 
 86  
     @Override
 87  
     public int size()
 88  
     {
 89  0
         final Cookie[] cookies = _httpServletRequest.getCookies();
 90  0
         return cookies == null ? 0 : cookies.length;
 91  
     }
 92  
 
 93  
     @Override
 94  
     public void putAll(final Map<? extends String, ? extends Object> t)
 95  
     {
 96  0
         throw new UnsupportedOperationException();
 97  
     }
 98  
 
 99  
     @Override
 100  
     protected Object getAttribute(final String key)
 101  
     {
 102  0
         final Cookie[] cookies = _httpServletRequest.getCookies();
 103  0
         if (cookies == null)
 104  
         {
 105  0
             return null;
 106  
         }
 107  0
         for (int i = 0, len = cookies.length; i < len; i++)
 108  
         {
 109  0
             if (cookies[i] != null && cookies[i].getName().equals(key))
 110  
             {
 111  0
                 return cookies[i];
 112  
             }
 113  
         }
 114  
 
 115  0
         return null;
 116  
     }
 117  
 
 118  
     @Override
 119  
     protected void setAttribute(final String key, final Object value)
 120  
     {
 121  0
         throw new UnsupportedOperationException(
 122  
             "Cannot set HttpRequest Cookies");
 123  
     }
 124  
 
 125  
     @Override
 126  
     protected void removeAttribute(final String key)
 127  
     {
 128  0
         throw new UnsupportedOperationException(
 129  
             "Cannot remove HttpRequest Cookies");
 130  
     }
 131  
 
 132  
     @Override
 133  
     protected Enumeration<String> getAttributeNames()
 134  
     {
 135  0
         final Cookie[] cookies = _httpServletRequest.getCookies();
 136  
 
 137  0
         return cookies == null ? new CookieNameEnumeration(EMPTY_ARRAY) : new CookieNameEnumeration(cookies);
 138  
   
 139  
     }
 140  
 
 141  0
     private static class CookieNameEnumeration implements Enumeration<String>
 142  
     {
 143  
         private final Cookie[] _cookies;
 144  
         private final int _length;
 145  
         private int _index;
 146  
 
 147  
         public CookieNameEnumeration(final Cookie[] cookies)
 148  0
         {
 149  0
             _cookies = cookies;
 150  0
             _length = cookies.length;
 151  0
         }
 152  
 
 153  
         public boolean hasMoreElements()
 154  
         {
 155  0
             return _index < _length;
 156  
         }
 157  
 
 158  
         public String nextElement()
 159  
         {
 160  0
             if (!hasMoreElements())
 161  
             {
 162  0
                 throw new NoSuchElementException();
 163  
             }
 164  0
             return _cookies[_index++].getName();
 165  
         }
 166  
     }
 167  
 }