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