[2801] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.gui.oauth;
|
---|
| 3 |
|
---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 |
|
---|
| 6 | import java.awt.Component;
|
---|
| 7 | import java.io.IOException;
|
---|
| 8 |
|
---|
| 9 | import javax.swing.JOptionPane;
|
---|
| 10 |
|
---|
[6643] | 11 | import org.openstreetmap.josm.Main;
|
---|
[2801] | 12 | import org.openstreetmap.josm.data.oauth.OAuthParameters;
|
---|
| 13 | import org.openstreetmap.josm.data.oauth.OAuthToken;
|
---|
| 14 | import org.openstreetmap.josm.gui.HelpAwareOptionPane;
|
---|
| 15 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
| 16 | import org.openstreetmap.josm.gui.help.HelpUtil;
|
---|
[9256] | 17 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
[4310] | 18 | import org.openstreetmap.josm.io.OsmTransferCanceledException;
|
---|
[2801] | 19 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
| 20 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
| 21 | import org.xml.sax.SAXException;
|
---|
| 22 |
|
---|
| 23 | /**
|
---|
| 24 | * Asynchronous task for retrieving an Access Token.
|
---|
[3530] | 25 | *
|
---|
[2801] | 26 | */
|
---|
| 27 | public class RetrieveAccessTokenTask extends PleaseWaitRunnable {
|
---|
| 28 |
|
---|
| 29 | private boolean canceled;
|
---|
| 30 | private OAuthToken accessToken;
|
---|
[9078] | 31 | private final OAuthParameters parameters;
|
---|
[2861] | 32 | private OsmOAuthAuthorizationClient client;
|
---|
[9078] | 33 | private final OAuthToken requestToken;
|
---|
| 34 | private final Component parent;
|
---|
[2801] | 35 |
|
---|
| 36 | /**
|
---|
| 37 | * Creates the task
|
---|
[3530] | 38 | *
|
---|
[5266] | 39 | * @param parent the parent component relative to which the {@link PleaseWaitRunnable}-Dialog
|
---|
[2801] | 40 | * is displayed
|
---|
| 41 | * @param parameters the OAuth parameters. Must not be null.
|
---|
| 42 | * @param requestToken the request token for which an Access Token is retrieved. Must not be null.
|
---|
[8291] | 43 | * @throws IllegalArgumentException if parameters is null.
|
---|
| 44 | * @throws IllegalArgumentException if requestToken is null.
|
---|
[2801] | 45 | */
|
---|
| 46 | public RetrieveAccessTokenTask(Component parent, OAuthParameters parameters, OAuthToken requestToken) {
|
---|
| 47 | super(parent, tr("Retrieving OAuth Access Token..."), false /* don't ignore exceptions */);
|
---|
| 48 | CheckParameterUtil.ensureParameterNotNull(parameters, "parameters");
|
---|
| 49 | CheckParameterUtil.ensureParameterNotNull(requestToken, "requestToken");
|
---|
| 50 | this.parameters = parameters;
|
---|
| 51 | this.requestToken = requestToken;
|
---|
| 52 | this.parent = parent;
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | @Override
|
---|
| 56 | protected void cancel() {
|
---|
| 57 | canceled = true;
|
---|
[8510] | 58 | synchronized (this) {
|
---|
[2801] | 59 | if (client != null) {
|
---|
| 60 | client.cancel();
|
---|
| 61 | }
|
---|
| 62 | }
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | @Override
|
---|
| 66 | protected void finish() { /* not used in this task */}
|
---|
| 67 |
|
---|
[11604] | 68 | protected void alertRetrievingAccessTokenFailed() {
|
---|
[2801] | 69 | HelpAwareOptionPane.showOptionDialog(
|
---|
| 70 | parent,
|
---|
| 71 | tr(
|
---|
| 72 | "<html>Retrieving an OAuth Access Token from ''{0}'' failed.</html>",
|
---|
| 73 | parameters.getAccessTokenUrl()
|
---|
| 74 | ),
|
---|
| 75 | tr("Request Failed"),
|
---|
| 76 | JOptionPane.ERROR_MESSAGE,
|
---|
| 77 | HelpUtil.ht("/OAuth#NotAuthorizedException")
|
---|
| 78 | );
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | @Override
|
---|
| 82 | protected void realRun() throws SAXException, IOException, OsmTransferException {
|
---|
| 83 | try {
|
---|
[8510] | 84 | synchronized (this) {
|
---|
[2861] | 85 | client = new OsmOAuthAuthorizationClient(parameters, requestToken);
|
---|
[2801] | 86 | }
|
---|
| 87 | accessToken = client.getAccessToken(getProgressMonitor().createSubTaskMonitor(0, false));
|
---|
[8510] | 88 | } catch (OsmTransferCanceledException e) {
|
---|
[10627] | 89 | Main.trace(e);
|
---|
[2801] | 90 | return;
|
---|
[9256] | 91 | } catch (final OsmOAuthAuthorizationException e) {
|
---|
[6643] | 92 | Main.error(e);
|
---|
[11604] | 93 | GuiHelper.runInEDT(this::alertRetrievingAccessTokenFailed);
|
---|
[2801] | 94 | accessToken = null;
|
---|
| 95 | } finally {
|
---|
[8510] | 96 | synchronized (this) {
|
---|
[2801] | 97 | client = null;
|
---|
| 98 | }
|
---|
| 99 | }
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | /**
|
---|
| 103 | * Replies true if the task was canceled.
|
---|
[3530] | 104 | *
|
---|
[5899] | 105 | * @return {@code true} if user aborted operation
|
---|
[2801] | 106 | */
|
---|
| 107 | public boolean isCanceled() {
|
---|
| 108 | return canceled;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | /**
|
---|
| 112 | * Replies the retrieved Access Token. null, if something went wrong.
|
---|
[3530] | 113 | *
|
---|
[2801] | 114 | * @return the retrieved Access Token
|
---|
| 115 | */
|
---|
| 116 | public OAuthToken getAccessToken() {
|
---|
| 117 | return accessToken;
|
---|
| 118 | }
|
---|
| 119 | }
|
---|