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