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 |
|
---|
11 | import org.openstreetmap.josm.data.oauth.OAuthParameters;
|
---|
12 | import org.openstreetmap.josm.data.oauth.OAuthToken;
|
---|
13 | import org.openstreetmap.josm.gui.HelpAwareOptionPane;
|
---|
14 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
15 | import org.openstreetmap.josm.gui.help.HelpUtil;
|
---|
16 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
17 | import org.openstreetmap.josm.io.OsmTransferCanceledException;
|
---|
18 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
19 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
20 | import org.openstreetmap.josm.tools.Logging;
|
---|
21 | import org.xml.sax.SAXException;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Asynchronous task for retrieving an Access Token.
|
---|
25 | *
|
---|
26 | */
|
---|
27 | public class RetrieveAccessTokenTask extends PleaseWaitRunnable {
|
---|
28 |
|
---|
29 | private boolean canceled;
|
---|
30 | private OAuthToken accessToken;
|
---|
31 | private final OAuthParameters parameters;
|
---|
32 | private OsmOAuthAuthorizationClient client;
|
---|
33 | private final OAuthToken requestToken;
|
---|
34 | private final Component parent;
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Creates the task
|
---|
38 | *
|
---|
39 | * @param parent the parent component relative to which the {@link PleaseWaitRunnable}-Dialog
|
---|
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.
|
---|
43 | * @throws IllegalArgumentException if parameters is null.
|
---|
44 | * @throws IllegalArgumentException if requestToken is null.
|
---|
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;
|
---|
58 | synchronized (this) {
|
---|
59 | if (client != null) {
|
---|
60 | client.cancel();
|
---|
61 | }
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | @Override
|
---|
66 | protected void finish() { /* not used in this task */}
|
---|
67 |
|
---|
68 | protected void alertRetrievingAccessTokenFailed() {
|
---|
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 {
|
---|
84 | synchronized (this) {
|
---|
85 | client = new OsmOAuthAuthorizationClient(parameters, requestToken);
|
---|
86 | }
|
---|
87 | accessToken = client.getAccessToken(getProgressMonitor().createSubTaskMonitor(0, false));
|
---|
88 | } catch (OsmTransferCanceledException e) {
|
---|
89 | Logging.trace(e);
|
---|
90 | return;
|
---|
91 | } catch (final OsmOAuthAuthorizationException e) {
|
---|
92 | Logging.error(e);
|
---|
93 | GuiHelper.runInEDT(this::alertRetrievingAccessTokenFailed);
|
---|
94 | accessToken = null;
|
---|
95 | } finally {
|
---|
96 | synchronized (this) {
|
---|
97 | client = null;
|
---|
98 | }
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Replies true if the task was canceled.
|
---|
104 | *
|
---|
105 | * @return {@code true} if user aborted operation
|
---|
106 | */
|
---|
107 | public boolean isCanceled() {
|
---|
108 | return canceled;
|
---|
109 | }
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * Replies the retrieved Access Token. null, if something went wrong.
|
---|
113 | *
|
---|
114 | * @return the retrieved Access Token
|
---|
115 | */
|
---|
116 | public OAuthToken getAccessToken() {
|
---|
117 | return accessToken;
|
---|
118 | }
|
---|
119 | }
|
---|