Ignore:
Timestamp:
2011-10-23T12:17:01+02:00 (13 years ago)
Author:
stoecker
Message:

improve header settings

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  
    88import java.net.URL;
    99import java.net.URLConnection;
     10import java.util.Map;
     11import java.util.Map.Entry;
     12import java.util.HashMap;
    1013
    1114import org.openstreetmap.gui.jmapviewer.interfaces.TileCache;
     
    2225
    2326    /**
    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.
    2628     */
    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;
    2933
    3034    protected TileLoaderListener listener;
    3135
    3236    public OsmTileLoader(TileLoaderListener listener) {
     37        headers.put("Accept", "text/html, image/png, image/jpeg, image/gif, */*");
    3338        this.listener = listener;
    3439    }
     
    102107
    103108    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());
    106111        }
    107         urlConn.setRequestProperty("Accept", ACCEPT);
     112        if(timeoutConnect != 0)
     113            urlConn.setConnectTimeout(timeoutConnect);
     114        if(timeoutRead != 0)
     115            urlConn.setReadTimeout(timeoutRead);
    108116    }
    109117
  • applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/tilesources/TemplatedTMSTileSource.java

    r26911 r26938  
    11package org.openstreetmap.gui.jmapviewer.tilesources;
    22
     3import java.util.Map;
     4import java.util.HashMap;
    35import java.util.Random;
    46import java.util.regex.Pattern;
     
    911    private Random rand = null;
    1012    private String[] randomParts = null;
     13    private Map<String, String> headers = new HashMap<String, String>();
    1114   
    12     public static final String PATTERN_ZOOM    = "\\{zoom\\}";
     15    public static final String PATTERN_ZOOM    = "\\{zoom([+-]\\d+)?\\}";
    1316    public static final String PATTERN_X       = "\\{x\\}";
    1417    public static final String PATTERN_Y       = "\\{y\\}";
    1518    public static final String PATTERN_Y_YAHOO = "\\{!y\\}";
    1619    public static final String PATTERN_SWITCH  = "\\{switch:[^}]+\\}";
     20    public static final String PATTERN_HEADER  = "\\{header\\(([^,]+),([^}]+)\\)\\}";
    1721   
    1822    public static final String[] ALL_PATTERNS = {
    19         PATTERN_ZOOM, PATTERN_X, PATTERN_Y, PATTERN_Y_YAHOO, PATTERN_SWITCH
     23        PATTERN_HEADER, PATTERN_ZOOM, PATTERN_X, PATTERN_Y, PATTERN_Y_YAHOO, PATTERN_SWITCH
    2024    };
    2125   
    2226    public TemplatedTMSTileSource(String name, String url, int maxZoom) {
    2327        super(name, url, maxZoom);
     28        handleTemplate();
    2429    }
    2530
    2631    public TemplatedTMSTileSource(String name, String url, int minZoom, int maxZoom) {
    2732        super(name, url, minZoom, maxZoom);
     33        handleTemplate();
     34    }
     35
     36    private void handleTemplate() {
    2837        // Capturing group pattern on switch values
    29         Matcher m = Pattern.compile(".*\\{switch:([^}]+)\\}.*").matcher(url);
     38        Matcher m = Pattern.compile(".*"+PATTERN_SWITCH+".*").matcher(baseUrl);
    3039        if (m.matches()) {
    3140            rand = new Random();
    3241            randomParts = m.group(1).split(",");
    3342        }
     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;
    3456    }
    3557
    3658    @Override
    3759    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        }
    3867        String r = this.baseUrl
    3968            .replaceAll(PATTERN_ZOOM, Integer.toString(zoom))
Note: See TracChangeset for help on using the changeset viewer.