Ignore:
Timestamp:
2021-05-06T17:39:27+02:00 (5 years ago)
Author:
simon04
Message:

fix #17177 - Add support for Mapbox Vector Tile (patch by taylor.smock)

Signed-off-by: Taylor Smock <tsmock@…>

File:
1 edited

Legend:

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

    r17509 r17862  
    22package org.openstreetmap.josm.data.cache;
    33
     4import java.io.File;
    45import java.io.FileNotFoundException;
    56import java.io.IOException;
     7import java.io.InputStream;
    68import java.net.HttpURLConnection;
    79import java.net.URL;
     10import java.nio.file.Files;
    811import java.security.SecureRandom;
    912import java.util.Collections;
     
    1821import java.util.regex.Matcher;
    1922
    20 import org.apache.commons.jcs3.access.behavior.ICacheAccess;
    21 import org.apache.commons.jcs3.engine.behavior.ICacheElement;
    2223import org.openstreetmap.josm.data.cache.ICachedLoaderListener.LoadResult;
    2324import org.openstreetmap.josm.data.imagery.TileJobOptions;
     
    2728import org.openstreetmap.josm.tools.Logging;
    2829import org.openstreetmap.josm.tools.Utils;
     30
     31import org.apache.commons.jcs3.access.behavior.ICacheAccess;
     32import org.apache.commons.jcs3.engine.behavior.ICacheElement;
    2933
    3034/**
     
    295299            attributes = new CacheEntryAttributes();
    296300        }
     301        final URL url = this.getUrlNoException();
     302        if (url == null) {
     303            return false;
     304        }
     305
     306        if (url.getProtocol().contains("http")) {
     307            return loadObjectHttp();
     308        }
     309        if (url.getProtocol().contains("file")) {
     310            return loadObjectFile(url);
     311        }
     312
     313        return false;
     314    }
     315
     316    private boolean loadObjectFile(URL url) {
     317        String fileName = url.toExternalForm();
     318        File file = new File(fileName.substring("file:/".length() - 1));
     319        if (!file.exists()) {
     320            file = new File(fileName.substring("file://".length() - 1));
     321        }
     322        try (InputStream fileInputStream = Files.newInputStream(file.toPath())) {
     323            cacheData = createCacheEntry(Utils.readBytesFromStream(fileInputStream));
     324            cache.put(getCacheKey(), cacheData, attributes);
     325            return true;
     326        } catch (IOException e) {
     327            Logging.error(e);
     328            attributes.setError(e);
     329            attributes.setException(e);
     330        }
     331        return false;
     332    }
     333
     334    /**
     335     * @return true if object was successfully downloaded via http, false, if there was a loading failure
     336     */
     337    private boolean loadObjectHttp() {
    297338        try {
    298339            // if we have object in cache, and host doesn't support If-Modified-Since nor If-None-Match
     
    554595            return getUrl();
    555596        } catch (IOException e) {
     597            Logging.trace(e);
    556598            return null;
    557599        }
Note: See TracChangeset for help on using the changeset viewer.