Changeset 9232 in josm for trunk/src/org
- Timestamp:
- 2016-01-01T12:07:51+01:00 (9 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/cache/JCSCachedTileLoaderJob.java
r9228 r9232 4 4 import java.io.FileNotFoundException; 5 5 import java.io.IOException; 6 import java.net.HttpURLConnection;7 6 import java.net.URL; 8 import java.net.URLConnection;9 7 import java.util.HashSet; 10 8 import java.util.List; 11 9 import java.util.Map; 12 import java.util.Map.Entry;13 10 import java.util.Random; 14 11 import java.util.Set; … … 27 24 import org.openstreetmap.josm.data.cache.ICachedLoaderListener.LoadResult; 28 25 import org.openstreetmap.josm.data.preferences.IntegerProperty; 26 import org.openstreetmap.josm.tools.HttpClient; 29 27 import org.openstreetmap.josm.tools.Utils; 30 28 … … 311 309 } 312 310 313 HttpURLConnection urlConn = getURLConnection(getUrl(), true);311 final HttpClient request = getRequest("GET", true); 314 312 315 313 if (isObjectLoadable() && 316 314 (now - attributes.getLastModification()) <= ABSOLUTE_EXPIRE_TIME_LIMIT) { 317 urlConn.setIfModifiedSince(attributes.getLastModification());315 request.setIfModifiedSince(attributes.getLastModification()); 318 316 } 319 317 if (isObjectLoadable() && attributes.getEtag() != null) { 320 urlConn.addRequestProperty("If-None-Match", attributes.getEtag()); 321 } 322 323 log.log(Level.INFO, "GET {0} -> {1}", new Object[]{getUrl(), urlConn.getResponseCode()}); 324 325 // follow redirects 326 for (int i = 0; i < 5; i++) { 327 if (urlConn.getResponseCode() == 302) { 328 urlConn = getURLConnection(new URL(urlConn.getHeaderField("Location")), true); 329 } else { 330 break; 331 } 332 } 318 request.setHeader("If-None-Match", attributes.getEtag()); 319 } 320 321 final HttpClient.Response urlConn = request.connect(); 322 333 323 if (urlConn.getResponseCode() == 304) { 334 324 // If isModifiedSince or If-None-Match has been set 335 325 // and the server answers with a HTTP 304 = "Not Modified" 336 log.log(Level.FINE, "JCS - If ModifiedSince/Etag test: local version is up to date: {0}", getUrl());326 log.log(Level.FINE, "JCS - If-Modified-Since/ETag test: local version is up to date: {0}", getUrl()); 337 327 return true; 338 } else if (isObjectLoadable() // we have an object in cache, but we haven't received 304 respo sne code328 } else if (isObjectLoadable() // we have an object in cache, but we haven't received 304 response code 339 329 && ( 340 (attributes.getEtag() != null && attributes.getEtag().equals(urlConn.get RequestProperty("ETag"))) ||330 (attributes.getEtag() != null && attributes.getEtag().equals(urlConn.getHeaderField("ETag"))) || 341 331 attributes.getLastModification() == urlConn.getLastModified()) 342 332 ) { … … 344 334 // for further requests - use HEAD 345 335 String serverKey = getServerKey(); 346 log.log(Level.INFO, "JCS - Host: {0} found not to return 304 codes for If-Modif ed-Since or If-None-Match headers",336 log.log(Level.INFO, "JCS - Host: {0} found not to return 304 codes for If-Modified-Since or If-None-Match headers", 347 337 serverKey); 348 338 useHead.put(serverKey, Boolean.TRUE); … … 361 351 byte[] raw; 362 352 if (urlConn.getResponseCode() == 200) { 363 raw = Utils.readBytesFromStream(urlConn.get InputStream());353 raw = Utils.readBytesFromStream(urlConn.getContent()); 364 354 } else { 365 355 raw = new byte[]{}; … … 434 424 protected abstract V createCacheEntry(byte[] content); 435 425 436 protected CacheEntryAttributes parseHeaders( URLConnectionurlConn) {426 protected CacheEntryAttributes parseHeaders(HttpClient.Response urlConn) { 437 427 CacheEntryAttributes ret = new CacheEntryAttributes(); 438 428 … … 461 451 ret.setEtag(urlConn.getHeaderField("ETag")); 462 452 463 if (Main.isDebugEnabled()) {464 for (Entry<String, List<String>> header: urlConn.getHeaderFields().entrySet()) {465 log.log(Level.FINE, "Response header - {0}: {1}", new Object[]{header.getKey(), header.getValue()});466 }467 }468 469 453 return ret; 470 454 } 471 455 472 private Http URLConnection getURLConnection(URL url, boolean noCache) throws IOException {473 HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();474 urlConn.set RequestProperty("Accept","text/html, image/png, image/jpeg, image/gif, */*");456 private HttpClient getRequest(String requestMethod, boolean noCache) throws IOException { 457 final HttpClient urlConn = HttpClient.create(getUrl(), requestMethod); 458 urlConn.setAccept("text/html, image/png, image/jpeg, image/gif, */*"); 475 459 urlConn.setReadTimeout(readTimeout); // 30 seconds read timeout 476 460 urlConn.setConnectTimeout(connectTimeout); 477 461 if (headers != null) { 478 for (Map.Entry<String, String> e: headers.entrySet()) { 479 urlConn.setRequestProperty(e.getKey(), e.getValue()); 480 } 462 urlConn.setHeaders(headers); 481 463 } 482 464 483 465 if (force || noCache) { 484 urlConn. setUseCaches(false);466 urlConn.useCache(false); 485 467 } 486 468 return urlConn; … … 488 470 489 471 private boolean isCacheValidUsingHead() throws IOException { 490 HttpURLConnection urlConn = getURLConnection(getUrl(), false); 491 urlConn.setRequestMethod("HEAD"); 492 for (int i = 0; i < 5; i++) { 493 if (urlConn.getResponseCode() == 302) { 494 urlConn = getURLConnection(new URL(urlConn.getHeaderField("Location")), false); 495 } else { 496 break; 497 } 498 } 472 final HttpClient.Response urlConn = getRequest("HEAD", false).connect(); 499 473 long lastModified = urlConn.getLastModified(); 500 return (attributes.getEtag() != null && attributes.getEtag().equals(urlConn.get RequestProperty("ETag"))) ||474 return (attributes.getEtag() != null && attributes.getEtag().equals(urlConn.getHeaderField("ETag"))) || 501 475 (lastModified != 0 && lastModified <= attributes.getLastModification()); 502 476 } -
trunk/src/org/openstreetmap/josm/data/imagery/TMSCachedTileLoaderJob.java
r9067 r9232 7 7 import java.io.IOException; 8 8 import java.net.URL; 9 import java.net.URLConnection;10 9 import java.util.HashSet; 11 10 import java.util.List; … … 31 30 import org.openstreetmap.josm.data.cache.ICachedLoaderListener; 32 31 import org.openstreetmap.josm.data.cache.JCSCachedTileLoaderJob; 32 import org.openstreetmap.josm.tools.HttpClient; 33 33 34 34 /** … … 240 240 241 241 @Override 242 protected CacheEntryAttributes parseHeaders( URLConnectionurlConn) {242 protected CacheEntryAttributes parseHeaders(HttpClient.Response urlConn) { 243 243 CacheEntryAttributes ret = super.parseHeaders(urlConn); 244 244 // keep the expiration time between MINIMUM_EXPIRES and MAXIMUM_EXPIRES, so we will cache the tiles -
trunk/src/org/openstreetmap/josm/gui/oauth/OsmOAuthAuthorizationClient.java
r9227 r9232 200 200 201 201 protected SessionId extractOsmSession() { 202 List<String> setCookies = connection.getHeaderFields( "Set-Cookie");202 List<String> setCookies = connection.getHeaderFields().get("Set-Cookie"); 203 203 if (setCookies == null) 204 204 // no cookies set -
trunk/src/org/openstreetmap/josm/tools/HttpClient.java
r9230 r9232 310 310 * Returns the {@code Content-Encoding} header. 311 311 * @return {@code Content-Encoding} HTTP header 312 * @see HttpURLConnection#getContentEncoding() 312 313 */ 313 314 public String getContentEncoding() { … … 324 325 325 326 /** 327 * Returns the {@code Expire} header. 328 * @return {@code Expire} HTTP header 329 * @see HttpURLConnection#getExpiration() 330 * @since 331 */ 332 public long getExpiration() { 333 return connection.getExpiration(); 334 } 335 336 /** 337 * Returns the {@code Last-Modified} header. 338 * @return {@code Last-Modified} HTTP header 339 * @see HttpURLConnection#getLastModified() 340 * @since 9232 341 */ 342 public long getLastModified() { 343 return connection.getLastModified(); 344 } 345 346 /** 326 347 * Returns the {@code Content-Length} header. 327 348 * @return {@code Content-Length} HTTP header 349 * @see HttpURLConnection#getContentLengthLong() 328 350 */ 329 351 public long getContentLength() { … … 343 365 344 366 /** 345 * Returns the list of Strings that represents the named header field values. 346 * @param name the name of a header field 347 * @return unmodifiable List of Strings that represents the corresponding field values 367 * Returns an unmodifiable Map mapping header keys to a List of header values. 368 * @return unmodifiable Map mapping header keys to a List of header values 348 369 * @see HttpURLConnection#getHeaderFields() 349 * @since 9 172350 */ 351 public List<String> getHeaderFields(String name) {352 return connection.getHeaderFields() .get(name);370 * @since 9232 371 */ 372 public Map<String, List<String>> getHeaderFields() { 373 return connection.getHeaderFields(); 353 374 } 354 375
Note:
See TracChangeset
for help on using the changeset viewer.