| 1 | // License: GPL. For details, see LICENSE file. |
|---|
| 2 | package org.openstreetmap.josm.gui.oauth; |
|---|
| 3 | |
|---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr; |
|---|
| 5 | |
|---|
| 6 | import java.awt.Component; |
|---|
| 7 | import java.io.IOException; |
|---|
| 8 | |
|---|
| 9 | import javax.swing.JOptionPane; |
|---|
| 10 | |
|---|
| 11 | import org.openstreetmap.josm.data.oauth.OAuthParameters; |
|---|
| 12 | import org.openstreetmap.josm.data.oauth.OAuthToken; |
|---|
| 13 | import org.openstreetmap.josm.gui.HelpAwareOptionPane; |
|---|
| 14 | import org.openstreetmap.josm.gui.PleaseWaitRunnable; |
|---|
| 15 | import org.openstreetmap.josm.gui.help.HelpUtil; |
|---|
| 16 | import org.openstreetmap.josm.io.OsmTransferCanceledException; |
|---|
| 17 | import org.openstreetmap.josm.io.OsmTransferException; |
|---|
| 18 | import org.openstreetmap.josm.tools.CheckParameterUtil; |
|---|
| 19 | import org.xml.sax.SAXException; |
|---|
| 20 | |
|---|
| 21 | /** |
|---|
| 22 | * Asynchronous task for retrieving a request token |
|---|
| 23 | */ |
|---|
| 24 | public 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 | } |
|---|