Changeset 9352 in josm for trunk/src/org/openstreetmap/josm/io/OsmConnection.java
- Timestamp:
- 2016-01-09T16:25:08+01:00 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/io/OsmConnection.java
r9309 r9352 4 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 5 6 import java.lang.reflect.InvocationTargetException; 6 7 import java.net.Authenticator.RequestorType; 8 import java.net.MalformedURLException; 9 import java.net.URL; 7 10 import java.nio.ByteBuffer; 8 11 import java.nio.CharBuffer; … … 10 13 import java.nio.charset.CharsetEncoder; 11 14 import java.nio.charset.StandardCharsets; 15 import java.util.Objects; 16 import java.util.concurrent.Callable; 17 import java.util.concurrent.FutureTask; 12 18 13 19 import org.openstreetmap.josm.Main; 14 20 import org.openstreetmap.josm.data.oauth.OAuthParameters; 21 import org.openstreetmap.josm.gui.oauth.OAuthAuthorizationWizard; 15 22 import org.openstreetmap.josm.gui.preferences.server.OAuthAccessTokenHolder; 16 23 import org.openstreetmap.josm.io.auth.CredentialsAgentException; … … 22 29 import oauth.signpost.OAuthConsumer; 23 30 import oauth.signpost.exception.OAuthException; 31 import org.openstreetmap.josm.tools.Utils; 32 33 import javax.swing.SwingUtilities; 24 34 25 35 /** … … 96 106 OAuthConsumer consumer = oauthParameters.buildConsumer(); 97 107 OAuthAccessTokenHolder holder = OAuthAccessTokenHolder.getInstance(); 98 if (!holder.containsAccessToken()) 108 if (!holder.containsAccessToken()) { 109 obtainAccessToken(connection); 110 } 111 if (!holder.containsAccessToken()) { // check if wizard completed 99 112 throw new MissingOAuthAccessTokenException(); 113 } 100 114 consumer.setTokenWithSecret(holder.getAccessTokenKey(), holder.getAccessTokenSecret()); 101 115 try { … … 106 120 } 107 121 122 /** 123 * Obtains an OAuth access token for the connection. Afterwards, the token is accessible via {@link OAuthAccessTokenHolder}. 124 * @param connection connection for which the access token should be obtained 125 * @throws MissingOAuthAccessTokenException if the process cannot be completec successfully 126 */ 127 protected void obtainAccessToken(final HttpClient connection) throws MissingOAuthAccessTokenException { 128 try { 129 final URL apiUrl = new URL(Main.pref.get("osm-server.url", OsmApi.DEFAULT_API_URL)); 130 if (!Objects.equals(apiUrl.getHost(), connection.getURL().getHost())) { 131 throw new MissingOAuthAccessTokenException(); 132 } 133 final Runnable authTask = new FutureTask<>(new Callable<OAuthAuthorizationWizard>() { 134 @Override 135 public OAuthAuthorizationWizard call() throws Exception { 136 // Concerning Utils.newDirectExecutor: Main.worker cannot be used since this connection is already 137 // executed via Main.worker. The OAuth connections would block otherwise. 138 final OAuthAuthorizationWizard wizard = new OAuthAuthorizationWizard( 139 Main.parent, apiUrl.toExternalForm(), Utils.newDirectExecutor()); 140 wizard.showDialog(); 141 OAuthAccessTokenHolder.getInstance().setSaveToPreferences(true); 142 OAuthAccessTokenHolder.getInstance().save(Main.pref, CredentialsManager.getInstance()); 143 return wizard; 144 } 145 }); 146 // exception handling differs from implementation at GuiHelper.runInEDTAndWait() 147 if (SwingUtilities.isEventDispatchThread()) { 148 authTask.run(); 149 } else { 150 SwingUtilities.invokeAndWait(authTask); 151 } 152 } catch (MalformedURLException | InterruptedException | InvocationTargetException e) { 153 throw new MissingOAuthAccessTokenException(); 154 } 155 } 156 108 157 protected void addAuth(HttpClient connection) throws OsmTransferException { 109 String authMethod = Main.pref.get("osm-server.auth-method", "basic");158 final String authMethod = OsmApi.getAuthMethod(); 110 159 if ("basic".equals(authMethod)) { 111 160 addBasicAuthorizationHeader(connection);
Note:
See TracChangeset
for help on using the changeset viewer.