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;
20  
21  import java.net.URI;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.concurrent.ConcurrentHashMap;
25  import java.util.concurrent.CopyOnWriteArrayList;
26  
27  import org.eclipse.jetty.client.api.Authentication;
28  import org.eclipse.jetty.client.api.AuthenticationStore;
29  
30  public class HttpAuthenticationStore implements AuthenticationStore
31  {
32      private final List<Authentication> authentications = new CopyOnWriteArrayList<>();
33      private final Map<URI, Authentication.Result> results = new ConcurrentHashMap<>();
34  
35      @Override
36      public void addAuthentication(Authentication authentication)
37      {
38          authentications.add(authentication);
39      }
40  
41      @Override
42      public void removeAuthentication(Authentication authentication)
43      {
44          authentications.remove(authentication);
45      }
46  
47      @Override
48      public void clearAuthentications()
49      {
50          authentications.clear();
51      }
52  
53      @Override
54      public Authentication findAuthentication(String type, URI uri, String realm)
55      {
56          for (Authentication authentication : authentications)
57          {
58              if (authentication.matches(type, uri, realm))
59                  return authentication;
60          }
61          return null;
62      }
63  
64      @Override
65      public void addAuthenticationResult(Authentication.Result result)
66      {
67          results.put(result.getURI(), result);
68      }
69  
70      @Override
71      public void removeAuthenticationResult(Authentication.Result result)
72      {
73          results.remove(result.getURI());
74      }
75  
76      @Override
77      public void clearAuthenticationResults()
78      {
79          results.clear();
80      }
81  
82      @Override
83      public Authentication.Result findAuthenticationResult(URI uri)
84      {
85          // TODO: I should match the longest URI
86          for (Map.Entry<URI, Authentication.Result> entry : results.entrySet())
87          {
88              if (uri.toString().startsWith(entry.getKey().toString()))
89                  return entry.getValue();
90          }
91          return null;
92      }
93  }