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