source: josm/trunk/src/org/openstreetmap/josm/gui/oauth/RetrieveAccessTokenTask.java@ 17318

Last change on this file since 17318 was 13849, checked in by Don-vip, 6 years ago

SonarQube - fix minor code issues

  • Property svn:eol-style set to native
File size: 3.9 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.data.oauth.OAuthParameters;
12import org.openstreetmap.josm.data.oauth.OAuthToken;
13import org.openstreetmap.josm.gui.HelpAwareOptionPane;
14import org.openstreetmap.josm.gui.PleaseWaitRunnable;
15import org.openstreetmap.josm.gui.help.HelpUtil;
16import org.openstreetmap.josm.gui.util.GuiHelper;
17import org.openstreetmap.josm.io.OsmTransferCanceledException;
18import org.openstreetmap.josm.io.OsmTransferException;
19import org.openstreetmap.josm.tools.CheckParameterUtil;
20import org.openstreetmap.josm.tools.Logging;
21import org.xml.sax.SAXException;
22
23/**
24 * Asynchronous task for retrieving an Access Token.
25 *
26 */
27public 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 } catch (final OsmOAuthAuthorizationException e) {
91 Logging.error(e);
92 GuiHelper.runInEDT(this::alertRetrievingAccessTokenFailed);
93 accessToken = null;
94 } finally {
95 synchronized (this) {
96 client = null;
97 }
98 }
99 }
100
101 /**
102 * Replies true if the task was canceled.
103 *
104 * @return {@code true} if user aborted operation
105 */
106 public boolean isCanceled() {
107 return canceled;
108 }
109
110 /**
111 * Replies the retrieved Access Token. null, if something went wrong.
112 *
113 * @return the retrieved Access Token
114 */
115 public OAuthToken getAccessToken() {
116 return accessToken;
117 }
118}
Note: See TracBrowser for help on using the repository browser.