Changeset 9171 in josm
- Timestamp:
- 2015-12-26T23:42:00+01:00 (9 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/actions/downloadtasks/DownloadSessionTask.java
r8921 r9171 13 13 import org.openstreetmap.josm.data.Bounds; 14 14 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 15 import org.openstreetmap.josm.tools. Utils;15 import org.openstreetmap.josm.tools.HttpClient; 16 16 17 17 /** … … 45 45 try { 46 46 URL u = new URL(url); 47 loader = new Loader( Utils.openURL(u), u.toURI(), url.endsWith(".joz"));47 loader = new Loader(HttpClient.create(u).connect().getContent(), u.toURI(), url.endsWith(".joz")); 48 48 return Main.worker.submit(loader); 49 49 } catch (URISyntaxException | IOException e) { -
trunk/src/org/openstreetmap/josm/data/imagery/CachedAttributionBingAerialTileSource.java
r8836 r9171 6 6 import java.net.URL; 7 7 import java.util.List; 8 import java.util.Scanner;9 8 import java.util.concurrent.Callable; 10 9 … … 14 13 import org.openstreetmap.josm.gui.util.GuiHelper; 15 14 import org.openstreetmap.josm.io.CacheCustomContent; 16 import org.openstreetmap.josm.io.UTFInputStreamReader; 17 import org.openstreetmap.josm.tools.Utils; 15 import org.openstreetmap.josm.tools.HttpClient; 18 16 import org.xml.sax.InputSource; 19 17 … … 55 53 protected byte[] updateData() throws IOException { 56 54 URL u = getAttributionUrl(); 57 try (Scanner scanner = new Scanner(UTFInputStreamReader.create(Utils.openURL(u)))) { 58 String r = scanner.useDelimiter("\\A").next(); 59 Main.info("Successfully loaded Bing attribution data."); 60 return r.getBytes("UTF-8"); 61 } 55 final String r = HttpClient.create(u).connect().fetchContent(); 56 Main.info("Successfully loaded Bing attribution data."); 57 return r.getBytes("UTF-8"); 62 58 } 63 59 } -
trunk/src/org/openstreetmap/josm/gui/MainApplication.java
r8870 r9171 65 65 import org.openstreetmap.josm.tools.BugReportExceptionHandler; 66 66 import org.openstreetmap.josm.tools.FontsManager; 67 import org.openstreetmap.josm.tools.HttpClient; 67 68 import org.openstreetmap.josm.tools.I18n; 68 69 import org.openstreetmap.josm.tools.ImageProvider; … … 392 393 for (String i : args.get(Option.LOAD_PREFERENCES)) { 393 394 info("Reading preferences from " + i); 394 try (InputStream is = Utils.openURL(new URL(i))) {395 try (InputStream is = HttpClient.create(new URL(i)).connect().getContent()) { 395 396 config.openAndReadXML(is); 396 397 } catch (Exception ex) { -
trunk/src/org/openstreetmap/josm/gui/dialogs/properties/PropertiesDialog.java
r9149 r9171 16 16 import java.beans.PropertyChangeEvent; 17 17 import java.beans.PropertyChangeListener; 18 import java.net.HttpURLConnection;19 18 import java.net.URI; 20 19 import java.net.URISyntaxException; … … 101 100 import org.openstreetmap.josm.tools.AlphanumComparator; 102 101 import org.openstreetmap.josm.tools.GBC; 102 import org.openstreetmap.josm.tools.HttpClient; 103 103 import org.openstreetmap.josm.tools.ImageProvider; 104 104 import org.openstreetmap.josm.tools.InputMapUtils; … … 1171 1171 try { 1172 1172 // find a page that actually exists in the wiki 1173 Http URLConnectionconn;1173 HttpClient.Response conn; 1174 1174 for (URI u : uris) { 1175 conn = Utils.openHttpConnection(u.toURL()); 1176 conn.setConnectTimeout(Main.pref.getInteger("socket.timeout.connect", 15)*1000); 1175 conn = HttpClient.create(u.toURL(), "HEAD").connect(); 1177 1176 1178 1177 if (conn.getResponseCode() != 200) { 1179 Main.info("{0} does not exist", u);1180 1178 conn.disconnect(); 1181 1179 } else { 1182 intosize = conn.getContentLength();1180 long osize = conn.getContentLength(); 1183 1181 if (osize > -1) { 1184 1182 conn.disconnect(); 1185 1183 1186 conn = Utils.openHttpConnection(new URI(u.toString()1184 final URI newURI = new URI(u.toString() 1187 1185 .replace("=", "%3D") /* do not URLencode whole string! */ 1188 1186 .replaceFirst("/wiki/", "/w/index.php?redirect=no&title=") 1189 ).toURL());1190 conn .setConnectTimeout(Main.pref.getInteger("socket.timeout.connect", 15)*1000);1187 ); 1188 conn = HttpClient.create(newURI.toURL(), "HEAD").connect(); 1191 1189 } 1192 1190 … … 1199 1197 conn.disconnect(); 1200 1198 } else { 1201 Main.info("browsing to {0}", u);1202 1199 conn.disconnect(); 1203 1200 -
trunk/src/org/openstreetmap/josm/gui/download/PlaceSelection.java
r9078 r9171 13 13 import java.awt.event.MouseEvent; 14 14 import java.io.IOException; 15 import java.io.InputStream;16 import java.io.InputStreamReader;17 15 import java.io.Reader; 18 import java.net.HttpURLConnection;19 16 import java.net.URL; 20 import java.nio.charset.StandardCharsets;21 17 import java.text.DecimalFormat; 22 18 import java.util.ArrayList; … … 57 53 import org.openstreetmap.josm.io.OsmTransferException; 58 54 import org.openstreetmap.josm.tools.GBC; 55 import org.openstreetmap.josm.tools.HttpClient; 59 56 import org.openstreetmap.josm.tools.ImageProvider; 60 57 import org.openstreetmap.josm.tools.OsmUrlToBounds; … … 328 325 329 326 private final String searchExpression; 330 private Http URLConnectionconnection;327 private HttpClient.Response connection; 331 328 private List<SearchResult> data; 332 329 private boolean canceled; … … 371 368 URL url = new URL(urlString); 372 369 synchronized (this) { 373 connection = Utils.openHttpConnection(url); 374 } 375 connection.setConnectTimeout(Main.pref.getInteger("socket.timeout.connect", 15)*1000); 376 try ( 377 InputStream inputStream = connection.getInputStream(); 378 Reader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8); 379 ) { 370 connection = HttpClient.create(url).connect(); 371 } 372 try (Reader reader = connection.getContentReader()) { 380 373 InputSource inputSource = new InputSource(reader); 381 374 NameFinderResultParser parser = new NameFinderResultParser(); -
trunk/src/org/openstreetmap/josm/gui/io/DownloadFileTask.java
r9059 r9171 11 11 import java.io.InputStream; 12 12 import java.io.OutputStream; 13 import java.net.HttpURLConnection;14 13 import java.net.MalformedURLException; 15 14 import java.net.URL; … … 22 21 import org.openstreetmap.josm.gui.PleaseWaitDialog; 23 22 import org.openstreetmap.josm.gui.PleaseWaitRunnable; 24 import org.openstreetmap.josm.tools. Utils;23 import org.openstreetmap.josm.tools.HttpClient; 25 24 import org.xml.sax.SAXException; 26 25 … … 68 67 69 68 private boolean canceled; 70 private Http URLConnectiondownloadConnection;69 private HttpClient.Response downloadConnection; 71 70 72 71 private synchronized void closeConnectionIfNeeded() { … … 100 99 101 100 URL url = new URL(address); 102 intsize;101 long size; 103 102 synchronized (this) { 104 downloadConnection = Utils.openHttpConnection(url); 105 downloadConnection.setRequestProperty("Cache-Control", "no-cache"); 106 downloadConnection.connect(); 103 downloadConnection = HttpClient.create(url).useCache(false).connect(); 107 104 size = downloadConnection.getContentLength(); 108 105 } … … 112 109 113 110 try ( 114 InputStream in = downloadConnection.get InputStream();111 InputStream in = downloadConnection.getContent(); 115 112 OutputStream out = new FileOutputStream(file) 116 113 ) { 117 114 byte[] buffer = new byte[32768]; 118 115 int count = 0; 119 intp1 = 0, p2 = 0;116 long p1 = 0, p2 = 0; 120 117 for (int read = in.read(buffer); read != -1; read = in.read(buffer)) { 121 118 out.write(buffer, 0, read); … … 124 121 p2 = 100 * count / size; 125 122 if (p2 != p1) { 126 progressMonitor.setTicks( p2);123 progressMonitor.setTicks((int) p2); 127 124 p1 = p2; 128 125 } -
trunk/src/org/openstreetmap/josm/io/imagery/WMSImagery.java
r9108 r9171 3 3 4 4 import java.awt.HeadlessException; 5 import java.io.BufferedReader;6 5 import java.io.IOException; 7 import java.io.InputStream;8 6 import java.io.StringReader; 9 7 import java.net.MalformedURLException; 10 8 import java.net.URL; 11 import java.net.URLConnection;12 9 import java.util.ArrayList; 13 10 import java.util.Collection; … … 27 24 import org.openstreetmap.josm.data.imagery.ImageryInfo; 28 25 import org.openstreetmap.josm.data.projection.Projections; 29 import org.openstreetmap.josm. io.UTFInputStreamReader;26 import org.openstreetmap.josm.tools.HttpClient; 30 27 import org.openstreetmap.josm.tools.Predicate; 31 28 import org.openstreetmap.josm.tools.Utils; … … 137 134 138 135 Main.info("GET " + getCapabilitiesUrl); 139 URLConnection openConnection = Utils.openHttpConnection(getCapabilitiesUrl, false, true); 140 StringBuilder ba = new StringBuilder(); 141 142 try ( 143 InputStream inputStream = openConnection.getInputStream(); 144 BufferedReader br = new BufferedReader(UTFInputStreamReader.create(inputStream)) 145 ) { 146 String line; 147 while ((line = br.readLine()) != null) { 148 ba.append(line); 149 ba.append('\n'); 150 } 151 } 152 String incomingData = ba.toString(); 136 final String incomingData = HttpClient.create(getCapabilitiesUrl).connect().fetchContent(); 153 137 Main.debug("Server response to Capabilities request:"); 154 138 Main.debug(incomingData); -
trunk/src/org/openstreetmap/josm/plugins/ReadRemotePluginInformationTask.java
r9062 r9171 6 6 import java.awt.Dimension; 7 7 import java.awt.GridBagLayout; 8 import java.io.BufferedReader;9 8 import java.io.ByteArrayInputStream; 10 9 import java.io.File; … … 13 12 import java.io.IOException; 14 13 import java.io.InputStream; 15 import java.io.InputStreamReader;16 14 import java.io.OutputStreamWriter; 17 15 import java.io.PrintWriter; 18 import java.net.HttpURLConnection;19 16 import java.net.MalformedURLException; 20 17 import java.net.URL; … … 42 39 import org.openstreetmap.josm.io.OsmTransferException; 43 40 import org.openstreetmap.josm.tools.GBC; 41 import org.openstreetmap.josm.tools.HttpClient; 44 42 import org.openstreetmap.josm.tools.Utils; 45 43 import org.xml.sax.SAXException; … … 53 51 private Collection<String> sites; 54 52 private boolean canceled; 55 private Http URLConnectionconnection;53 private HttpClient.Response connection; 56 54 private List<PluginInformation> availablePlugins; 57 55 private boolean displayErrMsg; … … 153 151 } 154 152 153 String content = null; 155 154 try { 156 155 monitor.beginTask(""); … … 158 157 159 158 URL url = new URL(site); 160 synchronized (this) { 161 connection = Utils.openHttpConnection(url); 162 connection.setRequestProperty("Cache-Control", "no-cache"); 163 connection.setRequestProperty("Accept-Charset", "utf-8"); 164 } 165 try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) { 166 StringBuilder sb = new StringBuilder(); 167 String line; 168 while ((line = in.readLine()) != null) { 169 sb.append(line).append('\n'); 170 } 171 return sb.toString(); 172 } 159 connection = HttpClient.create(url).useCache(false).connect(); 160 content = connection.fetchContent(); 161 if (connection.getResponseCode() != 200) { 162 throw new IOException(tr("Unsuccessful HTTP request")); 163 } 164 return content; 173 165 } catch (MalformedURLException e) { 174 166 if (canceled) return null; … … 177 169 } catch (IOException e) { 178 170 if (canceled) return null; 179 Main.addNetworkError(site, e); 180 handleIOException(monitor, e, tr("Plugin list download error"), tr("JOSM failed to download plugin list:"), displayErrMsg); 171 handleIOException(monitor, e, content); 181 172 return null; 182 173 } finally { … … 191 182 } 192 183 193 private void handleIOException(final ProgressMonitor monitor, IOException e, final String title, final String firstMessage, 194 boolean displayMsg) { 195 StringBuilder sb = new StringBuilder(); 196 try (InputStream errStream = connection.getErrorStream()) { 197 if (errStream != null) { 198 try (BufferedReader err = new BufferedReader(new InputStreamReader(errStream, StandardCharsets.UTF_8))) { 199 String line; 200 while ((line = err.readLine()) != null) { 201 sb.append(line).append('\n'); 202 } 203 } catch (Exception ex) { 204 Main.error(e); 205 Main.error(ex); 206 } 207 } 208 } catch (IOException ex) { 209 Main.warn(ex); 210 } 184 private void handleIOException(final ProgressMonitor monitor, IOException e, String details) { 211 185 final String msg = e.getMessage(); 212 final String details = sb.toString();213 186 if (details.isEmpty()) { 214 187 Main.error(e.getClass().getSimpleName()+": " + msg); … … 217 190 } 218 191 219 if (display Msg) {220 displayErrorMessage(monitor, msg, details, t itle, firstMessage);192 if (displayErrMsg) { 193 displayErrorMessage(monitor, msg, details, tr("Plugin list download error"), tr("JOSM failed to download plugin list:")); 221 194 } 222 195 } -
trunk/src/org/openstreetmap/josm/tools/HttpClient.java
r9169 r9171 8 8 import java.io.IOException; 9 9 import java.io.InputStream; 10 import java.io.InputStreamReader;11 10 import java.io.OutputStream; 12 11 import java.net.HttpURLConnection; 13 12 import java.net.URL; 14 import java.nio.charset.StandardCharsets;15 13 import java.util.Map; 14 import java.util.Scanner; 16 15 import java.util.concurrent.ConcurrentHashMap; 17 16 import java.util.zip.GZIPInputStream; … … 20 19 import org.openstreetmap.josm.data.Version; 21 20 import org.openstreetmap.josm.io.Compression; 21 import org.openstreetmap.josm.io.UTFInputStreamReader; 22 22 23 23 /** … … 38 38 private final Map<String, String> headers = new ConcurrentHashMap<>(); 39 39 private int maxRedirects = Main.pref.getInteger("socket.maxredirects", 5); 40 private boolean useCache; 41 private boolean keepAlive; 40 42 41 43 private HttpClient(URL url, String requestMethod) { … … 70 72 connection.setIfModifiedSince(ifModifiedSince); 71 73 } 74 connection.setUseCaches(useCache); 75 if (!useCache) { 76 connection.setRequestProperty("Cache-Control", "no-cache"); 77 } 78 if (!keepAlive) { 79 connection.setRequestProperty("Connection", "close"); 80 } 72 81 for (Map.Entry<String, String> header : headers.entrySet()) { 73 82 connection.setRequestProperty(header.getKey(), header.getValue()); … … 78 87 try { 79 88 connection.connect(); 89 Main.info("{0} {1} => {2}", requestMethod, url, connection.getResponseCode()); 80 90 } catch (IOException e) { 81 91 //noinspection ThrowableResultOfMethodCallIgnored … … 157 167 158 168 /** 159 * Returns {@link #getContent()} wrapped in a buffered reader 169 * Returns {@link #getContent()} wrapped in a buffered reader. 170 * 171 * Detects Unicode charset in use utilizing {@link UTFInputStreamReader}. 160 172 */ 161 173 public BufferedReader getContentReader() throws IOException { 162 return new BufferedReader(new InputStreamReader(getContent(), StandardCharsets.UTF_8)); 174 return new BufferedReader( 175 UTFInputStreamReader.create(getContent()) 176 ); 177 } 178 179 /** 180 * Fetches the HTTP response as String. 181 * @return the response 182 * @throws IOException 183 */ 184 public String fetchContent() throws IOException { 185 try (Scanner scanner = new Scanner(getContentReader())) { 186 return scanner.useDelimiter("\\A").next(); 187 } 163 188 } 164 189 … … 187 212 188 213 /** 214 * Returns the {@code Content-Length} header. 215 */ 216 public long getContentLength() { 217 return connection.getContentLengthLong(); 218 } 219 220 /** 189 221 * @see HttpURLConnection#disconnect() 190 222 */ … … 213 245 public static HttpClient create(URL url, String requestMethod) { 214 246 return new HttpClient(url, requestMethod); 247 } 248 249 /** 250 * Sets whether not to set header {@code Cache-Control=no-cache} 251 * 252 * @param useCache whether not to set header {@code Cache-Control=no-cache} 253 * @return {@code this} 254 * @see HttpURLConnection#setUseCaches(boolean) 255 */ 256 public HttpClient useCache(boolean useCache) { 257 this.useCache = useCache; 258 return this; 259 } 260 261 /** 262 * Sets whether not to set header {@code Connection=close} 263 * <p/> 264 * This might fix #7640, see <a href='https://web.archive.org/web/20140118201501/http://www.tikalk.com/java/forums/httpurlconnection-disable-keep-alive'>here</a>. 265 * 266 * @param keepAlive whether not to set header {@code Connection=close} 267 * @return {@code this} 268 */ 269 public HttpClient keepAlive(boolean keepAlive) { 270 this.keepAlive = keepAlive; 271 return this; 215 272 } 216 273
Note:
See TracChangeset
for help on using the changeset viewer.