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 a request token
|
---|
25 | */
|
---|
26 | public class RetrieveRequestTokenTask extends PleaseWaitRunnable {
|
---|
27 |
|
---|
28 | private boolean canceled;
|
---|
29 | private OAuthToken requestToken;
|
---|
30 | private final OAuthParameters parameters;
|
---|
31 | private OsmOAuthAuthorizationClient client;
|
---|
32 | private final Component parent;
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Creates the task
|
---|
36 | *
|
---|
37 | * @param parent the parent component relative to which the {@link PleaseWaitRunnable}-Dialog
|
---|
38 | * is displayed
|
---|
39 | * @param parameters the OAuth parameters. Must not be null.
|
---|
40 | * @throws IllegalArgumentException if parameters is null.
|
---|
41 | */
|
---|
42 | public RetrieveRequestTokenTask(Component parent, OAuthParameters parameters) {
|
---|
43 | super(parent, tr("Retrieving OAuth Request Token..."), false /* don't ignore exceptions */);
|
---|
44 | CheckParameterUtil.ensureParameterNotNull(parameters, "parameters");
|
---|
45 | this.parameters = parameters;
|
---|
46 | this.parent = parent;
|
---|
47 | }
|
---|
48 |
|
---|
49 | @Override
|
---|
50 | protected void cancel() {
|
---|
51 | canceled = true;
|
---|
52 | synchronized (this) {
|
---|
53 | if (client != null) {
|
---|
54 | client.cancel();
|
---|
55 | }
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | @Override
|
---|
60 | protected void finish() { /* not used in this task */}
|
---|
61 |
|
---|
62 | protected void alertRetrievingRequestTokenFailed() {
|
---|
63 | HelpAwareOptionPane.showOptionDialog(
|
---|
64 | parent,
|
---|
65 | tr(
|
---|
66 | "<html>Retrieving an OAuth Request Token from ''{0}'' failed.</html>",
|
---|
67 | parameters.getRequestTokenUrl()
|
---|
68 | ),
|
---|
69 | tr("Request Failed"),
|
---|
70 | JOptionPane.ERROR_MESSAGE,
|
---|
71 | HelpUtil.ht("/OAuth#NotAuthorizedException")
|
---|
72 | );
|
---|
73 | }
|
---|
74 |
|
---|
75 | @Override
|
---|
76 | protected void realRun() throws SAXException, IOException, OsmTransferException {
|
---|
77 | try {
|
---|
78 | synchronized (this) {
|
---|
79 | client = new OsmOAuthAuthorizationClient(parameters);
|
---|
80 | }
|
---|
81 | requestToken = client.getRequestToken(getProgressMonitor().createSubTaskMonitor(0, false));
|
---|
82 | } catch (OsmTransferCanceledException e) {
|
---|
83 | Logging.trace(e);
|
---|
84 | } catch (final OsmOAuthAuthorizationException e) {
|
---|
85 | Logging.error(e);
|
---|
86 | GuiHelper.runInEDT(this::alertRetrievingRequestTokenFailed);
|
---|
87 | requestToken = null;
|
---|
88 | } finally {
|
---|
89 | synchronized (this) {
|
---|
90 | client = null;
|
---|
91 | }
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * Replies true if the task was canceled
|
---|
97 | *
|
---|
98 | * @return true if the task was canceled
|
---|
99 | */
|
---|
100 | public boolean isCanceled() {
|
---|
101 | return canceled;
|
---|
102 | }
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * Replies the request token. null, if something went wrong.
|
---|
106 | *
|
---|
107 | * @return the request token
|
---|
108 | */
|
---|
109 | public OAuthToken getRequestToken() {
|
---|
110 | return requestToken;
|
---|
111 | }
|
---|
112 | }
|
---|