1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.feedparser.network;
18
19 import java.util.Enumeration;
20 import java.util.Hashtable;
21
22
23
24
25
26
27
28
29
30
31
32 public class URLCookieManager {
33
34 static Hashtable cookies = new Hashtable();
35
36
37
38
39
40
41 public static Hashtable getCookies( String site ) {
42
43 return (Hashtable)cookies.get( site );
44
45 }
46
47
48
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
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
95
96 }
97
98
99
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
129
130
131
132 public static Hashtable parseCookieHeader( String header ) {
133
134
135
136
137
138
139
140
141 int begin = 0;
142
143
144
145
146 int split = 0;
147
148
149
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
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 }