Coverage Report - net.sf.jabref.net.CookieHandlerImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
CookieHandlerImpl
0%
0/30
0%
0/20
6
 
 1  
 package net.sf.jabref.net;
 2  
 
 3  
 import java.net.CookieHandler;
 4  
 import java.net.URI;
 5  
 import java.util.*;
 6  
 import java.io.IOException;
 7  
 
 8  
 /**
 9  
  * 
 10  
  */
 11  0
 public class CookieHandlerImpl extends CookieHandler {
 12  
 
 13  
     // "Long" term storage for cookies, not serialized so only
 14  
     // for current JVM instance
 15  0
     private List<Cookie> cache = new LinkedList<Cookie>();
 16  
 
 17  
     /**
 18  
      * Saves all applicable cookies present in the response
 19  
      * headers into cache.
 20  
      *
 21  
      * @param uri             URI source of cookies
 22  
      * @param responseHeaders Immutable map from field names to
 23  
      *                        lists of field
 24  
      *                        values representing the response header fields returned
 25  
      */
 26  
 
 27  
     public void put(
 28  
             URI uri,
 29  
             Map<String, List<String>> responseHeaders)
 30  
             throws IOException {
 31  
 
 32  0
         List<String> setCookieList =
 33  
                 responseHeaders.get("Set-Cookie");
 34  0
         if (setCookieList != null) {
 35  0
             for (String item : setCookieList) {
 36  0
                 Cookie cookie = new Cookie(uri, item);
 37  
                 // Remove cookie if it already exists
 38  
                 // New one will replace
 39  0
                 for (Iterator<Cookie> i = cache.iterator(); i.hasNext();) {
 40  0
                     Cookie existingCookie = i.next();
 41  0
                     if ((cookie.getURI().equals(
 42  
                             existingCookie.getURI())) &&
 43  
                             (cookie.getName().equals(
 44  
                                     existingCookie.getName()))) {
 45  0
                         i.remove();
 46  0
                         break;
 47  
                     }
 48  0
                 }
 49  0
                 cache.add(cookie);
 50  0
             }
 51  
         }
 52  0
     }
 53  
 
 54  
     /**
 55  
      * Gets all the applicable cookies from a cookie cache for
 56  
      * the specified uri in the request header.
 57  
      *
 58  
      * @param uri            URI to send cookies to in a request
 59  
      * @param requestHeaders Map from request header field names
 60  
      *                       to lists of field values representing the current request
 61  
      *                       headers
 62  
      * @return Immutable map, with field name "Cookie" to a list
 63  
      *         of cookies
 64  
      */
 65  
 
 66  
     public Map<String, List<String>> get(
 67  
             URI uri,
 68  
             Map<String, List<String>> requestHeaders)
 69  
             throws IOException {
 70  
 
 71  
         // Retrieve all the cookies for matching URI
 72  
         // Put in comma-separated list
 73  0
         StringBuilder cookies = new StringBuilder();
 74  0
         for (Iterator<Cookie> i = cache.iterator(); i.hasNext();) {
 75  
         //for (Cookie cookie : cache) {
 76  0
             Cookie cookie = i.next();
 77  
             // Remove cookies that have expired
 78  0
             if (cookie.hasExpired()) {
 79  0
                 i.remove();
 80  0
             } else if (cookie.matches(uri)) {
 81  0
                 if (cookies.length() > 0) {
 82  0
                     cookies.append(", ");
 83  
                 }
 84  0
                 cookies.append(cookie.toString());
 85  
             }
 86  0
         }
 87  
 
 88  
         // Map to return
 89  0
         Map<String, List<String>> cookieMap =
 90  
                 new HashMap<String, List<String>>(requestHeaders);
 91  
 
 92  
         // Convert StringBuilder to List, store in map
 93  0
         if (cookies.length() > 0) {
 94  0
             List<String> list =
 95  
                     Collections.singletonList(cookies.toString());
 96  0
             cookieMap.put("Cookie", list);
 97  
         }
 98  0
         return Collections.unmodifiableMap(cookieMap);
 99  
     }
 100  
 }
 101  
 
 102