Changeset 16426 in josm


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
Files:
12 edited

Legend:

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

    r16367 r16426  
    2121import java.util.HashMap;
    2222import java.util.HashSet;
    23 import java.util.Iterator;
    2423import java.util.List;
    2524import java.util.Map;
     
    4342import org.openstreetmap.josm.data.preferences.PreferencesWriter;
    4443import org.openstreetmap.josm.gui.MainApplication;
    45 import org.openstreetmap.josm.io.OfflineAccessException;
    46 import org.openstreetmap.josm.io.OnlineResource;
     44import org.openstreetmap.josm.io.NetworkManager;
    4745import org.openstreetmap.josm.spi.preferences.AbstractPreferences;
    4846import org.openstreetmap.josm.spi.preferences.Config;
     
    804802    public Collection<String> getOnlinePluginSites() {
    805803        Collection<String> pluginSites = new ArrayList<>(getPluginSites());
    806         for (Iterator<String> it = pluginSites.iterator(); it.hasNext();) {
    807             try {
    808                 OnlineResource.JOSM_WEBSITE.checkOfflineAccess(it.next(), Config.getUrls().getJOSMWebsite());
    809             } catch (OfflineAccessException ex) {
    810                 Logging.log(Logging.LEVEL_WARN, ex);
    811                 it.remove();
    812             }
    813         }
     804        pluginSites.removeIf(NetworkManager::isOffline);
    814805        return pluginSites;
    815806    }
  • trunk/src/org/openstreetmap/josm/data/imagery/CachedAttributionBingAerialTileSource.java

    r14746 r16426  
    1515import org.openstreetmap.josm.gui.util.GuiHelper;
    1616import org.openstreetmap.josm.io.CacheCustomContent;
    17 import org.openstreetmap.josm.io.OnlineResource;
     17import org.openstreetmap.josm.io.NetworkManager;
    1818import org.openstreetmap.josm.tools.HttpClient;
    1919import org.openstreetmap.josm.tools.Logging;
     
    6363
    6464        @Override
    65         protected void checkOfflineAccess() {
     65        protected boolean isOffline() {
    6666            try {
    67                 String attributionUrl = getAttributionUrl().toExternalForm();
    68                 OnlineResource.ALL.checkOfflineAccess(attributionUrl, attributionUrl);
     67                return NetworkManager.isOffline(getAttributionUrl().toExternalForm());
    6968            } catch (MalformedURLException e) {
    7069                Logging.error(e);
     70                return false;
    7171            }
    7272        }
  • trunk/src/org/openstreetmap/josm/data/imagery/ImageryLayerInfo.java

    r14214 r16426  
    2222import org.openstreetmap.josm.gui.PleaseWaitRunnable;
    2323import org.openstreetmap.josm.io.CachedFile;
    24 import org.openstreetmap.josm.io.OfflineAccessException;
    25 import org.openstreetmap.josm.io.OnlineResource;
     24import org.openstreetmap.josm.io.NetworkManager;
    2625import org.openstreetmap.josm.io.imagery.ImageryReader;
    2726import org.openstreetmap.josm.spi.preferences.Config;
     
    159158
    160159        protected void loadSource(String source) {
    161             boolean online = true;
    162             try {
    163                 OnlineResource.JOSM_WEBSITE.checkOfflineAccess(source, Config.getUrls().getJOSMWebsite());
    164             } catch (OfflineAccessException e) {
    165                 Logging.log(Logging.LEVEL_WARN, e);
    166                 online = false;
    167             }
     160            boolean online = !NetworkManager.isOffline(source);
    168161            if (clearCache && online) {
    169162                CachedFile.cleanup(source);
  • trunk/src/org/openstreetmap/josm/gui/GettingStarted.java

    r16006 r16426  
    3030import org.openstreetmap.josm.gui.widgets.JosmEditorPane;
    3131import org.openstreetmap.josm.io.CacheCustomContent;
     32import org.openstreetmap.josm.io.NetworkManager;
    3233import org.openstreetmap.josm.io.OnlineResource;
    3334import org.openstreetmap.josm.spi.preferences.Config;
     
    107108
    108109        @Override
    109         protected void checkOfflineAccess() {
    110             OnlineResource.JOSM_WEBSITE.checkOfflineAccess(new WikiReader().getBaseUrlWiki(), Config.getUrls().getJOSMWebsite());
     110        protected boolean isOffline() {
     111            return NetworkManager.isOffline(OnlineResource.JOSM_WEBSITE);
    111112        }
    112113
  • 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;
  • trunk/src/org/openstreetmap/josm/plugins/PluginHandler.java

    r15801 r16426  
    6767import org.openstreetmap.josm.gui.widgets.JosmTextArea;
    6868import org.openstreetmap.josm.io.NetworkManager;
    69 import org.openstreetmap.josm.io.OfflineAccessException;
    70 import org.openstreetmap.josm.io.OnlineResource;
    7169import org.openstreetmap.josm.spi.preferences.Config;
    7270import org.openstreetmap.josm.tools.Destroyable;
     
    436434     */
    437435    public static boolean checkAndConfirmPluginUpdate(Component parent) {
    438         if (!checkOfflineAccess()) {
     436        if (Preferences.main().getPluginSites().stream().anyMatch(NetworkManager::isOffline)) {
    439437            Logging.info(tr("{0} not available (offline mode)", tr("Plugin update")));
    440438            return false;
     
    541539        }
    542540        return ret == 0;
    543     }
    544 
    545     private static boolean checkOfflineAccess() {
    546         if (NetworkManager.isOffline(OnlineResource.ALL)) {
    547             return false;
    548         }
    549         if (NetworkManager.isOffline(OnlineResource.JOSM_WEBSITE)) {
    550             for (String updateSite : Preferences.main().getPluginSites()) {
    551                 try {
    552                     OnlineResource.JOSM_WEBSITE.checkOfflineAccess(updateSite, Config.getUrls().getJOSMWebsite());
    553                 } catch (OfflineAccessException e) {
    554                     Logging.trace(e);
    555                     return false;
    556                 }
    557             }
    558         }
    559         return true;
    560541    }
    561542
  • trunk/test/unit/org/openstreetmap/josm/io/NetworkManagerTest.java

    r16425 r16426  
    5454    public void testOfflineResources() {
    5555        NetworkManager.setOnline(OnlineResource.ALL);
     56        assertFalse(NetworkManager.isOffline("http://www.example.com/"));
    5657        assertTrue(NetworkManager.getOfflineResources().isEmpty());
    5758        assertFalse(NetworkManager.isOffline(OnlineResource.JOSM_WEBSITE));
    5859        NetworkManager.setOffline(OnlineResource.JOSM_WEBSITE);
     60        assertTrue(NetworkManager.isOffline("https://josm.openstreetmap.de/maps"));
     61        assertFalse(NetworkManager.isOffline("http://www.example.com/"));
    5962        assertTrue(NetworkManager.isOffline(OnlineResource.JOSM_WEBSITE));
    6063        NetworkManager.setOnline(OnlineResource.JOSM_WEBSITE);
     64        assertFalse(NetworkManager.isOffline("https://josm.openstreetmap.de/maps"));
    6165        assertFalse(NetworkManager.isOffline(OnlineResource.JOSM_WEBSITE));
    6266        NetworkManager.setOffline(OnlineResource.ALL);
     67        assertTrue(NetworkManager.isOffline("https://josm.openstreetmap.de/maps"));
     68        assertTrue(NetworkManager.isOffline("http://www.example.com/"));
    6369        assertTrue(NetworkManager.isOffline(OnlineResource.JOSM_WEBSITE));
    6470        assertTrue(NetworkManager.isOffline(OnlineResource.OSM_API));
Note: See TracChangeset for help on using the changeset viewer.