Ticket #11548: HttpURL.2.patch
File HttpURL.2.patch, 3.7 KB (added by , 9 years ago) |
---|
-
src/org/openstreetmap/josm/data/cache/JCSCachedTileLoaderJob.java
318 318 return true; 319 319 } 320 320 321 HttpURLConnection urlConn = getURLConnection();321 URLConnection urlConn = getURLConnection(); 322 322 323 323 if (isObjectLoadable() && 324 324 (now - attributes.getLastModification()) <= ABSOLUTE_EXPIRE_TIME_LIMIT) { … … 327 327 if (isObjectLoadable() && attributes.getEtag() != null) { 328 328 urlConn.addRequestProperty("If-None-Match", attributes.getEtag()); 329 329 } 330 if ( urlConn.getResponseCode() == 304) {330 if (responseCode(urlConn) == 304) { 331 331 // If isModifiedSince or If-None-Match has been set 332 332 // and the server answers with a HTTP 304 = "Not Modified" 333 333 log.log(Level.FINE, "JCS - IfModifiedSince/Etag test: local version is up to date: {0}", getUrl()); … … 348 348 attributes = parseHeaders(urlConn); 349 349 350 350 for (int i = 0; i < 5; ++i) { 351 if ( urlConn.getResponseCode() == 503) {351 if (responseCode(urlConn) == 503) { 352 352 Thread.sleep(5000+(new Random()).nextInt(5000)); 353 353 continue; 354 354 } 355 355 356 attributes.setResponseCode( urlConn.getResponseCode());356 attributes.setResponseCode(responseCode(urlConn)); 357 357 byte[] raw = read(urlConn); 358 358 359 if (isResponseLoadable(urlConn.getHeaderFields(), urlConn.getResponseCode(), raw)) {359 if (isResponseLoadable(urlConn.getHeaderFields(), responseCode(urlConn), raw)) { 360 360 // we need to check cacheEmpty, so for cases, when data is returned, but we want to store 361 361 // as empty (eg. empty tile images) to save some space 362 362 cacheData = createCacheEntry(raw); … … 447 447 return ret; 448 448 } 449 449 450 private HttpURLConnection getURLConnection() throws IOException {451 HttpURLConnection urlConn = (HttpURLConnection)getUrl().openConnection();450 private URLConnection getURLConnection() throws IOException { 451 URLConnection urlConn = getUrl().openConnection(); 452 452 urlConn.setRequestProperty("Accept", "text/html, image/png, image/jpeg, image/gif, */*"); 453 453 urlConn.setReadTimeout(readTimeout); // 30 seconds read timeout 454 454 urlConn.setConnectTimeout(connectTimeout); … … 462 462 } 463 463 464 464 private boolean isCacheValidUsingHead() throws IOException { 465 HttpURLConnection urlConn = (HttpURLConnection) getUrl().openConnection(); 466 urlConn.setRequestMethod("HEAD"); 465 URLConnection urlConn = getUrl().openConnection(); 466 if (urlConn instanceof HttpURLConnection) { 467 ((HttpURLConnection)urlConn).setRequestMethod("HEAD"); 468 } 467 469 long lastModified = urlConn.getLastModified(); 468 470 return (attributes.getEtag() != null && attributes.getEtag().equals(urlConn.getRequestProperty("ETag"))) || 469 471 (lastModified != 0 && lastModified <= attributes.getLastModification()); … … 521 523 public void handleJobCancellation() { 522 524 finishLoading(LoadResult.CANCELED); 523 525 } 526 527 private int responseCode(URLConnection urlConn) throws IOException { 528 if (urlConn instanceof HttpURLConnection) { 529 return ((HttpURLConnection) urlConn).getResponseCode(); 530 } else { 531 return 500; 532 } 533 } 524 534 }