Ignore:
Timestamp:
2017-09-11T01:54:52+02:00 (2 years ago)
Author:
Don-vip
Message:

see #15229 - see #15182 - remove GUI references from I/O subsystem

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/io/auth/AbstractCredentialsAgent.java

    r12646 r12821  
    22package org.openstreetmap.josm.io.auth;
    33
    4 import java.awt.GraphicsEnvironment;
    54import java.net.Authenticator.RequestorType;
    65import java.net.PasswordAuthentication;
    76import java.util.EnumMap;
    87import java.util.Map;
     8import java.util.Objects;
    99
    10 import org.openstreetmap.josm.gui.io.CredentialDialog;
    11 import org.openstreetmap.josm.gui.util.GuiHelper;
     10import org.openstreetmap.josm.tools.Logging;
    1211
    1312/**
    1413 * Partial implementation of the {@link CredentialsAgent} interface.
    1514 * <p>
    16  * Provides a memory cache for the credentials and means to query the information
    17  * from the user.
     15 * Provides a memory cache for the credentials and means to query the information from the user.
     16 * @since 4246
    1817 */
    1918public abstract class AbstractCredentialsAgent implements CredentialsAgent {
     19
     20    /**
     21     * Synchronous credentials provider. Called if no credentials are cached. Can be used for user login prompt.
     22     * @since 12821
     23     */
     24    @FunctionalInterface
     25    public interface CredentialsProvider {
     26        /**
     27         * Fills the given response with appropriate user credentials.
     28         * @param requestorType type of the entity requesting authentication
     29         * @param agent the credentials agent requesting credentials
     30         * @param response authentication response to fill
     31         * @param username the known username, if any. Likely to be empty
     32         * @param password the known password, if any. Likely to be empty
     33         * @param host the host against authentication will be performed
     34         */
     35        void provideCredentials(RequestorType requestorType, AbstractCredentialsAgent agent, CredentialsAgentResponse response,
     36                String username, String password, String host);
     37    }
     38
     39    private static CredentialsProvider credentialsProvider = (a, b, c, d, e, f) -> Logging.error("Credentials provider has not been set");
     40
     41    /**
     42     * Sets the global credentials provider.
     43     * @param provider credentials provider. Called if no credentials are cached. Can be used for user login prompt
     44     */
     45    public static void setCredentialsProvider(CredentialsProvider provider) {
     46        credentialsProvider = Objects.requireNonNull(provider, "provider");
     47    }
    2048
    2149    protected Map<RequestorType, PasswordAuthentication> memoryCredentialsCache = new EnumMap<>(RequestorType.class);
     
    5179         */
    5280        } else if (noSuccessWithLastResponse || username.isEmpty() || password.isEmpty()) {
    53             if (!GraphicsEnvironment.isHeadless()) {
    54                 GuiHelper.runInEDTAndWait(() -> {
    55                     CredentialDialog dialog;
    56                     if (requestorType.equals(RequestorType.PROXY))
    57                         dialog = CredentialDialog.getHttpProxyCredentialDialog(
    58                                 username, password, host, getSaveUsernameAndPasswordCheckboxText());
    59                     else
    60                         dialog = CredentialDialog.getOsmApiCredentialDialog(
    61                                 username, password, host, getSaveUsernameAndPasswordCheckboxText());
    62                     dialog.setVisible(true);
    63                     response.setCanceled(dialog.isCanceled());
    64                     if (dialog.isCanceled())
    65                         return;
    66                     response.setUsername(dialog.getUsername());
    67                     response.setPassword(dialog.getPassword());
    68                     response.setSaveCredentials(dialog.isSaveCredentials());
    69                 });
    70             }
     81            credentialsProvider.provideCredentials(requestorType, this, response, username, password, host);
    7182            if (response.isCanceled() || response.getUsername() == null || response.getPassword() == null) {
    7283                return response;
Note: See TracChangeset for help on using the changeset viewer.