Changeset 19651 in josm
- Timestamp:
- 2026-10-22T16:46:16+02:00 (4 hours ago)
- Location:
- trunk
- Files:
-
- 8 edited
-
. (modified) (1 prop)
-
src/org/openstreetmap/josm/data/oauth/OAuthParameters.java (modified) (2 diffs)
-
src/org/openstreetmap/josm/gui/oauth/TestAccessTokenTask.java (modified) (3 diffs)
-
src/org/openstreetmap/josm/gui/preferences/server/ApiUrlTestTask.java (modified) (8 diffs)
-
src/org/openstreetmap/josm/gui/preferences/server/OsmApiUrlInputPanel.java (modified) (5 diffs)
-
src/org/openstreetmap/josm/io/OsmApi.java (modified) (2 diffs)
-
test/unit/org/openstreetmap/josm/data/preferences/JosmUrlsTest.java (modified) (1 diff)
-
test/unit/org/openstreetmap/josm/io/OsmApiTest.java (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk
- Property svn:ignore
-
old new 25 25 target 26 26 failed.png 27 dependency-reduced-pom.xml
-
- Property svn:ignore
-
trunk/src/org/openstreetmap/josm/data/oauth/OAuthParameters.java
r19638 r19651 148 148 final String redirectUri = "http://127.0.0.1:8111/oauth_authorization"; 149 149 final String baseUrl; 150 apiUrl = apiUrl == null ? OsmApi.getOsmApi().getServerUrl() : apiUrl; 150 apiUrl = OsmApi.normalizeApiUrl(apiUrl == null ? OsmApi.getOsmApi().getServerUrl() : apiUrl); 151 151 switch (apiUrl) { 152 152 case OSM_API_DEV: … … 239 239 */ 240 240 public static IOAuthParameters createFromApiUrl(String apiUrl, OAuthVersion oAuthVersion) { 241 apiUrl = OsmApi.normalizeApiUrl(apiUrl); 241 242 final String originalApiUrl = apiUrl; 242 243 // We actually need the host -
trunk/src/org/openstreetmap/josm/gui/oauth/TestAccessTokenTask.java
r18991 r19651 19 19 import org.openstreetmap.josm.gui.PleaseWaitRunnable; 20 20 import org.openstreetmap.josm.gui.help.HelpUtil; 21 import org.openstreetmap.josm.io.OsmApi; 21 22 import org.openstreetmap.josm.io.OsmApiException; 22 23 import org.openstreetmap.josm.io.OsmServerUserInfoReader; … … 60 61 this.tokenOAuth2 = accessToken; 61 62 this.parent = parent; 62 this.apiUrl = apiUrl;63 this.apiUrl = OsmApi.normalizeApiUrl(apiUrl); 63 64 } 64 65 … … 82 83 } 83 84 84 protected String normalizeApiUrl(String url) {85 // remove leading and trailing white space86 url = url.trim();87 88 // remove trailing slashes89 while (url.endsWith("/")) {90 url = url.substring(0, url.lastIndexOf('/'));91 }92 return url;93 }94 95 85 protected UserInfo getUserDetails() throws OsmOAuthAuthorizationException, XmlParsingException, OsmTransferException { 96 86 boolean authenticatorEnabled = true; 97 87 try { 98 URL url = new URL( normalizeApiUrl(apiUrl)+ "/0.6/user/details");88 URL url = new URL(apiUrl + "/0.6/user/details"); 99 89 authenticatorEnabled = DefaultAuthenticator.getInstance().isEnabled(); 100 90 DefaultAuthenticator.getInstance().setEnabled(false); -
trunk/src/org/openstreetmap/josm/gui/preferences/server/ApiUrlTestTask.java
r13849 r19651 17 17 import org.openstreetmap.josm.gui.help.HelpUtil; 18 18 import org.openstreetmap.josm.io.Capabilities; 19 import org.openstreetmap.josm.io.OsmApi; 19 20 import org.openstreetmap.josm.io.OsmTransferException; 20 21 import org.openstreetmap.josm.tools.CheckParameterUtil; … … 49 50 CheckParameterUtil.ensureParameterNotNull(url, "url"); 50 51 this.parent = parent; 51 this.url = url;52 this.url = OsmApi.normalizeApiUrl(url); 52 53 } 53 54 … … 67 68 } 68 69 69 protected void alertInvalidCapabilitiesUrl(String url) {70 protected void alertInvalidCapabilitiesUrl(String capabilitiesUrl) { 70 71 HelpAwareOptionPane.showMessageDialogInEDT( 71 72 parent, … … 74 75 + "Please check the spelling of ''{1}'' and validate again." 75 76 +"</html>", 76 url,77 getNormalizedApiUrl()77 capabilitiesUrl, 78 url 78 79 ), 79 80 tr("Invalid API URL"), … … 91 92 +"</html>", 92 93 url, 93 getNormalizedApiUrl()94 url 94 95 ), 95 96 tr("Connection to API failed"), … … 108 109 + "</html>", 109 110 retCode, 110 getNormalizedApiUrl()111 url 111 112 ), 112 113 tr("Connection to API failed"), … … 124 125 + "Please check the spelling of ''{0}'' and validate again." 125 126 + "</html>", 126 getNormalizedApiUrl()127 url 127 128 ), 128 129 tr("Connection to API failed"), … … 147 148 } 148 149 149 /**150 * Removes leading and trailing whitespace from the API URL and removes trailing '/'.151 *152 * @return the normalized API URL153 */154 protected String getNormalizedApiUrl() {155 String apiUrl = url.trim();156 while (apiUrl.endsWith("/")) {157 apiUrl = apiUrl.substring(0, apiUrl.lastIndexOf('/'));158 }159 return apiUrl;160 }161 162 150 @Override 163 151 protected void realRun() throws SAXException, IOException, OsmTransferException { 164 152 try { 165 153 try { 166 new URL( getNormalizedApiUrl());154 new URL(url); 167 155 } catch (MalformedURLException e) { 168 alertInvalidUrl( getNormalizedApiUrl());156 alertInvalidUrl(url); 169 157 return; 170 158 } 171 159 URL capabilitiesUrl; 172 String getCapabilitiesUrl = getNormalizedApiUrl()+ "/0.6/capabilities";160 String getCapabilitiesUrl = url + "/0.6/capabilities"; 173 161 try { 174 162 capabilitiesUrl = new URL(getCapabilitiesUrl); -
trunk/src/org/openstreetmap/josm/gui/preferences/server/OsmApiUrlInputPanel.java
r19629 r19651 126 126 public void saveToPreferences() { 127 127 String oldUrl = OsmApi.getOsmApi().getServerUrl(); 128 String hmiUrl = get StrippedApiUrl();128 String hmiUrl = getApiUrl(); 129 129 if (cbUseDefaultServerUrl.isSelected() || Config.getUrls().getDefaultOsmApiUrl().equals(hmiUrl)) { 130 130 Config.getPref().put("osm-server.url", null); … … 147 147 148 148 /** 149 * Returns the entered API URL, stripped of leading and trailing white characters. 150 * @return the entered API URL, stripped of leading and trailing white characters. May be an empty string 151 * if nothing has been entered. In this case, it means the user wants to use {@link IUrls#getDefaultOsmApiUrl}. 152 * @see Utils#strip(String) 153 * @since 6602 154 */ 155 public final String getStrippedApiUrl() { 156 return Utils.strip(tfOsmServerUrl.getText()); 149 * Returns the entered API URL, normalized with {@link OsmApi#normalizeApiUrl(String)}, that is stripped of 150 * leading and trailing white characters and of trailing '/'. 151 * @return the entered API URL. May be an empty string if nothing has been entered. In this case, it means 152 * the user wants to use {@link IUrls#getDefaultOsmApiUrl}. 153 * @see OsmApi#normalizeApiUrl(String) 154 * @since 19651 (renamed from {@code getStrippedApiUrl}, which existed since 6602) 155 */ 156 public final String getApiUrl() { 157 return OsmApi.normalizeApiUrl(tfOsmServerUrl.getText()); 157 158 } 158 159 … … 168 169 @Override 169 170 public void actionPerformed(ActionEvent arg0) { 170 final String url = get StrippedApiUrl();171 final String url = getApiUrl(); 171 172 final ApiUrlTestTask task = new ApiUrlTestTask(OsmApiUrlInputPanel.this, url); 172 173 MainApplication.worker.submit(task); … … 191 192 192 193 protected final void updateEnabledState() { 193 String url = get StrippedApiUrl();194 String url = getApiUrl(); 194 195 boolean enabled = !url.isEmpty() && !url.equals(lastTestedUrl); 195 196 if (enabled) { … … 276 277 class ApiUrlPropagator extends FocusAdapter implements ActionListener { 277 278 protected void propagate() { 278 propagate(get StrippedApiUrl());279 propagate(getApiUrl()); 279 280 } 280 281 -
trunk/src/org/openstreetmap/josm/io/OsmApi.java
r19629 r19651 134 134 */ 135 135 public static OsmApi getOsmApi(String serverUrl) { 136 OsmApi api = instances.get(serverUrl); 136 String url = normalizeApiUrl(serverUrl); 137 OsmApi api = instances.get(url); 137 138 if (api == null) { 138 api = new OsmApi( serverUrl);139 api = new OsmApi(url); 139 140 cacheInstance(api); 140 141 } 141 142 return api; 143 } 144 145 /** 146 * Normalizes an OSM API URL: removes leading and trailing white space and any trailing {@code '/'}. 147 * <p> 148 * The URL may be entered with a trailing slash, but code which compares it to a known API URL or derives 149 * another URL from it does not expect one: OAuth would not find the client id of the server, and the links 150 * to the web site of the server (browse, history, user) would be built from the API path. 151 * @param serverUrl the server URL, may be null 152 * @return the normalized URL, or null if {@code serverUrl} is null 153 * @since 19651 154 */ 155 public static String normalizeApiUrl(String serverUrl) { 156 if (serverUrl == null) { 157 return null; 158 } 159 String url = Utils.strip(serverUrl); 160 while (url.endsWith("/") && !url.endsWith("://")) { 161 url = url.substring(0, url.length() - 1); 162 } 163 return url; 142 164 } 143 165 … … 182 204 protected OsmApi(String serverUrl) { 183 205 CheckParameterUtil.ensureParameterNotNull(serverUrl, "serverUrl"); 184 this.serverUrl = serverUrl; 206 this.serverUrl = normalizeApiUrl(serverUrl); 185 207 } 186 208 -
trunk/test/unit/org/openstreetmap/josm/data/preferences/JosmUrlsTest.java
r18870 r19651 20 20 assertEquals("https://api06.dev.openstreetmap.org/user", Config.getUrls().getBaseUserUrl()); 21 21 } 22 23 /** 24 * An API URL entered with a trailing slash must give the same web site URLs as one without, so that 25 * "History (web)" and the other links to the server are built from the web site and not from the API path. 26 */ 27 @Test 28 void testApiUrlWithTrailingSlash() { 29 Config.getPref().put("osm-server.url", "https://api06.dev.openstreetmap.org/api/"); 30 assertEquals("https://api06.dev.openstreetmap.org", Config.getUrls().getOSMWebsiteDependingOnSelectedApi()); 31 assertEquals("https://api06.dev.openstreetmap.org", Config.getUrls().getBaseBrowseUrl()); 32 assertEquals("https://api06.dev.openstreetmap.org/user", Config.getUrls().getBaseUserUrl()); 33 } 34 35 /** 36 * The default API URL is recognized even when it is entered with a trailing slash, so the links point to 37 * the OSM web site rather than to the API host. 38 */ 39 @Test 40 void testDefaultApiUrlWithTrailingSlash() { 41 Config.getPref().put("osm-server.url", Config.getUrls().getDefaultOsmApiUrl() + '/'); 42 assertEquals(Config.getUrls().getOSMWebsite(), Config.getUrls().getOSMWebsiteDependingOnSelectedApi()); 43 } 22 44 } -
trunk/test/unit/org/openstreetmap/josm/io/OsmApiTest.java
r18853 r19651 3 3 4 4 import static org.junit.jupiter.api.Assertions.assertEquals; 5 import static org.junit.jupiter.api.Assertions.assertNull; 6 import static org.junit.jupiter.api.Assertions.assertSame; 5 7 6 8 import java.io.ByteArrayInputStream; … … 11 13 import org.openstreetmap.josm.data.osm.User; 12 14 import org.openstreetmap.josm.gui.progress.NullProgressMonitor; 15 import org.openstreetmap.josm.spi.preferences.Config; 16 import org.openstreetmap.josm.testutils.annotations.BasicPreferences; 13 17 14 18 /** 15 19 * Unit tests of {@link OsmApi} class. 16 20 */ 21 @BasicPreferences 22 @org.openstreetmap.josm.testutils.annotations.OsmApi 17 23 class OsmApiTest { 18 24 /** … … 37 43 assertEquals(User.getAnonymous(), cs2.getUser()); 38 44 } 45 46 /** 47 * Unit test of {@link OsmApi#normalizeApiUrl}: white space and trailing slashes are removed. 48 */ 49 @Test 50 void testNormalizeApiUrl() { 51 assertEquals("https://example.com/api", OsmApi.normalizeApiUrl("https://example.com/api")); 52 assertEquals("https://example.com/api", OsmApi.normalizeApiUrl("https://example.com/api/")); 53 assertEquals("https://example.com/api", OsmApi.normalizeApiUrl("https://example.com/api///")); 54 assertEquals("https://example.com/api", OsmApi.normalizeApiUrl(" https://example.com/api/\t")); 55 assertEquals("https://example.com", OsmApi.normalizeApiUrl("https://example.com/")); 56 assertEquals("", OsmApi.normalizeApiUrl(" ")); 57 assertNull(OsmApi.normalizeApiUrl(null)); 58 // the slashes of the scheme must not be eaten 59 assertEquals("https://", OsmApi.normalizeApiUrl("https://")); 60 } 61 62 /** 63 * A URL which only differs by a trailing slash is the same API, and is stored without the slash. 64 */ 65 @Test 66 void testGetOsmApiIgnoresTrailingSlash() { 67 OsmApi api = OsmApi.getOsmApi("https://example.com/api/"); 68 assertEquals("https://example.com/api", api.getServerUrl()); 69 assertSame(api, OsmApi.getOsmApi("https://example.com/api")); 70 assertSame(api, OsmApi.getOsmApi(" https://example.com/api// ")); 71 } 72 73 /** 74 * An API URL stored with a trailing slash is used without it, so that code comparing it to a known URL or 75 * deriving another URL from it works. Non-regression test for the OAuth and "History (web)" breakage. 76 */ 77 @Test 78 void testTrailingSlashInPreference() { 79 Config.getPref().put("osm-server.url", "https://example.com/api/"); 80 assertEquals("https://example.com/api", OsmApi.getOsmApi().getServerUrl()); 81 assertEquals("https://example.com/api/", OsmApi.getOsmApi().getBaseUrl()); 82 } 39 83 }
Note:
See TracChangeset
for help on using the changeset viewer.
