| 1 | package hhtznr;
|
|---|
| 2 |
|
|---|
| 3 | import java.io.BufferedReader;
|
|---|
| 4 | import java.io.File;
|
|---|
| 5 | import java.io.FileInputStream;
|
|---|
| 6 | import java.io.IOException;
|
|---|
| 7 | import java.io.InputStream;
|
|---|
| 8 | import java.io.InputStreamReader;
|
|---|
| 9 | import java.net.CookieHandler;
|
|---|
| 10 | import java.net.CookieManager;
|
|---|
| 11 | import java.net.CookiePolicy;
|
|---|
| 12 | import java.net.HttpURLConnection;
|
|---|
| 13 | import java.net.URL;
|
|---|
| 14 | import java.nio.charset.StandardCharsets;
|
|---|
| 15 | import java.nio.file.Paths;
|
|---|
| 16 | import java.util.Base64;
|
|---|
| 17 | import java.util.Properties;
|
|---|
| 18 |
|
|---|
| 19 | import org.openstreetmap.josm.data.Preferences;
|
|---|
| 20 | import org.openstreetmap.josm.spi.preferences.Config;
|
|---|
| 21 | import org.openstreetmap.josm.tools.Http1Client;
|
|---|
| 22 | import org.openstreetmap.josm.tools.HttpClient;
|
|---|
| 23 |
|
|---|
| 24 | public class JosmNasaEarthdataAuthTest {
|
|---|
| 25 |
|
|---|
| 26 | /*
|
|---|
| 27 | * Prefix used to identify redirects to URS for the purpose of adding
|
|---|
| 28 | * authentication headers.
|
|---|
| 29 | */
|
|---|
| 30 | public static final String URS = "https://urs.earthdata.nasa.gov";
|
|---|
| 31 |
|
|---|
| 32 | public static void main(String[] args) {
|
|---|
| 33 | Preferences prefs = Preferences.main();
|
|---|
| 34 | Config.setPreferencesInstance(prefs);
|
|---|
| 35 | HttpClient.setFactory(Http1Client::new);
|
|---|
| 36 |
|
|---|
| 37 | String resource1 = "https://e4ftl01.cr.usgs.gov/MEASURES/SRTMGL3.003/2000.02.11/N44E010.SRTMGL3.hgt.zip.xml";
|
|---|
| 38 | String resource2 = "https://e4ftl01.cr.usgs.gov/MEASURES/SRTMGL3.003/2000.02.11/N44E011.SRTMGL3.hgt.zip.xml";
|
|---|
| 39 | String[] resources = { resource1, resource2 };
|
|---|
| 40 |
|
|---|
| 41 | String username;
|
|---|
| 42 | String password;
|
|---|
| 43 | File propFile = Paths
|
|---|
| 44 | .get(Preferences.main().getDirs().getPreferencesDirectory(false).toString(), "nasa_earthdata_urs.properties")
|
|---|
| 45 | .toFile();
|
|---|
| 46 | Properties prop = new Properties();
|
|---|
| 47 | try {
|
|---|
| 48 | prop.load(new FileInputStream(propFile));
|
|---|
| 49 | username = prop.getProperty("username");
|
|---|
| 50 | password = prop.getProperty("password");
|
|---|
| 51 | System.out.println("NASA URS: username = " + username);
|
|---|
| 52 | } catch (IOException e) {
|
|---|
| 53 | e.printStackTrace();
|
|---|
| 54 | return;
|
|---|
| 55 | }
|
|---|
| 56 | String authHeader = "Basic "
|
|---|
| 57 | + Base64.getEncoder().encodeToString((username + ":" + password).getBytes(StandardCharsets.UTF_8));
|
|---|
| 58 |
|
|---|
| 59 | /*
|
|---|
| 60 | * Set up a cookie handler to maintain session cookies. A custom CookiePolicy
|
|---|
| 61 | * could be used to limit cookies to just the resource server and URS.
|
|---|
| 62 | */
|
|---|
| 63 | CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
|
|---|
| 64 |
|
|---|
| 65 | for (String resource : resources) {
|
|---|
| 66 | System.out.println("Accessing resource " + resource);
|
|---|
| 67 | try {
|
|---|
| 68 |
|
|---|
| 69 | /* Retrieve a stream for the resource */
|
|---|
| 70 | InputStream in = getResource(resource, authHeader);
|
|---|
| 71 | /* Dump the resource out (not a good idea for binary data) */
|
|---|
| 72 | BufferedReader bin = new BufferedReader(new InputStreamReader(in));
|
|---|
| 73 | String line;
|
|---|
| 74 | while ((line = bin.readLine()) != null) {
|
|---|
| 75 | System.out.println(line);
|
|---|
| 76 | }
|
|---|
| 77 | bin.close();
|
|---|
| 78 | } catch (Exception e) {
|
|---|
| 79 | System.out.println("ERROR: Failed to retrieve resource");
|
|---|
| 80 | System.out.println(e.getMessage());
|
|---|
| 81 | e.printStackTrace();
|
|---|
| 82 | }
|
|---|
| 83 | System.out.println();
|
|---|
| 84 | }
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | public static InputStream getResource(String resource, String authHeader) throws Exception {
|
|---|
| 88 | int maxRedirects = 10;
|
|---|
| 89 | HttpClient httpClient = HttpClient.create(new URL(resource));
|
|---|
| 90 | httpClient.setMaxRedirects(maxRedirects);
|
|---|
| 91 | httpClient.useCache(false);
|
|---|
| 92 |
|
|---|
| 93 | HttpClient.Response response = httpClient.connect(null, URS, authHeader);
|
|---|
| 94 |
|
|---|
| 95 | if (response.getResponseCode() == HttpURLConnection.HTTP_OK)
|
|---|
| 96 | return response.getContent();
|
|---|
| 97 | else
|
|---|
| 98 | throw new Exception("Server responded: " + response.getResponseCode() + " - " + response.getResponseCode());
|
|---|
| 99 | }
|
|---|
| 100 | }
|
|---|