Source code: com/sonalb/net/http/cookie/Client.java
1 /*
2 * -*- mode: java; c-basic-indent: 4; indent-tabs-mode: nil -*-
3 * :indentSize=4:noTabs=true:tabSize=4:indentOnTab=true:indentOnEnter=true:mode=java:
4 * ex: set tabstop=4 expandtab:
5 *
6 * MrPostman - webmail <-> email gateway
7 * Copyright (C) 2002-2003 MrPostman Development Group
8 * Projectpage: http://mrbook.org/mrpostman/
9 *
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 * In particular, this implies that users are responsible for
21 * using MrPostman after reading the terms and conditions given
22 * by their web-mail provider.
23 *
24 * You should have received a copy of the GNU General Public License
25 * Named LICENSE in the base directory of this distribution,
26 * if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 */
29
30 package com.sonalb.net.http.cookie;
31
32 import com.sonalb.net.http.Header;
33 import com.sonalb.net.http.HeaderUtils;
34
35 import java.net.URL;
36 import java.net.URLConnection;
37
38 import java.util.logging.Logger;
39
40
41 /*
42
43 According to RFC2965, if Set-Cookie and set-cookie2 both describe the same cookie, then sc2 should be used.
44
45 This distinction has not been incorporated.
46
47 If Request URL="http://www.example.com/acme" and Cookie header path="/acme". The cookie
48
49 object path="/acme/". Thus, pathMatch fails. Is this bad ?
50
51
52
53 Strict parsing means that if one cookie is bad, then processing stops. and MCE is thrown. Lenient
54
55 parsing ignores bad cookies and continues trying to parse valid ones.
56
57 */
58
59 /**
60
61 * This class is used to invoke the cookie-handling logic of the jCookie Library.
62
63 * It is the developer's view of the library. All cookie-handling methods are invoked on this
64
65 * object. The following snippet shows common-case usage. The highlighted portion is the only
66
67 * cookie-handling code, as far as the developer is concerned.
68
69 * <br>
70
71 * <pre>
72
73 * import com.sonalb.net.http.cookie.*;
74
75 * import java.net.*;
76
77 * import java.io.*;
78
79 * ...
80
81 *
82
83 * public class Example
84
85 * {
86
87 * ...
88
89 *
90
91 * public void someMethod()
92
93 * {
94
95 * ...
96
97 * URL url = new URL("http://www.site.com/");
98
99 * HttpURLConnection huc = (HttpURLConnection) url.openConnection();
100
101 *
102
103 * // Setup the HttpURLConnection here
104
105 * ...
106
107 *
108
109 * huc.connect();
110
111 * InputStream is = huc.getInputStream();
112
113 * <strong>Client client = new Client();
114
115 * CookieJar cj = client.getCookies(huc);</strong>
116
117 *
118
119 * // Do some processing
120
121 * ...
122
123 *
124
125 * huc.disconnect();
126
127 *
128
129 * // Make another request
130
131 * url = new URL("http://www.site.com/");
132
133 * huc = (HttpURLConnection) url.openConnection();
134
135 * <strong>client.setCookies(huc, cj);</strong>
136
137 *
138
139 * huc.connect();
140
141 * ...
142
143 * }
144
145 * }
146
147 * </pre>
148
149 * @author Sonal Bansal
150
151 */
152 public class Client {
153 public static final String CVSID = "$Id: Client.java,v 1.6 2003/02/09 23:38:11 lbruand Exp $";
154 private static final CookieParser defaultCookieParser = new RFC2965CookieParser();
155 private static Logger logger = Logger.getLogger("com.sonalb.net.http.cookie.Client");
156 private CookieParser currentCookieParser = defaultCookieParser;
157
158 /**
159
160 * Constructs an instance using the default CookieParser
161
162 *
163
164 * @see CookieParser
165
166 * @see #getDefaultCookieParser()
167
168 */
169 public Client() {
170 }
171
172 /**
173
174 * Returns the built-in <code>CookieParser</code> implementation. Current implementation
175
176 * conforms to RFC-2965.
177
178 *
179
180 * @return the default <code>CookieParser</code> implementation
181
182 * @see RFC2965CookieParser
183
184 */
185 public static CookieParser getDefaultCookieParser() {
186 return (defaultCookieParser);
187 }
188
189 /**
190
191 * Resets the <code>CookieParser</code> implementation to be used for this instance, to the default
192
193 * (built-in) implementation.
194
195 *
196
197 * @see #setCookieParser(CookieParser)
198
199 */
200 public void resetToDefaultCookieParser() {
201 synchronized (currentCookieParser) {
202 currentCookieParser = defaultCookieParser;
203 }
204 }
205
206 /**
207
208 * Sets the <code>CookieParser</code> implementation to be used in this instance.
209
210 *
211
212 * @param cp the CookieParser to be used
213
214 */
215 public void setCookieParser(CookieParser cp) {
216 if (cp == null) {
217 return;
218 }
219
220 synchronized (currentCookieParser) {
221 currentCookieParser = cp;
222 }
223 }
224
225 /**
226
227 * Gets the <code>CookieParser</code> implementation being used in this instance.
228
229 *
230
231 * @return the CookieParser in use
232
233 */
234 public CookieParser getCookieParser() {
235 return (currentCookieParser);
236 }
237
238 /**
239
240 * Processes cookie headers from the given URLConnection. This method <em>must</em> be called
241
242 * <strong>after</strong> the URLConnection is connected.
243
244 *
245
246 * @param urlConn the URLConnection to be processed
247
248 * @returns the CookieJar containing all the Cookies extracted
249
250 * @throws MalformedCookieException if there was some error during cookie processing
251
252 */
253 public CookieJar getCookies(URLConnection urlConn)
254 throws MalformedCookieException {
255 return (getCookies(urlConn, urlConn.getURL()));
256 }
257
258 protected CookieJar getCookies(URLConnection urlConn, URL url)
259 throws MalformedCookieException {
260 if (urlConn == null) {
261 return (getCookies((Header) null, url));
262 }
263
264 return (getCookies(HeaderUtils.extractHeaders(urlConn), url));
265 }
266
267 protected CookieJar getCookies(Header header, URL url)
268 throws MalformedCookieException {
269 return (currentCookieParser.parseCookies(header, url));
270 }
271
272 /**
273
274 * Sets cookie headers on the given URLConnection, using Cookies in the CookieJar.
275
276 * This method <em>must</em> be called <strong>before</strong> the URLConnection is connected.
277
278 *
279
280 * @param urlConn the URLConnection to be processed
281
282 * @param cj the CookieJar containing the Cookies to be set
283
284 * @returns the CookieJar containing the Cookies that were actually set
285
286 */
287 public CookieJar setCookies(URLConnection urlConn, CookieJar cj) {
288 /*A particular cookie header should contain only one version of cookies.
289
290 Are multiple cookie headers allowed ?*/
291 if ((urlConn == null) || (cj == null)) {
292 throw new IllegalArgumentException("Null URLConnection or CookieJar");
293 }
294
295 if (cj.isEmpty()) {
296 return (cj);
297 }
298
299 logger.finer("Client.setCookies(): I've been asked to set following cookies:-");
300
301 logger.finer(cj.toString());
302
303 CookieJar eligibleCookies = CookieUtils.getCookiesForURL(cj, currentCookieParser, urlConn.getURL(), true);
304
305 logger.finer("Client.setCookies(): Eligible cookies are:-");
306
307 logger.finer(eligibleCookies.toString());
308
309 Header h = currentCookieParser.getCookieHeaders(eligibleCookies);
310
311 logger.finer("Client.setCookies(): Headers Set = " + h);
312
313 HeaderUtils.setHeaders(urlConn, h);
314
315 return (eligibleCookies);
316 }
317 }