- Timestamp:
- 2012-11-18T00:44:10+01:00 (12 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 16 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/dialogs/properties/PropertiesDialog.java
r5520 r5587 1412 1412 HttpURLConnection conn; 1413 1413 for (URI u : uris) { 1414 conn = (HttpURLConnection) u.toURL().openConnection();1414 conn = Utils.openHttpConnection(u.toURL()); 1415 1415 conn.setConnectTimeout(Main.pref.getInteger("socket.timeout.connect",15)*1000); 1416 1416 … … 1422 1422 conn.disconnect(); 1423 1423 1424 conn = (HttpURLConnection)new URI(u.toString()1424 conn = Utils.openHttpConnection(new URI(u.toString() 1425 1425 .replace("=", "%3D") /* do not URLencode whole string! */ 1426 1426 .replaceFirst("/wiki/", "/w/index.php?redirect=no&title=") 1427 ).toURL() .openConnection();1427 ).toURL()); 1428 1428 conn.setConnectTimeout(Main.pref.getInteger("socket.timeout.connect",15)*1000); 1429 1429 -
trunk/src/org/openstreetmap/josm/gui/download/PlaceSelection.java
r5429 r5587 56 56 import org.openstreetmap.josm.tools.ImageProvider; 57 57 import org.openstreetmap.josm.tools.OsmUrlToBounds; 58 import org.openstreetmap.josm.tools.Utils; 58 59 import org.xml.sax.Attributes; 59 60 import org.xml.sax.InputSource; … … 362 363 URL url = new URL(urlString); 363 364 synchronized(this) { 364 connection = (HttpURLConnection)url.openConnection();365 connection = Utils.openHttpConnection(url); 365 366 } 366 367 connection.setConnectTimeout(Main.pref.getInteger("socket.timeout.connect",15)*1000); -
trunk/src/org/openstreetmap/josm/gui/help/HelpContentReader.java
r5266 r5587 9 9 import java.net.URL; 10 10 11 import org.openstreetmap.josm.tools.Utils; 11 12 import org.openstreetmap.josm.tools.WikiReader; 12 13 … … 46 47 */ 47 48 public String fetchHelpTopicContent(String helpTopicUrl, boolean dotest) throws HelpContentReaderException { 48 URL url = null;49 49 HttpURLConnection con = null; 50 50 BufferedReader in = null; 51 51 try { 52 url = new URL(helpTopicUrl); 53 con = (HttpURLConnection)url.openConnection(); 52 con = Utils.openHttpConnection(new URL(helpTopicUrl)); 54 53 con.connect(); 55 54 in = new BufferedReader(new InputStreamReader(con.getInputStream(),"utf-8")); -
trunk/src/org/openstreetmap/josm/gui/io/DownloadFileTask.java
r5266 r5587 1 // License: GPL. For details, see LICENSE file. 1 2 package org.openstreetmap.josm.gui.io; 2 3 4 5 // License: GPL. For details, see LICENSE file.6 3 7 4 import static org.openstreetmap.josm.tools.I18n.tr; … … 17 14 import java.net.MalformedURLException; 18 15 import java.net.URL; 19 20 import java.net.URLConnection;21 16 import java.util.Enumeration; 22 23 17 import java.util.zip.ZipEntry; 24 18 import java.util.zip.ZipFile; 25 import org.openstreetmap.josm.data.Version; 19 26 20 import org.openstreetmap.josm.gui.PleaseWaitDialog; 27 21 import org.openstreetmap.josm.gui.PleaseWaitRunnable; … … 31 25 32 26 /** 33 * Asynchronous task for downloading and ndunpacking arbitrary file lists34 * Shows progress bar when do nloading27 * Asynchronous task for downloading and unpacking arbitrary file lists 28 * Shows progress bar when downloading 35 29 */ 36 30 public class DownloadFileTask extends PleaseWaitRunnable{ … … 63 57 64 58 private boolean canceled; 65 private URLConnection downloadConnection;59 private HttpURLConnection downloadConnection; 66 60 67 61 private synchronized void closeConnectionIfNeeded() { 68 if (downloadConnection != null && downloadConnection instanceof HttpURLConnection) { 69 HttpURLConnection conn = ((HttpURLConnection) downloadConnection); 70 conn.disconnect(); 62 if (downloadConnection != null) { 63 downloadConnection.disconnect(); 71 64 } 72 65 downloadConnection = null; … … 97 90 int size; 98 91 synchronized(this) { 99 downloadConnection = url.openConnection();92 downloadConnection = Utils.openHttpConnection(url); 100 93 downloadConnection.setRequestProperty("Cache-Control", "no-cache"); 101 downloadConnection.setRequestProperty("User-Agent",Version.getInstance().getAgentString());102 downloadConnection.setRequestProperty("Host", url.getHost());103 94 downloadConnection.connect(); 104 95 size = downloadConnection.getContentLength(); … … 176 167 try { 177 168 zf = new ZipFile(file); 178 Enumeration es = zf.entries();169 Enumeration<?> es = zf.entries(); 179 170 ZipEntry ze; 180 171 while (es.hasMoreElements()) { -
trunk/src/org/openstreetmap/josm/gui/oauth/OsmOAuthAuthorizationClient.java
r5422 r5587 30 30 31 31 import org.openstreetmap.josm.Main; 32 import org.openstreetmap.josm.data.Version;33 32 import org.openstreetmap.josm.data.oauth.OAuthParameters; 34 33 import org.openstreetmap.josm.data.oauth.OAuthToken; … … 38 37 import org.openstreetmap.josm.io.OsmTransferCanceledException; 39 38 import org.openstreetmap.josm.tools.CheckParameterUtil; 39 import org.openstreetmap.josm.tools.Utils; 40 40 41 41 /** … … 325 325 URL url = new URL(sb.toString()); 326 326 synchronized(this) { 327 connection = (HttpURLConnection)url.openConnection();327 connection = Utils.openHttpConnection(url); 328 328 } 329 329 connection.setRequestMethod("GET"); 330 330 connection.setDoInput(true); 331 331 connection.setDoOutput(false); 332 setHttpRequestParameters(connection);333 332 connection.connect(); 334 333 SessionId sessionId = extractOsmSession(connection); … … 355 354 URL url = new URL(getAuthoriseUrl(requestToken)); 356 355 synchronized(this) { 357 connection = (HttpURLConnection)url.openConnection();356 connection = Utils.openHttpConnection(url); 358 357 } 359 358 connection.setRequestMethod("GET"); … … 361 360 connection.setDoOutput(false); 362 361 connection.setRequestProperty("Cookie", "_osm_session=" + sessionId.id + "; _osm_username=" + sessionId.userName); 363 setHttpRequestParameters(connection);364 362 connection.connect(); 365 363 sessionId.token = extractToken(connection); … … 380 378 URL url = new URL(buildOsmLoginUrl()); 381 379 synchronized(this) { 382 connection = (HttpURLConnection)url.openConnection();380 connection = Utils.openHttpConnection(url); 383 381 } 384 382 connection.setRequestMethod("POST"); … … 401 399 // make sure we can catch 302 Moved Temporarily below 402 400 connection.setInstanceFollowRedirects(false); 403 setHttpRequestParameters(connection);404 401 405 402 connection.connect(); … … 437 434 URL url = new URL(buildOsmLogoutUrl()); 438 435 synchronized(this) { 439 connection = (HttpURLConnection)url.openConnection();436 connection = Utils.openHttpConnection(url); 440 437 } 441 438 connection.setRequestMethod("GET"); 442 439 connection.setDoInput(true); 443 440 connection.setDoOutput(false); 444 setHttpRequestParameters(connection);445 441 connection.connect(); 446 } catch(MalformedURLException e) {442 } catch(MalformedURLException e) { 447 443 throw new OsmOAuthAuthorizationException(e); 448 444 } catch(IOException e) { … … 484 480 URL url = new URL(oauthProviderParameters.getAuthoriseUrl()); 485 481 synchronized(this) { 486 connection = (HttpURLConnection)url.openConnection();482 connection = Utils.openHttpConnection(url); 487 483 } 488 484 connection.setRequestMethod("POST"); … … 494 490 connection.setRequestProperty("Cookie", "_osm_session=" + sessionId.id + "; _osm_username=" + sessionId.userName); 495 491 connection.setInstanceFollowRedirects(false); 496 setHttpRequestParameters(connection);497 492 498 493 connection.connect(); … … 520 515 } 521 516 } 522 }523 524 protected void setHttpRequestParameters(HttpURLConnection connection) {525 connection.setRequestProperty("User-Agent", Version.getInstance().getAgentString());526 connection.setRequestProperty("Host", connection.getURL().getHost());527 517 } 528 518 -
trunk/src/org/openstreetmap/josm/gui/oauth/TestAccessTokenTask.java
r5411 r5587 17 17 import oauth.signpost.exception.OAuthException; 18 18 19 import org.openstreetmap.josm.data.Version;20 19 import org.openstreetmap.josm.data.oauth.OAuthParameters; 21 20 import org.openstreetmap.josm.data.oauth.OAuthToken; … … 30 29 import org.openstreetmap.josm.io.auth.DefaultAuthenticator; 31 30 import org.openstreetmap.josm.tools.CheckParameterUtil; 31 import org.openstreetmap.josm.tools.Utils; 32 32 import org.w3c.dom.Document; 33 33 import org.xml.sax.SAXException; … … 104 104 DefaultAuthenticator.getInstance().setEnabled(false); 105 105 synchronized(this) { 106 connection = (HttpURLConnection)url.openConnection();106 connection = Utils.openHttpConnection(url); 107 107 } 108 108 109 109 connection.setDoOutput(true); 110 110 connection.setRequestMethod("GET"); 111 connection.setRequestProperty("User-Agent", Version.getInstance().getAgentString());112 connection.setRequestProperty("Host", connection.getURL().getHost());113 111 sign(connection); 114 112 connection.connect(); -
trunk/src/org/openstreetmap/josm/gui/preferences/imagery/AddWMSLayerPanel.java
r5461 r5587 60 60 import org.openstreetmap.josm.io.UTFInputStreamReader; 61 61 import org.openstreetmap.josm.tools.GBC; 62 import org.openstreetmap.josm.tools.Utils; 62 63 import org.w3c.dom.Document; 63 64 import org.w3c.dom.Element; … … 342 343 try { 343 344 System.out.println("GET "+getCapabilitiesUrl.toString()); 344 URLConnection openConnection = getCapabilitiesUrl.openConnection();345 URLConnection openConnection = Utils.openHttpConnection(getCapabilitiesUrl); 345 346 InputStream inputStream = openConnection.getInputStream(); 346 347 BufferedReader br = new BufferedReader(UTFInputStreamReader.create(inputStream, "UTF-8")); -
trunk/src/org/openstreetmap/josm/gui/preferences/server/ApiUrlTestTask.java
r5266 r5587 20 20 import org.openstreetmap.josm.io.OsmTransferException; 21 21 import org.openstreetmap.josm.tools.CheckParameterUtil; 22 import org.openstreetmap.josm.tools.Utils; 22 23 import org.xml.sax.SAXException; 23 24 … … 182 183 183 184 synchronized(this) { 184 connection = (HttpURLConnection)capabilitiesUrl.openConnection();185 connection = Utils.openHttpConnection(capabilitiesUrl); 185 186 } 186 187 connection.setDoInput(true); 187 188 connection.setDoOutput(false); 188 189 connection.setRequestMethod("GET"); 189 connection.setRequestProperty("User-Agent", Version.getInstance().getAgentString());190 connection.setRequestProperty("Host", connection.getURL().getHost());191 190 connection.connect(); 192 191 -
trunk/src/org/openstreetmap/josm/io/MirroredInputStream.java
r4812 r5587 269 269 int numRedirects = 0; 270 270 while(true) { 271 con = (HttpURLConnection)downloadUrl.openConnection();271 con = Utils.openHttpConnection(downloadUrl); 272 272 con.setInstanceFollowRedirects(false); 273 273 con.setConnectTimeout(Main.pref.getInteger("socket.timeout.connect",15)*1000); -
trunk/src/org/openstreetmap/josm/io/OsmApi.java
r5422 r5587 36 36 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 37 37 import org.openstreetmap.josm.tools.CheckParameterUtil; 38 import org.openstreetmap.josm.tools.Utils; 38 39 import org.xml.sax.Attributes; 39 40 import org.xml.sax.InputSource; … … 585 586 URL url = new URL(new URL(getBaseUrl()), urlSuffix); 586 587 System.out.print(requestMethod + " " + url + "... "); 587 activeConnection = (HttpURLConnection)url.openConnection();588 588 // fix #5369, see http://www.tikalk.com/java/forums/httpurlconnection-disable-keep-alive 589 activeConnection .setRequestProperty("Connection", "close");589 activeConnection = Utils.openHttpConnection(url, false); 590 590 activeConnection.setConnectTimeout(fastFail ? 1000 : Main.pref.getInteger("socket.timeout.connect",15)*1000); 591 591 if (fastFail) { -
trunk/src/org/openstreetmap/josm/io/OsmServerReader.java
r5584 r5587 19 19 import org.openstreetmap.josm.data.osm.DataSet; 20 20 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 21 import org.openstreetmap.josm.tools.Utils; 21 22 22 23 /** … … 64 65 } 65 66 try { 66 activeConnection = (HttpURLConnection)url.openConnection();67 67 // fix #7640, see http://www.tikalk.com/java/forums/httpurlconnection-disable-keep-alive 68 activeConnection .setRequestProperty("Connection", "close");68 activeConnection = Utils.openHttpConnection(url, false); 69 69 } catch(Exception e) { 70 70 throw new OsmTransferException(tr("Failed to open connection to API {0}.", url.toExternalForm()), e); -
trunk/src/org/openstreetmap/josm/io/imagery/OsmosnimkiOffsetServer.java
r4869 r5587 14 14 import org.openstreetmap.josm.data.imagery.ImageryInfo; 15 15 import org.openstreetmap.josm.data.preferences.StringProperty; 16 import org.openstreetmap.josm.tools.Utils; 16 17 17 18 public class OsmosnimkiOffsetServer implements OffsetServer { … … 28 29 URL url = new URL(this.url + "action=CheckAvailability&id=" + URLEncoder.encode(info.getUrl(), "UTF-8")); 29 30 System.out.println(tr("Querying offset availability: {0}", url)); 30 final BufferedReader rdr = new BufferedReader(new InputStreamReader( url.openConnection().getInputStream(), "UTF-8"));31 final BufferedReader rdr = new BufferedReader(new InputStreamReader(Utils.openHttpConnection(url).getInputStream(), "UTF-8")); 31 32 String response = rdr.readLine(); 32 33 System.out.println(tr("Offset server response: {0}", response)); … … 46 47 URL url = new URL(this.url + "action=GetOffsetForPoint&lat=" + ll.lat() + "&lon=" + ll.lon() + "&id=" + URLEncoder.encode(info.getUrl(), "UTF-8")); 47 48 System.out.println(tr("Querying offset: {0}", url.toString())); 48 final BufferedReader rdr = new BufferedReader(new InputStreamReader( url.openConnection().getInputStream(), "UTF-8"));49 final BufferedReader rdr = new BufferedReader(new InputStreamReader(Utils.openHttpConnection(url).getInputStream(), "UTF-8")); 49 50 String s = rdr.readLine(); 50 51 if (s == null) -
trunk/src/org/openstreetmap/josm/io/imagery/WMSGrabber.java
r5017 r5587 162 162 System.out.println("Grabbing WMS " + (attempt > 1? "(attempt " + attempt + ") ":"") + url); 163 163 164 HttpURLConnection conn = (HttpURLConnection) url.openConnection();164 HttpURLConnection conn = Utils.openHttpConnection(url); 165 165 for(Entry<String, String> e : props.entrySet()) { 166 166 conn.setRequestProperty(e.getKey(), e.getValue()); -
trunk/src/org/openstreetmap/josm/plugins/PluginDownloadTask.java
r5266 r5587 119 119 URL url = new URL(pi.downloadlink); 120 120 synchronized(this) { 121 downloadConnection = (HttpURLConnection)url.openConnection();121 downloadConnection = Utils.openHttpConnection(url); 122 122 downloadConnection.setRequestProperty("Cache-Control", "no-cache"); 123 downloadConnection.setRequestProperty("User-Agent",Version.getInstance().getAgentString());124 downloadConnection.setRequestProperty("Host", url.getHost());125 123 downloadConnection.connect(); 126 124 } -
trunk/src/org/openstreetmap/josm/plugins/ReadRemotePluginInformationTask.java
r5266 r5587 162 162 URL url = new URL(site); 163 163 synchronized(this) { 164 connection = (HttpURLConnection)url.openConnection();164 connection = Utils.openHttpConnection(url); 165 165 connection.setRequestProperty("Cache-Control", "no-cache"); 166 connection.setRequestProperty("User-Agent",Version.getInstance().getAgentString());167 connection.setRequestProperty("Host", url.getHost());168 166 connection.setRequestProperty("Accept-Charset", "utf-8"); 169 167 } … … 211 209 URL url = new URL(site); 212 210 synchronized(this) { 213 connection = (HttpURLConnection)url.openConnection();211 connection = Utils.openHttpConnection(url); 214 212 connection.setRequestProperty("Cache-Control", "no-cache"); 215 connection.setRequestProperty("User-Agent",Version.getInstance().getAgentString());216 connection.setRequestProperty("Host", url.getHost());217 213 } 218 214 in = connection.getInputStream(); -
trunk/src/org/openstreetmap/josm/tools/Utils.java
r5578 r5587 16 16 import java.io.Reader; 17 17 import java.io.UnsupportedEncodingException; 18 import java.net.HttpURLConnection; 19 import java.net.URL; 18 20 import java.security.MessageDigest; 19 21 import java.security.NoSuchAlgorithmException; … … 25 27 import java.util.Iterator; 26 28 import java.util.List; 29 30 import org.openstreetmap.josm.data.Version; 27 31 28 32 /** … … 533 537 return new Color(Integer.parseInt(clr, 16)); 534 538 } 535 539 540 /** 541 * Opens a HTTP connection to the given URL and sets the User-Agent property to JOSM's one. 542 * @param httpURL The HTTP url to open (must use http:// or https://) 543 * @return An open HTTP connection to the given URL 544 * @throws IOException if an I/O exception occurs. 545 * @since 5587 546 */ 547 public static HttpURLConnection openHttpConnection(URL httpURL) throws IOException { 548 if (httpURL == null || !httpURL.getProtocol().matches("https?")) { 549 throw new IllegalArgumentException("Invalid HTTP url"); 550 } 551 HttpURLConnection connection = (HttpURLConnection) httpURL.openConnection(); 552 connection.setRequestProperty("User-Agent", Version.getInstance().getAgentString()); 553 return connection; 554 } 555 556 /** 557 * Opens a HTTP connection to the given URL, sets the User-Agent property to JOSM's one and optionnaly disables Keep-Alive. 558 * @param httpURL The HTTP url to open (must use http:// or https://) 559 * @param keepAlive 560 * @return An open HTTP connection to the given URL 561 * @throws IOException if an I/O exception occurs. 562 * @since 5587 563 */ 564 public static HttpURLConnection openHttpConnection(URL httpURL, boolean keepAlive) throws IOException { 565 HttpURLConnection connection = openHttpConnection(httpURL); 566 if (!keepAlive) { 567 connection.setRequestProperty("Connection", "close"); 568 } 569 return connection; 570 } 536 571 }
Note:
See TracChangeset
for help on using the changeset viewer.