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 | 0 | public class URLCookieManager { |
33 | |
|
34 | 0 | static Hashtable cookies = new Hashtable(); |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
public static Hashtable getCookies( String site ) { |
42 | |
|
43 | 0 | return (Hashtable)cookies.get( site ); |
44 | |
|
45 | |
} |
46 | |
|
47 | |
|
48 | |
|
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 | |
|
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 | |
|
95 | |
|
96 | 0 | } |
97 | |
|
98 | |
|
99 | |
|
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 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
public static Hashtable parseCookieHeader( String header ) { |
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | 0 | int begin = 0; |
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | 0 | int split = 0; |
147 | |
|
148 | |
|
149 | |
|
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 | |
|
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 | |
} |