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