001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.data.cache; 003 004import java.io.FileNotFoundException; 005import java.io.IOException; 006import java.net.HttpURLConnection; 007import java.net.URL; 008import java.security.SecureRandom; 009import java.util.HashSet; 010import java.util.List; 011import java.util.Map; 012import java.util.Set; 013import java.util.concurrent.ConcurrentHashMap; 014import java.util.concurrent.ConcurrentMap; 015import java.util.concurrent.LinkedBlockingDeque; 016import java.util.concurrent.ThreadPoolExecutor; 017import java.util.concurrent.TimeUnit; 018import java.util.logging.Level; 019import java.util.logging.Logger; 020 021import org.apache.commons.jcs.access.behavior.ICacheAccess; 022import org.apache.commons.jcs.engine.behavior.ICacheElement; 023import org.openstreetmap.gui.jmapviewer.FeatureAdapter; 024import org.openstreetmap.josm.Main; 025import org.openstreetmap.josm.data.cache.ICachedLoaderListener.LoadResult; 026import org.openstreetmap.josm.data.preferences.IntegerProperty; 027import org.openstreetmap.josm.tools.CheckParameterUtil; 028import org.openstreetmap.josm.tools.HttpClient; 029import org.openstreetmap.josm.tools.Utils; 030 031/** 032 * Generic loader for HTTP based tiles. Uses custom attribute, to check, if entry has expired 033 * according to HTTP headers sent with tile. If so, it tries to verify using Etags 034 * or If-Modified-Since / Last-Modified. 035 * 036 * If the tile is not valid, it will try to download it from remote service and put it 037 * to cache. If remote server will fail it will try to use stale entry. 038 * 039 * This class will keep only one Job running for specified tile. All others will just finish, but 040 * listeners will be gathered and notified, once download job will be finished 041 * 042 * @author Wiktor Niesiobędzki 043 * @param <K> cache entry key type 044 * @param <V> cache value type 045 * @since 8168 046 */ 047public abstract class JCSCachedTileLoaderJob<K, V extends CacheEntry> implements ICachedLoaderJob<K> { 048 private static final Logger LOG = FeatureAdapter.getLogger(JCSCachedTileLoaderJob.class.getCanonicalName()); 049 protected static final long DEFAULT_EXPIRE_TIME = TimeUnit.DAYS.toMillis(7); 050 // Limit for the max-age value send by the server. 051 protected static final long EXPIRE_TIME_SERVER_LIMIT = TimeUnit.DAYS.toMillis(28); 052 // Absolute expire time limit. Cached tiles that are older will not be used, 053 // even if the refresh from the server fails. 054 protected static final long ABSOLUTE_EXPIRE_TIME_LIMIT = TimeUnit.DAYS.toMillis(365); 055 056 /** 057 * maximum download threads that will be started 058 */ 059 public static final IntegerProperty THREAD_LIMIT = new IntegerProperty("cache.jcs.max_threads", 10); 060 061 /* 062 * ThreadPoolExecutor starts new threads, until THREAD_LIMIT is reached. Then it puts tasks into LinkedBlockingDeque. 063 * 064 * The queue works FIFO, so one needs to take care about ordering of the entries submitted 065 * 066 * There is no point in canceling tasks, that are already taken by worker threads (if we made so much effort, we can at least cache 067 * the response, so later it could be used). We could actually cancel what is in LIFOQueue, but this is a tradeoff between simplicity 068 * and performance (we do want to have something to offer to worker threads before tasks will be resubmitted by class consumer) 069 */ 070 071 private static final ThreadPoolExecutor DEFAULT_DOWNLOAD_JOB_DISPATCHER = new ThreadPoolExecutor( 072 1, // we have a small queue, so threads will be quickly started (threads are started only, when queue is full) 073 THREAD_LIMIT.get(), // do not this number of threads 074 30, // keepalive for thread 075 TimeUnit.SECONDS, 076 // make queue of LIFO type - so recently requested tiles will be loaded first (assuming that these are which user is waiting to see) 077 new LinkedBlockingDeque<Runnable>(), 078 Utils.newThreadFactory("JCS-downloader-%d", Thread.NORM_PRIORITY) 079 ); 080 081 082 083 private static final ConcurrentMap<String, Set<ICachedLoaderListener>> inProgress = new ConcurrentHashMap<>(); 084 private static final ConcurrentMap<String, Boolean> useHead = new ConcurrentHashMap<>(); 085 086 protected final long now; // when the job started 087 088 private final ICacheAccess<K, V> cache; 089 private ICacheElement<K, V> cacheElement; 090 protected V cacheData; 091 protected CacheEntryAttributes attributes; 092 093 // HTTP connection parameters 094 private final int connectTimeout; 095 private final int readTimeout; 096 private final Map<String, String> headers; 097 private final ThreadPoolExecutor downloadJobExecutor; 098 private Runnable finishTask; 099 private boolean force; 100 101 /** 102 * @param cache cache instance that we will work on 103 * @param headers HTTP headers to be sent together with request 104 * @param readTimeout when connecting to remote resource 105 * @param connectTimeout when connecting to remote resource 106 * @param downloadJobExecutor that will be executing the jobs 107 */ 108 public JCSCachedTileLoaderJob(ICacheAccess<K, V> cache, 109 int connectTimeout, int readTimeout, 110 Map<String, String> headers, 111 ThreadPoolExecutor downloadJobExecutor) { 112 CheckParameterUtil.ensureParameterNotNull(cache, "cache"); 113 this.cache = cache; 114 this.now = System.currentTimeMillis(); 115 this.connectTimeout = connectTimeout; 116 this.readTimeout = readTimeout; 117 this.headers = headers; 118 this.downloadJobExecutor = downloadJobExecutor; 119 } 120 121 /** 122 * @param cache cache instance that we will work on 123 * @param headers HTTP headers to be sent together with request 124 * @param readTimeout when connecting to remote resource 125 * @param connectTimeout when connecting to remote resource 126 */ 127 public JCSCachedTileLoaderJob(ICacheAccess<K, V> cache, 128 int connectTimeout, int readTimeout, 129 Map<String, String> headers) { 130 this(cache, connectTimeout, readTimeout, 131 headers, DEFAULT_DOWNLOAD_JOB_DISPATCHER); 132 } 133 134 private void ensureCacheElement() { 135 if (cacheElement == null && getCacheKey() != null) { 136 cacheElement = cache.getCacheElement(getCacheKey()); 137 if (cacheElement != null) { 138 attributes = (CacheEntryAttributes) cacheElement.getElementAttributes(); 139 cacheData = cacheElement.getVal(); 140 } 141 } 142 } 143 144 @Override 145 public V get() { 146 ensureCacheElement(); 147 return cacheData; 148 } 149 150 @Override 151 public void submit(ICachedLoaderListener listener, boolean force) throws IOException { 152 this.force = force; 153 boolean first = false; 154 URL url = getUrl(); 155 String deduplicationKey = null; 156 if (url != null) { 157 // url might be null, for example when Bing Attribution is not loaded yet 158 deduplicationKey = url.toString(); 159 } 160 if (deduplicationKey == null) { 161 LOG.log(Level.WARNING, "No url returned for: {0}, skipping", getCacheKey()); 162 throw new IllegalArgumentException("No url returned"); 163 } 164 synchronized (inProgress) { 165 Set<ICachedLoaderListener> newListeners = inProgress.get(deduplicationKey); 166 if (newListeners == null) { 167 newListeners = new HashSet<>(); 168 inProgress.put(deduplicationKey, newListeners); 169 first = true; 170 } 171 newListeners.add(listener); 172 } 173 174 if (first || force) { 175 // submit all jobs to separate thread, so calling thread is not blocked with IO when loading from disk 176 LOG.log(Level.FINE, "JCS - Submitting job for execution for url: {0}", getUrlNoException()); 177 downloadJobExecutor.execute(this); 178 } 179 } 180 181 /** 182 * This method is run when job has finished 183 */ 184 protected void executionFinished() { 185 if (finishTask != null) { 186 finishTask.run(); 187 } 188 } 189 190 /** 191 * 192 * @return checks if object from cache has sufficient data to be returned 193 */ 194 protected boolean isObjectLoadable() { 195 if (cacheData == null) { 196 return false; 197 } 198 return cacheData.getContent().length > 0; 199 } 200 201 /** 202 * Simple implementation. All errors should be cached as empty. Though some JDK (JDK8 on Windows for example) 203 * doesn't return 4xx error codes, instead they do throw an FileNotFoundException or IOException 204 * 205 * @return true if we should put empty object into cache, regardless of what remote resource has returned 206 */ 207 protected boolean cacheAsEmpty() { 208 return attributes.getResponseCode() < 500; 209 } 210 211 /** 212 * @return key under which discovered server settings will be kept 213 */ 214 protected String getServerKey() { 215 try { 216 return getUrl().getHost(); 217 } catch (IOException e) { 218 Main.trace(e); 219 return null; 220 } 221 } 222 223 @Override 224 public void run() { 225 final Thread currentThread = Thread.currentThread(); 226 final String oldName = currentThread.getName(); 227 currentThread.setName("JCS Downloading: " + getUrlNoException()); 228 LOG.log(Level.FINE, "JCS - starting fetch of url: {0} ", getUrlNoException()); 229 ensureCacheElement(); 230 try { 231 // try to fetch from cache 232 if (!force && cacheElement != null && isCacheElementValid() && isObjectLoadable()) { 233 // we got something in cache, and it's valid, so lets return it 234 LOG.log(Level.FINE, "JCS - Returning object from cache: {0}", getCacheKey()); 235 finishLoading(LoadResult.SUCCESS); 236 return; 237 } 238 239 // try to load object from remote resource 240 if (loadObject()) { 241 finishLoading(LoadResult.SUCCESS); 242 } else { 243 // if loading failed - check if we can return stale entry 244 if (isObjectLoadable()) { 245 // try to get stale entry in cache 246 finishLoading(LoadResult.SUCCESS); 247 LOG.log(Level.FINE, "JCS - found stale object in cache: {0}", getUrlNoException()); 248 } else { 249 // failed completely 250 finishLoading(LoadResult.FAILURE); 251 } 252 } 253 } finally { 254 executionFinished(); 255 currentThread.setName(oldName); 256 } 257 } 258 259 private void finishLoading(LoadResult result) { 260 Set<ICachedLoaderListener> listeners; 261 synchronized (inProgress) { 262 try { 263 listeners = inProgress.remove(getUrl().toString()); 264 } catch (IOException e) { 265 listeners = null; 266 Main.trace(e); 267 } 268 } 269 if (listeners == null) { 270 LOG.log(Level.WARNING, "Listener not found for URL: {0}. Listener not notified!", getUrlNoException()); 271 return; 272 } 273 for (ICachedLoaderListener l: listeners) { 274 l.loadingFinished(cacheData, attributes, result); 275 } 276 } 277 278 protected boolean isCacheElementValid() { 279 long expires = attributes.getExpirationTime(); 280 281 // check by expire date set by server 282 if (expires != 0L) { 283 // put a limit to the expire time (some servers send a value 284 // that is too large) 285 expires = Math.min(expires, attributes.getCreateTime() + EXPIRE_TIME_SERVER_LIMIT); 286 if (now > expires) { 287 LOG.log(Level.FINE, "JCS - Object {0} has expired -> valid to {1}, now is: {2}", 288 new Object[]{getUrlNoException(), Long.toString(expires), Long.toString(now)}); 289 return false; 290 } 291 } else if (attributes.getLastModification() > 0 && 292 now - attributes.getLastModification() > DEFAULT_EXPIRE_TIME) { 293 // check by file modification date 294 LOG.log(Level.FINE, "JCS - Object has expired, maximum file age reached {0}", getUrlNoException()); 295 return false; 296 } else if (now - attributes.getCreateTime() > DEFAULT_EXPIRE_TIME) { 297 LOG.log(Level.FINE, "JCS - Object has expired, maximum time since object creation reached {0}", getUrlNoException()); 298 return false; 299 } 300 return true; 301 } 302 303 /** 304 * @return true if object was successfully downloaded, false, if there was a loading failure 305 */ 306 private boolean loadObject() { 307 if (attributes == null) { 308 attributes = new CacheEntryAttributes(); 309 } 310 try { 311 // if we have object in cache, and host doesn't support If-Modified-Since nor If-None-Match 312 // then just use HEAD request and check returned values 313 if (isObjectLoadable() && 314 Boolean.TRUE.equals(useHead.get(getServerKey())) && 315 isCacheValidUsingHead()) { 316 LOG.log(Level.FINE, "JCS - cache entry verified using HEAD request: {0}", getUrl()); 317 return true; 318 } 319 320 final HttpClient request = getRequest("GET", true); 321 322 if (isObjectLoadable() && 323 (now - attributes.getLastModification()) <= ABSOLUTE_EXPIRE_TIME_LIMIT) { 324 request.setIfModifiedSince(attributes.getLastModification()); 325 } 326 if (isObjectLoadable() && attributes.getEtag() != null) { 327 request.setHeader("If-None-Match", attributes.getEtag()); 328 } 329 330 final HttpClient.Response urlConn = request.connect(); 331 332 if (urlConn.getResponseCode() == 304) { 333 // If isModifiedSince or If-None-Match has been set 334 // and the server answers with a HTTP 304 = "Not Modified" 335 LOG.log(Level.FINE, "JCS - If-Modified-Since/ETag test: local version is up to date: {0}", getUrl()); 336 return true; 337 } else if (isObjectLoadable() // we have an object in cache, but we haven't received 304 response code 338 && ( 339 (attributes.getEtag() != null && attributes.getEtag().equals(urlConn.getHeaderField("ETag"))) || 340 attributes.getLastModification() == urlConn.getLastModified()) 341 ) { 342 // we sent ETag or If-Modified-Since, but didn't get 304 response code 343 // for further requests - use HEAD 344 String serverKey = getServerKey(); 345 LOG.log(Level.INFO, "JCS - Host: {0} found not to return 304 codes for If-Modified-Since or If-None-Match headers", 346 serverKey); 347 useHead.put(serverKey, Boolean.TRUE); 348 } 349 350 attributes = parseHeaders(urlConn); 351 352 for (int i = 0; i < 5; ++i) { 353 if (urlConn.getResponseCode() == HttpURLConnection.HTTP_UNAVAILABLE) { 354 Thread.sleep(5000L+new SecureRandom().nextInt(5000)); 355 continue; 356 } 357 358 attributes.setResponseCode(urlConn.getResponseCode()); 359 byte[] raw; 360 if (urlConn.getResponseCode() == HttpURLConnection.HTTP_OK) { 361 raw = Utils.readBytesFromStream(urlConn.getContent()); 362 } else { 363 raw = new byte[]{}; 364 } 365 366 if (isResponseLoadable(urlConn.getHeaderFields(), urlConn.getResponseCode(), raw)) { 367 // we need to check cacheEmpty, so for cases, when data is returned, but we want to store 368 // as empty (eg. empty tile images) to save some space 369 cacheData = createCacheEntry(raw); 370 cache.put(getCacheKey(), cacheData, attributes); 371 LOG.log(Level.FINE, "JCS - downloaded key: {0}, length: {1}, url: {2}", 372 new Object[] {getCacheKey(), raw.length, getUrl()}); 373 return true; 374 } else if (cacheAsEmpty()) { 375 cacheData = createCacheEntry(new byte[]{}); 376 cache.put(getCacheKey(), cacheData, attributes); 377 LOG.log(Level.FINE, "JCS - Caching empty object {0}", getUrl()); 378 return true; 379 } else { 380 LOG.log(Level.FINE, "JCS - failure during load - reponse is not loadable nor cached as empty"); 381 return false; 382 } 383 } 384 } catch (FileNotFoundException e) { 385 LOG.log(Level.FINE, "JCS - Caching empty object as server returned 404 for: {0}", getUrlNoException()); 386 attributes.setResponseCode(404); 387 attributes.setError(e); 388 boolean doCache = isResponseLoadable(null, 404, null) || cacheAsEmpty(); 389 if (doCache) { 390 cacheData = createCacheEntry(new byte[]{}); 391 cache.put(getCacheKey(), cacheData, attributes); 392 } 393 return doCache; 394 } catch (IOException e) { 395 LOG.log(Level.FINE, "JCS - IOExecption during communication with server for: {0}", getUrlNoException()); 396 if (isObjectLoadable()) { 397 return true; 398 } else { 399 attributes.setError(e); 400 attributes.setResponseCode(599); // set dummy error code, greater than 500 so it will be not cached 401 return false; 402 } 403 404 } catch (InterruptedException e) { 405 attributes.setError(e); 406 LOG.log(Level.WARNING, "JCS - Exception during download {0}", getUrlNoException()); 407 Main.warn(e); 408 } 409 LOG.log(Level.WARNING, "JCS - Silent failure during download: {0}", getUrlNoException()); 410 return false; 411 } 412 413 /** 414 * Check if the object is loadable. This means, if the data will be parsed, and if this response 415 * will finish as successful retrieve. 416 * 417 * This simple implementation doesn't load empty response, nor client (4xx) and server (5xx) errors 418 * 419 * @param headerFields headers sent by server 420 * @param responseCode http status code 421 * @param raw data read from server 422 * @return true if object should be cached and returned to listener 423 */ 424 protected boolean isResponseLoadable(Map<String, List<String>> headerFields, int responseCode, byte[] raw) { 425 if (raw == null || raw.length == 0 || responseCode >= 400) { 426 return false; 427 } 428 return true; 429 } 430 431 protected abstract V createCacheEntry(byte[] content); 432 433 protected CacheEntryAttributes parseHeaders(HttpClient.Response urlConn) { 434 CacheEntryAttributes ret = new CacheEntryAttributes(); 435 436 Long lng = urlConn.getExpiration(); 437 if (lng.equals(0L)) { 438 try { 439 String str = urlConn.getHeaderField("Cache-Control"); 440 if (str != null) { 441 for (String token: str.split(",")) { 442 if (token.startsWith("max-age=")) { 443 lng = TimeUnit.SECONDS.toMillis(Long.parseLong(token.substring(8))) + System.currentTimeMillis(); 444 } 445 } 446 } 447 } catch (NumberFormatException e) { 448 // ignore malformed Cache-Control headers 449 Main.trace(e); 450 } 451 } 452 453 ret.setExpirationTime(lng); 454 ret.setLastModification(now); 455 ret.setEtag(urlConn.getHeaderField("ETag")); 456 457 return ret; 458 } 459 460 private HttpClient getRequest(String requestMethod, boolean noCache) throws IOException { 461 final HttpClient urlConn = HttpClient.create(getUrl(), requestMethod); 462 urlConn.setAccept("text/html, image/png, image/jpeg, image/gif, */*"); 463 urlConn.setReadTimeout(readTimeout); // 30 seconds read timeout 464 urlConn.setConnectTimeout(connectTimeout); 465 if (headers != null) { 466 urlConn.setHeaders(headers); 467 } 468 469 if (force || noCache) { 470 urlConn.useCache(false); 471 } 472 return urlConn; 473 } 474 475 private boolean isCacheValidUsingHead() throws IOException { 476 final HttpClient.Response urlConn = getRequest("HEAD", false).connect(); 477 long lastModified = urlConn.getLastModified(); 478 return (attributes.getEtag() != null && attributes.getEtag().equals(urlConn.getHeaderField("ETag"))) || 479 (lastModified != 0 && lastModified <= attributes.getLastModification()); 480 } 481 482 /** 483 * TODO: move to JobFactory 484 * cancels all outstanding tasks in the queue. 485 */ 486 public void cancelOutstandingTasks() { 487 for (Runnable r: downloadJobExecutor.getQueue()) { 488 if (downloadJobExecutor.remove(r) && r instanceof JCSCachedTileLoaderJob) { 489 ((JCSCachedTileLoaderJob<?, ?>) r).handleJobCancellation(); 490 } 491 } 492 } 493 494 /** 495 * Sets a job, that will be run, when job will finish execution 496 * @param runnable that will be executed 497 */ 498 public void setFinishedTask(Runnable runnable) { 499 this.finishTask = runnable; 500 501 } 502 503 /** 504 * Marks this job as canceled 505 */ 506 public void handleJobCancellation() { 507 finishLoading(LoadResult.CANCELED); 508 } 509 510 private URL getUrlNoException() { 511 try { 512 return getUrl(); 513 } catch (IOException e) { 514 return null; 515 } 516 } 517}