| 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 an Access Token. |
|---|
| 23 | * |
|---|
| 24 | */ |
|---|
| 25 | public 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 | } |
|---|