1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
7 |
|
---|
8 | /**
|
---|
9 | * Online resources directly used by JOSM.
|
---|
10 | * This does not include websites where user can sometimes be redirected through its web browser,
|
---|
11 | * but only those to we establish a connection.
|
---|
12 | *
|
---|
13 | * @since 7434
|
---|
14 | */
|
---|
15 | public enum OnlineResource {
|
---|
16 |
|
---|
17 | /** The OSM API, used for download, upload, history, etc. */
|
---|
18 | OSM_API(tr("OSM API")),
|
---|
19 | /** The JOSM website, used for startup page, imagery/presets/styles/rules entries, help, etc. */
|
---|
20 | JOSM_WEBSITE(tr("JOSM website")),
|
---|
21 | /** Various government certificates downloaded on Windows to make https imagery work in some countries */
|
---|
22 | CERTIFICATES(tr("Certificates")),
|
---|
23 | /** Value used to represent all online resources */
|
---|
24 | ALL(tr("All"));
|
---|
25 |
|
---|
26 | private final String locName;
|
---|
27 |
|
---|
28 | OnlineResource(String locName) {
|
---|
29 | this.locName = locName;
|
---|
30 | }
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Replies the localized name.
|
---|
34 | * @return the localized name
|
---|
35 | */
|
---|
36 | public final String getLocName() {
|
---|
37 | return locName;
|
---|
38 | }
|
---|
39 |
|
---|
40 | /**
|
---|
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 | /**
|
---|
62 | * Ensures resource is not accessed in offline mode.
|
---|
63 | * @param downloadString The attempted download string
|
---|
64 | * @param ignore ignored
|
---|
65 | * @throws OfflineAccessException if resource is accessed in offline mode, in any protocol
|
---|
66 | * @deprecated use {@link NetworkManager#isOffline(String)}
|
---|
67 | */
|
---|
68 | @Deprecated
|
---|
69 | public final void checkOfflineAccess(String downloadString, String ignore) {
|
---|
70 | if (NetworkManager.isOffline(downloadString)) {
|
---|
71 | throw new OfflineAccessException(tr("Unable to access ''{0}'': {1} not available (offline mode)", downloadString, getLocName()));
|
---|
72 | }
|
---|
73 | }
|
---|
74 | }
|
---|