Changeset 9396 in josm for trunk/src/org/openstreetmap/josm/io
- Timestamp:
- 2016-01-10T20:09:16+01:00 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/io/auth/DefaultAuthenticator.java
r9313 r9396 4 4 import java.net.Authenticator; 5 5 import java.net.PasswordAuthentication; 6 import java.util. EnumMap;7 import java.util. Map;6 import java.util.Collection; 7 import java.util.HashSet; 8 8 import java.util.Objects; 9 9 10 10 import org.openstreetmap.josm.Main; 11 11 import org.openstreetmap.josm.io.OsmApi; 12 import org.openstreetmap.josm.tools.Pair; 12 13 13 14 /** … … 17 18 */ 18 19 public final class DefaultAuthenticator extends Authenticator { 19 private static volatile DefaultAuthenticator instance;20 private static final DefaultAuthenticator INSTANCE = new DefaultAuthenticator(); 20 21 21 22 /** … … 24 25 */ 25 26 public static DefaultAuthenticator getInstance() { 26 return instance;27 return INSTANCE; 27 28 } 28 29 29 /** 30 * Creates the unique instance 31 */ 32 public static void createInstance() { 33 instance = new DefaultAuthenticator(); 34 } 35 36 private final Map<RequestorType, Boolean> credentialsTried = new EnumMap<>(RequestorType.class); 30 private final Collection<Pair<String, RequestorType>> failedCredentials = new HashSet<>(); 37 31 private boolean enabled = true; 38 32 … … 54 48 return null; 55 49 } 56 boolean tried = credentialsTried.get(getRequestorType()) != null; 57 CredentialsAgentResponse response = CredentialsManager.getInstance().getCredentials(getRequestorType(), getRequestingHost(), tried); 58 if (response == null || response.isCanceled()) 50 final Pair<String, RequestorType> hostTypePair = Pair.create(getRequestingHost(), getRequestorType()); 51 final boolean hasFailedPreviously = failedCredentials.contains(hostTypePair); 52 final CredentialsAgentResponse response = CredentialsManager.getInstance().getCredentials( 53 getRequestorType(), getRequestingHost(), hasFailedPreviously); 54 if (response == null || response.isCanceled()) { 59 55 return null; 60 credentialsTried.put(getRequestorType(), Boolean.TRUE); 56 } 57 if (RequestorType.PROXY.equals(getRequestorType())) { 58 // Query user in case this authenticator is called (indicating that the authentication failed) the next time. 59 failedCredentials.add(hostTypePair); 60 } else { 61 // Other parallel requests should not ask the user again, thus wait till this request is finished. 62 // In case of invalid authentication, the host is added again to failedCredentials at HttpClient.connect() 63 failedCredentials.remove(hostTypePair); 64 } 61 65 return new PasswordAuthentication(response.getUsername(), response.getPassword()); 62 66 } catch (CredentialsAgentException e) { … … 66 70 } 67 71 72 /** 73 * Determines whether this authenticator is enabled, i.e., 74 * provides {@link #getPasswordAuthentication() password authentication} via {@link CredentialsManager}. 75 * @return whether this authenticator is enabled 76 */ 68 77 public boolean isEnabled() { 69 78 return enabled; 70 79 } 71 80 81 /** 82 * Enabled/disables this authenticator, i.e., decides whether it 83 * should provide {@link #getPasswordAuthentication() password authentication} via {@link CredentialsManager}. 84 * @param enabled whether this authenticator should be enabled 85 */ 72 86 public void setEnabled(boolean enabled) { 73 87 this.enabled = enabled; 74 88 } 89 90 /** 91 * Marks for this host that the authentication failed, i.e., 92 * the {@link CredentialsManager} will show a dialog at the next time. 93 * @param host the host to mark 94 * @return as per {@link Collection#add(Object)} 95 */ 96 public boolean addFailedCredentialHost(String host) { 97 return failedCredentials.add(Pair.create(host, RequestorType.SERVER)); 98 } 99 100 /** 101 * Un-marks the failed authentication attempt for the host 102 * @param host the host to un-mark 103 * @return as per {@link Collection#remove(Object)} 104 */ 105 public boolean removeFailedCredentialHost(String host) { 106 return failedCredentials.remove(Pair.create(host, RequestorType.SERVER)); 107 } 75 108 }
Note:
See TracChangeset
for help on using the changeset viewer.