Ticket #23913: 23913.3.patch

File 23913.3.patch, 3.2 KB (added by taylor.smock, 5 weeks ago)

Rewrite getKey to have multiple return points instead of assigning to a variable that is later returned

  • src/org/openstreetmap/gui/jmapviewer/tilesources/AbstractTMSTileSource.java

     
    4343        if (baseUrl != null && baseUrl.endsWith("/")) {
    4444            baseUrl = baseUrl.substring(0, baseUrl.length()-1);
    4545        }
    46         this.id = info.getUrl();
     46        this.id = info.getId();
    4747        this.noTileHeaders = info.getNoTileHeaders();
    4848        this.noTileChecksums = info.getNoTileChecksums();
    4949        this.metadataHeaders = info.getMetadataHeaders();
  • src/org/openstreetmap/gui/jmapviewer/tilesources/BingAerialTileSource.java

     
    8080     * Constructs a new {@code BingAerialTileSource}.
    8181     */
    8282    public BingAerialTileSource() {
    83         super(new TileSourceInfo("Bing", null, null));
     83        super(new TileSourceInfo("Bing", null, "Bing"));
    8484        minZoom = 1;
    8585    }
    8686
     
    131131    protected URL getAttributionUrl() throws MalformedURLException {
    132132        try {
    133133            return new URI(FeatureAdapter.getSetting(METADATA_API_SETTING, METADATA_API_URL)
    134                     .replace(API_KEY_PLACEHOLDER, FeatureAdapter.getSetting(API_KEY_SETTING, API_KEY))
     134                    .replace(API_KEY_PLACEHOLDER, getKey())
    135135                    .replace(API_KEY_LAYER, this.layer)).toURL();
    136136        } catch (URISyntaxException e) {
    137137            MalformedURLException malformedURLException = new MalformedURLException(e.getMessage());
     
    140140        }
    141141    }
    142142
     143    /**
     144     * Get the API key for Bing imagery
     145     * Order of preference is as follows:
     146     * <ol>
     147     *     <li>Custom API key provided by {@link FeatureAdapter#getSetting(String, String)} via {@link #API_KEY_SETTING}</li>
     148     *     <li>API key provided by {@link FeatureAdapter#retrieveApiKey(String)}</li>
     149     *     <li>The hardcoded API key. This should not be used whenever possible.</li>
     150     * </ol>
     151     * @return The API key to use
     152     */
     153    private String getKey() {
     154        // Preference order for key
     155        // 1. Custom API key
     156        // 2. Remote API key
     157        // 3. Hardcoded API key
     158        final String customKey = FeatureAdapter.getSetting(API_KEY_SETTING, API_KEY);
     159        if (!API_KEY.equals(customKey)) {
     160            return customKey;
     161        }
     162        try {
     163            return FeatureAdapter.retrieveApiKey(this.getId());
     164        } catch (IOException ioException) {
     165            FeatureAdapter.getLogger(this.getClass()).log(Level.WARNING, "Failed to retrieve api key", ioException);
     166        }
     167        return API_KEY;
     168    }
     169
    143170    protected List<Attribution> parseAttributionText(InputSource xml) throws IOException {
    144171        try {
    145172            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();