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

Last change on this file since 11710 was 11510, checked in by Don-vip, 7 years ago

sonar - fb-contrib:ACEM_ABSTRACT_CLASS_EMPTY_METHODS

  • Property svn:eol-style set to native
File size: 3.4 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.util.List;
9import java.util.concurrent.Callable;
10import java.util.concurrent.TimeUnit;
11
12import org.openstreetmap.gui.jmapviewer.tilesources.BingAerialTileSource;
13import org.openstreetmap.gui.jmapviewer.tilesources.TileSourceInfo;
14import org.openstreetmap.josm.Main;
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.xml.sax.InputSource;
20
21/**
22 * Bing TileSource with cached attribution
23 *
24 * @author Wiktor Niesiobędzki
25 * @since 8526
26 */
27public 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 Main.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 Main.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 Main.warn(ex, "Could not connect to Bing API. Will retry in " + waitTimeSec + " seconds.");
90 Thread.sleep(TimeUnit.SECONDS.toMillis(waitTimeSec));
91 waitTimeSec *= 2;
92 }
93 }
94 };
95 }
96}
Note: See TracBrowser for help on using the repository browser.