source: josm/trunk/src/org/openstreetmap/josm/gui/oauth/RetrieveRequestTokenTask.java@ 10189

Last change on this file since 10189 was 9354, checked in by simon04, 8 years ago

Fix EDT violation

  • Property svn:eol-style set to native
File size: 3.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.oauth;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Component;
7import java.io.IOException;
8
9import javax.swing.JOptionPane;
10
11import org.openstreetmap.josm.Main;
12import org.openstreetmap.josm.data.oauth.OAuthParameters;
13import org.openstreetmap.josm.data.oauth.OAuthToken;
14import org.openstreetmap.josm.gui.HelpAwareOptionPane;
15import org.openstreetmap.josm.gui.PleaseWaitRunnable;
16import org.openstreetmap.josm.gui.help.HelpUtil;
17import org.openstreetmap.josm.gui.util.GuiHelper;
18import org.openstreetmap.josm.io.OsmTransferCanceledException;
19import org.openstreetmap.josm.io.OsmTransferException;
20import org.openstreetmap.josm.tools.CheckParameterUtil;
21import org.xml.sax.SAXException;
22
23/**
24 * Asynchronous task for retrieving a request token
25 */
26public 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(OsmOAuthAuthorizationException e) {
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 return;
84 } catch (final OsmOAuthAuthorizationException e) {
85 Main.error(e);
86 GuiHelper.runInEDT(new Runnable() {
87 @Override
88 public void run() {
89 alertRetrievingRequestTokenFailed(e);
90 }
91 });
92 requestToken = null;
93 } finally {
94 synchronized (this) {
95 client = null;
96 }
97 }
98 }
99
100 /**
101 * Replies true if the task was canceled
102 *
103 * @return true if the task was canceled
104 */
105 public boolean isCanceled() {
106 return canceled;
107 }
108
109 /**
110 * Replies the request token. null, if something went wrong.
111 *
112 * @return the request token
113 */
114 public OAuthToken getRequestToken() {
115 return requestToken;
116 }
117}
Note: See TracBrowser for help on using the repository browser.