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

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