001 /* 002 * Copyright 1999,2004 The Apache Software Foundation. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017 package org.apache.commons.feedparser.network; 018 019 import java.util.Enumeration; 020 import java.util.Hashtable; 021 022 /** 023 * 024 * By default java.net.URL does NOT handle cookies. This is a simple extension 025 * that allows us to persist cookies in the VM during runtime. 026 * 027 * FIXME: How can we make sure to delete older sites...?! no need for this to 028 * grow to infinite size. 029 * 030 * @author <a href="mailto:burton@peerfear.org">Kevin A. Burton</a> 031 */ 032 public class URLCookieManager { 033 034 static Hashtable cookies = new Hashtable(); 035 036 /** 037 * Get the cookies for a site. When none are available return null. 038 * 039 * 040 */ 041 public static Hashtable getCookies( String site ) { 042 043 return (Hashtable)cookies.get( site ); 044 045 } 046 047 /** 048 * Add cookies to this request and perform any other init. 049 * 050 * 051 */ 052 public static void init( ResourceRequest request ) { 053 054 String site = getSite( request ); 055 056 Hashtable cookies = getCookies( site ); 057 058 if ( cookies == null ) 059 return; 060 061 String header = getCookiesHeader( cookies ); 062 063 request.setRequestHeaderField( "Cookies", header ); 064 065 } 066 067 public static String getSite( ResourceRequest request ) { 068 069 String resource = request.getResource(); 070 071 int end = resource.indexOf( "://" ); 072 end = resource.indexOf( "/", end ); 073 074 if ( end == -1 ) 075 end = resource.length(); 076 077 return resource.substring( 0, end ); 078 079 } 080 081 /** 082 * Save the cookies FROM this request. 083 * 084 * 085 */ 086 public static void save( ResourceRequest request ) { 087 088 String header = request.getHeaderField( "Set-Cookie" ); 089 090 Hashtable cookies = parseCookieHeader( header ); 091 092 String site = getSite( request ); 093 094 //FIXME: merge these... new cookies into the site cookies 095 096 } 097 098 /** 099 * 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 }