View Javadoc

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