Changeset 12803 in josm for trunk/src/org/openstreetmap/josm/io
- Timestamp:
- 2017-09-09T16:45:41+02:00 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/io/OsmConnection.java
r12686 r12803 11 11 import java.util.Base64; 12 12 import java.util.Objects; 13 import java.util.concurrent.FutureTask;14 15 import javax.swing.SwingUtilities;16 13 17 14 import org.openstreetmap.josm.Main; 18 15 import org.openstreetmap.josm.data.oauth.OAuthAccessTokenHolder; 19 16 import org.openstreetmap.josm.data.oauth.OAuthParameters; 20 import org.openstreetmap.josm.gui.oauth.OAuthAuthorizationWizard;21 17 import org.openstreetmap.josm.io.auth.CredentialsAgentException; 22 18 import org.openstreetmap.josm.io.auth.CredentialsAgentResponse; 23 19 import org.openstreetmap.josm.io.auth.CredentialsManager; 24 20 import org.openstreetmap.josm.tools.HttpClient; 21 import org.openstreetmap.josm.tools.JosmRuntimeException; 25 22 import org.openstreetmap.josm.tools.Logging; 26 import org.openstreetmap.josm.tools.Utils;27 23 28 24 import oauth.signpost.OAuthConsumer; … … 39 35 protected HttpClient activeConnection; 40 36 protected OAuthParameters oauthParameters; 37 38 /** 39 * Retrieves OAuth access token. 40 * @since 12803 41 */ 42 public interface OAuthAccessTokenFetcher { 43 /** 44 * Obtains an OAuth access token for the connection. Afterwards, the token is accessible via {@link OAuthAccessTokenHolder}. 45 * @param serverUrl the URL to OSM server 46 * @throws InterruptedException if we're interrupted while waiting for the event dispatching thread to finish OAuth authorization task 47 * @throws InvocationTargetException if an exception is thrown while running OAuth authorization task 48 */ 49 void obtainAccessToken(URL serverUrl) throws InvocationTargetException, InterruptedException; 50 } 51 52 static OAuthAccessTokenFetcher fetcher = u -> { 53 throw new JosmRuntimeException("OsmConnection.setOAuthAccessTokenFetcher() has not been called"); 54 }; 55 56 /** 57 * Sets the OAuth access token fetcher. 58 * @param tokenFetcher new OAuth access token fetcher. Cannot be null 59 * @since 12803 60 */ 61 public static void setOAuthAccessTokenFetcher(OAuthAccessTokenFetcher tokenFetcher) { 62 fetcher = Objects.requireNonNull(tokenFetcher, "tokenFetcher"); 63 } 41 64 42 65 /** … … 110 133 111 134 /** 112 * Obtains an OAuth access token for the connection. Afterwards, the token is accessible via {@link OAuthAccessTokenHolder}. 135 * Obtains an OAuth access token for the connection. 136 * Afterwards, the token is accessible via {@link OAuthAccessTokenHolder} / {@link CredentialsManager}. 113 137 * @param connection connection for which the access token should be obtained 114 * @throws MissingOAuthAccessTokenException if the process cannot be complete csuccessfully138 * @throws MissingOAuthAccessTokenException if the process cannot be completed successfully 115 139 */ 116 140 protected void obtainAccessToken(final HttpClient connection) throws MissingOAuthAccessTokenException { … … 120 144 throw new MissingOAuthAccessTokenException(); 121 145 } 122 final Runnable authTask = new FutureTask<>(() -> { 123 // Concerning Utils.newDirectExecutor: Main worker cannot be used since this connection is already 124 // executed via main worker. The OAuth connections would block otherwise. 125 final OAuthAuthorizationWizard wizard = new OAuthAuthorizationWizard( 126 Main.parent, apiUrl.toExternalForm(), Utils.newDirectExecutor()); 127 wizard.showDialog(); 128 OAuthAccessTokenHolder.getInstance().setSaveToPreferences(true); 129 OAuthAccessTokenHolder.getInstance().save(Main.pref, CredentialsManager.getInstance()); 130 return wizard; 131 }); 132 // exception handling differs from implementation at GuiHelper.runInEDTAndWait() 133 if (SwingUtilities.isEventDispatchThread()) { 134 authTask.run(); 135 } else { 136 SwingUtilities.invokeAndWait(authTask); 137 } 146 fetcher.obtainAccessToken(apiUrl); 147 OAuthAccessTokenHolder.getInstance().setSaveToPreferences(true); 148 OAuthAccessTokenHolder.getInstance().save(Main.pref, CredentialsManager.getInstance()); 138 149 } catch (MalformedURLException | InterruptedException | InvocationTargetException e) { 139 150 throw new MissingOAuthAccessTokenException(e);
Note:
See TracChangeset
for help on using the changeset viewer.