Changeset 26938 in osm for applications/viewer/jmapviewer/src/org/openstreetmap/gui
- Timestamp:
- 2011-10-23T12:17:01+02:00 (13 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/OsmTileLoader.java
r26508 r26938 8 8 import java.net.URL; 9 9 import java.net.URLConnection; 10 import java.util.Map; 11 import java.util.Map.Entry; 12 import java.util.HashMap; 10 13 11 14 import org.openstreetmap.gui.jmapviewer.interfaces.TileCache; … … 22 25 23 26 /** 24 * Holds the used user agent used for HTTP requests. If this field is 25 * <code>null</code>, the default Java user agent is used. 27 * Holds the HTTP headers. Insert e.g. User-Agent here when default should not be used. 26 28 */ 27 public static String USER_AGENT = null; 28 public static String ACCEPT = "text/html, image/png, image/jpeg, image/gif, */*"; 29 public Map<String, String> headers = new HashMap<String, String>(); 30 31 public int timeoutConnect = 0; 32 public int timeoutRead = 0; 29 33 30 34 protected TileLoaderListener listener; 31 35 32 36 public OsmTileLoader(TileLoaderListener listener) { 37 headers.put("Accept", "text/html, image/png, image/jpeg, image/gif, */*"); 33 38 this.listener = listener; 34 39 } … … 102 107 103 108 protected void prepareHttpUrlConnection(HttpURLConnection urlConn) { 104 if (USER_AGENT != null) {105 urlConn.setRequestProperty( "User-agent", USER_AGENT);109 for(Entry<String, String> e : headers.entrySet()) { 110 urlConn.setRequestProperty(e.getKey(), e.getValue()); 106 111 } 107 urlConn.setRequestProperty("Accept", ACCEPT); 112 if(timeoutConnect != 0) 113 urlConn.setConnectTimeout(timeoutConnect); 114 if(timeoutRead != 0) 115 urlConn.setReadTimeout(timeoutRead); 108 116 } 109 117 -
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/tilesources/TemplatedTMSTileSource.java
r26911 r26938 1 1 package org.openstreetmap.gui.jmapviewer.tilesources; 2 2 3 import java.util.Map; 4 import java.util.HashMap; 3 5 import java.util.Random; 4 6 import java.util.regex.Pattern; … … 9 11 private Random rand = null; 10 12 private String[] randomParts = null; 13 private Map<String, String> headers = new HashMap<String, String>(); 11 14 12 public static final String PATTERN_ZOOM = "\\{zoom \\}";15 public static final String PATTERN_ZOOM = "\\{zoom([+-]\\d+)?\\}"; 13 16 public static final String PATTERN_X = "\\{x\\}"; 14 17 public static final String PATTERN_Y = "\\{y\\}"; 15 18 public static final String PATTERN_Y_YAHOO = "\\{!y\\}"; 16 19 public static final String PATTERN_SWITCH = "\\{switch:[^}]+\\}"; 20 public static final String PATTERN_HEADER = "\\{header\\(([^,]+),([^}]+)\\)\\}"; 17 21 18 22 public static final String[] ALL_PATTERNS = { 19 PATTERN_ ZOOM, PATTERN_X, PATTERN_Y, PATTERN_Y_YAHOO, PATTERN_SWITCH23 PATTERN_HEADER, PATTERN_ZOOM, PATTERN_X, PATTERN_Y, PATTERN_Y_YAHOO, PATTERN_SWITCH 20 24 }; 21 25 22 26 public TemplatedTMSTileSource(String name, String url, int maxZoom) { 23 27 super(name, url, maxZoom); 28 handleTemplate(); 24 29 } 25 30 26 31 public TemplatedTMSTileSource(String name, String url, int minZoom, int maxZoom) { 27 32 super(name, url, minZoom, maxZoom); 33 handleTemplate(); 34 } 35 36 private void handleTemplate() { 28 37 // Capturing group pattern on switch values 29 Matcher m = Pattern.compile(".* \\{switch:([^}]+)\\}.*").matcher(url);38 Matcher m = Pattern.compile(".*"+PATTERN_SWITCH+".*").matcher(baseUrl); 30 39 if (m.matches()) { 31 40 rand = new Random(); 32 41 randomParts = m.group(1).split(","); 33 42 } 43 Pattern pattern = Pattern.compile(PATTERN_HEADER); 44 StringBuffer output = new StringBuffer(); 45 Matcher matcher = pattern.matcher(baseUrl); 46 while (matcher.find()) { 47 headers.put(matcher.group(1),matcher.group(2)); 48 matcher.appendReplacement(output, ""); 49 } 50 matcher.appendTail(output); 51 baseUrl = output.toString(); 52 } 53 54 public Map<String, String> getHeaders() { 55 return headers; 34 56 } 35 57 36 58 @Override 37 59 public String getTileUrl(int zoom, int tilex, int tiley) { 60 Matcher m = Pattern.compile(".*"+PATTERN_ZOOM+".*").matcher(this.baseUrl); 61 if (m.matches() && !m.group(1).isEmpty()) { 62 String ofs = m.group(1); 63 if(ofs.startsWith("+")) 64 ofs = ofs.substring(1); 65 zoom += Integer.valueOf(ofs); 66 } 38 67 String r = this.baseUrl 39 68 .replaceAll(PATTERN_ZOOM, Integer.toString(zoom))
Note:
See TracChangeset
for help on using the changeset viewer.