Changeset 24847 in osm for applications/viewer/jmapviewer/src/org/openstreetmap
- Timestamp:
- 2010-12-23T15:04:09+01:00 (14 years ago)
- Location:
- applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/OsmFileCacheTileLoader.java
r24840 r24847 3 3 //License: GPL. Copyright 2008 by Jan Peter Stotz 4 4 5 import java.io.BufferedReader; 5 6 import java.io.ByteArrayInputStream; 6 7 import java.io.ByteArrayOutputStream; 7 8 import java.io.File; 8 9 import java.io.FileInputStream; 10 import java.io.FileNotFoundException; 9 11 import java.io.FileOutputStream; 10 12 import java.io.IOException; 11 13 import java.io.InputStream; 14 import java.io.InputStreamReader; 15 import java.io.OutputStreamWriter; 16 import java.io.PrintWriter; 12 17 import java.net.HttpURLConnection; 13 18 import java.net.URL; 14 19 import java.net.URLConnection; 15 20 import java.nio.charset.Charset; 21 import java.util.Map.Entry; 16 22 import java.util.logging.Level; 17 23 import java.util.logging.Logger; … … 36 42 37 43 private static final String ETAG_FILE_EXT = ".etag"; 38 39 private static final Charset ETAG_CHARSET = Charset.forName("UTF-8"); 44 private static final String TAGS_FILE_EXT = ".tags"; 45 46 private static final Charset TAGS_CHARSET = Charset.forName("UTF-8"); 40 47 41 48 public static final long FILE_AGE_ONE_DAY = 1000 * 60 * 60 * 24; … … 167 174 } 168 175 if (source.getTileUpdate() == TileUpdate.ETag || source.getTileUpdate() == TileUpdate.IfNoneMatch) { 169 if (tileFile != null) { 170 String fileETag = loadETagfromFile(); 171 if (fileETag != null) { 172 switch (source.getTileUpdate()) { 173 case IfNoneMatch: 174 urlConn.addRequestProperty("If-None-Match", fileETag); 175 break; 176 case ETag: 177 if (hasOsmTileETag(fileETag)) { 178 tile.setLoaded(true); 179 tileFile.setLastModified(System.currentTimeMillis() - maxCacheFileAge 180 + recheckAfter); 181 return; 182 } 176 String fileETag = tile.getValue("etag"); 177 if (fileETag != null) { 178 switch (source.getTileUpdate()) { 179 case IfNoneMatch: 180 urlConn.addRequestProperty("If-None-Match", fileETag); 181 break; 182 case ETag: 183 if (hasOsmTileETag(fileETag)) { 184 tile.setLoaded(true); 185 tileFile.setLastModified(System.currentTimeMillis() - maxCacheFileAge 186 + recheckAfter); 187 return; 183 188 } 184 189 } 185 190 } 186 187 String eTag = urlConn.getHeaderField("ETag"); 188 saveETagToFile(eTag); 189 } 190 loadTileMetadata(tile, urlConn); 191 tile.putValue("etag", urlConn.getHeaderField("ETag")); 192 } 191 193 if (urlConn instanceof HttpURLConnection && ((HttpURLConnection)urlConn).getResponseCode() == 304) { 192 194 // If we are isModifiedSince or If-None-Match has been set … … 197 199 return; 198 200 } 201 202 loadTileMetadata(tile, urlConn); 203 saveTagsToFile(); 199 204 200 205 byte[] buffer = loadTileInBuffer(urlConn); … … 229 234 tile.loadImage(fin); 230 235 fin.close(); 236 237 loadTagsFromFile(); 238 231 239 fileAge = tileFile.lastModified(); 232 240 boolean oldTile = System.currentTimeMillis() - fileAge > maxCacheFileAge; … … 337 345 } 338 346 339 protected void saveETagToFile(String eTag) { 340 try { 341 FileOutputStream f = new FileOutputStream(tileCacheDir + "/" + tile.getZoom() + "_" + tile.getXtile() 342 + "_" + tile.getYtile() + ETAG_FILE_EXT); 343 f.write(eTag.getBytes(ETAG_CHARSET.name())); 347 protected void saveTagsToFile() { 348 File tagsFile = new File(tileCacheDir, tile.getZoom() + "_" 349 + tile.getXtile() + "_" + tile.getYtile() + TAGS_FILE_EXT); 350 if (tile.getMetadata() == null) { 351 tagsFile.delete(); 352 return; 353 } 354 try { 355 final PrintWriter f = new PrintWriter(new OutputStreamWriter(new FileOutputStream(tagsFile))); 356 for (Entry<String, String> entry : tile.getMetadata().entrySet()) { 357 f.println(entry.getKey() + "=" + entry.getValue()); 358 } 344 359 f.close(); 345 360 } catch (Exception e) { 346 System.err.println("Failed to save ETag: " + e.getLocalizedMessage()); 347 } 348 } 349 350 protected String loadETagfromFile() { 351 try { 352 FileInputStream f = new FileInputStream(tileCacheDir + "/" + tile.getZoom() + "_" + tile.getXtile() 353 + "_" + tile.getYtile() + ETAG_FILE_EXT); 361 System.err.println("Failed to save tile tags: " + e.getLocalizedMessage()); 362 } 363 } 364 365 /** Load backward-compatiblity .etag file and if it exists move it to new .tags file*/ 366 private void loadOldETagfromFile() { 367 File etagFile = new File(tileCacheDir, tile.getZoom() + "_" 368 + tile.getXtile() + "_" + tile.getYtile() + ETAG_FILE_EXT); 369 if (!etagFile.exists()) return; 370 try { 371 FileInputStream f = new FileInputStream(etagFile); 354 372 byte[] buf = new byte[f.available()]; 355 373 f.read(buf); 356 374 f.close(); 357 return new String(buf, ETAG_CHARSET.name()); 375 String etag = new String(buf, TAGS_CHARSET.name()); 376 tile.putValue("etag", etag); 377 if (etagFile.delete()) { 378 saveTagsToFile(); 379 } 380 } catch (IOException e) { 381 System.err.println("Failed to load compatiblity etag: " + e.getLocalizedMessage()); 382 } 383 } 384 385 protected void loadTagsFromFile() { 386 loadOldETagfromFile(); 387 File tagsFile = new File(tileCacheDir, tile.getZoom() + "_" 388 + tile.getXtile() + "_" + tile.getYtile() + TAGS_FILE_EXT); 389 try { 390 final BufferedReader f = new BufferedReader(new InputStreamReader(new FileInputStream(tagsFile))); 391 for (String line = f.readLine(); line != null; line = f.readLine()) { 392 final int i = line.indexOf('='); 393 if (i == -1 || i == 0) { 394 System.err.println("Malformed tile tag in file '" + tagsFile.getName() + "':" + line); 395 continue; 396 } 397 tile.putValue(line.substring(0,i),line.substring(i+1)); 398 } 399 f.close(); 400 } catch (FileNotFoundException e) { 358 401 } catch (Exception e) { 359 return null;402 System.err.println("Failed to load tile tags: " + e.getLocalizedMessage()); 360 403 } 361 404 } -
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/Tile.java
r24840 r24847 51 51 protected boolean error = false; 52 52 53 /** Tile Source-specific tile metadata */53 /** TileLoader-specific tile metadata */ 54 54 protected Map<String, String> metadata; 55 55 … … 272 272 273 273 public void putValue(String key, String value) { 274 if (value == null || "".equals(value)) { 275 if (metadata != null) { 276 metadata.remove(key); 277 } 278 return; 279 } 274 280 if (metadata == null) { 275 281 metadata = new HashMap<String,String>();
Note:
See TracChangeset
for help on using the changeset viewer.