source: josm/trunk/src/org/openstreetmap/josm/io/imagery/ApiKeyProvider.java@ 15855

Last change on this file since 15855 was 15855, checked in by Don-vip, 4 years ago

see #18440 - Fetch imagery API keys from JOSM website. Restore access to Maxar imagery. Requires JMapViewer 2.13

File size: 1.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io.imagery;
3
4import java.io.IOException;
5import java.net.HttpURLConnection;
6import java.net.URL;
7import java.util.Collections;
8import java.util.List;
9
10import org.openstreetmap.josm.data.Preferences;
11import org.openstreetmap.josm.spi.preferences.Config;
12import org.openstreetmap.josm.tools.HttpClient;
13import org.openstreetmap.josm.tools.HttpClient.Response;
14import org.openstreetmap.josm.tools.Utils;
15
16/**
17 * Provider of confidential imagery API keys.
18 * @since 15855
19 */
20public final class ApiKeyProvider {
21
22 private ApiKeyProvider() {
23 // Hide public constructor
24 }
25
26 private static List<String> getApiKeySites() {
27 return Preferences.main().getList("apikey.sites",
28 Collections.singletonList(Config.getUrls().getJOSMWebsite()+"/mapkey/"));
29 }
30
31 /**
32 * Retrieves the API key for the given imagery id.
33 * @param imageryId imagery id
34 * @return the API key for the given imagery id
35 * @throws IOException in case of I/O error
36 */
37 public static String retrieveApiKey(String imageryId) throws IOException {
38 for (String siteUrl : getApiKeySites()) {
39 Response response = HttpClient.create(new URL(siteUrl + imageryId)).connect();
40 try {
41 if (response.getResponseCode() == HttpURLConnection.HTTP_OK) {
42 return Utils.strip(response.fetchContent());
43 }
44 } finally {
45 response.disconnect();
46 }
47 }
48 return null;
49 }
50}
Note: See TracBrowser for help on using the repository browser.