source: josm/trunk/src/org/openstreetmap/josm/data/imagery/CachedAttributionBingAerialTileSource.java@ 14746

Last change on this file since 14746 was 14746, checked in by simon04, 5 years ago

Refactoring: use StandardCharsets

  • Property svn:eol-style set to native
File size: 3.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.imagery;
3
4import java.io.IOException;
5import java.io.StringReader;
6import java.net.MalformedURLException;
7import java.net.URL;
8import java.nio.charset.StandardCharsets;
9import java.util.List;
10import java.util.concurrent.Callable;
11import java.util.concurrent.TimeUnit;
12
13import org.openstreetmap.gui.jmapviewer.tilesources.BingAerialTileSource;
14import org.openstreetmap.gui.jmapviewer.tilesources.TileSourceInfo;
15import org.openstreetmap.josm.gui.util.GuiHelper;
16import org.openstreetmap.josm.io.CacheCustomContent;
17import org.openstreetmap.josm.io.OnlineResource;
18import org.openstreetmap.josm.tools.HttpClient;
19import org.openstreetmap.josm.tools.Logging;
20import org.xml.sax.InputSource;
21
22/**
23 * Bing TileSource with cached attribution
24 *
25 * @author Wiktor Niesiobędzki
26 * @since 8526
27 */
28public 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}
Note: See TracBrowser for help on using the repository browser.