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 org.eclipse.aether.repository.Authentication;
23  import org.eclipse.aether.repository.AuthenticationSelector;
24  import org.eclipse.aether.repository.RemoteRepository;
25  
26  /**
27   * An authentication selector that delegates to another selector but only if a repository has no authentication data
28   * yet. If authentication has already been assigned to a repository, that is selected.
29   */
30  public final class ConservativeAuthenticationSelector
31      implements AuthenticationSelector
32  {
33  
34      private final AuthenticationSelector selector;
35  
36      /**
37       * Creates a new selector that delegates to the specified selector.
38       * 
39       * @param selector The selector to delegate to in case a repository has no authentication yet, must not be
40       *            {@code null}.
41       */
42      public ConservativeAuthenticationSelector( AuthenticationSelector selector )
43      {
44          if ( selector == null )
45          {
46              throw new IllegalArgumentException( "no authentication selector specified" );
47          }
48          this.selector = selector;
49      }
50  
51      public Authentication getAuthentication( RemoteRepository repository )
52      {
53          Authentication auth = repository.getAuthentication();
54          if ( auth != null )
55          {
56              return auth;
57          }
58          return selector.getAuthentication( repository );
59      }
60  
61  }