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

Revision 4310, 3.8 KB checked in by stoecker, 10 months ago (diff)

fix #6680, fix #6677 - i18n issues

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