Ignore:
Timestamp:
2020-05-16T20:59:34+02:00 (4 years ago)
Author:
simon04
Message:

see #18712 - Add NetworkManager.isOffline(String) to test offline status of given URL

Deprecates OnlineResource.checkOfflineAccess(String, String)

Location:
trunk/src/org/openstreetmap/josm/io
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/io/CacheCustomContent.java

    r13647 r16426  
    9595    }
    9696
    97     private boolean isOffline() {
    98         try {
    99             checkOfflineAccess();
    100             return false;
    101         } catch (OfflineAccessException e) {
    102             Logging.trace(e);
    103             return true;
    104         }
    105     }
    106 
    107     /**
    108      * Ensures underlying resource is not accessed in offline mode.
    109      * @throws OfflineAccessException if resource is accessed in offline mode
    110      */
    111     protected abstract void checkOfflineAccess();
     97    /**
     98     * Checks underlying resource is not accessed in offline mode.
     99     * @return whether resource is accessed in offline mode
     100     */
     101    protected abstract boolean isOffline();
    112102
    113103    /**
  • trunk/src/org/openstreetmap/josm/io/CachedFile.java

    r15588 r16426  
    435435        File localFile = null;
    436436        List<String> localPathEntry = new ArrayList<>(Config.getPref().getList(prefKey));
    437         boolean offline = false;
    438         try {
    439             checkOfflineAccess(urlStr);
    440         } catch (OfflineAccessException e) {
    441             Logging.trace(e);
    442             offline = true;
    443         }
     437        boolean offline = NetworkManager.isOffline(urlStr);
    444438        if (localPathEntry.size() == 2) {
    445439            localFile = new File(localPathEntry.get(1));
     
    534528    }
    535529
    536     private static void checkOfflineAccess(String urlString) {
    537         OnlineResource.JOSM_WEBSITE.checkOfflineAccess(urlString, Config.getUrls().getJOSMWebsite());
    538         OnlineResource.OSM_API.checkOfflineAccess(urlString, OsmApi.getOsmApi().getServerUrl());
    539     }
    540 
    541530    private static String truncatePath(String directory, String fileName) {
    542531        if (directory.length() + fileName.length() > 255) {
  • trunk/src/org/openstreetmap/josm/io/NetworkManager.java

    r14273 r16426  
    7474
    7575    /**
     76     * Determines if the given online resource specified as URL is currently offline.
     77     * @param url the online resource specified as URL
     78     * @return {@code true} if {@code url} is offline and should not be accessed
     79     * @since 16426
     80     */
     81    public static boolean isOffline(String url) {
     82        return OFFLINE_RESOURCES.stream().anyMatch(r -> r.matches(url));
     83    }
     84
     85    /**
    7686     * Determines if the given online resource is currently offline.
    7787     * @param r the online resource
  • trunk/src/org/openstreetmap/josm/io/OnlineResource.java

    r14999 r16426  
    33
    44import static org.openstreetmap.josm.tools.I18n.tr;
     5
     6import org.openstreetmap.josm.spi.preferences.Config;
    57
    68/**
     
    3739
    3840    /**
     41     * Replies whether the given URL matches this online resource
     42     * @return whether the given URL matches this online resource
     43     */
     44    public final boolean matches(String url) {
     45        final String baseUrl;
     46        switch (this) {
     47            case ALL:
     48                return true;
     49            case OSM_API:
     50                baseUrl = OsmApi.getOsmApi().getServerUrl();
     51                break;
     52            case JOSM_WEBSITE:
     53                baseUrl = Config.getUrls().getJOSMWebsite();
     54                break;
     55            default:
     56                return false;
     57        }
     58        return url.startsWith(baseUrl.substring(baseUrl.indexOf("://")), url.indexOf("://"));
     59    }
     60
     61    /**
    3962     * Ensures resource is not accessed in offline mode.
    4063     * @param downloadString The attempted download string
    41      * @param resourceString The resource download string that should not be accessed
     64     * @param ignore ignored
    4265     * @throws OfflineAccessException if resource is accessed in offline mode, in any protocol
     66     * @deprecated use {@link NetworkManager#isOffline(String)}
    4367     */
    44     public final void checkOfflineAccess(String downloadString, String resourceString) {
    45         if (NetworkManager.isOffline(this) && downloadString
    46                 .startsWith(resourceString.substring(resourceString.indexOf("://")), downloadString.indexOf("://"))) {
     68    @Deprecated
     69    public final void checkOfflineAccess(String downloadString, String ignore) {
     70        if (NetworkManager.isOffline(downloadString)) {
    4771            throw new OfflineAccessException(tr("Unable to access ''{0}'': {1} not available (offline mode)", downloadString, getLocName()));
    4872        }
  • trunk/src/org/openstreetmap/josm/io/OsmApi.java

    r16422 r16426  
    209209
    210210        @Override
    211         protected void checkOfflineAccess() {
    212             OnlineResource.OSM_API.checkOfflineAccess(getBaseUrl(getServerUrlFromPref(), "0.6")+CAPABILITIES, getServerUrlFromPref());
     211        protected boolean isOffline() {
     212            return NetworkManager.isOffline(OnlineResource.OSM_API);
    213213        }
    214214
  • trunk/src/org/openstreetmap/josm/io/OsmServerReader.java

    r16422 r16426  
    2020import org.openstreetmap.josm.io.auth.CredentialsAgentException;
    2121import org.openstreetmap.josm.io.auth.CredentialsManager;
    22 import org.openstreetmap.josm.spi.preferences.Config;
    2322import org.openstreetmap.josm.tools.HttpClient;
    2423import org.openstreetmap.josm.tools.Logging;
     
    158157            boolean uncompressAccordingToContentDisposition, String httpMethod, byte[] requestBody) throws OsmTransferException {
    159158        try {
    160             OnlineResource.JOSM_WEBSITE.checkOfflineAccess(urlStr, Config.getUrls().getJOSMWebsite());
    161             OnlineResource.OSM_API.checkOfflineAccess(urlStr, OsmApi.getOsmApi().getServerUrl());
     159            if (NetworkManager.isOffline(urlStr)) {
     160                throw new OsmApiException(new OfflineAccessException(tr("{0} not available (offline mode)", urlStr)));
     161            }
    162162
    163163            URL url = null;
Note: See TracChangeset for help on using the changeset viewer.