Ticket #21850: 21850.patch

File 21850.patch, 4.8 KB (added by taylor.smock, 4 years ago)
  • src/org/openstreetmap/josm/data/imagery/ImageryPatterns.java

    diff --git a/src/org/openstreetmap/josm/data/imagery/ImageryPatterns.java b/src/org/openstreetmap/josm/data/imagery/ImageryPatterns.java
    index 30d0e99e0b..bcc6893462 100644
    a b package org.openstreetmap.josm.data.imagery;  
    44import static org.openstreetmap.josm.tools.I18n.marktr;
    55import static org.openstreetmap.josm.tools.I18n.tr;
    66
     7import java.io.IOException;
    78import java.util.Arrays;
    89import java.util.Map;
    910import java.util.Objects;
    1011import java.util.regex.Matcher;
    1112import java.util.regex.Pattern;
    1213
     14import org.openstreetmap.gui.jmapviewer.FeatureAdapter;
     15
    1316/**
    1417 * Patterns that can be replaced in imagery URLs.
    1518 * @since 17578
    public final class ImageryPatterns {  
    2932    static final Pattern PATTERN_HEIGHT = Pattern.compile("\\{height\\}");
    3033    static final Pattern PATTERN_TIME   = Pattern.compile("\\{time\\}"); // Sentinel-2
    3134    static final Pattern PATTERN_PARAM  = Pattern.compile("\\{([^}]+)\\}");
     35    /**
     36     * The api key pattern is used to allow us to quickly switch apikeys. This is functionally the same as the pattern
     37     * in {@link org.openstreetmap.gui.jmapviewer.tilesources.TemplatedTMSTileSource}.
     38     */
     39    static final Pattern PATTERN_API_KEY = Pattern.compile("\\{apikey}");
    3240    // CHECKSTYLE.ON: SingleSpaceSeparator
    3341
    3442    private static final Pattern[] ALL_WMS_PATTERNS = {
    3543            PATTERN_HEADER, PATTERN_PROJ, PATTERN_WKID, PATTERN_BBOX,
    3644            PATTERN_W, PATTERN_S, PATTERN_E, PATTERN_N,
    37             PATTERN_WIDTH, PATTERN_HEIGHT, PATTERN_TIME
     45            PATTERN_WIDTH, PATTERN_HEIGHT, PATTERN_TIME,
     46            PATTERN_API_KEY
    3847    };
    3948
    4049    private static final Pattern[] ALL_WMTS_PATTERNS = {
    41             PATTERN_HEADER
     50            PATTERN_HEADER, PATTERN_API_KEY
    4251    };
    4352
    4453    private ImageryPatterns() {
    public final class ImageryPatterns {  
    7483        matcher.appendTail(output);
    7584        return output.toString();
    7685    }
     86
     87    /**
     88     * Handle the {@link #PATTERN_API_KEY} replacement
     89     * @param id The id of the info
     90     * @param url The templated url
     91     * @return The templated url with {@link #PATTERN_API_KEY} replaced
     92     */
     93    static String handleApiKeyTemplate(final String id, final String url) {
     94        if (id != null && url != null) {
     95            try {
     96                final String apiKey = FeatureAdapter.retrieveApiKey(id);
     97                return PATTERN_API_KEY.matcher(url).replaceAll(apiKey);
     98            } catch (IOException | NullPointerException e) {
     99                // Match behavior in JMapViewer TemplatedTMSTileSource
     100                throw new IllegalArgumentException(e);
     101            }
     102        }
     103        return url;
     104    }
    77105}
  • src/org/openstreetmap/josm/data/imagery/TemplatedWMSTileSource.java

    diff --git a/src/org/openstreetmap/josm/data/imagery/TemplatedWMSTileSource.java b/src/org/openstreetmap/josm/data/imagery/TemplatedWMSTileSource.java
    index e2803b993b..ba00497521 100644
    a b public class TemplatedWMSTileSource extends AbstractWMSTileSource implements Tem  
    4444        this.serverProjections = new TreeSet<>(info.getServerProjections());
    4545        this.headers.putAll(info.getCustomHttpHeaders());
    4646        this.date = info.getDate();
    47         this.baseUrl = ImageryPatterns.handleHeaderTemplate(baseUrl, headers);
     47        this.baseUrl = ImageryPatterns.handleApiKeyTemplate(info.getId(), ImageryPatterns.handleHeaderTemplate(baseUrl, headers));
    4848        initProjection();
    4949        // Bounding box coordinates have to be switched for WMS 1.3.0 EPSG:4326.
    5050        //
  • src/org/openstreetmap/josm/data/imagery/WMTSTileSource.java

    diff --git a/src/org/openstreetmap/josm/data/imagery/WMTSTileSource.java b/src/org/openstreetmap/josm/data/imagery/WMTSTileSource.java
    index 5ab3a3a358..11c743e4ba 100644
    a b public class WMTSTileSource extends AbstractTMSTileSource implements TemplatedTi  
    360360        super(info);
    361361        CheckParameterUtil.ensureThat(info.getDefaultLayers().size() < 2, "At most 1 default layer for WMTS is supported");
    362362        this.headers.putAll(info.getCustomHttpHeaders());
    363         this.baseUrl = GetCapabilitiesParseHelper.normalizeCapabilitiesUrl(ImageryPatterns.handleHeaderTemplate(info.getUrl(), headers));
     363        this.baseUrl = GetCapabilitiesParseHelper.normalizeCapabilitiesUrl(
     364                ImageryPatterns.handleApiKeyTemplate(info.getId(), ImageryPatterns.handleHeaderTemplate(info.getUrl(), headers)));
    364365        WMTSCapabilities capabilities = getCapabilities(baseUrl, headers);
    365366        this.layers = capabilities.getLayers();
    366367        this.baseUrl = capabilities.getBaseUrl();