001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.oauth;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.awt.Component;
007import java.io.IOException;
008
009import javax.swing.JOptionPane;
010
011import org.openstreetmap.josm.data.oauth.OAuthParameters;
012import org.openstreetmap.josm.data.oauth.OAuthToken;
013import org.openstreetmap.josm.gui.HelpAwareOptionPane;
014import org.openstreetmap.josm.gui.PleaseWaitRunnable;
015import org.openstreetmap.josm.gui.help.HelpUtil;
016import org.openstreetmap.josm.io.OsmTransferCanceledException;
017import org.openstreetmap.josm.io.OsmTransferException;
018import org.openstreetmap.josm.tools.CheckParameterUtil;
019import org.xml.sax.SAXException;
020
021/**
022 * Asynchronous task for retrieving a request token
023 */
024public class RetrieveRequestTokenTask extends PleaseWaitRunnable {
025
026    private boolean canceled;
027    private OAuthToken requestToken;
028    private OAuthParameters parameters;
029    private OsmOAuthAuthorizationClient client;
030    private Component parent;
031
032    /**
033     * Creates the task
034     *
035     * @param parent the parent component relative to which the {@link PleaseWaitRunnable}-Dialog
036     * is displayed
037     * @param parameters the OAuth parameters. Must not be null.
038     * @throws IllegalArgumentException thrown if parameters is null.
039     */
040    public RetrieveRequestTokenTask(Component parent, OAuthParameters parameters ) {
041        super(parent, tr("Retrieving OAuth Request Token..."), false /* don't ignore exceptions */);
042        CheckParameterUtil.ensureParameterNotNull(parameters, "parameters");
043        this.parameters = parameters;
044        this.parent = parent;
045    }
046
047    @Override
048    protected void cancel() {
049        canceled = true;
050        synchronized(this) {
051            if (client != null) {
052                client.cancel();
053            }
054        }
055    }
056
057    @Override
058    protected void finish() { /* not used in this task */}
059
060    protected void alertRetrievingRequestTokenFailed(OsmOAuthAuthorizationException e) {
061        HelpAwareOptionPane.showOptionDialog(
062                parent,
063                tr(
064                        "<html>Retrieving an OAuth Request Token from ''{0}'' failed.</html>",
065                        parameters.getRequestTokenUrl()
066                ),
067                tr("Request Failed"),
068                JOptionPane.ERROR_MESSAGE,
069                HelpUtil.ht("/OAuth#NotAuthorizedException")
070        );
071    }
072
073    @Override
074    protected void realRun() throws SAXException, IOException, OsmTransferException {
075        try {
076            synchronized(this) {
077                client = new OsmOAuthAuthorizationClient(parameters);
078            }
079            requestToken = client.getRequestToken(getProgressMonitor().createSubTaskMonitor(0, false));
080        } catch(OsmTransferCanceledException e) {
081            return;
082        } catch (OsmOAuthAuthorizationException e) {
083            e.printStackTrace();
084            alertRetrievingRequestTokenFailed(e);
085            requestToken = null;
086        } finally {
087            synchronized(this) {
088                client = null;
089            }
090        }
091    }
092
093    /**
094     * Replies true if the task was canceled
095     *
096     * @return true if the task was canceled
097     */
098    public boolean isCanceled() {
099        return canceled;
100    }
101
102    /**
103     * Replies the request token. null, if something went wrong.
104     *
105     * @return the request token
106     */
107    public OAuthToken getRequestToken() {
108        return requestToken;
109    }
110}