Ignore:
Timestamp:
2018-05-12T14:18:57+02:00 (23 months ago)
Author:
wiktorn
Message:

Imagery definition refactor

Extend imagery definitions by:

  • allowing setting default layers for WMS_ENDPOINT and WMTS
  • allowing setting minimum expires time for tile for this imagery
  • allowing setting custom headers that will be sent for all requests

(get map, get capabilities) for this imagery

Additional changes in code:

  • use TileJobOptions to pass miscellaneous options to loaders
  • refactor WMSImagery to use SAX parser

See: #15981, #7953, #16224, #15940, #16249

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/cache/JCSCachedTileLoaderJob.java

    r13358 r13733  
    2121import org.apache.commons.jcs.engine.behavior.ICacheElement;
    2222import org.openstreetmap.josm.data.cache.ICachedLoaderListener.LoadResult;
     23import org.openstreetmap.josm.data.imagery.TileJobOptions;
    2324import org.openstreetmap.josm.data.preferences.IntegerProperty;
    2425import org.openstreetmap.josm.tools.CheckParameterUtil;
     
    9394    private Runnable finishTask;
    9495    private boolean force;
     96    private long minimumExpiryTime;
    9597
    9698    /**
    9799     * @param cache cache instance that we will work on
    98      * @param headers HTTP headers to be sent together with request
    99      * @param readTimeout when connecting to remote resource
    100      * @param connectTimeout when connecting to remote resource
     100     * @param options options of the request
    101101     * @param downloadJobExecutor that will be executing the jobs
    102102     */
    103103    public JCSCachedTileLoaderJob(ICacheAccess<K, V> cache,
    104             int connectTimeout, int readTimeout,
    105             Map<String, String> headers,
     104            TileJobOptions options,
    106105            ThreadPoolExecutor downloadJobExecutor) {
    107106        CheckParameterUtil.ensureParameterNotNull(cache, "cache");
    108107        this.cache = cache;
    109108        this.now = System.currentTimeMillis();
    110         this.connectTimeout = connectTimeout;
    111         this.readTimeout = readTimeout;
    112         this.headers = headers;
     109        this.connectTimeout = options.getConnectionTimeout();
     110        this.readTimeout = options.getReadTimeout();
     111        this.headers = options.getHeaders();
    113112        this.downloadJobExecutor = downloadJobExecutor;
     113        this.minimumExpiryTime = TimeUnit.SECONDS.toMillis(options.getMinimumExpiryTime());
    114114    }
    115115
    116116    /**
    117117     * @param cache cache instance that we will work on
    118      * @param headers HTTP headers to be sent together with request
    119      * @param readTimeout when connecting to remote resource
    120      * @param connectTimeout when connecting to remote resource
     118     * @param options of the request
    121119     */
    122120    public JCSCachedTileLoaderJob(ICacheAccess<K, V> cache,
    123             int connectTimeout, int readTimeout,
    124             Map<String, String> headers) {
    125         this(cache, connectTimeout, readTimeout,
    126                 headers, DEFAULT_DOWNLOAD_JOB_DISPATCHER);
     121            TileJobOptions options) {
     122        this(cache, options, DEFAULT_DOWNLOAD_JOB_DISPATCHER);
    127123    }
    128124
     
    278274            // put a limit to the expire time (some servers send a value
    279275            // that is too large)
    280             expires = Math.min(expires, attributes.getCreateTime() + EXPIRE_TIME_SERVER_LIMIT);
     276            expires = Math.min(expires, attributes.getCreateTime() + Math.max(EXPIRE_TIME_SERVER_LIMIT, minimumExpiryTime));
    281277            if (now > expires) {
    282278                Logging.debug("JCS - Object {0} has expired -> valid to {1}, now is: {2}",
     
    285281            }
    286282        } else if (attributes.getLastModification() > 0 &&
    287                 now - attributes.getLastModification() > DEFAULT_EXPIRE_TIME) {
     283                now - attributes.getLastModification() > Math.max(DEFAULT_EXPIRE_TIME, minimumExpiryTime)) {
    288284            // check by file modification date
    289285            Logging.debug("JCS - Object has expired, maximum file age reached {0}", getUrlNoException());
    290286            return false;
    291         } else if (now - attributes.getCreateTime() > DEFAULT_EXPIRE_TIME) {
     287        } else if (now - attributes.getCreateTime() > Math.max(DEFAULT_EXPIRE_TIME, minimumExpiryTime)) {
    292288            Logging.debug("JCS - Object has expired, maximum time since object creation reached {0}", getUrlNoException());
    293289            return false;
     
    330326                // and the server answers with a HTTP 304 = "Not Modified"
    331327                Logging.debug("JCS - If-Modified-Since/ETag test: local version is up to date: {0}", getUrl());
     328                // update cache attributes
     329                attributes = parseHeaders(urlConn);
     330                cache.put(getCacheKey(), cacheData, attributes);
    332331                return true;
    333332            } else if (isObjectLoadable() // we have an object in cache, but we haven't received 304 response code
     
    453452                Logging.trace(e);
    454453            }
    455         }
    456 
    457         ret.setExpirationTime(lng);
     454            if (lng.equals(0L)) {
     455                lng = System.currentTimeMillis() + DEFAULT_EXPIRE_TIME;
     456            }
     457        }
     458
     459        ret.setExpirationTime(Math.max(minimumExpiryTime + System.currentTimeMillis(), lng));
    458460        ret.setLastModification(now);
    459461        ret.setEtag(urlConn.getHeaderField("ETag"));
     
    480482        final HttpClient.Response urlConn = getRequest("HEAD", false).connect();
    481483        long lastModified = urlConn.getLastModified();
    482         return (attributes.getEtag() != null && attributes.getEtag().equals(urlConn.getHeaderField("ETag"))) ||
     484        boolean ret = (attributes.getEtag() != null && attributes.getEtag().equals(urlConn.getHeaderField("ETag"))) ||
    483485                (lastModified != 0 && lastModified <= attributes.getLastModification());
     486        if (ret) {
     487            // update attributes
     488            attributes = parseHeaders(urlConn);
     489            cache.put(getCacheKey(), cacheData, attributes);
     490        }
     491        return ret;
    484492    }
    485493
Note: See TracChangeset for help on using the changeset viewer.