001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.gui.oauth; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005 006import java.awt.Component; 007import java.io.IOException; 008 009import javax.swing.JOptionPane; 010 011import org.openstreetmap.josm.Main; 012import org.openstreetmap.josm.data.oauth.OAuthParameters; 013import org.openstreetmap.josm.data.oauth.OAuthToken; 014import org.openstreetmap.josm.gui.HelpAwareOptionPane; 015import org.openstreetmap.josm.gui.PleaseWaitRunnable; 016import org.openstreetmap.josm.gui.help.HelpUtil; 017import org.openstreetmap.josm.io.OsmTransferCanceledException; 018import org.openstreetmap.josm.io.OsmTransferException; 019import org.openstreetmap.josm.tools.CheckParameterUtil; 020import org.xml.sax.SAXException; 021 022/** 023 * Asynchronous task for retrieving a request token 024 */ 025public class RetrieveRequestTokenTask extends PleaseWaitRunnable { 026 027 private boolean canceled; 028 private OAuthToken requestToken; 029 private OAuthParameters parameters; 030 private OsmOAuthAuthorizationClient client; 031 private Component parent; 032 033 /** 034 * Creates the task 035 * 036 * @param parent the parent component relative to which the {@link PleaseWaitRunnable}-Dialog 037 * is displayed 038 * @param parameters the OAuth parameters. Must not be null. 039 * @throws IllegalArgumentException thrown if parameters is null. 040 */ 041 public RetrieveRequestTokenTask(Component parent, OAuthParameters parameters ) { 042 super(parent, tr("Retrieving OAuth Request Token..."), false /* don't ignore exceptions */); 043 CheckParameterUtil.ensureParameterNotNull(parameters, "parameters"); 044 this.parameters = parameters; 045 this.parent = parent; 046 } 047 048 @Override 049 protected void cancel() { 050 canceled = true; 051 synchronized(this) { 052 if (client != null) { 053 client.cancel(); 054 } 055 } 056 } 057 058 @Override 059 protected void finish() { /* not used in this task */} 060 061 protected void alertRetrievingRequestTokenFailed(OsmOAuthAuthorizationException e) { 062 HelpAwareOptionPane.showOptionDialog( 063 parent, 064 tr( 065 "<html>Retrieving an OAuth Request Token from ''{0}'' failed.</html>", 066 parameters.getRequestTokenUrl() 067 ), 068 tr("Request Failed"), 069 JOptionPane.ERROR_MESSAGE, 070 HelpUtil.ht("/OAuth#NotAuthorizedException") 071 ); 072 } 073 074 @Override 075 protected void realRun() throws SAXException, IOException, OsmTransferException { 076 try { 077 synchronized(this) { 078 client = new OsmOAuthAuthorizationClient(parameters); 079 } 080 requestToken = client.getRequestToken(getProgressMonitor().createSubTaskMonitor(0, false)); 081 } catch(OsmTransferCanceledException e) { 082 return; 083 } catch (OsmOAuthAuthorizationException e) { 084 Main.error(e); 085 alertRetrievingRequestTokenFailed(e); 086 requestToken = null; 087 } finally { 088 synchronized(this) { 089 client = null; 090 } 091 } 092 } 093 094 /** 095 * Replies true if the task was canceled 096 * 097 * @return true if the task was canceled 098 */ 099 public boolean isCanceled() { 100 return canceled; 101 } 102 103 /** 104 * Replies the request token. null, if something went wrong. 105 * 106 * @return the request token 107 */ 108 public OAuthToken getRequestToken() { 109 return requestToken; 110 } 111}