12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.net;
27
28 import java.util.Map;
29 import java.util.List;
30 import java.util.Collections;
31 import java.util.Comparator;
32 import java.io.IOException;
33
34 /**
35 * CookieManager provides a concrete implementation of {@link CookieHandler},
36 * which separates the storage of cookies from the policy surrounding accepting
37 * and rejecting cookies. A CookieManager is initialized with a {@link CookieStore}
38 * which manages storage, and a {@link CookiePolicy} object, which makes
39 * policy decisions on cookie acceptance/rejection.
40 *
41 * <p> The HTTP cookie management in java.net package looks like:
42 * <blockquote>
43 * <pre>
44 * use
45 * CookieHandler <------- HttpURLConnection
46 * ^
47 * | impl
48 * | use
49 * CookieManager -------> CookiePolicy
50 * | use
51 * |--------> HttpCookie
203 new java.util.HashMap<String, List<String>>();
204 // if there's no default CookieStore, no way for us to get any cookie
205 if (cookieJar == null)
206 return Collections.unmodifiableMap(cookieMap);
207
208 List<HttpCookie> cookies = new java.util.ArrayList<HttpCookie>();
209 for (HttpCookie cookie : cookieJar.get(uri)) {
210 // apply path-matches rule (RFC 2965 sec. 3.3.4)
211 if (pathMatches(uri.getPath(), cookie.getPath())) {
212 cookies.add(cookie);
213 }
214 }
215
216 // apply sort rule (RFC 2965 sec. 3.3.4)
217 List<String> cookieHeader = sortByPath(cookies);
218
219 cookieMap.put("Cookie", cookieHeader);
220 return Collections.unmodifiableMap(cookieMap);
221 }
222
223
224 public void
225 put(URI uri, Map<String, List<String>> responseHeaders)
226 throws IOException
227 {
228 // pre-condition check
229 if (uri == null || responseHeaders == null) {
230 throw new IllegalArgumentException("Argument is null");
231 }
232
233
234 // if there's no default CookieStore, no need to remember any cookie
235 if (cookieJar == null)
236 return;
237
238 for (String headerKey : responseHeaders.keySet()) {
239 // RFC 2965 3.2.2, key must be 'Set-Cookie2'
240 // we also accept 'Set-Cookie' here for backward compatibility
241 if (headerKey == null
242 || !(headerKey.equalsIgnoreCase("Set-Cookie2")
243 || headerKey.equalsIgnoreCase("Set-Cookie")
244 )
245 )
246 {
247 continue;
248 }
249
250 for (String headerValue : responseHeaders.get(headerKey)) {
251 try {
252 List<HttpCookie> cookies = HttpCookie.parse(headerValue);
253 for (HttpCookie cookie : cookies) {
254 if (shouldAcceptInternal(uri, cookie)) {
255 cookieJar.add(uri, cookie);
256 }
257 }
258 } catch (IllegalArgumentException e) {
259 // invalid set-cookie header string
260 // no-op
261 }
262 }
263 }
264 }
265
266
267 /* ---------------- Private operations -------------- */
268
269 // to determine whether or not accept this cookie
270 private boolean shouldAcceptInternal(URI uri, HttpCookie cookie) {
271 try {
272 return policyCallback.shouldAccept(uri, cookie);
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.net;
27
28 import java.util.Map;
29 import java.util.List;
30 import java.util.Collections;
31 import java.util.Comparator;
32 import java.util.logging.Level;
33 import java.util.logging.Logger;
34 import java.io.IOException;
35
36 /**
37 * CookieManager provides a concrete implementation of {@link CookieHandler},
38 * which separates the storage of cookies from the policy surrounding accepting
39 * and rejecting cookies. A CookieManager is initialized with a {@link CookieStore}
40 * which manages storage, and a {@link CookiePolicy} object, which makes
41 * policy decisions on cookie acceptance/rejection.
42 *
43 * <p> The HTTP cookie management in java.net package looks like:
44 * <blockquote>
45 * <pre>
46 * use
47 * CookieHandler <------- HttpURLConnection
48 * ^
49 * | impl
50 * | use
51 * CookieManager -------> CookiePolicy
52 * | use
53 * |--------> HttpCookie
205 new java.util.HashMap<String, List<String>>();
206 // if there's no default CookieStore, no way for us to get any cookie
207 if (cookieJar == null)
208 return Collections.unmodifiableMap(cookieMap);
209
210 List<HttpCookie> cookies = new java.util.ArrayList<HttpCookie>();
211 for (HttpCookie cookie : cookieJar.get(uri)) {
212 // apply path-matches rule (RFC 2965 sec. 3.3.4)
213 if (pathMatches(uri.getPath(), cookie.getPath())) {
214 cookies.add(cookie);
215 }
216 }
217
218 // apply sort rule (RFC 2965 sec. 3.3.4)
219 List<String> cookieHeader = sortByPath(cookies);
220
221 cookieMap.put("Cookie", cookieHeader);
222 return Collections.unmodifiableMap(cookieMap);
223 }
224
225 public void
226 put(URI uri, Map<String, List<String>> responseHeaders)
227 throws IOException
228 {
229 // pre-condition check
230 if (uri == null || responseHeaders == null) {
231 throw new IllegalArgumentException("Argument is null");
232 }
233
234
235 // if there's no default CookieStore, no need to remember any cookie
236 if (cookieJar == null)
237 return;
238
239 Logger logger = Logger.getLogger("java.net.CookieManager");
240 for (String headerKey : responseHeaders.keySet()) {
241 // RFC 2965 3.2.2, key must be 'Set-Cookie2'
242 // we also accept 'Set-Cookie' here for backward compatibility
243 if (headerKey == null
244 || !(headerKey.equalsIgnoreCase("Set-Cookie2")
245 || headerKey.equalsIgnoreCase("Set-Cookie")
246 )
247 )
248 {
249 continue;
250 }
251
252 for (String headerValue : responseHeaders.get(headerKey)) {
253 try {
254 List<HttpCookie> cookies;
255 try {
256 cookies = HttpCookie.parse(headerValue);
257 } catch (IllegalArgumentException e) {
258 // Bogus header, make an empty list and log the error
259 cookies = java.util.Collections.emptyList();
260 if (logger.isLoggable(Level.SEVERE)) {
261 logger.severe("Invalid cookie for " + uri + ": " + headerValue);
262 }
263 }
264 for (HttpCookie cookie : cookies) {
265 if (shouldAcceptInternal(uri, cookie)) {
266 cookieJar.add(uri, cookie);
267 }
268 }
269 } catch (IllegalArgumentException e) {
270 // invalid set-cookie header string
271 // no-op
272 }
273 }
274 }
275 }
276
277
278 /* ---------------- Private operations -------------- */
279
280 // to determine whether or not accept this cookie
281 private boolean shouldAcceptInternal(URI uri, HttpCookie cookie) {
282 try {
283 return policyCallback.shouldAccept(uri, cookie);
|