View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.client.util;
20  
21  import java.net.URI;
22  
23  import org.eclipse.jetty.client.HttpClient;
24  import org.eclipse.jetty.client.api.Authentication;
25  
26  public abstract class AbstractAuthentication implements Authentication
27  {
28      private final URI uri;
29      private final String realm;
30  
31      public AbstractAuthentication(URI uri, String realm)
32      {
33          this.uri = uri;
34          this.realm = realm;
35      }
36  
37      public abstract String getType();
38  
39      public URI getURI()
40      {
41          return uri;
42      }
43  
44      public String getRealm()
45      {
46          return realm;
47      }
48  
49      @Override
50      public boolean matches(String type, URI uri, String realm)
51      {
52          if (!getType().equalsIgnoreCase(type))
53              return false;
54  
55          if (!this.realm.equals(realm))
56              return false;
57  
58          return matchesURI(this.uri, uri);
59      }
60  
61      public static boolean matchesURI(URI uri1, URI uri2)
62      {
63          String scheme = uri1.getScheme();
64          if (scheme.equalsIgnoreCase(uri2.getScheme()))
65          {
66              if (uri1.getHost().equalsIgnoreCase(uri2.getHost()))
67              {
68                  // Handle default HTTP ports.
69                  int thisPort = HttpClient.normalizePort(scheme, uri1.getPort());
70                  int thatPort = HttpClient.normalizePort(scheme, uri2.getPort());
71                  if (thisPort == thatPort)
72                  {
73                      // Use decoded URI paths.
74                      return uri2.getPath().startsWith(uri1.getPath());
75                  }
76              }
77          }
78          return false;
79      }
80  }