Changeset 12821 in josm for trunk


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

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

Location:
trunk/src/org/openstreetmap/josm
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/MainApplication.java

    r12808 r12821  
    9797import org.openstreetmap.josm.gui.bugreport.BugReportDialog;
    9898import org.openstreetmap.josm.gui.download.DownloadDialog;
     99import org.openstreetmap.josm.gui.io.CredentialDialog;
    99100import org.openstreetmap.josm.gui.io.CustomConfigurator.XMLCommandProcessor;
    100101import org.openstreetmap.josm.gui.io.SaveLayersDialog;
     
    132133import org.openstreetmap.josm.io.OsmTransferCanceledException;
    133134import org.openstreetmap.josm.io.OsmTransferException;
     135import org.openstreetmap.josm.io.auth.AbstractCredentialsAgent;
    134136import org.openstreetmap.josm.io.auth.CredentialsManager;
    135137import org.openstreetmap.josm.io.auth.DefaultAuthenticator;
     
    10891091    static void setupCallbacks() {
    10901092        OsmConnection.setOAuthAccessTokenFetcher(OAuthAuthorizationWizard::obtainAccessToken);
     1093        AbstractCredentialsAgent.setCredentialsProvider(CredentialDialog::promptCredentials);
    10911094        MessageNotifier.setNotifierCallback(MainApplication::notifyNewMessages);
    10921095        DeleteCommand.setDeletionCallback(DeleteAction.defaultDeletionCallback);
  • trunk/src/org/openstreetmap/josm/gui/io/CredentialDialog.java

    r12805 r12821  
    1818import java.awt.event.WindowAdapter;
    1919import java.awt.event.WindowEvent;
     20import java.net.Authenticator.RequestorType;
    2021import java.util.Objects;
    2122
     
    3233import org.openstreetmap.josm.gui.help.ContextSensitiveHelpAction;
    3334import org.openstreetmap.josm.gui.help.HelpUtil;
     35import org.openstreetmap.josm.gui.util.GuiHelper;
    3436import org.openstreetmap.josm.gui.util.WindowGeometry;
    3537import org.openstreetmap.josm.gui.widgets.JMultilineLabel;
     
    3840import org.openstreetmap.josm.io.DefaultProxySelector;
    3941import org.openstreetmap.josm.io.OsmApi;
     42import org.openstreetmap.josm.io.auth.AbstractCredentialsAgent;
     43import org.openstreetmap.josm.io.auth.CredentialsAgentResponse;
    4044import org.openstreetmap.josm.tools.ImageProvider;
    4145import org.openstreetmap.josm.tools.InputMapUtils;
     
    6872        dialog.pack();
    6973        return dialog;
     74    }
     75
     76    /**
     77     * Prompts the user (in the EDT) for credentials and fills the given response with what has been entered.
     78     * @param requestorType type of the entity requesting authentication
     79     * @param agent the credentials agent requesting credentials
     80     * @param response authentication response to fill
     81     * @param username the known username, if any. Likely to be empty
     82     * @param password the known password, if any. Likely to be empty
     83     * @param host the host against authentication will be performed
     84     * @since 12821
     85     */
     86    public static void promptCredentials(RequestorType requestorType, AbstractCredentialsAgent agent, CredentialsAgentResponse response,
     87            String username, String password, String host) {
     88        GuiHelper.runInEDTAndWait(() -> {
     89            CredentialDialog dialog;
     90            if (requestorType.equals(RequestorType.PROXY))
     91                dialog = getHttpProxyCredentialDialog(
     92                        username, password, host, agent.getSaveUsernameAndPasswordCheckboxText());
     93            else
     94                dialog = getOsmApiCredentialDialog(
     95                        username, password, host, agent.getSaveUsernameAndPasswordCheckboxText());
     96            dialog.setVisible(true);
     97            response.setCanceled(dialog.isCanceled());
     98            if (dialog.isCanceled())
     99                return;
     100            response.setUsername(dialog.getUsername());
     101            response.setPassword(dialog.getPassword());
     102            response.setSaveCredentials(dialog.isSaveCredentials());
     103        });
    70104    }
    71105
  • 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.