1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 package org.apache.commons.httpclient.cookie;
31
32 import java.util.Comparator;
33
34 import org.apache.commons.httpclient.Cookie;
35
36 /***
37 * This cookie comparator ensures that multiple cookies satisfying
38 * a common criteria are ordered in the <tt>Cookie</tt> header such
39 * that those with more specific Path attributes precede those with
40 * less specific.
41 *
42 * <p>
43 * This comparator assumes that Path attributes of two cookies
44 * path-match a commmon request-URI. Otherwise, the result of the
45 * comparison is undefined.
46 * </p>
47 *
48 * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
49 *
50 * @since 3.1
51 */
52 public class CookiePathComparator implements Comparator {
53
54 private String normalizePath(final Cookie cookie) {
55 String path = cookie.getPath();
56 if (path == null) {
57 path = "/";
58 }
59 if (!path.endsWith("/")) {
60 path = path + "/";
61 }
62 return path;
63 }
64
65 public int compare(final Object o1, final Object o2) {
66 Cookie c1 = (Cookie) o1;
67 Cookie c2 = (Cookie) o2;
68 String path1 = normalizePath(c1);
69 String path2 = normalizePath(c2);
70 if (path1.equals(path2)) {
71 return 0;
72 } else if (path1.startsWith(path2)) {
73 return -1;
74 } else if (path2.startsWith(path1)) {
75 return 1;
76 } else {
77
78 return 0;
79 }
80 }
81
82 }