001// License: GPL. For details, see Readme.txt file. 002package org.openstreetmap.gui.jmapviewer; 003 004import java.io.IOException; 005import java.io.InputStream; 006import java.net.HttpURLConnection; 007import java.net.URL; 008import java.net.URLConnection; 009import java.util.HashMap; 010import java.util.Map; 011import java.util.Map.Entry; 012import java.util.concurrent.Executor; 013import java.util.concurrent.Executors; 014 015import org.openstreetmap.gui.jmapviewer.interfaces.TileJob; 016import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader; 017import org.openstreetmap.gui.jmapviewer.interfaces.TileLoaderListener; 018 019/** 020 * A {@link TileLoader} implementation that loads tiles from OSM. 021 * 022 * @author Jan Peter Stotz 023 */ 024public class OsmTileLoader implements TileLoader { 025 private static final Executor jobDispatcher = Executors.newSingleThreadExecutor(); 026 027 private final class OsmTileJob implements TileJob { 028 private final Tile tile; 029 private InputStream input; 030 private boolean force; 031 032 private OsmTileJob(Tile tile) { 033 this.tile = tile; 034 } 035 036 @Override 037 public void run() { 038 synchronized (tile) { 039 if ((tile.isLoaded() && !tile.hasError()) || tile.isLoading()) 040 return; 041 tile.loaded = false; 042 tile.error = false; 043 tile.loading = true; 044 } 045 try { 046 URLConnection conn = loadTileFromOsm(tile); 047 if (force) { 048 conn.setUseCaches(false); 049 } 050 loadTileMetadata(tile, conn); 051 if ("no-tile".equals(tile.getValue("tile-info"))) { 052 tile.setError("No tile at this zoom level"); 053 } else { 054 input = conn.getInputStream(); 055 try { 056 tile.loadImage(input); 057 } finally { 058 input.close(); 059 input = null; 060 } 061 } 062 tile.setLoaded(true); 063 listener.tileLoadingFinished(tile, true); 064 } catch (Exception e) { 065 tile.setError(e.getMessage()); 066 listener.tileLoadingFinished(tile, false); 067 if (input == null) { 068 try { 069 System.err.println("Failed loading " + tile.getUrl() +": " 070 +e.getClass() + ": " + e.getMessage()); 071 } catch (IOException ioe) { 072 ioe.printStackTrace(); 073 } 074 } 075 } finally { 076 tile.loading = false; 077 tile.setLoaded(true); 078 } 079 } 080 081 @Override 082 public Tile getTile() { 083 return tile; 084 } 085 086 @Override 087 public void submit() { 088 submit(false); 089 } 090 091 @Override 092 public void submit(boolean force) { 093 this.force = force; 094 jobDispatcher.execute(this); 095 } 096 } 097 098 /** 099 * Holds the HTTP headers. Insert e.g. User-Agent here when default should not be used. 100 */ 101 public Map<String, String> headers = new HashMap<>(); 102 103 public int timeoutConnect; 104 public int timeoutRead; 105 106 protected TileLoaderListener listener; 107 108 public OsmTileLoader(TileLoaderListener listener) { 109 this(listener, null); 110 } 111 112 public OsmTileLoader(TileLoaderListener listener, Map<String, String> headers) { 113 this.headers.put("Accept", "text/html, image/png, image/jpeg, image/gif, */*"); 114 if (headers != null) { 115 this.headers.putAll(headers); 116 } 117 this.listener = listener; 118 } 119 120 @Override 121 public TileJob createTileLoaderJob(final Tile tile) { 122 return new OsmTileJob(tile); 123 } 124 125 protected URLConnection loadTileFromOsm(Tile tile) throws IOException { 126 URL url; 127 url = new URL(tile.getUrl()); 128 URLConnection urlConn = url.openConnection(); 129 if (urlConn instanceof HttpURLConnection) { 130 prepareHttpUrlConnection((HttpURLConnection) urlConn); 131 } 132 return urlConn; 133 } 134 135 protected void loadTileMetadata(Tile tile, URLConnection urlConn) { 136 String str = urlConn.getHeaderField("X-VE-TILEMETA-CaptureDatesRange"); 137 if (str != null) { 138 tile.putValue("capture-date", str); 139 } 140 str = urlConn.getHeaderField("X-VE-Tile-Info"); 141 if (str != null) { 142 tile.putValue("tile-info", str); 143 } 144 145 Long lng = urlConn.getExpiration(); 146 if (lng.equals(0L)) { 147 try { 148 str = urlConn.getHeaderField("Cache-Control"); 149 if (str != null) { 150 for (String token: str.split(",")) { 151 if (token.startsWith("max-age=")) { 152 lng = Long.parseLong(token.substring(8)) * 1000 + 153 System.currentTimeMillis(); 154 } 155 } 156 } 157 } catch (NumberFormatException e) { 158 // ignore malformed Cache-Control headers 159 if (JMapViewer.debug) { 160 System.err.println(e.getMessage()); 161 } 162 } 163 } 164 if (!lng.equals(0L)) { 165 tile.putValue("expires", lng.toString()); 166 } 167 } 168 169 protected void prepareHttpUrlConnection(HttpURLConnection urlConn) { 170 for (Entry<String, String> e : headers.entrySet()) { 171 urlConn.setRequestProperty(e.getKey(), e.getValue()); 172 } 173 if (timeoutConnect != 0) 174 urlConn.setConnectTimeout(timeoutConnect); 175 if (timeoutRead != 0) 176 urlConn.setReadTimeout(timeoutRead); 177 } 178 179 @Override 180 public String toString() { 181 return getClass().getSimpleName(); 182 } 183 184 @Override 185 public void cancelOutstandingTasks() { 186 // intentionally left empty - OsmTileLoader doesn't maintain queue 187 } 188}