Changeset 18371 in josm


Ignore:
Timestamp:
2022-02-09T17:03:51+01:00 (2 years ago)
Author:
taylor.smock
Message:

fix #21850: WMS and WMTS don't support {apikey}

This adds {apikey} as a valid template for WMS and WMTS.

Location:
trunk/src/org/openstreetmap/josm/data/imagery
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/imagery/ImageryPatterns.java

    r17578 r18371  
    55import static org.openstreetmap.josm.tools.I18n.tr;
    66
     7import java.io.IOException;
    78import java.util.Arrays;
    89import java.util.Map;
     
    1011import java.util.regex.Matcher;
    1112import java.util.regex.Pattern;
     13
     14import org.openstreetmap.gui.jmapviewer.FeatureAdapter;
    1215
    1316/**
     
    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
     
    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
     
    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 rough behavior in JMapViewer TemplatedTMSTileSource, but with better error message.
     100                throw new IllegalArgumentException(tr("Could not retrieve API key for imagery with id={0}. Cannot add layer.", id), e);
     101            }
     102        }
     103        return url;
     104    }
    77105}
  • trunk/src/org/openstreetmap/josm/data/imagery/TemplatedWMSTileSource.java

    r17578 r18371  
    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.
  • trunk/src/org/openstreetmap/josm/data/imagery/WMTSTileSource.java

    r17578 r18371  
    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();
Note: See TracChangeset for help on using the changeset viewer.