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