1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io.imagery;
|
---|
3 |
|
---|
4 | import java.io.IOException;
|
---|
5 | import java.net.HttpURLConnection;
|
---|
6 | import java.net.URL;
|
---|
7 | import java.util.Collections;
|
---|
8 | import java.util.List;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.data.Preferences;
|
---|
11 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
12 | import org.openstreetmap.josm.tools.HttpClient;
|
---|
13 | import org.openstreetmap.josm.tools.HttpClient.Response;
|
---|
14 | import org.openstreetmap.josm.tools.Utils;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * Provider of confidential imagery API keys.
|
---|
18 | * @since 15855
|
---|
19 | */
|
---|
20 | public 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 | }
|
---|