View Javadoc

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  public class URLCookieManager {
33  
34      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          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          String site = getSite( request );
55          
56          Hashtable cookies = getCookies( site );
57  
58          if ( cookies == null )
59              return;
60  
61          String header = getCookiesHeader( cookies );
62  
63          request.setRequestHeaderField( "Cookies", header );
64          
65      }
66  
67      public static String getSite( ResourceRequest request ) {
68  
69          String resource = request.getResource();
70  
71          int end = resource.indexOf( "://" );
72          end = resource.indexOf( "/", end );
73  
74          if ( end == -1 )
75              end = resource.length();
76          
77          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          String header = request.getHeaderField( "Set-Cookie" );
89  
90          Hashtable cookies = parseCookieHeader( header );
91  
92          String site = getSite( request );
93  
94          //FIXME: merge these... new cookies into the site cookies 
95  
96      }
97  
98      /**
99       * Parse a given Cookie header into a hashtable.
100      *
101      * 
102      */
103     public static String getCookiesHeader( Hashtable cookies ) {
104 
105         Enumeration keys = cookies.keys();
106 
107         StringBuffer buff = new StringBuffer( 1024 );
108 
109         while ( keys.hasMoreElements() ) {
110 
111             String name = (String)keys.nextElement();
112             String value = (String)cookies.get( name );
113 
114             if ( buff.length() > 0 )
115                 buff.append( "; " );
116 
117             buff.append( name );
118             buff.append( "=" );
119             buff.append( value );
120             
121         }
122         
123         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         int begin = 0;
142 
143         /**
144          * Where do we spit into the variable
145          */
146         int split = 0;
147 
148         /**
149          * Where is the end of the cookie.
150          */
151         int end = 0;
152 
153         Hashtable result = new Hashtable();
154         
155         while ( (split = header.indexOf( "=", begin )) != -1 ) {
156 
157             end = header.indexOf( ";", split );
158 
159             if ( end == -1 )
160                 end = header.length();
161             
162             String name = header.substring( begin, split );
163             String value = header.substring( split+1, end );
164             
165             //move to the next one.
166             begin = end + 2;
167 
168             result.put( name, value );
169             
170         }
171 
172         return result;
173         
174     }
175     
176     public static void main( String[] args ) {
177 
178         parseCookieHeader( "password=HvS11dffnlD50bOLZYgG4oZFA-U; username=burtonator; rojoWeb=12.43.53.196.1091730560640949; JSESSIONID=B1245A7FEB43537E994324A157330F3A" );
179         
180     }
181 
182 }