Changeset 9352 in josm
- Timestamp:
- 2016-01-09T16:25:08+01:00 (9 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/oauth/FullyAutomaticAuthorizationUI.java
r9350 r9352 15 15 import java.net.Authenticator.RequestorType; 16 16 import java.net.PasswordAuthentication; 17 import java.util.concurrent.Executor; 17 18 18 19 import javax.swing.AbstractAction; … … 67 68 private JPanel pnlActionButtonsPanel; 68 69 private JPanel pnlResult; 70 private final Executor executor; 69 71 70 72 /** … … 296 298 * Constructs a new {@code FullyAutomaticAuthorizationUI} for the given API URL. 297 299 * @param apiUrl The OSM API URL 300 * @param executor the executor used for running the HTTP requests for the authorization 298 301 * @since 5422 299 302 */ 300 public FullyAutomaticAuthorizationUI(String apiUrl ) {303 public FullyAutomaticAuthorizationUI(String apiUrl, Executor executor) { 301 304 super(apiUrl); 305 this.executor = executor; 302 306 build(); 303 307 } … … 327 331 @Override 328 332 public void actionPerformed(ActionEvent evt) { 329 Main.worker.submit(new FullyAutomaticAuthorisationTask(FullyAutomaticAuthorizationUI.this));333 executor.execute(new FullyAutomaticAuthorisationTask(FullyAutomaticAuthorizationUI.this)); 330 334 } 331 335 … … 377 381 @Override 378 382 public void actionPerformed(ActionEvent arg0) { 379 Main.worker.submit(new TestAccessTokenTask(383 executor.execute(new TestAccessTokenTask( 380 384 FullyAutomaticAuthorizationUI.this, 381 385 getApiUrl(), -
trunk/src/org/openstreetmap/josm/gui/oauth/ManualAuthorizationUI.java
r9059 r9352 12 12 import java.beans.PropertyChangeEvent; 13 13 import java.beans.PropertyChangeListener; 14 import java.util.concurrent.Executor; 14 15 15 16 import javax.swing.AbstractAction; … … 23 24 import javax.swing.text.JTextComponent; 24 25 25 import org.openstreetmap.josm.Main;26 26 import org.openstreetmap.josm.data.oauth.OAuthToken; 27 27 import org.openstreetmap.josm.gui.SideButton; … … 47 47 private JCheckBox cbSaveToPreferences; 48 48 private HtmlPanel pnlMessage; 49 private final Executor executor; 49 50 50 51 protected JPanel buildAccessTokenPanel() { … … 163 164 * Constructs a new {@code ManualAuthorizationUI} for the given API URL. 164 165 * @param apiUrl The OSM API URL 166 * @param executor the executor used for running the HTTP requests for the authorization 165 167 * @since 5422 166 168 */ 167 public ManualAuthorizationUI(String apiUrl ) {169 public ManualAuthorizationUI(String apiUrl, Executor executor) { 168 170 super(apiUrl); 171 this.executor = executor; 169 172 build(); 170 173 } … … 261 264 getAccessToken() 262 265 ); 263 Main.worker.submit(task);266 executor.execute(task); 264 267 } 265 268 -
trunk/src/org/openstreetmap/josm/gui/oauth/OAuthAuthorizationWizard.java
r9300 r9352 22 22 import java.beans.PropertyChangeEvent; 23 23 import java.beans.PropertyChangeListener; 24 import java.util.concurrent.Executor; 24 25 25 26 import javax.swing.AbstractAction; … … 44 45 import org.openstreetmap.josm.gui.help.ContextSensitiveHelpAction; 45 46 import org.openstreetmap.josm.gui.help.HelpUtil; 47 import org.openstreetmap.josm.gui.preferences.server.OAuthAccessTokenHolder; 46 48 import org.openstreetmap.josm.gui.util.GuiHelper; 47 49 import org.openstreetmap.josm.gui.widgets.HtmlPanel; … … 49 51 import org.openstreetmap.josm.tools.ImageProvider; 50 52 import org.openstreetmap.josm.tools.OpenBrowser; 53 import org.openstreetmap.josm.tools.UserCancelException; 51 54 import org.openstreetmap.josm.tools.WindowGeometry; 52 55 … … 65 68 private ManualAuthorizationUI pnlManualAuthorisationUI; 66 69 private JScrollPane spAuthorisationProcedureUI; 70 private final Executor executor; 71 72 /** 73 * Launches the wizard, {@link OAuthAccessTokenHolder#setAccessToken(OAuthToken) sets the token} 74 * and {@link OAuthAccessTokenHolder#setSaveToPreferences(boolean) saves to preferences}. 75 * @throws UserCancelException if user cancels the operation 76 */ 77 public void showDialog() throws UserCancelException { 78 setVisible(true); 79 if (isCanceled()) { 80 throw new UserCancelException(); 81 } 82 OAuthAccessTokenHolder holder = OAuthAccessTokenHolder.getInstance(); 83 holder.setAccessToken(getAccessToken()); 84 holder.setSaveToPreferences(isSaveAccessTokenToPreferences()); 85 } 67 86 68 87 /** … … 170 189 this.setMinimumSize(new Dimension(600, 420)); 171 190 172 pnlFullyAutomaticAuthorisationUI = new FullyAutomaticAuthorizationUI(apiUrl );173 pnlSemiAutomaticAuthorisationUI = new SemiAutomaticAuthorizationUI(apiUrl );174 pnlManualAuthorisationUI = new ManualAuthorizationUI(apiUrl );191 pnlFullyAutomaticAuthorisationUI = new FullyAutomaticAuthorizationUI(apiUrl, executor); 192 pnlSemiAutomaticAuthorisationUI = new SemiAutomaticAuthorizationUI(apiUrl, executor); 193 pnlManualAuthorisationUI = new ManualAuthorizationUI(apiUrl, executor); 175 194 176 195 spAuthorisationProcedureUI = GuiHelper.embedInVerticalScrollPane(new JPanel()); … … 209 228 * Creates the wizard. 210 229 * 211 * @param apiUrl the API URL. Must not be null.212 * @throws IllegalArgumentException if apiUrl is null213 */214 public OAuthAuthorizationWizard(String apiUrl) {215 this(Main.parent, apiUrl);216 }217 218 /**219 * Creates the wizard.220 *221 230 * @param parent the component relative to which the dialog is displayed 222 231 * @param apiUrl the API URL. Must not be null. 232 * @param executor the executor used for running the HTTP requests for the authorization 223 233 * @throws IllegalArgumentException if apiUrl is null 224 234 */ 225 public OAuthAuthorizationWizard(Component parent, String apiUrl ) {235 public OAuthAuthorizationWizard(Component parent, String apiUrl, Executor executor) { 226 236 super(JOptionPane.getFrameForComponent(parent), ModalityType.DOCUMENT_MODAL); 227 237 CheckParameterUtil.ensureParameterNotNull(apiUrl, "apiUrl"); 228 238 this.apiUrl = apiUrl; 239 this.executor = executor; 229 240 build(); 230 241 } -
trunk/src/org/openstreetmap/josm/gui/oauth/SemiAutomaticAuthorizationUI.java
r9256 r9352 14 14 import java.awt.event.ItemEvent; 15 15 import java.awt.event.ItemListener; 16 import java.util.concurrent.Executor; 16 17 17 18 import javax.swing.AbstractAction; … … 21 22 import javax.swing.JPanel; 22 23 23 import org.openstreetmap.josm.Main;24 24 import org.openstreetmap.josm.data.oauth.OAuthToken; 25 25 import org.openstreetmap.josm.gui.SideButton; … … 47 47 private RetrieveAccessTokenPanel pnlRetrieveAccessToken; 48 48 private ShowAccessTokenPanel pnlShowAccessToken; 49 49 private final Executor executor; 50 50 51 /** 51 52 * build the UI … … 63 64 * Constructs a new {@code SemiAutomaticAuthorizationUI} for the given API URL. 64 65 * @param apiUrl The OSM API URL 66 * @param executor the executor used for running the HTTP requests for the authorization 65 67 * @since 5422 66 68 */ 67 public SemiAutomaticAuthorizationUI(String apiUrl ) {69 public SemiAutomaticAuthorizationUI(String apiUrl, Executor executor) { 68 70 super(apiUrl); 71 this.executor = executor; 69 72 build(); 70 73 } … … 396 399 getAdvancedPropertiesPanel().getAdvancedParameters() 397 400 ); 398 Main.worker.submit(task);401 executor.execute(task); 399 402 Runnable r = new Runnable() { 400 403 @Override … … 411 414 } 412 415 }; 413 Main.worker.submit(r);416 executor.execute(r); 414 417 } 415 418 } … … 433 436 requestToken 434 437 ); 435 Main.worker.submit(task);438 executor.execute(task); 436 439 Runnable r = new Runnable() { 437 440 @Override … … 448 451 } 449 452 }; 450 Main.worker.submit(r);453 executor.execute(r); 451 454 } 452 455 } … … 471 474 getAccessToken() 472 475 ); 473 Main.worker.submit(task);476 executor.execute(task); 474 477 } 475 478 } -
trunk/src/org/openstreetmap/josm/gui/preferences/server/AuthenticationPreferencesPanel.java
r9059 r9352 21 21 import org.openstreetmap.josm.gui.help.HelpUtil; 22 22 import org.openstreetmap.josm.gui.widgets.VerticallyScrollablePanel; 23 import org.openstreetmap.josm.io.OsmApi; 23 24 import org.openstreetmap.josm.io.auth.CredentialsManager; 24 25 … … 57 58 gc.anchor = GridBagConstraints.NORTHWEST; 58 59 gc.fill = GridBagConstraints.HORIZONTAL; 59 gc.weightx = 0.0; 60 gc.gridx = 1; 61 gc.weightx = 1.0; 60 62 gc.insets = new Insets(0, 0, 0, 3); 61 63 add(rbBasicAuthentication = new JRadioButton(), gc); … … 65 67 66 68 //-- radio button for OAuth 67 gc.gridx = 1;68 gc.weightx = 1.0;69 gc.gridx = 0; 70 gc.weightx = 0.0; 69 71 add(rbOAuth = new JRadioButton(), gc); 70 72 rbOAuth.setText(tr("Use OAuth")); … … 118 120 */ 119 121 public final void initFromPreferences() { 120 String authMethod = Main.pref.get("osm-server.auth-method", "basic");122 final String authMethod = OsmApi.getAuthMethod(); 121 123 if ("basic".equals(authMethod)) { 122 124 rbBasicAuthentication.setSelected(true); -
trunk/src/org/openstreetmap/josm/gui/preferences/server/OAuthAuthenticationPreferencesPanel.java
r8836 r9352 35 35 import org.openstreetmap.josm.io.auth.CredentialsManager; 36 36 import org.openstreetmap.josm.tools.ImageProvider; 37 import org.openstreetmap.josm.tools.UserCancelException; 37 38 38 39 /** … … 325 326 OAuthAuthorizationWizard wizard = new OAuthAuthorizationWizard( 326 327 OAuthAuthenticationPreferencesPanel.this, 327 apiUrl 328 ); 329 wizard.setVisible(true); 330 if (wizard.isCanceled()) return; 331 OAuthAccessTokenHolder holder = OAuthAccessTokenHolder.getInstance(); 332 holder.setAccessToken(wizard.getAccessToken()); 333 holder.setSaveToPreferences(wizard.isSaveAccessTokenToPreferences()); 328 apiUrl, 329 Main.worker); 330 try { 331 wizard.showDialog(); 332 } catch (UserCancelException ignore) { 333 Main.trace(ignore.toString()); 334 return; 335 } 334 336 pnlAdvancedProperties.setAdvancedParameters(wizard.getOAuthParameters()); 335 337 refreshView(); … … 340 342 * Launches the OAuthAuthorisationWizard to generate a new Access Token 341 343 */ 342 private class RenewAuthorisationAction extends A bstractAction {344 private class RenewAuthorisationAction extends AuthoriseNowAction { 343 345 /** 344 346 * Constructs a new {@code RenewAuthorisationAction}. … … 348 350 putValue(SHORT_DESCRIPTION, tr("Click to step through the OAuth authorization process and generate a new Access Token")); 349 351 putValue(SMALL_ICON, ImageProvider.get("oauth", "oauth-small")); 350 351 }352 353 @Override354 public void actionPerformed(ActionEvent arg0) {355 OAuthAuthorizationWizard wizard = new OAuthAuthorizationWizard(356 OAuthAuthenticationPreferencesPanel.this,357 apiUrl358 );359 wizard.setVisible(true);360 if (wizard.isCanceled()) return;361 OAuthAccessTokenHolder holder = OAuthAccessTokenHolder.getInstance();362 holder.setAccessToken(wizard.getAccessToken());363 holder.setSaveToPreferences(wizard.isSaveAccessTokenToPreferences());364 pnlAdvancedProperties.setAdvancedParameters(wizard.getOAuthParameters());365 refreshView();366 352 } 367 353 } -
trunk/src/org/openstreetmap/josm/io/OsmApi.java
r9309 r9352 582 582 * @since 6349 583 583 */ 584 public static final boolean isUsingOAuth() { 585 return "oauth".equals(Main.pref.get("osm-server.auth-method", "basic")); 584 public static boolean isUsingOAuth() { 585 return "oauth".equals(getAuthMethod()); 586 } 587 588 /** 589 * Returns the authentication method set in the preferences 590 * @return the authentication method 591 */ 592 public static String getAuthMethod() { 593 return Main.pref.get("osm-server.auth-method", "oauth"); 586 594 } 587 595 -
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); -
trunk/src/org/openstreetmap/josm/tools/Utils.java
r9351 r9352 49 49 import java.util.Locale; 50 50 import java.util.Objects; 51 import java.util.concurrent.ExecutorService; 52 import java.util.concurrent.Executors; 51 import java.util.concurrent.Executor; 53 52 import java.util.concurrent.ForkJoinPool; 54 53 import java.util.concurrent.ForkJoinWorkerThread; … … 1431 1430 } 1432 1431 }, null, true); 1432 } 1433 1434 /** 1435 * Returns an executor which executes commands in the calling thread 1436 * @return an executor 1437 */ 1438 public static Executor newDirectExecutor() { 1439 return new Executor() { 1440 @Override 1441 public void execute(Runnable command) { 1442 command.run(); 1443 } 1444 }; 1433 1445 } 1434 1446
Note:
See TracChangeset
for help on using the changeset viewer.