Coverage Report - org.apache.commons.feedparser.network.URLCookieManager
 
Classes in this File Line Coverage Branch Coverage Complexity
URLCookieManager
0%
0/48
0%
0/12
2
 
 1  
 /*
 2  
  * Copyright 1999,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  
 
 17  
 package org.apache.commons.feedparser.network;
 18  
 
 19  
 import java.util.Enumeration;
 20  
 import java.util.Hashtable;
 21  
 
 22  
 /**
 23  
  * 
 24  
  * By default java.net.URL does NOT handle cookies.  This is a simple extension
 25  
  * that allows us to persist cookies in the VM during runtime.
 26  
  * 
 27  
  * FIXME: How can we make sure to delete older sites...?!  no need for this to
 28  
  * grow to infinite size.
 29  
  * 
 30  
  * @author <a href="mailto:burton@peerfear.org">Kevin A. Burton</a>
 31  
  */
 32  0
 public class URLCookieManager {
 33  
 
 34  0
     static Hashtable cookies = new Hashtable();
 35  
     
 36  
     /**
 37  
      * Get the cookies for a site.  When none are available return null.
 38  
      *
 39  
      * 
 40  
      */
 41  
     public static Hashtable getCookies( String site ) {
 42  
 
 43  0
         return (Hashtable)cookies.get( site );
 44  
         
 45  
     }
 46  
 
 47  
     /**
 48  
      * Add cookies to this request and perform any other init.
 49  
      *
 50  
      * 
 51  
      */
 52  
     public static void init( ResourceRequest request ) {
 53  
 
 54  0
         String site = getSite( request );
 55  
         
 56  0
         Hashtable cookies = getCookies( site );
 57  
 
 58  0
         if ( cookies == null )
 59  0
             return;
 60  
 
 61  0
         String header = getCookiesHeader( cookies );
 62  
 
 63  0
         request.setRequestHeaderField( "Cookies", header );
 64  
         
 65  0
     }
 66  
 
 67  
     public static String getSite( ResourceRequest request ) {
 68  
 
 69  0
         String resource = request.getResource();
 70  
 
 71  0
         int end = resource.indexOf( "://" );
 72  0
         end = resource.indexOf( "/", end );
 73  
 
 74  0
         if ( end == -1 )
 75  0
             end = resource.length();
 76  
         
 77  0
         return resource.substring( 0, end );
 78  
 
 79  
     }
 80  
     
 81  
     /**
 82  
      * Save the cookies FROM this request.
 83  
      *
 84  
      * 
 85  
      */
 86  
     public static void save( ResourceRequest request ) {
 87  
 
 88  0
         String header = request.getHeaderField( "Set-Cookie" );
 89  
 
 90  0
         Hashtable cookies = parseCookieHeader( header );
 91  
 
 92  0
         String site = getSite( request );
 93  
 
 94  
         //FIXME: merge these... new cookies into the site cookies 
 95  
 
 96  0
     }
 97  
 
 98  
     /**
 99  
      * Parse a given Cookie header into a hashtable.
 100  
      *
 101  
      * 
 102  
      */
 103  
     public static String getCookiesHeader( Hashtable cookies ) {
 104  
 
 105  0
         Enumeration keys = cookies.keys();
 106  
 
 107  0
         StringBuffer buff = new StringBuffer( 1024 );
 108  
 
 109  0
         while ( keys.hasMoreElements() ) {
 110  
 
 111  0
             String name = (String)keys.nextElement();
 112  0
             String value = (String)cookies.get( name );
 113  
 
 114  0
             if ( buff.length() > 0 )
 115  0
                 buff.append( "; " );
 116  
 
 117  0
             buff.append( name );
 118  0
             buff.append( "=" );
 119  0
             buff.append( value );
 120  
             
 121  0
         }
 122  
         
 123  0
         return buff.toString();
 124  
 
 125  
     }
 126  
 
 127  
     /**
 128  
      * Parse a given Cookie header into a hashtable.
 129  
      *
 130  
      * 
 131  
      */
 132  
     public static Hashtable parseCookieHeader( String header ) {
 133  
 
 134  
         //this is a simple format and easy to parse
 135  
 
 136  
         //Cookie: password=HvS11dffnlD50bOLZYgG4oZFA-U
 137  
 
 138  
         /**
 139  
          * Where should we read the cookie name from
 140  
          */
 141  0
         int begin = 0;
 142  
 
 143  
         /**
 144  
          * Where do we spit into the variable
 145  
          */
 146  0
         int split = 0;
 147  
 
 148  
         /**
 149  
          * Where is the end of the cookie.
 150  
          */
 151  0
         int end = 0;
 152  
 
 153  0
         Hashtable result = new Hashtable();
 154  
         
 155  0
         while ( (split = header.indexOf( "=", begin )) != -1 ) {
 156  
 
 157  0
             end = header.indexOf( ";", split );
 158  
 
 159  0
             if ( end == -1 )
 160  0
                 end = header.length();
 161  
             
 162  0
             String name = header.substring( begin, split );
 163  0
             String value = header.substring( split+1, end );
 164  
             
 165  
             //move to the next one.
 166  0
             begin = end + 2;
 167  
 
 168  0
             result.put( name, value );
 169  
             
 170  0
         }
 171  
 
 172  0
         return result;
 173  
         
 174  
     }
 175  
     
 176  
     public static void main( String[] args ) {
 177  
 
 178  0
         parseCookieHeader( "password=HvS11dffnlD50bOLZYgG4oZFA-U; username=burtonator; rojoWeb=12.43.53.196.1091730560640949; JSESSIONID=B1245A7FEB43537E994324A157330F3A" );
 179  
         
 180  0
     }
 181  
 
 182  
 }