Ticket #7612: 7612-v1.patch

File 7612-v1.patch, 21.7 KB (added by simon04, 8 years ago)
  • src/org/openstreetmap/josm/gui/oauth/FullyAutomaticAuthorizationUI.java

    diff --git a/src/org/openstreetmap/josm/gui/oauth/FullyAutomaticAuthorizationUI.java b/src/org/openstreetmap/josm/gui/oauth/FullyAutomaticAuthorizationUI.java
    index d564184..ef1645f 100644
    a b import java.awt.event.ActionEvent;  
    1414import java.io.IOException;
    1515import java.net.Authenticator.RequestorType;
    1616import java.net.PasswordAuthentication;
     17import java.util.concurrent.Executor;
    1718
    1819import javax.swing.AbstractAction;
    1920import javax.swing.BorderFactory;
    public class FullyAutomaticAuthorizationUI extends AbstractAuthorizationUI {  
    6667    private JPanel pnlPropertiesPanel;
    6768    private JPanel pnlActionButtonsPanel;
    6869    private JPanel pnlResult;
     70    private final Executor executor;
    6971
    7072    /**
    7173     * Builds the panel with the three privileges the user can grant JOSM
    public class FullyAutomaticAuthorizationUI extends AbstractAuthorizationUI {  
    314316    /**
    315317     * Constructs a new {@code FullyAutomaticAuthorizationUI} for the given API URL.
    316318     * @param apiUrl The OSM API URL
     319     * @param executor the executor used for running the HTTP requests for the authorization
    317320     * @since 5422
    318321     */
    319     public FullyAutomaticAuthorizationUI(String apiUrl) {
     322    public FullyAutomaticAuthorizationUI(String apiUrl, Executor executor) {
    320323        super(apiUrl);
     324        this.executor = executor;
    321325        build();
    322326    }
    323327
    public class FullyAutomaticAuthorizationUI extends AbstractAuthorizationUI {  
    345349
    346350        @Override
    347351        public void actionPerformed(ActionEvent evt) {
    348             Main.worker.submit(new FullyAutomaticAuthorisationTask(FullyAutomaticAuthorizationUI.this));
     352            executor.execute(new FullyAutomaticAuthorisationTask(FullyAutomaticAuthorizationUI.this));
    349353        }
    350354
    351355        protected final void updateEnabledState() {
    public class FullyAutomaticAuthorizationUI extends AbstractAuthorizationUI {  
    395399
    396400        @Override
    397401        public void actionPerformed(ActionEvent arg0) {
    398             Main.worker.submit(new TestAccessTokenTask(
     402            executor.execute(new TestAccessTokenTask(
    399403                    FullyAutomaticAuthorizationUI.this,
    400404                    getApiUrl(),
    401405                    getAdvancedPropertiesPanel().getAdvancedParameters(),
  • src/org/openstreetmap/josm/gui/oauth/ManualAuthorizationUI.java

    diff --git a/src/org/openstreetmap/josm/gui/oauth/ManualAuthorizationUI.java b/src/org/openstreetmap/josm/gui/oauth/ManualAuthorizationUI.java
    index 63dc2a2..865aaee 100644
    a b import java.awt.Insets;  
    1111import java.awt.event.ActionEvent;
    1212import java.beans.PropertyChangeEvent;
    1313import java.beans.PropertyChangeListener;
     14import java.util.concurrent.Executor;
    1415
    1516import javax.swing.AbstractAction;
    1617import javax.swing.BorderFactory;
    import javax.swing.event.DocumentEvent;  
    2223import javax.swing.event.DocumentListener;
    2324import javax.swing.text.JTextComponent;
    2425
    25 import org.openstreetmap.josm.Main;
    2626import org.openstreetmap.josm.data.oauth.OAuthToken;
    2727import org.openstreetmap.josm.gui.SideButton;
    2828import org.openstreetmap.josm.gui.preferences.server.OAuthAccessTokenHolder;
    public class ManualAuthorizationUI extends AbstractAuthorizationUI {  
    4646    private transient AccessTokenSecretValidator valAccessTokenSecret;
    4747    private JCheckBox cbSaveToPreferences;
    4848    private HtmlPanel pnlMessage;
     49    private final Executor executor;
    4950
    5051    protected JPanel buildAccessTokenPanel() {
    5152        JPanel pnl = new JPanel(new GridBagLayout());
    public class ManualAuthorizationUI extends AbstractAuthorizationUI {  
    162163    /**
    163164     * Constructs a new {@code ManualAuthorizationUI} for the given API URL.
    164165     * @param apiUrl The OSM API URL
     166     * @param executor the executor used for running the HTTP requests for the authorization
    165167     * @since 5422
    166168     */
    167     public ManualAuthorizationUI(String apiUrl) {
     169    public ManualAuthorizationUI(String apiUrl, Executor executor) {
    168170        super(apiUrl);
     171        this.executor = executor;
    169172        build();
    170173    }
    171174
    public class ManualAuthorizationUI extends AbstractAuthorizationUI {  
    260263                    getAdvancedPropertiesPanel().getAdvancedParameters(),
    261264                    getAccessToken()
    262265            );
    263             Main.worker.submit(task);
     266            executor.execute(task);
    264267        }
    265268
    266269        protected final void updateEnabledState() {
  • src/org/openstreetmap/josm/gui/oauth/OAuthAuthorizationWizard.java

    diff --git a/src/org/openstreetmap/josm/gui/oauth/OAuthAuthorizationWizard.java b/src/org/openstreetmap/josm/gui/oauth/OAuthAuthorizationWizard.java
    index 2b44935..68d49cc 100644
    a b import java.awt.event.WindowAdapter;  
    2121import java.awt.event.WindowEvent;
    2222import java.beans.PropertyChangeEvent;
    2323import java.beans.PropertyChangeListener;
     24import java.util.concurrent.Executor;
    2425
    2526import javax.swing.AbstractAction;
    2627import javax.swing.BorderFactory;
    import org.openstreetmap.josm.data.oauth.OAuthToken;  
    4344import org.openstreetmap.josm.gui.SideButton;
    4445import org.openstreetmap.josm.gui.help.ContextSensitiveHelpAction;
    4546import org.openstreetmap.josm.gui.help.HelpUtil;
     47import org.openstreetmap.josm.gui.preferences.server.OAuthAccessTokenHolder;
    4648import org.openstreetmap.josm.gui.util.GuiHelper;
    4749import org.openstreetmap.josm.gui.widgets.HtmlPanel;
    4850import org.openstreetmap.josm.tools.CheckParameterUtil;
    4951import org.openstreetmap.josm.tools.ImageProvider;
    5052import org.openstreetmap.josm.tools.OpenBrowser;
     53import org.openstreetmap.josm.tools.UserCancelException;
    5154import org.openstreetmap.josm.tools.WindowGeometry;
    5255
    5356/**
    public class OAuthAuthorizationWizard extends JDialog {  
    6467    private SemiAutomaticAuthorizationUI pnlSemiAutomaticAuthorisationUI;
    6568    private ManualAuthorizationUI pnlManualAuthorisationUI;
    6669    private JScrollPane spAuthorisationProcedureUI;
     70    private final Executor executor;
     71
     72    /**
     73     * Launches the wizard, {@link OAuthAccessTokenHolder#setAccessToken(OAuthToken) sets the token}
     74     * and {@link OAuthAccessTokenHolder#setSaveToPreferences(boolean) saves to preferences}.
     75     * @param saveAccessTokenToPreferences whether the retrieved token should be saved to preferences
     76     *                                     (overrides {@link #isSaveAccessTokenToPreferences()})
     77     * @throws UserCancelException if user cancels the operation
     78     */
     79    public void showDialog(final boolean saveAccessTokenToPreferences) throws UserCancelException {
     80        setVisible(true);
     81        if (isCanceled()) {
     82            throw new UserCancelException();
     83        }
     84        OAuthAccessTokenHolder holder = OAuthAccessTokenHolder.getInstance();
     85        holder.setAccessToken(getAccessToken());
     86        holder.setSaveToPreferences(saveAccessTokenToPreferences || isSaveAccessTokenToPreferences());
     87    }
    6788
    6889    /**
    6990     * Builds the row with the action buttons
    public class OAuthAuthorizationWizard extends JDialog {  
    169190        setTitle(tr("Get an Access Token for ''{0}''", apiUrl));
    170191        this.setMinimumSize(new Dimension(600, 420));
    171192
    172         pnlFullyAutomaticAuthorisationUI = new FullyAutomaticAuthorizationUI(apiUrl);
    173         pnlSemiAutomaticAuthorisationUI = new SemiAutomaticAuthorizationUI(apiUrl);
    174         pnlManualAuthorisationUI = new ManualAuthorizationUI(apiUrl);
     193        pnlFullyAutomaticAuthorisationUI = new FullyAutomaticAuthorizationUI(apiUrl, executor);
     194        pnlSemiAutomaticAuthorisationUI = new SemiAutomaticAuthorizationUI(apiUrl, executor);
     195        pnlManualAuthorisationUI = new ManualAuthorizationUI(apiUrl, executor);
    175196
    176197        spAuthorisationProcedureUI = GuiHelper.embedInVerticalScrollPane(new JPanel());
    177198        spAuthorisationProcedureUI.getVerticalScrollBar().addComponentListener(
    public class OAuthAuthorizationWizard extends JDialog {  
    208229    /**
    209230     * Creates the wizard.
    210231     *
    211      * @param apiUrl the API URL. Must not be null.
    212      * @throws IllegalArgumentException if apiUrl is null
    213      */
    214     public OAuthAuthorizationWizard(String apiUrl) {
    215         this(Main.parent, apiUrl);
    216     }
    217 
    218     /**
    219      * Creates the wizard.
    220      *
    221232     * @param parent the component relative to which the dialog is displayed
    222233     * @param apiUrl the API URL. Must not be null.
     234     * @param executor the executor used for running the HTTP requests for the authorization
    223235     * @throws IllegalArgumentException if apiUrl is null
    224236     */
    225     public OAuthAuthorizationWizard(Component parent, String apiUrl) {
     237    public OAuthAuthorizationWizard(Component parent, String apiUrl, Executor executor) {
    226238        super(JOptionPane.getFrameForComponent(parent), ModalityType.DOCUMENT_MODAL);
    227239        CheckParameterUtil.ensureParameterNotNull(apiUrl, "apiUrl");
    228240        this.apiUrl = apiUrl;
     241        this.executor = executor;
    229242        build();
    230243    }
    231244
  • src/org/openstreetmap/josm/gui/oauth/SemiAutomaticAuthorizationUI.java

    diff --git a/src/org/openstreetmap/josm/gui/oauth/SemiAutomaticAuthorizationUI.java b/src/org/openstreetmap/josm/gui/oauth/SemiAutomaticAuthorizationUI.java
    index c8f3b74..ddc0aad 100644
    a b import java.awt.Insets;  
    1313import java.awt.event.ActionEvent;
    1414import java.awt.event.ItemEvent;
    1515import java.awt.event.ItemListener;
     16import java.util.concurrent.Executor;
    1617
    1718import javax.swing.AbstractAction;
    1819import javax.swing.BorderFactory;
    import javax.swing.JCheckBox;  
    2021import javax.swing.JLabel;
    2122import javax.swing.JPanel;
    2223
    23 import org.openstreetmap.josm.Main;
    2424import org.openstreetmap.josm.data.oauth.OAuthToken;
    2525import org.openstreetmap.josm.gui.SideButton;
    2626import org.openstreetmap.josm.gui.preferences.server.OAuthAccessTokenHolder;
    public class SemiAutomaticAuthorizationUI extends AbstractAuthorizationUI {  
    4646    private RetrieveRequestTokenPanel pnlRetrieveRequestToken;
    4747    private RetrieveAccessTokenPanel pnlRetrieveAccessToken;
    4848    private ShowAccessTokenPanel pnlShowAccessToken;
    49 
     49    private final Executor executor;
     50   
    5051    /**
    5152     * build the UI
    5253     */
    public class SemiAutomaticAuthorizationUI extends AbstractAuthorizationUI {  
    6263    /**
    6364     * Constructs a new {@code SemiAutomaticAuthorizationUI} for the given API URL.
    6465     * @param apiUrl The OSM API URL
     66     * @param executor the executor used for running the HTTP requests for the authorization
    6567     * @since 5422
    6668     */
    67     public SemiAutomaticAuthorizationUI(String apiUrl) {
     69    public SemiAutomaticAuthorizationUI(String apiUrl, Executor executor) {
    6870        super(apiUrl);
     71        this.executor = executor;
    6972        build();
    7073    }
    7174
    public class SemiAutomaticAuthorizationUI extends AbstractAuthorizationUI {  
    395398                    SemiAutomaticAuthorizationUI.this,
    396399                    getAdvancedPropertiesPanel().getAdvancedParameters()
    397400            );
    398             Main.worker.submit(task);
     401            executor.execute(task);
    399402            Runnable r  = new Runnable() {
    400403                @Override
    401404                public void run() {
    public class SemiAutomaticAuthorizationUI extends AbstractAuthorizationUI {  
    410413                    });
    411414                }
    412415            };
    413             Main.worker.submit(r);
     416            executor.execute(r);
    414417        }
    415418    }
    416419
    public class SemiAutomaticAuthorizationUI extends AbstractAuthorizationUI {  
    432435                    getAdvancedPropertiesPanel().getAdvancedParameters(),
    433436                    requestToken
    434437            );
    435             Main.worker.submit(task);
     438            executor.execute(task);
    436439            Runnable r  = new Runnable() {
    437440                @Override
    438441                public void run() {
    public class SemiAutomaticAuthorizationUI extends AbstractAuthorizationUI {  
    447450                    });
    448451                }
    449452            };
    450             Main.worker.submit(r);
     453            executor.execute(r);
    451454        }
    452455    }
    453456
    public class SemiAutomaticAuthorizationUI extends AbstractAuthorizationUI {  
    470473                    getAdvancedPropertiesPanel().getAdvancedParameters(),
    471474                    getAccessToken()
    472475            );
    473             Main.worker.submit(task);
     476            executor.execute(task);
    474477        }
    475478    }
    476479}
  • src/org/openstreetmap/josm/gui/preferences/server/AuthenticationPreferencesPanel.java

    diff --git a/src/org/openstreetmap/josm/gui/preferences/server/AuthenticationPreferencesPanel.java b/src/org/openstreetmap/josm/gui/preferences/server/AuthenticationPreferencesPanel.java
    index 308bbde..f5837fc 100644
    a b import javax.swing.JSeparator;  
    2020import org.openstreetmap.josm.Main;
    2121import org.openstreetmap.josm.gui.help.HelpUtil;
    2222import org.openstreetmap.josm.gui.widgets.VerticallyScrollablePanel;
     23import org.openstreetmap.josm.io.OsmApi;
    2324import org.openstreetmap.josm.io.auth.CredentialsManager;
    2425
    2526/**
    public class AuthenticationPreferencesPanel extends VerticallyScrollablePanel im  
    117118     * Initializes the panel from preferences
    118119     */
    119120    public final void initFromPreferences() {
    120         String authMethod = Main.pref.get("osm-server.auth-method", "basic");
     121        final String authMethod = OsmApi.getAuthMethod();
    121122        if ("basic".equals(authMethod)) {
    122123            rbBasicAuthentication.setSelected(true);
    123124        } else if ("oauth".equals(authMethod)) {
  • src/org/openstreetmap/josm/gui/preferences/server/OAuthAuthenticationPreferencesPanel.java

    diff --git a/src/org/openstreetmap/josm/gui/preferences/server/OAuthAuthenticationPreferencesPanel.java b/src/org/openstreetmap/josm/gui/preferences/server/OAuthAuthenticationPreferencesPanel.java
    index 6a7f553..cfd23b6 100644
    a b import org.openstreetmap.josm.gui.widgets.JosmTextField;  
    3434import org.openstreetmap.josm.io.OsmApi;
    3535import org.openstreetmap.josm.io.auth.CredentialsManager;
    3636import org.openstreetmap.josm.tools.ImageProvider;
     37import org.openstreetmap.josm.tools.UserCancelException;
    3738
    3839/**
    3940 * The preferences panel for the OAuth preferences. This just a summary panel
    public class OAuthAuthenticationPreferencesPanel extends JPanel implements Prope  
    324325        public void actionPerformed(ActionEvent arg0) {
    325326            OAuthAuthorizationWizard wizard = new OAuthAuthorizationWizard(
    326327                    OAuthAuthenticationPreferencesPanel.this,
    327                     apiUrl
    328             );
    329             wizard.setVisible(true);
    330             if (wizard.isCanceled()) return;
    331             OAuthAccessTokenHolder holder = OAuthAccessTokenHolder.getInstance();
    332             holder.setAccessToken(wizard.getAccessToken());
    333             holder.setSaveToPreferences(wizard.isSaveAccessTokenToPreferences());
     328                    apiUrl,
     329                    Main.worker);
     330            try {
     331                wizard.showDialog(false);
     332            } catch (UserCancelException ignore) {
     333                Main.trace(ignore.toString());
     334                return;
     335            }
    334336            pnlAdvancedProperties.setAdvancedParameters(wizard.getOAuthParameters());
    335337            refreshView();
    336338        }
    public class OAuthAuthenticationPreferencesPanel extends JPanel implements Prope  
    339341    /**
    340342     * Launches the OAuthAuthorisationWizard to generate a new Access Token
    341343     */
    342     private class RenewAuthorisationAction extends AbstractAction {
     344    private class RenewAuthorisationAction extends AuthoriseNowAction {
    343345        /**
    344346         * Constructs a new {@code RenewAuthorisationAction}.
    345347         */
    public class OAuthAuthenticationPreferencesPanel extends JPanel implements Prope  
    347349            putValue(NAME, tr("New Access Token"));
    348350            putValue(SHORT_DESCRIPTION, tr("Click to step through the OAuth authorization process and generate a new Access Token"));
    349351            putValue(SMALL_ICON, ImageProvider.get("oauth", "oauth-small"));
    350 
    351         }
    352 
    353         @Override
    354         public void actionPerformed(ActionEvent arg0) {
    355             OAuthAuthorizationWizard wizard = new OAuthAuthorizationWizard(
    356                     OAuthAuthenticationPreferencesPanel.this,
    357                     apiUrl
    358             );
    359             wizard.setVisible(true);
    360             if (wizard.isCanceled()) return;
    361             OAuthAccessTokenHolder holder = OAuthAccessTokenHolder.getInstance();
    362             holder.setAccessToken(wizard.getAccessToken());
    363             holder.setSaveToPreferences(wizard.isSaveAccessTokenToPreferences());
    364             pnlAdvancedProperties.setAdvancedParameters(wizard.getOAuthParameters());
    365             refreshView();
    366352        }
    367353    }
    368354
  • src/org/openstreetmap/josm/io/OsmApi.java

    diff --git a/src/org/openstreetmap/josm/io/OsmApi.java b/src/org/openstreetmap/josm/io/OsmApi.java
    index be950e9..b3f93dd 100644
    a b public class OsmApi extends OsmConnection {  
    581581     * @return {@code true} if JOSM is configured to access OSM API via OAuth, {@code false} otherwise
    582582     * @since 6349
    583583     */
    584     public static final boolean isUsingOAuth() {
    585         return "oauth".equals(Main.pref.get("osm-server.auth-method", "basic"));
     584    public static boolean isUsingOAuth() {
     585        return "oauth".equals(getAuthMethod());
     586    }
     587
     588    /**
     589     * Returns the authentication method set in the preferences
     590     * @return the authentication method
     591     */
     592    public static String getAuthMethod() {
     593        return Main.pref.get("osm-server.auth-method", "oauth");
    586594    }
    587595
    588596    protected final String sendRequest(String requestMethod, String urlSuffix, String requestBody, ProgressMonitor monitor)
  • src/org/openstreetmap/josm/io/OsmConnection.java

    diff --git a/src/org/openstreetmap/josm/io/OsmConnection.java b/src/org/openstreetmap/josm/io/OsmConnection.java
    index 7d690a9..5910083 100644
    a b package org.openstreetmap.josm.io;  
    33
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
     6import java.lang.reflect.InvocationTargetException;
    67import java.net.Authenticator.RequestorType;
     8import java.net.MalformedURLException;
     9import java.net.URL;
    710import java.nio.ByteBuffer;
    811import java.nio.CharBuffer;
    912import java.nio.charset.CharacterCodingException;
    1013import java.nio.charset.CharsetEncoder;
    1114import java.nio.charset.StandardCharsets;
     15import java.util.Objects;
     16import java.util.concurrent.Callable;
     17import java.util.concurrent.Executor;
     18import java.util.concurrent.FutureTask;
    1219
    1320import org.openstreetmap.josm.Main;
    1421import org.openstreetmap.josm.data.oauth.OAuthParameters;
     22import org.openstreetmap.josm.gui.oauth.OAuthAuthorizationWizard;
    1523import org.openstreetmap.josm.gui.preferences.server.OAuthAccessTokenHolder;
    1624import org.openstreetmap.josm.io.auth.CredentialsAgentException;
    1725import org.openstreetmap.josm.io.auth.CredentialsAgentResponse;
    import org.openstreetmap.josm.tools.HttpClient;  
    2230import oauth.signpost.OAuthConsumer;
    2331import oauth.signpost.exception.OAuthException;
    2432
     33import javax.swing.SwingUtilities;
     34
    2535/**
    2636 * Base class that handles common things like authentication for the reader and writer
    2737 * to the osm server.
    public class OsmConnection {  
    95105        }
    96106        OAuthConsumer consumer = oauthParameters.buildConsumer();
    97107        OAuthAccessTokenHolder holder = OAuthAccessTokenHolder.getInstance();
    98         if (!holder.containsAccessToken())
     108        if (!holder.containsAccessToken()) {
     109            try {
     110                final URL apiUrl = new URL(Main.pref.get("osm-server.url", OsmApi.DEFAULT_API_URL));
     111                if (!Objects.equals(apiUrl.getHost(), connection.getURL().getHost())) {
     112                    throw new MissingOAuthAccessTokenException();
     113                }
     114                final FutureTask<OAuthAuthorizationWizard> authTask = new FutureTask<>(new Callable<OAuthAuthorizationWizard>() {
     115                    @Override
     116                    public OAuthAuthorizationWizard call() throws Exception {
     117                        final OAuthAuthorizationWizard wizard = new OAuthAuthorizationWizard(
     118                                Main.parent, apiUrl.toExternalForm(), new Executor() {
     119                            @Override
     120                            public void execute(Runnable command) {
     121                                command.run();
     122                            }
     123                        });
     124                        wizard.showDialog(true);
     125                        return wizard;
     126                    }
     127                });
     128                SwingUtilities.invokeAndWait(authTask);
     129            } catch (MalformedURLException | InterruptedException | InvocationTargetException e) {
     130                throw new MissingOAuthAccessTokenException();
     131            }
     132        }
     133        if (!holder.containsAccessToken()) { // check if wizard completed
    99134            throw new MissingOAuthAccessTokenException();
     135        }
    100136        consumer.setTokenWithSecret(holder.getAccessTokenKey(), holder.getAccessTokenSecret());
    101137        try {
    102138            consumer.sign(connection);
    public class OsmConnection {  
    106142    }
    107143
    108144    protected void addAuth(HttpClient connection) throws OsmTransferException {
    109         String authMethod = Main.pref.get("osm-server.auth-method", "basic");
     145        final String authMethod = OsmApi.getAuthMethod();
    110146        if ("basic".equals(authMethod)) {
    111147            addBasicAuthorizationHeader(connection);
    112148        } else if ("oauth".equals(authMethod)) {