Ignore:
Timestamp:
2017-09-09T16:45:41+02:00 (2 years ago)
Author:
Don-vip
Message:

see #15229 - see #15182 - remove GUI references from OsmConnection

File:
1 edited

Legend:

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

    r12686 r12803  
    1111import java.util.Base64;
    1212import java.util.Objects;
    13 import java.util.concurrent.FutureTask;
    14 
    15 import javax.swing.SwingUtilities;
    1613
    1714import org.openstreetmap.josm.Main;
    1815import org.openstreetmap.josm.data.oauth.OAuthAccessTokenHolder;
    1916import org.openstreetmap.josm.data.oauth.OAuthParameters;
    20 import org.openstreetmap.josm.gui.oauth.OAuthAuthorizationWizard;
    2117import org.openstreetmap.josm.io.auth.CredentialsAgentException;
    2218import org.openstreetmap.josm.io.auth.CredentialsAgentResponse;
    2319import org.openstreetmap.josm.io.auth.CredentialsManager;
    2420import org.openstreetmap.josm.tools.HttpClient;
     21import org.openstreetmap.josm.tools.JosmRuntimeException;
    2522import org.openstreetmap.josm.tools.Logging;
    26 import org.openstreetmap.josm.tools.Utils;
    2723
    2824import oauth.signpost.OAuthConsumer;
     
    3935    protected HttpClient activeConnection;
    4036    protected OAuthParameters oauthParameters;
     37
     38    /**
     39     * Retrieves OAuth access token.
     40     * @since 12803
     41     */
     42    public interface OAuthAccessTokenFetcher {
     43        /**
     44         * Obtains an OAuth access token for the connection. Afterwards, the token is accessible via {@link OAuthAccessTokenHolder}.
     45         * @param serverUrl the URL to OSM server
     46         * @throws InterruptedException if we're interrupted while waiting for the event dispatching thread to finish OAuth authorization task
     47         * @throws InvocationTargetException if an exception is thrown while running OAuth authorization task
     48         */
     49        void obtainAccessToken(URL serverUrl) throws InvocationTargetException, InterruptedException;
     50    }
     51
     52    static OAuthAccessTokenFetcher fetcher = u -> {
     53        throw new JosmRuntimeException("OsmConnection.setOAuthAccessTokenFetcher() has not been called");
     54    };
     55
     56    /**
     57     * Sets the OAuth access token fetcher.
     58     * @param tokenFetcher new OAuth access token fetcher. Cannot be null
     59     * @since 12803
     60     */
     61    public static void setOAuthAccessTokenFetcher(OAuthAccessTokenFetcher tokenFetcher) {
     62        fetcher = Objects.requireNonNull(tokenFetcher, "tokenFetcher");
     63    }
    4164
    4265    /**
     
    110133
    111134    /**
    112      * Obtains an OAuth access token for the connection. Afterwards, the token is accessible via {@link OAuthAccessTokenHolder}.
     135     * Obtains an OAuth access token for the connection.
     136     * Afterwards, the token is accessible via {@link OAuthAccessTokenHolder} / {@link CredentialsManager}.
    113137     * @param connection connection for which the access token should be obtained
    114      * @throws MissingOAuthAccessTokenException if the process cannot be completec successfully
     138     * @throws MissingOAuthAccessTokenException if the process cannot be completed successfully
    115139     */
    116140    protected void obtainAccessToken(final HttpClient connection) throws MissingOAuthAccessTokenException {
     
    120144                throw new MissingOAuthAccessTokenException();
    121145            }
    122             final Runnable authTask = new FutureTask<>(() -> {
    123                 // Concerning Utils.newDirectExecutor: Main worker cannot be used since this connection is already
    124                 // executed via main worker. The OAuth connections would block otherwise.
    125                 final OAuthAuthorizationWizard wizard = new OAuthAuthorizationWizard(
    126                         Main.parent, apiUrl.toExternalForm(), Utils.newDirectExecutor());
    127                 wizard.showDialog();
    128                 OAuthAccessTokenHolder.getInstance().setSaveToPreferences(true);
    129                 OAuthAccessTokenHolder.getInstance().save(Main.pref, CredentialsManager.getInstance());
    130                 return wizard;
    131             });
    132             // exception handling differs from implementation at GuiHelper.runInEDTAndWait()
    133             if (SwingUtilities.isEventDispatchThread()) {
    134                 authTask.run();
    135             } else {
    136                 SwingUtilities.invokeAndWait(authTask);
    137             }
     146            fetcher.obtainAccessToken(apiUrl);
     147            OAuthAccessTokenHolder.getInstance().setSaveToPreferences(true);
     148            OAuthAccessTokenHolder.getInstance().save(Main.pref, CredentialsManager.getInstance());
    138149        } catch (MalformedURLException | InterruptedException | InvocationTargetException e) {
    139150            throw new MissingOAuthAccessTokenException(e);
Note: See TracChangeset for help on using the changeset viewer.