View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.cookie;
29  
30  import java.io.Serializable;
31  import java.util.Comparator;
32  
33  import org.apache.http.annotation.Contract;
34  import org.apache.http.annotation.ThreadingBehavior;
35  
36  /**
37   * This cookie comparator ensures that multiple cookies satisfying
38   * a common criteria are ordered in the {@code Cookie} 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   *
49   * @since 4.0
50   */
51  @Contract(threading = ThreadingBehavior.IMMUTABLE)
52  public class CookiePathComparator implements Serializable, Comparator<Cookie> {
53  
54      public static final CookiePathComparator INSTANCE = new CookiePathComparator();
55  
56      private static final long serialVersionUID = 7523645369616405818L;
57  
58      private String normalizePath(final Cookie cookie) {
59          String path = cookie.getPath();
60          if (path == null) {
61              path = "/";
62          }
63          if (!path.endsWith("/")) {
64              path = path + '/';
65          }
66          return path;
67      }
68  
69      @Override
70      public int compare(final Cookie c1, final Cookie c2) {
71          final String path1 = normalizePath(c1);
72          final String path2 = normalizePath(c2);
73          if (path1.equals(path2)) {
74              return 0;
75          } else if (path1.startsWith(path2)) {
76              return -1;
77          } else if (path2.startsWith(path1)) {
78              return 1;
79          } else {
80              // Does not really matter
81              return 0;
82          }
83      }
84  
85  }