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.Immutable;
34  
35  /**
36   * This cookie comparator ensures that multiple cookies satisfying
37   * a common criteria are ordered in the {@code Cookie} header such
38   * that those with more specific Path attributes precede those with
39   * less specific.
40   *
41   * <p>
42   * This comparator assumes that Path attributes of two cookies
43   * path-match a commmon request-URI. Otherwise, the result of the
44   * comparison is undefined.
45   * </p>
46   *
47   *
48   * @since 4.0
49   */
50  @Immutable
51  public class CookiePathComparator implements Serializable, Comparator<Cookie> {
52  
53      public static final CookiePathComparator INSTANCE = new CookiePathComparator();
54  
55      private static final long serialVersionUID = 7523645369616405818L;
56  
57      private String normalizePath(final Cookie cookie) {
58          String path = cookie.getPath();
59          if (path == null) {
60              path = "/";
61          }
62          if (!path.endsWith("/")) {
63              path = path + '/';
64          }
65          return path;
66      }
67  
68      @Override
69      public int compare(final Cookie c1, final Cookie c2) {
70          final String path1 = normalizePath(c1);
71          final String path2 = normalizePath(c2);
72          if (path1.equals(path2)) {
73              return 0;
74          } else if (path1.startsWith(path2)) {
75              return -1;
76          } else if (path2.startsWith(path1)) {
77              return 1;
78          } else {
79              // Does not really matter
80              return 0;
81          }
82      }
83  
84  }