Changeset 12803 in josm


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

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

Legend:

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

    r12796 r12803  
    107107import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    108108import org.openstreetmap.josm.gui.layer.TMSLayer;
     109import org.openstreetmap.josm.gui.oauth.OAuthAuthorizationWizard;
    109110import org.openstreetmap.josm.gui.preferences.ToolbarPreferences;
    110111import org.openstreetmap.josm.gui.preferences.display.LafPreference;
     
    126127import org.openstreetmap.josm.io.OsmApi;
    127128import org.openstreetmap.josm.io.OsmApiInitializationException;
     129import org.openstreetmap.josm.io.OsmConnection;
    128130import org.openstreetmap.josm.io.OsmTransferCanceledException;
    129131import org.openstreetmap.josm.io.OsmTransferException;
     
    10701072
    10711073    static void setupCallbacks() {
     1074        OsmConnection.setOAuthAccessTokenFetcher(OAuthAuthorizationWizard::obtainAccessToken);
    10721075        MessageNotifier.setNotifierCallback(MainApplication::notifyNewMessages);
    10731076        DeleteCommand.setDeletionCallback(DeleteAction.defaultDeletionCallback);
  • trunk/src/org/openstreetmap/josm/gui/oauth/OAuthAuthorizationWizard.java

    r12686 r12803  
    2121import java.beans.PropertyChangeEvent;
    2222import java.beans.PropertyChangeListener;
     23import java.lang.reflect.InvocationTargetException;
     24import java.net.URL;
    2325import java.util.concurrent.Executor;
     26import java.util.concurrent.FutureTask;
    2427
    2528import javax.swing.AbstractAction;
     
    3033import javax.swing.JPanel;
    3134import javax.swing.JScrollPane;
     35import javax.swing.SwingUtilities;
    3236import javax.swing.UIManager;
    3337import javax.swing.event.HyperlinkEvent;
     
    5155import org.openstreetmap.josm.tools.OpenBrowser;
    5256import org.openstreetmap.josm.tools.UserCancelException;
     57import org.openstreetmap.josm.tools.Utils;
    5358
    5459/**
     
    333338    }
    334339
     340    /**
     341     * Obtains an OAuth access token for the connection. Afterwards, the token is accessible via {@link OAuthAccessTokenHolder}.
     342     * @param serverUrl the URL to OSM server
     343     * @throws InterruptedException if we're interrupted while waiting for the event dispatching thread to finish OAuth authorization task
     344     * @throws InvocationTargetException if an exception is thrown while running OAuth authorization task
     345     * @since 12803
     346     */
     347    public static void obtainAccessToken(final URL serverUrl) throws InvocationTargetException, InterruptedException {
     348        final Runnable authTask = new FutureTask<>(() -> {
     349            // Concerning Utils.newDirectExecutor: Main worker cannot be used since this connection is already
     350            // executed via main worker. The OAuth connections would block otherwise.
     351            final OAuthAuthorizationWizard wizard = new OAuthAuthorizationWizard(
     352                    Main.parent, serverUrl.toExternalForm(), Utils.newDirectExecutor());
     353            wizard.showDialog();
     354            return wizard;
     355        });
     356        // exception handling differs from implementation at GuiHelper.runInEDTAndWait()
     357        if (SwingUtilities.isEventDispatchThread()) {
     358            authTask.run();
     359        } else {
     360            SwingUtilities.invokeAndWait(authTask);
     361        }
     362    }
     363
    335364    class AuthorisationProcedureChangeListener implements ItemListener {
    336365        @Override
  • 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);
  • trunk/src/org/openstreetmap/josm/tools/OsmUrlToBounds.java

    r12796 r12803  
    77import java.util.HashMap;
    88import java.util.Map;
     9import java.util.Objects;
    910import java.util.function.Supplier;
    1011
     
    211212     */
    212213    public static void setMapSizeSupplier(Supplier<Dimension> mapSizeSupplier) {
    213         mapSize = mapSizeSupplier;
     214        mapSize = Objects.requireNonNull(mapSizeSupplier, "mapSizeSupplier");
    214215    }
    215216
Note: See TracChangeset for help on using the changeset viewer.