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

Last change on this file since 8673 was 8510, checked in by Don-vip, 9 years ago

checkstyle: enable relevant whitespace checks and fix them

  • Property svn:eol-style set to native
File size: 3.8 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.io.OsmTransferCanceledException;
18import org.openstreetmap.josm.io.OsmTransferException;
19import org.openstreetmap.josm.tools.CheckParameterUtil;
20import org.xml.sax.SAXException;
21
22/**
23 * Asynchronous task for retrieving an Access Token.
24 *
25 */
26public class RetrieveAccessTokenTask extends PleaseWaitRunnable {
27
28 private boolean canceled;
29 private OAuthToken accessToken;
30 private OAuthParameters parameters;
31 private OsmOAuthAuthorizationClient client;
32 private OAuthToken requestToken;
33 private Component parent;
34
35 /**
36 * Creates the task
37 *
38 * @param parent the parent component relative to which the {@link PleaseWaitRunnable}-Dialog
39 * is displayed
40 * @param parameters the OAuth parameters. Must not be null.
41 * @param requestToken the request token for which an Access Token is retrieved. Must not be null.
42 * @throws IllegalArgumentException if parameters is null.
43 * @throws IllegalArgumentException if requestToken is null.
44 */
45 public RetrieveAccessTokenTask(Component parent, OAuthParameters parameters, OAuthToken requestToken) {
46 super(parent, tr("Retrieving OAuth Access Token..."), false /* don't ignore exceptions */);
47 CheckParameterUtil.ensureParameterNotNull(parameters, "parameters");
48 CheckParameterUtil.ensureParameterNotNull(requestToken, "requestToken");
49 this.parameters = parameters;
50 this.requestToken = requestToken;
51 this.parent = parent;
52 }
53
54 @Override
55 protected void cancel() {
56 canceled = true;
57 synchronized (this) {
58 if (client != null) {
59 client.cancel();
60 }
61 }
62 }
63
64 @Override
65 protected void finish() { /* not used in this task */}
66
67 protected void alertRetrievingAccessTokenFailed(OsmOAuthAuthorizationException e) {
68 HelpAwareOptionPane.showOptionDialog(
69 parent,
70 tr(
71 "<html>Retrieving an OAuth Access Token from ''{0}'' failed.</html>",
72 parameters.getAccessTokenUrl()
73 ),
74 tr("Request Failed"),
75 JOptionPane.ERROR_MESSAGE,
76 HelpUtil.ht("/OAuth#NotAuthorizedException")
77 );
78 }
79
80 @Override
81 protected void realRun() throws SAXException, IOException, OsmTransferException {
82 try {
83 synchronized (this) {
84 client = new OsmOAuthAuthorizationClient(parameters, requestToken);
85 }
86 accessToken = client.getAccessToken(getProgressMonitor().createSubTaskMonitor(0, false));
87 } catch (OsmTransferCanceledException e) {
88 return;
89 } catch (OsmOAuthAuthorizationException e) {
90 Main.error(e);
91 alertRetrievingAccessTokenFailed(e);
92 accessToken = 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 {@code true} if user aborted operation
104 */
105 public boolean isCanceled() {
106 return canceled;
107 }
108
109 /**
110 * Replies the retrieved Access Token. null, if something went wrong.
111 *
112 * @return the retrieved Access Token
113 */
114 public OAuthToken getAccessToken() {
115 return accessToken;
116 }
117}
Note: See TracBrowser for help on using the repository browser.