Ticket #23913: 23913.patch

File 23913.patch, 2.5 KB (added by taylor.smock, 5 weeks ago)

JMapViewer changes

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

     
    7575    private String[] subdomains;
    7676    private String brandLogoUri;
    7777    private String layer = "Aerial";
     78    private final String imageryId = "Bing";
    7879
    7980    /**
    8081     * Constructs a new {@code BingAerialTileSource}.
     
    131132    protected URL getAttributionUrl() throws MalformedURLException {
    132133        try {
    133134            return new URI(FeatureAdapter.getSetting(METADATA_API_SETTING, METADATA_API_URL)
    134                     .replace(API_KEY_PLACEHOLDER, FeatureAdapter.getSetting(API_KEY_SETTING, API_KEY))
     135                    .replace(API_KEY_PLACEHOLDER, getKey())
    135136                    .replace(API_KEY_LAYER, this.layer)).toURL();
    136137        } catch (URISyntaxException e) {
    137138            MalformedURLException malformedURLException = new MalformedURLException(e.getMessage());
     
    140141        }
    141142    }
    142143
     144    /**
     145     * Get the API key for Bing imagery
     146     * Order of preference is as follows:
     147     * <ol>
     148     *     <li>Custom API key provided by {@link FeatureAdapter#getSetting(String, String)} via {@link #API_KEY_SETTING}</li>
     149     *     <li>API key provided by {@link FeatureAdapter#retrieveApiKey(String)}</li>
     150     *     <li>The hardcoded API key. This should not be used whenever possible.</li>
     151     * </ol>
     152     * @return The API key to use
     153     */
     154    private String getKey() {
     155        // Preference order for key
     156        // 1. Custom API key
     157        // 2. Remote API key
     158        // 3. Hardcoded API key
     159        final String customKey = FeatureAdapter.getSetting(API_KEY_SETTING, API_KEY);
     160        String key;
     161        try {
     162            key = FeatureAdapter.retrieveApiKey(imageryId); // id is getURL from info, for some reason. Probably Bing.
     163            if (!API_KEY.equals(customKey)) {
     164                key = customKey;
     165            }
     166        } catch (IOException ioException) {
     167            FeatureAdapter.getLogger(this.getClass()).log(Level.WARNING, "Failed to retrieve api key", ioException);
     168            key = customKey;
     169        }
     170        return key;
     171    }
     172
    143173    protected List<Attribution> parseAttributionText(InputSource xml) throws IOException {
    144174        try {
    145175            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();