1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.imagery;
|
---|
3 |
|
---|
4 | import java.io.IOException;
|
---|
5 | import java.io.StringReader;
|
---|
6 | import java.net.MalformedURLException;
|
---|
7 | import java.net.URL;
|
---|
8 | import java.nio.charset.StandardCharsets;
|
---|
9 | import java.util.List;
|
---|
10 | import java.util.concurrent.Callable;
|
---|
11 | import java.util.concurrent.TimeUnit;
|
---|
12 |
|
---|
13 | import org.openstreetmap.gui.jmapviewer.tilesources.BingAerialTileSource;
|
---|
14 | import org.openstreetmap.gui.jmapviewer.tilesources.TileSourceInfo;
|
---|
15 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
16 | import org.openstreetmap.josm.io.CacheCustomContent;
|
---|
17 | import org.openstreetmap.josm.io.OnlineResource;
|
---|
18 | import org.openstreetmap.josm.tools.HttpClient;
|
---|
19 | import org.openstreetmap.josm.tools.Logging;
|
---|
20 | import org.xml.sax.InputSource;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Bing TileSource with cached attribution
|
---|
24 | *
|
---|
25 | * @author Wiktor Niesiobędzki
|
---|
26 | * @since 8526
|
---|
27 | */
|
---|
28 | public class CachedAttributionBingAerialTileSource extends BingAerialTileSource {
|
---|
29 | private Runnable attributionDownloadedTask;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Creates tile source
|
---|
33 | * @param info ImageryInfo description of this tile source
|
---|
34 | */
|
---|
35 | public CachedAttributionBingAerialTileSource(ImageryInfo info) {
|
---|
36 | super(info);
|
---|
37 | }
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Creates tile source
|
---|
41 | * @param info ImageryInfo description of this tile source
|
---|
42 | * @param attributionDownloadedTask runnable to be executed once attribution is loaded
|
---|
43 | */
|
---|
44 |
|
---|
45 | public CachedAttributionBingAerialTileSource(TileSourceInfo info, Runnable attributionDownloadedTask) {
|
---|
46 | super(info);
|
---|
47 | this.attributionDownloadedTask = attributionDownloadedTask;
|
---|
48 | }
|
---|
49 |
|
---|
50 | class BingAttributionData extends CacheCustomContent<IOException> {
|
---|
51 |
|
---|
52 | BingAttributionData() {
|
---|
53 | super("bing.attribution.xml", CacheCustomContent.INTERVAL_HOURLY);
|
---|
54 | }
|
---|
55 |
|
---|
56 | @Override
|
---|
57 | protected byte[] updateData() throws IOException {
|
---|
58 | URL u = getAttributionUrl();
|
---|
59 | final String r = HttpClient.create(u).connect().fetchContent();
|
---|
60 | Logging.info("Successfully loaded Bing attribution data.");
|
---|
61 | return r.getBytes(StandardCharsets.UTF_8);
|
---|
62 | }
|
---|
63 |
|
---|
64 | @Override
|
---|
65 | protected void checkOfflineAccess() {
|
---|
66 | try {
|
---|
67 | String attributionUrl = getAttributionUrl().toExternalForm();
|
---|
68 | OnlineResource.ALL.checkOfflineAccess(attributionUrl, attributionUrl);
|
---|
69 | } catch (MalformedURLException e) {
|
---|
70 | Logging.error(e);
|
---|
71 | }
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | @Override
|
---|
76 | protected Callable<List<Attribution>> getAttributionLoaderCallable() {
|
---|
77 | return () -> {
|
---|
78 | BingAttributionData attributionLoader = new BingAttributionData();
|
---|
79 | int waitTimeSec = 1;
|
---|
80 | while (true) {
|
---|
81 | try {
|
---|
82 | String xml = attributionLoader.updateIfRequiredString();
|
---|
83 | List<Attribution> ret = parseAttributionText(new InputSource(new StringReader(xml)));
|
---|
84 | if (attributionDownloadedTask != null) {
|
---|
85 | GuiHelper.runInEDT(attributionDownloadedTask);
|
---|
86 | attributionDownloadedTask = null;
|
---|
87 | }
|
---|
88 | return ret;
|
---|
89 | } catch (IOException ex) {
|
---|
90 | Logging.log(Logging.LEVEL_WARN, "Could not connect to Bing API. Will retry in " + waitTimeSec + " seconds.", ex);
|
---|
91 | Thread.sleep(TimeUnit.SECONDS.toMillis(waitTimeSec));
|
---|
92 | waitTimeSec *= 2;
|
---|
93 | }
|
---|
94 | }
|
---|
95 | };
|
---|
96 | }
|
---|
97 | }
|
---|