View Javadoc
1   package org.eclipse.aether.util.repository;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   * 
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import org.eclipse.aether.repository.Authentication;
26  import org.eclipse.aether.repository.AuthenticationSelector;
27  import org.eclipse.aether.repository.RemoteRepository;
28  
29  import static java.util.Objects.requireNonNull;
30  
31  /**
32   * A simple authentication selector that selects authentication based on repository identifiers.
33   */
34  public final class DefaultAuthenticationSelector
35      implements AuthenticationSelector
36  {
37  
38      private final Map<String, Authentication> repos = new HashMap<>();
39  
40      /**
41       * Adds the specified authentication info for the given repository identifier.
42       * 
43       * @param id The identifier of the repository to add the authentication for, must not be {@code null}.
44       * @param auth The authentication to add, may be {@code null}.
45       * @return This selector for chaining, never {@code null}.
46       */
47      public DefaultAuthenticationSelector add( String id, Authentication auth )
48      {
49          if ( auth != null )
50          {
51              repos.put( id, auth );
52          }
53          else
54          {
55              repos.remove( id );
56          }
57  
58          return this;
59      }
60  
61      public Authentication getAuthentication( RemoteRepository repository )
62      {
63          requireNonNull( repository, "repository cannot be null" );
64          return repos.get( repository.getId() );
65      }
66  
67  }